Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id C9DEA16D3788 for ; Sun, 8 Jul 2012 12:56:50 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sntqs-000707-46 for pgsql-performance@postgresql.org; Sun, 08 Jul 2012 15:56:50 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q68FuZVJ003858; Sun, 8 Jul 2012 11:56:36 -0400 (EDT) From: Tom Lane To: Nate Allan cc: "pgsql-performance@postgresql.org" Subject: Re: Terrible plan for join to nested union In-reply-to: <9B2D6747F4AB8A47BE45216B06DEDAF92ABE66BC@PREXMB01.myfamily.int> References: <9B2D6747F4AB8A47BE45216B06DEDAF92ABE667B@PREXMB01.myfamily.int> <19124.1341706090@sss.pgh.pa.us> <9B2D6747F4AB8A47BE45216B06DEDAF92ABE66BC@PREXMB01.myfamily.int> Comments: In-reply-to Nate Allan message dated "Sun, 08 Jul 2012 05:50:01 -0000" Date: Sun, 08 Jul 2012 11:56:35 -0400 Message-ID: <3857.1341762995@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/79 X-Sequence-Number: 47260 Nate Allan writes: > It seems to me that the join condition (and hence the restriction) should be pushed down into both sides of the union to bring the cardinality limit from millions to 1. I'm imagining a rewrite like this: > R(a) J (b U c) -> (b J R(a)) U (c J R(a)) > ...where R = Restrict, J = Join, U = Union [ eyes that suspiciously ... ] I'm not convinced that such a transformation is either correct in general (you seem to be assuming at least that A's join column is unique, and what is the UNION operator supposed to do with A's other columns?) or likely to lead to a performance improvement in general. We possibly could push down a join condition on the inner side of a nestloop, similarly to what's done in the UNION ALL case ... but that would require a complete refactoring of what the planner does with UNIONs. By and large, very little optimization effort has been put into non-ALL UNION (or INTERSECT or EXCEPT). You should not expect that to change on a time scale of less than years. regards, tom lane