pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: sthomas@optionshouse.com
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Setting Statistics on Functional Indexes
Date: Fri, 26 Oct 2012 17:08:18 -0400
Message-ID: <3040.1351285698@sss.pgh.pa.us> (raw)
In-Reply-To: <508AEB1A.2040001@optionshouse.com>
References: <50881D73.100@optionshouse.com>
	<6668.1351105908@sss.pgh.pa.us>
	<508841FF.9040303@optionshouse.com>
	<5088478C.6000905@optionshouse.com>
	<1511.1351280130@sss.pgh.pa.us>
	<508AEB1A.2040001@optionshouse.com>

Shaun Thomas <sthomas@optionshouse.com> writes:
> But I just noticed the lag in your response. :) It turns out, even 
> though I was substituting 2012-10-24 or 2012-10-25, what I really meant 
> was current_date. That does make all the difference, actually.

Ah.  [ pokes at that for awhile... ]  OK, this has nothing to do with
functional indexes, and everything to do with the edge-case behavior of
scalarltsel.  What you've got is a histogram whose last entry
(corresponding to the highest observed value of the date) is
current_date, and the question is what we should assume when estimating
how many rows have a value >= that.  The answer of course is "one, plus
any duplicates" ... but we don't know how many duplicates there are,
and what we do know is it's not a particularly large number because the
value isn't present in the most-common-values stats.  So the code there
assumes there aren't any dups.

Once you have enough histogram resolution for current_date to show up
as the next-to-last as well as the last histogram entry, then of course
the estimate gets a lot better, since we can now tell that there's at
least one histogram bin's worth of duplicates.

Interestingly, this is a case where the get_actual_variable_range patch
(commit 40608e7f, which appeared in 9.0) makes the results worse.
Before that, there was a (very arbitrary) lower bound on what we'd
believe as the selectivity of a >= condition, but now, when we know the
actual upper limit of the variable, we don't clamp the result that way.
I think the clamp must have been saving you in your previous version,
because it more-or-less-accidentally accounted for the end value not
being unique.

So the bottom line is that this is a case where you need a lot of
resolution in the histogram.  I'm not sure there's anything good
we can do to avoid that.   I spent a bit of time thinking about whether
we could use n_distinct to get some idea of how many duplicates there
might be for the endpoint value, but n_distinct is unreliable enough
that I can't develop a lot of faith in such a thing.  Or we could just
arbitarily assume some fraction-of-a-histogram-bin's worth of
duplicates, but that would make the results worse for some people.

			regards, tom lane




view thread (16+ messages)  latest in thread

Message-ID: <3040.1351285698@sss.pgh.pa.us>
Permalink:  ../3040.1351285698@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3040.1351285698@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, sthomas@optionshouse.com
  Subject: Re: Setting Statistics on Functional Indexes
  In-Reply-To: <3040.1351285698@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