create extension if not exists citext;
create table t1 (id int primary key, t1 text not null, t2 citext not null);
create table t2 (id int primary key, t1 text not null, t2 citext not null);
insert into t1 values(1,'one','ONE');
insert into t1 values(2,'two','TWO');
insert into t2 values(1,'one','ONE');
insert into t2 values(2,'two','TWO');

create extension if not exists postgres_fdw;

select 'create server test_server foreign data wrapper postgres_fdw options(host ''localhost'', port ''' || current_setting('port') || ''', dbname ''' || current_database() || ''', extensions ''citext'');';
\gexec

create foreign table ft_t1(id int, t1 text not null, t2 citext not null) server test_server options (schema_name 'public', table_name 't1');
create foreign table ft_t2(id int, t1 text not null, t2 citext not null) server test_server options (schema_name 'public', table_name 't2');
select 'create user mapping for "' || current_user || '" server test_server options(user ''' || current_user || ''');';
\gexec

analyze ft_t1;
analyze ft_t2;

explain verbose select * from ft_t1 inner join ft_t2 on ft_t1.id = ft_t2.id; -- pushes down join
explain verbose select * from ft_t1 left join ft_t2 on ft_t1.id = ft_t2.id and ft_t1.t2 = 'one'; -- performs local join
