pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl>
Cc: pgsql-hackers@postgreSQL.org
Cc: pgsql-performance@postgreSQL.org
Subject: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
Date: Wed, 06 Apr 2005 18:09:37 -0400
Message-ID: <6326.1112825377@sss.pgh.pa.us> (raw)
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>

I wrote:
> Arjen van der Meijden <acmmailing@vulcanus.its.tudelft.nl> 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



view thread (12+ messages)  latest in thread

Message-ID: <6326.1112825377@sss.pgh.pa.us>
Permalink:  ../6326.1112825377@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/6326.1112825377@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, acmmailing@vulcanus.its.tudelft.nl, pgsql-hackers@postgreSQL.org
  Subject: Re: Recognizing range constraints (was Re: Plan for relatively simple query seems to be very inefficient)
  In-Reply-To: <6326.1112825377@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox