create table t1 (
    a int,
    b int,
    c int,
    d int,
    unique(a,b),
    unique(a,b,c,d));

create table t2 (
    a int,
    b int,
    c int,
    d int);

insert into t1
     select i, i, i, i from generate_series(1,1000000) s(i);

insert into t2
     select i, i, i, i from generate_series(1,1000000) s(i);

analyze;

alter table t2 add foreign key (a,b) references t1(a,b);
alter table t2 add foreign key (a,b,c,d) references t1 (a,b,c,d);


explain select * from t1 join t2 using (a,b);

                               -- QUERY PLAN                                
-- -------------------------------------------------------------------------
 -- Hash Join  (cost=35289.00..72844.01 rows=1000000 width=24)
   -- Hash Cond: ((t1.a = t2.a) AND (t1.b = t2.b))
   -- ->  Seq Scan on t1  (cost=0.00..15406.00 rows=1000000 width=16)
   -- ->  Hash  (cost=15406.00..15406.00 rows=1000000 width=16)
         -- ->  Seq Scan on t2  (cost=0.00..15406.00 rows=1000000 width=16)
-- (5 rows)


explain select * from t1 join t2 using (a,b,c);

                               -- QUERY PLAN                                
-- -------------------------------------------------------------------------
 -- Hash Join  (cost=37789.00..79094.01 rows=1 width=20)
   -- Hash Cond: ((t1.a = t2.a) AND (t1.b = t2.b) AND (t1.c = t2.c))
   -- ->  Seq Scan on t1  (cost=0.00..15406.00 rows=1000000 width=16)
   -- ->  Hash  (cost=15406.00..15406.00 rows=1000000 width=16)
         -- ->  Seq Scan on t2  (cost=0.00..15406.00 rows=1000000 width=16)
-- (5 rows)

explain select * from t1 join t2 using (a,b,c,d);

                                     -- QUERY PLAN                                     
-- ------------------------------------------------------------------------------------
 -- Hash Join  (cost=40289.00..85344.01 rows=1 width=16)
   -- Hash Cond: ((t1.a = t2.a) AND (t1.b = t2.b) AND (t1.c = t2.c) AND (t1.d = t2.d))
   -- ->  Seq Scan on t1  (cost=0.00..15406.00 rows=1000000 width=16)
   -- ->  Hash  (cost=15406.00..15406.00 rows=1000000 width=16)
         -- ->  Seq Scan on t2  (cost=0.00..15406.00 rows=1000000 width=16)
-- (5 rows)

explain select * from t1 join t2 using (a,c);

                               -- QUERY PLAN                                
-- -------------------------------------------------------------------------
 -- Hash Join  (cost=35289.00..72844.01 rows=1 width=24)
   -- Hash Cond: ((t1.a = t2.a) AND (t1.c = t2.c))
   -- ->  Seq Scan on t1  (cost=0.00..15406.00 rows=1000000 width=16)
   -- ->  Hash  (cost=15406.00..15406.00 rows=1000000 width=16)
         -- ->  Seq Scan on t2  (cost=0.00..15406.00 rows=1000000 width=16)
-- (5 rows)
