drop table if exists a cascade; drop table if exists b; create table a (a1 int, a2 int, a3 int, a4 int not null, a5 int not null, primary key(a1,a2,a3), unique (a4,a5)); create table b (b1 int, b2 int, b3 int, b4 int not null, b5 int not null); insert into a select a1,a2,a3,a1*a2,a2*a3 from generate_series(1,100) a1(a1), generate_series(1,100) a2(a2),generate_series(1,100) a3(a3); insert into b select * from a; analyze a; analyze b; select relname,reltuples from pg_class where relname in('a','b'); alter table b add constraint b_b1_b2_b3_fkey foreign key (b1,b2,b3) references a; alter table b add constraint b_b4_b5_fkey foreign key (b4,b5) references a(a4,a5); explain select * from a inner join b on a1=b1 and a2=b2 and a3=b3 and a3=b3 and a4=b4 and a5=b5; alter table b drop constraint b_b4_b5_fkey; explain select * from a inner join b on a1=b1 and a2=b2 and a3=b3 and a3=b3 and a4=b4 and a5=b5;