X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A92A753A26; Wed, 6 Apr 2005 23:09:41 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 21227-10; Wed, 6 Apr 2005 22:09:38 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2F3E353A25; Wed, 6 Apr 2005 23:09:37 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j36M9bgo006327; Wed, 6 Apr 2005 18:09:37 -0400 (EDT) To: Arjen van der Meijden Cc: pgsql-hackers@postgreSQL.org, pgsql-performance@postgreSQL.org Subject: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient) In-reply-to: <5999.1112820690@sss.pgh.pa.us> References: <425413D3.5030304@vulcanus.its.tudelft.nl> <28552.1112809334@sss.pgh.pa.us> <425423A9.9000504@vulcanus.its.tudelft.nl> <5999.1112820690@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 06 Apr 2005 16:51:30 -0400" Date: Wed, 06 Apr 2005 18:09:37 -0400 Message-ID: <6326.1112825377@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/125 X-Sequence-Number: 11522 I wrote: > Arjen van der Meijden writes: >> SELECT COUNT(*) FROM >> data_main AS dm, >> postcodes AS p >> WHERE dm.range BETWEEN p.range_from AND p.range_till > Planner error ... because it doesn't have any good way to estimate the > number of matching rows, it thinks that way is a bit more expensive than > data_main as the outside, but in reality it seems a good deal cheaper: BTW, it would get the right answer if it had recognized the WHERE clause as a range restriction --- it still doesn't know exactly what fraction of rows will match, but its default estimate is a great deal tighter for "WHERE x > something AND x < somethingelse" than it is for two unrelated inequality constraints. Enough tighter that it would have gone for the correct plan. The problem is that it doesn't recognize the WHERE as a range constraint on dm.range. I thought for a moment that this might be a recently-introduced bug, but actually the code is operating as designed: clauselist_selectivity says * See if it looks like a restriction clause with a pseudoconstant * on one side. (Anything more complicated than that might not * behave in the simple way we are expecting.) "Pseudoconstant" in this context means "a constant, parameter symbol, or non-volatile functions of these" ... so comparisons against values from another table don't qualify. It seems like we're missing a bet though. Can anyone suggest a more general rule? Do we need for example to consider whether the relation membership is the same in two clauses that might be opposite sides of a range restriction? It seems like a.x > b.y AND a.x < b.z probably can be treated as a range restriction on a.x for this purpose, but I'm much less sure that the same is true of a.x > b.y AND a.x < c.z Thoughts? regards, tom lane