pgsql: Arrange to convert EXISTS subqueries that are equivalent to

From: tgl(at)postgresql(dot)org (Tom Lane)
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Arrange to convert EXISTS subqueries that are equivalent to
Date: 2008-08-22 00:16:04
Message-ID: 20080822001604.D2B30755316@cvs.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Log Message:
-----------
Arrange to convert EXISTS subqueries that are equivalent to hashable IN
subqueries into the same thing you'd have gotten from IN (except always with
unknownEqFalse = true, so as to get the proper semantics for an EXISTS).
I believe this fixes the last case within CVS HEAD in which an EXISTS could
give worse performance than an equivalent IN subquery.

The tricky part of this is that if the upper query probes the EXISTS for only
a few rows, the hashing implementation can actually be worse than the default,
and therefore we need to make a cost-based decision about which way to use.
But at the time when the planner generates plans for subqueries, it doesn't
really know how many times the subquery will be executed. The least invasive
solution seems to be to generate both plans and postpone the choice until
execution. Therefore, in a query that has been optimized this way, EXPLAIN
will show two subplans for the EXISTS, of which only one will actually get
executed.

There is a lot more that could be done based on this infrastructure: in
particular it's interesting to consider switching to the hash plan if we start
out using the non-hashed plan but find a lot more upper rows going by than we
expected. I have therefore left some minor inefficiencies in place, such as
initializing both subplans even though we will currently only use one.

Modified Files:
--------------
pgsql/src/backend/catalog:
dependency.c (r1.78 -> r1.79)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/catalog/dependency.c?r1=1.78&r2=1.79)
pgsql/src/backend/executor:
execQual.c (r1.231 -> r1.232)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/executor/execQual.c?r1=1.231&r2=1.232)
nodeSubplan.c (r1.93 -> r1.94)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/executor/nodeSubplan.c?r1=1.93&r2=1.94)
pgsql/src/backend/nodes:
copyfuncs.c (r1.400 -> r1.401)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/nodes/copyfuncs.c?r1=1.400&r2=1.401)
equalfuncs.c (r1.327 -> r1.328)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/nodes/equalfuncs.c?r1=1.327&r2=1.328)
outfuncs.c (r1.334 -> r1.335)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/nodes/outfuncs.c?r1=1.334&r2=1.335)
pgsql/src/backend/optimizer/path:
clausesel.c (r1.92 -> r1.93)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/path/clausesel.c?r1=1.92&r2=1.93)
costsize.c (r1.194 -> r1.195)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/path/costsize.c?r1=1.194&r2=1.195)
pgsql/src/backend/optimizer/plan:
subselect.c (r1.137 -> r1.138)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/plan/subselect.c?r1=1.137&r2=1.138)
pgsql/src/backend/optimizer/util:
clauses.c (r1.262 -> r1.263)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/util/clauses.c?r1=1.262&r2=1.263)
var.c (r1.75 -> r1.76)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/util/var.c?r1=1.75&r2=1.76)
pgsql/src/backend/parser:
parse_expr.c (r1.229 -> r1.230)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/parser/parse_expr.c?r1=1.229&r2=1.230)
pgsql/src/backend/rewrite:
rewriteManip.c (r1.109 -> r1.110)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/rewrite/rewriteManip.c?r1=1.109&r2=1.110)
pgsql/src/backend/utils/adt:
ruleutils.c (r1.279 -> r1.280)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/adt/ruleutils.c?r1=1.279&r2=1.280)
pgsql/src/include/executor:
nodeSubplan.h (r1.27 -> r1.28)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/executor/nodeSubplan.h?r1=1.27&r2=1.28)
pgsql/src/include/nodes:
execnodes.h (r1.186 -> r1.187)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/nodes/execnodes.h?r1=1.186&r2=1.187)
nodes.h (r1.208 -> r1.209)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/nodes/nodes.h?r1=1.208&r2=1.209)
primnodes.h (r1.138 -> r1.139)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/nodes/primnodes.h?r1=1.138&r2=1.139)
pgsql/src/include/optimizer:
clauses.h (r1.92 -> r1.93)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/optimizer/clauses.h?r1=1.92&r2=1.93)
cost.h (r1.91 -> r1.92)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/optimizer/cost.h?r1=1.91&r2=1.92)
pgsql/src/include/rewrite:
rewriteManip.h (r1.45 -> r1.46)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/include/rewrite/rewriteManip.h?r1=1.45&r2=1.46)

Browse pgsql-committers by date

  From Date Subject
Next Message Bruce Momjian 2008-08-22 00:20:40 pgsql: Improve wording of error message when a postgresql.conf setting
Previous Message Bruce Momjian 2008-08-21 22:26:12 pgsql: Mention that pg_dump does not dump ALTER DATABASE ...