Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 234B6B5DC00 for ; Wed, 21 Sep 2011 14:11:08 -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 67312-02; Wed, 21 Sep 2011 17:11:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 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 125F3B5DBD1; Wed, 21 Sep 2011 14:11:00 -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 p8LHB024025531; Wed, 21 Sep 2011 13:11:00 -0400 (EDT) To: pgsql-hackers@postgreSQL.org cc: pgsql-performance@postgreSQL.org Subject: Re: [PERFORM] Constraint exclusion on UNION ALL subqueries with WHERE conditions In-reply-to: <12971543.2235.1316510130842.JavaMail.geo-discussion-forums@yqcd38> References: <25440.1316404770@sss.pgh.pa.us> <12971543.2235.1316510130842.JavaMail.geo-discussion-forums@yqcd38> Comments: In-reply-to =?ISO-8859-1?Q?Gunnlaugur_=DE=F3r_Briem?= message dated "Tue, 20 Sep 2011 02:15:30 -0700" Date: Wed, 21 Sep 2011 13:11:00 -0400 Message-ID: <25530.1316625060@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/1062 X-Sequence-Number: 195251 =?ISO-8859-1?Q?Gunnlaugur_=DE=F3r_Briem?= writes: > On Monday, September 19, 2011 3:59:30 AM UTC, Tom Lane wrote: >> Works for me in 8.4.8. Do you have constraint_exclusion set to ON? > I did try with constraint_exclusion set to on, though the docs suggest partition should be enough ("examine constraints only for ... UNION ALL subqueries") > Here's a minimal test case (which I should have supplied in the original post, sorry), tried just now in 8.4.8: > CREATE OR REPLACE VIEW v_heavy_view > AS SELECT (random()*1e5)::integer col > FROM generate_series(1, 1e6::integer); > CREATE OR REPLACE VIEW v_test_constraint_exclusion AS > SELECT col FROM v_heavy_view WHERE col < 3 > UNION ALL SELECT col FROM v_heavy_view WHERE col >= 3; > EXPLAIN SELECT * FROM v_test_constraint_exclusion WHERE col=2; Hmm. The reason this particular case doesn't work is that we don't apply relation_excluded_by_constraints() to functions-in-FROM. It's only used for plain-table RTEs, not subqueries, functions, etc. I suspect the complainant's real case involved an unflattenable subquery. Probably the rationale for that coding was that only plain tables could have CHECK constraints; but the portion of the logic that looks for mutually contradictory scan constraints could apply to non-table relations. Should we change the code to make such checks in these cases? The default behavior (with constraint_exclusion = partition) would still be to do nothing extra, but it would add planning expense when constraint_exclusion = on. regards, tom lane