DROP TABLE a CASCADE;
DROP TABLE b CASCADE;
DROP TABLE f CASCADE;

CREATE TABLE a (a1 INT, a2 INT, PRIMARY KEY (a1, a2));
CREATE TABLE b (b1 INT, b2 INT, PRIMARY KEY (b1, b2));

CREATE TABLE f (a1 INT, a2 INT, b1 INT, b2 INT,
                FOREIGN KEY (a1, a2) REFERENCES a (a1, a2),
                FOREIGN KEY (b1, b2) REFERENCES b (b1, b2));

INSERT INTO a SELECT i, i FROM generate_series(0,9999) s(i);
INSERT INTO b SELECT i, i FROM generate_series(0,9999) s(i);

INSERT INTO f SELECT mod(i,10000), mod(i,10000),
                     mod(i,10000), mod(i,10000)
                FROM generate_series(1,1000000) s(i);

ANALYZE;

EXPLAIN ANALYZE
SELECT * FROM f JOIN a ON (f.a1 = a.a1 AND f.a2 = a.a2)
                JOIN b ON (f.b1 = b.b1 AND f.b2 = b.b2);
