DROP EXTENSION IF EXISTS postgres_fdw CASCADE;
 
CREATE EXTENSION postgres_fdw;
CREATE SERVER loopback1 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (async_capable 'true');
CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (async_capable 'true');
CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (async_capable 'true');

CREATE USER MAPPING FOR PUBLIC SERVER loopback1;
CREATE USER MAPPING FOR PUBLIC SERVER loopback2;
CREATE USER MAPPING FOR PUBLIC SERVER loopback3;
 
CREATE TABLE l1(a int);
CREATE FOREIGN TABLE f1(a int) SERVER loopback1 OPTIONS (table_name 'l1');
CREATE TABLE l2(a int);
CREATE FOREIGN TABLE f2(a int) SERVER loopback2 OPTIONS (table_name 'l2');
CREATE TABLE l3(a int);
CREATE FOREIGN TABLE f3(a int) SERVER loopback3 OPTIONS (table_name 'l3');
 
CREATE TABLE part(a int) PARTITION BY HASH (a);
ALTER TABLE part ATTACH PARTITION f1
    FOR VALUES WITH (MODULUS 3, REMAINDER 0);
ALTER TABLE part ATTACH PARTITION f2
    FOR VALUES WITH (MODULUS 3, REMAINDER 1);
ALTER TABLE part ATTACH PARTITION f3
    FOR VALUES WITH (MODULUS 3, REMAINDER 2);
     
INSERT INTO part SELECT * FROM generate_series(1,10000);
INSERT INTO part SELECT * FROM generate_series(1,10000);
ANALYZE l1,l2,l3,f1,f2,f3,part;
