CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS pg CASCADE;
CREATE SERVER pg FOREIGN DATA WRAPPER postgres_fdw
	OPTIONS (host 'localhost', dbname 'postgres');
CREATE USER MAPPING FOR public SERVER pg;

CREATE TABLE p (id int PRIMARY KEY, val text);

DROP FOREIGN TABLE IF EXISTS pf;
CREATE FOREIGN TABLE pf (id int, val text)
	SERVER pg
	OPTIONS (table_name 'p');


CREATE TABLE parttbl (id int PRIMARY KEY, val text)
	PARTITION BY HASH (id);

CREATE TABLE parttbl_p0
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 0);
CREATE TABLE parttbl_p1
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 1);
CREATE TABLE parttbl_p2
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 2);
CREATE TABLE parttbl_p3
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 3);
CREATE TABLE parttbl_p4
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 4);
CREATE TABLE parttbl_p5
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 5);
CREATE TABLE parttbl_p6
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 6);
CREATE TABLE parttbl_p7
	PARTITION OF parttbl
	FOR VALUES WITH (MODULUS 8, REMAINDER 7);


CREATE TABLE parttblf (id int, val text)
	PARTITION BY HASH (id);

CREATE TABLE parttblf_p0 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p1 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p2 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p3 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p4 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p5 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p6 (id int PRIMARY KEY, val text);
CREATE TABLE parttblf_p7 (id int PRIMARY KEY, val text);

DROP FOREIGN TABLE IF EXISTS  parttbl_p0f;
CREATE FOREIGN TABLE parttbl_p0f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 0)
	SERVER pg
	OPTIONS (table_name 'parttblf_p0');
DROP FOREIGN TABLE IF EXISTS  parttbl_p1f;
CREATE FOREIGN TABLE parttbl_p1f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 1)
	SERVER pg
	OPTIONS (table_name 'parttblf_p1');
DROP FOREIGN TABLE IF EXISTS  parttbl_p2f;
CREATE FOREIGN TABLE parttbl_p2f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 2)
	SERVER pg
	OPTIONS (table_name 'parttblf_p2');
DROP FOREIGN TABLE IF EXISTS  parttbl_p3f;
CREATE FOREIGN TABLE parttbl_p3f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 3)
	SERVER pg
	OPTIONS (table_name 'parttblf_p3');
DROP FOREIGN TABLE IF EXISTS  parttbl_p4f;
CREATE FOREIGN TABLE parttbl_p4f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 4)
	SERVER pg
	OPTIONS (table_name 'parttblf_p4');
DROP FOREIGN TABLE IF EXISTS  parttbl_p5f;
CREATE FOREIGN TABLE parttbl_p5f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 5)
	SERVER pg
	OPTIONS (table_name 'parttblf_p5');
DROP FOREIGN TABLE IF EXISTS  parttbl_p6f;
CREATE FOREIGN TABLE parttbl_p6f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 6)
	SERVER pg
	OPTIONS (table_name 'parttblf_p6');
DROP FOREIGN TABLE IF EXISTS  parttbl_p7f;
CREATE FOREIGN TABLE parttbl_p7f
	PARTITION OF parttblf
	FOR VALUES WITH (MODULUS 8, REMAINDER 7)
	SERVER pg
	OPTIONS (table_name 'parttblf_p7');

CREATE TABLE t AS SELECT i, md5(i::text) FROM generate_series(1,1000000) s(i);
VACUUM ANALYZE t;

CHECKPOINT;
