Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 482078E3528 for ; Sat, 3 Mar 2012 07:29:41 -0400 (AST) Received: from v1.rz.uni-leipzig.de ([139.18.1.26]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S3n9d-0000DZ-Nv for pgsql-bugs@postgresql.org; Sat, 03 Mar 2012 11:29:40 +0000 Received: from localhost (localhost [127.0.0.1]) by v1.rz.uni-leipzig.de (Postfix) with ESMTP id C29B0F805A for ; Sat, 3 Mar 2012 12:29:24 +0100 (CET) X-Virus-Scanned: by amavisd-new at v1-ul Received: from v1.rz.uni-leipzig.de ([127.0.0.1]) by localhost (v1.rz.uni-leipzig.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7WIVUEmXdac4 for ; Sat, 3 Mar 2012 12:29:24 +0100 (CET) Received: from isun.informatik.uni-leipzig.de (isun.informatik.uni-leipzig.de [139.18.13.50]) by v1.rz.uni-leipzig.de (Postfix) with ESMTP id 99F9BF8042 for ; Sat, 3 Mar 2012 12:29:24 +0100 (CET) Received: from smtp.informatik.uni-leipzig.de (smtp.informatik.uni-leipzig.de [139.18.13.51]) by isun.informatik.uni-leipzig.de (8.14.4+Sun/8.14.4) with ESMTP id q23BTNO7002318 for ; Sat, 3 Mar 2012 12:29:23 +0100 (CET) Received: from [192.168.0.156] (178-25-16-104-dynip.superkabel.de [178.25.16.104]) (authenticated bits=0) by smtp.informatik.uni-leipzig.de (8.14.3/8.14.3) with ESMTP id q23BTOkq014832 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 3 Mar 2012 12:29:24 +0100 (CET) Message-ID: <4F52008E.1050409@informatik.uni-leipzig.de> Date: Sat, 03 Mar 2012 12:29:18 +0100 From: Claus Stadler User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Regression from 8.4 to 9.1.2/9.1.3: Optimizing filters on constants in unions Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201203/18 X-Sequence-Number: 32579 Hi, not sure if this bug is already known, so sorry if it is ;) This is about an issue of the optimizer when having a union of selects that yield columns of constant value. The first minimal example shows a regression, the second one a missed optimization. Cheers, Claus Example 1: Filtering a Union on constant custom type: ------------------------------------------------------------------------------- DROP VIEW v; DROP TABLE a; DROP TABLE b; DROP TYPE IF EXISTS mytype; CREATE TYPE mytype AS ENUM ('x', 'y'); CREATE TABLE a (id INT); CREATE TABLE b (id INT); CREATE VIEW v AS SELECT t, id FROM (SELECT 'x'::mytype t, id FROM a UNION ALL SELECT 'y'::mytype t, id FROM b) c; EXPLAIN SELECT * FROM v WHERE t = 'y'; PostgreSQL 8.4.10 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2, 64-bit Result (cost=0.00..34.00 rows=2400 width=8) -> Append (cost=0.00..34.00 rows=2400 width=8) -> Seq Scan on b (cost=0.00..34.00 rows=2400 width=8) PostgreSQL 9.1.2 on x86_64-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 64-bit and also PostgreSQL 9.1.3 on x86_64-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 64-bit Result (cost=0.00..80.00 rows=24 width=8) -> Append (cost=0.00..80.00 rows=24 width=8) -> Seq Scan on a (cost=0.00..40.00 rows=12 width=8) Filter: ('x'::mytype = 'y'::mytype) -> Seq Scan on b (cost=0.00..40.00 rows=12 width=8) Filter: ('y'::mytype = 'y'::mytype) Regression: Expected result: That of 8.4.10 ===================================== Example 2: Additionally, it is interesting that it does not work with text (not tested with other types such as integers): ------------------------------------------------------------------------------- DROP VIEW v; DROP TABLE a; DROP TABLE b; CREATE TABLE a (id INT); CREATE TABLE b (id INT); CREATE VIEW v AS SELECT t, id FROM (SELECT 'x' t, id FROM a UNION ALL SELECT 'y' t, id FROM b) c; EXPLAIN SELECT * FROM v WHERE t = 'y'; PostgreSQL 8.4.10 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2, 64-bit Subquery Scan d (cost=0.00..1107744.06 rows=150000 width=36) Filter: (d.t = 'y'::text) -> Append (cost=0.00..732744.04 rows=30000002 width=4) -> Subquery Scan "*SELECT* 1" (cost=0.00..244247.54 rows=9999977 width=4) -> Seq Scan on a (cost=0.00..144247.77 rows=9999977 width=4) -> Subquery Scan "*SELECT* 2" (cost=0.00..244248.96 rows=10000048 width=4) -> Seq Scan on b (cost=0.00..144248.48 rows=10000048 width=4) -> Subquery Scan "*SELECT* 3" (cost=0.00..244247.54 rows=9999977 width=4) -> Seq Scan on c (cost=0.00..144247.77 rows=9999977 width=4) PostgreSQL 9.1.2 on x86_64-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 64-bit and also PostgreSQL 9.1.3 on x86_64-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 64-bit Result (cost=0.00..80.00 rows=24 width=36) -> Append (cost=0.00..80.00 rows=24 width=36) -> Seq Scan on a (cost=0.00..40.00 rows=12 width=36) Filter: ('x'::text = 'y'::text) -> Seq Scan on b (cost=0.00..40.00 rows=12 width=36) Filter: ('y'::text = 'y'::text) Expected: Query optimizer should discard the scan as filter cannot be satisfied. Filter: ('x'::text = 'y'::text) ================================================================================