Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id EC191B5DC19 for ; Mon, 19 Sep 2011 00:59:41 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 78290-03 for ; Mon, 19 Sep 2011 03:59:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id EFC7BB5DBC4 for ; Mon, 19 Sep 2011 00:59:30 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p8J3xUSF025441 for ; Sun, 18 Sep 2011 23:59:30 -0400 (EDT) To: pgsql-performance@postgresql.org Subject: Re: Constraint exclusion on UNION ALL subqueries with WHERE conditions In-reply-to: References: Comments: In-reply-to =?ISO-8859-1?Q?Gunnlaugur_=DE=F3r_Briem?= message dated "Wed, 14 Sep 2011 10:53:22 -0700" Date: Sun, 18 Sep 2011 23:59:30 -0400 Message-ID: <25440.1316404770@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/260 X-Sequence-Number: 45036 =?ISO-8859-1?Q?Gunnlaugur_=DE=F3r_Briem?= writes: > I did this: > CREATE VIEW unionview AS > SELECT col, otherstuff FROM (heavy subquery) > WHERE col BETWEEN 1 AND 3 > UNION ALL > SELECT col, otherstuff FROM (another heavy subquery) > WHERE col BETWEEN 4 AND 6; > hoping that the planner could use the WHERE conditions (like it would use check constraints on tables) to exclude one of the subqueries, for a query like: > SELECT * FROM unionview WHERE col=2; > But it doesn't. (In PostgreSQL 8.4.5, at least.) Works for me in 8.4.8. Do you have constraint_exclusion set to ON? regards, tom lane