create table a (
    a1 int,
    a2 int,
    unique (a1,a2));

create table b (
    b1 int,
    b2 int,
    unique (b1,b2));

create table f (
    a1 int,
    a2 int,
    b1 int,
    b2 int);

insert into a select i,i from generate_series(0,999999) s(i);
insert into b select i,i from generate_series(0,999999) s(i);

insert into f select i/10,i/10,i/10,i/10 from generate_series(0,9999999) s(i);

analyze a;
analyze b;
analyze f;

alter table f add foreign key (a1,a2) references a(a1,a2);
alter table f add foreign key (b1,b2) references b(b1,b2);

explain select * from f join a using (a1,a2)
                        join b using (b1,b2);

-- original patch:
                                 -- QUERY PLAN                                  
-- -----------------------------------------------------------------------------
 -- Hash Join  (cost=66664.00..573853.57 rows=10000175 width=16)
   -- Hash Cond: ((f.b1 = b.b1) AND (f.b2 = b.b2))
   -- ->  Hash Join  (cost=33332.00..363955.16 rows=10000175 width=16)
         -- Hash Cond: ((f.a1 = a.a1) AND (f.a2 = a.a2))
         -- ->  Seq Scan on f  (cost=0.00..154056.75 rows=10000175 width=16)
         -- ->  Hash  (cost=14425.00..14425.00 rows=1000000 width=8)
               -- ->  Seq Scan on a  (cost=0.00..14425.00 rows=1000000 width=8)
   -- ->  Hash  (cost=14425.00..14425.00 rows=1000000 width=8)
         -- ->  Seq Scan on b  (cost=0.00..14425.00 rows=1000000 width=8)
-- (9 rows)

-- David's patch
                                 -- QUERY PLAN                                  
-- -----------------------------------------------------------------------------
 -- Hash Join  (cost=66664.00..573853.57 rows=10 width=16)
   -- Hash Cond: ((f.b1 = b.b1) AND (f.b2 = b.b2))
   -- ->  Hash Join  (cost=33332.00..363955.16 rows=10000175 width=16)
         -- Hash Cond: ((f.a1 = a.a1) AND (f.a2 = a.a2))
         -- ->  Seq Scan on f  (cost=0.00..154056.75 rows=10000175 width=16)
         -- ->  Hash  (cost=14425.00..14425.00 rows=1000000 width=8)
               -- ->  Seq Scan on a  (cost=0.00..14425.00 rows=1000000 width=8)
   -- ->  Hash  (cost=14425.00..14425.00 rows=1000000 width=8)
         -- ->  Seq Scan on b  (cost=0.00..14425.00 rows=1000000 width=8)
-- (9 rows)

