Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQpri-0000nU-Bk for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Feb 2015 04:15:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YQprh-0005Bq-6L for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Feb 2015 04:15:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YQhyD-0006YV-ME for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 19:50:09 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQhy7-0008CI-TC for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 19:50:07 +0000 MIME-Version: 1.0 Subject: BUG #12805: Planner estimates query at higher cost when execution can be skipped To: pgsql-bugs@postgresql.org From: abrashears@justin.tv Date: Wed, 25 Feb 2015 19:49:53 +0000 Message-ID: <20150225194953.2546.86836@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.2 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 12805 Logged by: Aaron Brashears Email address: abrashears@justin.tv PostgreSQL version: 9.3.5 Operating system: Linux kernel 3.13.0-73 64 bit Description: The issue appears to be when a SQL statement contains a clause which causes no rows to be returned and the planner detects this condition, the returned plan is more expensive than a plan that actually has to run and read things. To reproduce the issue: create table trivial_plans ( id integer primary key ); insert into trivial_plans (id) values (1),(2),(3),(4),(5),(6),(7); -- simple query explain select * from trivial_plans where id = 5; QUERY PLAN --------------------------------------------------------------------------------------------- Index Only Scan using trivial_plans_pkey on trivial_plans (cost=0.15..6.17 rows=1 width=4) Index Cond: (id = 5) -- query which never returns a row because of "0=1" where clause which is always false. explain select * from trivial_plans where id = 5 and 0 = 1; QUERY PLAN -------------------------------------------------------------------- Result (cost=0.00..34.00 rows=1 width=4) One-Time Filter: false -> Seq Scan on trivial_plans (cost=0.00..34.00 rows=1 width=4) Note that the cost of the query which will never need to execute or read rows is estimated at a cost of 0.00..34.0 -- higher than a plan that actually has to do work and estimated with a cost cap of 6.17. I would expect the cost of the second query cost to be 0.00..0.01 or 0.00..0.0 and even rows = 0. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs