Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 3C471B5DC15 for ; Tue, 20 Sep 2011 06:30:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 60204-06 for ; Tue, 20 Sep 2011 09:30:02 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 79490B5DC01 for ; Tue, 20 Sep 2011 06:30:02 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id p8K9U1bo071241 for ; Tue, 20 Sep 2011 06:30:01 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id p8K9HMjH051001 for pgsql-performance@postgresql.org; Tue, 20 Sep 2011 06:17:22 -0300 (ADT) (envelope-from news) From: =?ISO-8859-1?Q?Gunnlaugur_=DE=F3r_Briem?= X-Newsgroups: pgsql.performance Subject: Re: Constraint exclusion on UNION ALL subqueries with WHERE conditions Date: Tue, 20 Sep 2011 02:15:30 -0700 (PDT) Organization: http://groups.google.com Lines: 33 Message-ID: <12971543.2235.1316510130842.JavaMail.geo-discussion-forums@yqcd38> References: <25440.1316404770@sss.pgh.pa.us> Reply-To: pgsql.performance@googlegroups.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Complaints-To: groups-abuse@google.com In-Reply-To: <25440.1316404770@sss.pgh.pa.us> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=157.157.92.142; posting-account=YRSnRgoAAABkgkyuFACt4iC5RsuqV7Ez User-Agent: G2/1.0 X-Google-Web-Client: true To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.393 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/324 X-Sequence-Number: 45100 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; QUERY PLAN -------------------------------------------------------------------------- Result (cost=0.00..70.04 rows=4 width=4) -> Append (cost=0.00..70.04 rows=4 width=4) -> Subquery Scan v_heavy_view (cost=0.00..35.00 rows=2 width=4) Filter: ((v_heavy_view.col < 3) AND (v_heavy_view.col = 2)) -> Function Scan on generate_series (cost=0.00..20.00 rows=1000 width=0) -> Subquery Scan v_heavy_view (cost=0.00..35.00 rows=2 width=4) Filter: ((v_heavy_view.col >= 3) AND (v_heavy_view.col = 2)) -> Function Scan on generate_series (cost=0.00..20.00 rows=1000 width=0) I want the planner to notice that (v_heavy_view.col >= 3) AND (v_heavy_view.col = 2) can never be satisfied, and skip that subquery. Regards, - Gulli