Dig down into sub-selects to look for column statistics.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 4 Sep 2011 19:13:46 +0000 (15:13 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 4 Sep 2011 19:13:46 +0000 (15:13 -0400)
commit1cb108efb0e60d87e4adec38e7636b6e8efbeb57
tree214db4676aae05201fb129ad62458ea13cd810ef
parent698df3350d37cdaa5b9fcbcb78b5ad5e66e99348
Dig down into sub-selects to look for column statistics.

If a sub-select's output column is a simple Var, recursively look for
statistics applying to that Var, and use them if available.  The need for
this was foreseen ages ago, but we didn't have enough infrastructure to do
it with reasonable speed until just now.

We punt and stick with default estimates if the subquery uses set
operations, GROUP BY, or DISTINCT, since those operations would change the
underlying column statistics (particularly, the relative frequencies of
different values) beyond recognition.  This means that the types of
sub-selects for which this improvement applies are fairly limited, since
most subqueries satisfying those restrictions would have gotten flattened
into the parent query anyway.  But it does help for some cases, such as
subqueries with ORDER BY or LIMIT.
src/backend/utils/adt/selfuncs.c