drop database foobar;
create database foobar;
\c foobar

CREATE EXTENSION IF NOT EXISTS postgres_fdw WITH SCHEMA public;

CREATE SERVER remote_server FOREIGN DATA WRAPPER postgres_fdw options (dbname 'foobar') ;
CREATE USER MAPPING FOR postgres SERVER remote_server OPTIONS (
    "user" 'postgres'
);

CREATE SCHEMA remote;
SET search_path = remote, pg_catalog;

CREATE TABLE remote1 as select floor(random()*10000000)::bigint as id, floor(random()*10000000)::bigint as data from generate_series(1,1000000);
create index on remote1 (id);
CREATE TABLE remote2 as select id::text as id, random() as cutoff from remote1;
create index on remote2 (cutoff);
vacuum analyze;

reset search_path;

CREATE FOREIGN TABLE remote1 (
   id integer,
   data integer
)
SERVER remote_server
OPTIONS (
    schema_name 'remote',
    table_name 'remote1',
    use_remote_estimate 'true'
);

CREATE FOREIGN TABLE remote2 (
   id text,
   cutoff double precision 
)
SERVER remote_server
OPTIONS (
    schema_name 'remote',
    table_name 'remote2',
    use_remote_estimate 'true'
);

analyze;

explain analyze select data from remote2 join remote1 on ((remote2.id)::bigint=remote1.id) where cutoff > 0.9999;
