The following bug has been logged online: Bug reference: 3308 Logged by: George Sakkis Email address: [EMAIL PROTECTED] PostgreSQL version: 8.2.3 Operating system: Linux Description: Nested outer join gives wrong results Details:
I have the following two subqueries, both returning a single column q_id; also the result set of A is a strict superset of B: Query A ------- SELECT query.id as q_id FROM ranker, run, query WHERE ranker.id = 72 AND run.id = ranker.run_id AND query.set_id = run.set_id (966 rows) Query B ------- SELECT serp_result.q_id FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url) WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND editor_rating.grade is null (251 rows) Now, the left outer join of A and B should be equal to A in this case since A is a superset of B. If I save A in a temp table and use this for the join, that's indeed the result: SELECT query.id as q_id INTO TEMP t1 FROM ranker, run, query WHERE ranker.id = 72 AND run.id = ranker.run_id AND query.set_id = run.set_id SELECT * FROM t1 LEFT JOIN ( SELECT serp_result.q_id FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url) WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND editor_rating.grade is null ) AS t2 USING (q_id) ; (966 rows) Likewise if I save B into a temp table and join with A: SELECT serp_result.q_id into TEMP t2 FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url) WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND editor_rating.grade is null SELECT * FROM ( SELECT query.id as q_id FROM ranker, run, query WHERE ranker.id = 72 AND run.id = ranker.run_id AND query.set_id = run.set_id ) AS t1 LEFT JOIN t2 USING (q_id) (966 rows) If I don't use temp tables though, the result is equal to B instead: SELECT * FROM ( SELECT query.id as q_id FROM ranker, run, query WHERE ranker.id = 72 AND run.id = ranker.run_id AND query.set_id = run.set_id ) AS t1 LEFT JOIN ( SELECT serp_result.q_id FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url) WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND editor_rating.grade is null ) AS t2 USING (q_id) ; (251 rows) What gives ? ---------------------------(end of broadcast)--------------------------- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match