CREATE EXTENSION postgres_fdw;
CREATE SERVER this_server FOREIGN DATA WRAPPER postgres_fdw;
CREATE USER MAPPING FOR current_user SERVER this_server;

CREATE FOREIGN TABLE frgn (
    id bigint,
    title text
)
SERVER this_server
OPTIONS (
    table_name 'local',
    use_remote_estimate 'true'
);

CREATE SEQUENCE local_id_seq START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE TABLE local (
    id bigint DEFAULT nextval('local_id_seq'::regclass) NOT NULL,
    title text
);

CREATE SEQUENCE other_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE TABLE other (
    id bigint DEFAULT nextval('other_id_seq'::regclass) NOT NULL,
    title text
);

CREATE UNIQUE INDEX idxlocal_id ON local USING btree (id);
CREATE INDEX idxlocal_title ON local USING btree (title);
CREATE UNIQUE INDEX idxother_id ON other USING btree (id);
CREATE INDEX idxother_title ON other USING btree (title);

DO $$
  DECLARE
    v_counter bigint;
  BEGIN
    v_counter := 1;
    WHILE v_counter <= 10000 LOOP
      INSERT INTO local (title) SELECT md5(nextval('local_id_seq')::text);
      INSERT INTO other (title) SELECT md5(nextval('other_id_seq')::text);
      v_counter := v_counter + 1;
    END LOOP;
  END;
$$;
