-- number of foreign keys generated
\set n 1000

\o out.sql
select 'create table t' || x || ' (a int not null, b int not null, primary key(a,b));' from generate_series(1,:n) x(x);
\gexec

create table f1 (a int not null, b int not null);
select 'alter table f1 add constraint f1_t' || x || '_fkey foreign key (a,b) references t' || x from generate_series(1,:n) x(x);
\gexec

create table f2 (a int not null, b int not null);
select 'alter table f2 add constraint f2_t' || x || '_fkey foreign key (a,b) references t' || x from generate_series(1,:n) x(x);
\gexec

create table f3 (a int not null, b int not null);
select 'alter table f3 add constraint f3_t' || x || '_fkey foreign key (a,b) references t' || x from generate_series(1,:n) x(x);
\gexec

create table f4 (a int not null, b int not null);
select 'alter table f4 add constraint f4_t' || x || '_fkey foreign key (a,b) references t' || x from generate_series(1,:n) x(x);
\gexec

create table f5 (a int not null, b int not null);
select 'alter table f5 add constraint f5_t' || x || '_fkey foreign key (a,b) references t' || x from generate_series(1,:n) x(x);
\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b' from generate_series(1,100) s(i);

\o explain-2.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b' from generate_series(1,100) s(i);

\o explain-3.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b
inner join f4 on f1.a = f4.a and f1.b = f4.b' from generate_series(1,100) s(i);

\o explain-4.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b
inner join f4 on f1.a = f4.a and f1.b = f4.b
inner join f5 on f1.a = f5.a and f1.b = f5.b' from generate_series(1,100) s(i);

\o explain-5.log

\gexec

\o /dev/null

SET enable_fkey_estimates = off;

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b' from generate_series(1,100) s(i);

\o explain-2-off.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b' from generate_series(1,100) s(i);

\o explain-3-off.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b
inner join f4 on f1.a = f4.a and f1.b = f4.b' from generate_series(1,100) s(i);

\o explain-4-off.log

\gexec

\o /dev/null

select 'explain analyze
select * from f1
inner join f2 on f1.a = f2.a and f1.b = f2.b
inner join f3 on f1.a = f3.a and f1.b = f3.b
inner join f4 on f1.a = f4.a and f1.b = f4.b
inner join f5 on f1.a = f5.a and f1.b = f5.b' from generate_series(1,100) s(i);

\o explain-5-off.log

\gexec
