From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: In the planner, replace an empty FROM clause with a dummy RTE.
Date: Mon, 28 Jan 2019 22:54:34 +0000
Message-ID: <E1goFnW-0001rq-KG@gemulon.postgresql.org> (raw)
In the planner, replace an empty FROM clause with a dummy RTE.
The fact that "SELECT expression" has no base relations has long been a
thorn in the side of the planner. It makes it hard to flatten a sub-query
that looks like that, or is a trivial VALUES() item, because the planner
generally uses relid sets to identify sub-relations, and such a sub-query
would have an empty relid set if we flattened it. prepjointree.c contains
some baroque logic that works around this in certain special cases --- but
there is a much better answer. We can replace an empty FROM clause with a
dummy RTE that acts like a table of one row and no columns, and then there
are no such corner cases to worry about. Instead we need some logic to
get rid of useless dummy RTEs, but that's simpler and covers more cases
than what was there before.
For really trivial cases, where the query is just "SELECT expression" and
nothing else, there's a hazard that adding the extra RTE makes for a
noticeable slowdown; even though it's not much processing, there's not
that much for the planner to do overall. However testing says that the
penalty is very small, close to the noise level. In more complex queries,
this is able to find optimizations that we could not find before.
The new RTE type is called RTE_RESULT, since the "scan" plan type it
gives rise to is a Result node (the same plan we produced for a "SELECT
expression" query before). To avoid confusion, rename the old ResultPath
path type to GroupResultPath, reflecting that it's only used in degenerate
grouping cases where we know the query produces just one grouped row.
(It wouldn't work to unify the two cases, because there are different
rules about where the associated quals live during query_planner.)
Note: although this touches readfuncs.c, I don't think a catversion
bump is required, because the added case can't occur in stored rules,
only plans.
Patch by me, reviewed by David Rowley and Mark Dilger
Discussion: https://postgr.es/m/15944.1521127664@sss.pgh.pa.us
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/4be058fe9ec5e630239b656af21fc083371f30ed
Modified Files
--------------
contrib/pg_stat_statements/pg_stat_statements.c | 2 +
contrib/postgres_fdw/expected/postgres_fdw.out | 2 +-
src/backend/executor/execAmi.c | 5 +-
src/backend/nodes/nodeFuncs.c | 18 +-
src/backend/nodes/outfuncs.c | 12 +-
src/backend/nodes/print.c | 4 +
src/backend/nodes/readfuncs.c | 3 +
src/backend/optimizer/README | 13 +-
src/backend/optimizer/path/allpaths.c | 59 +-
src/backend/optimizer/path/costsize.c | 57 ++
src/backend/optimizer/plan/createplan.c | 87 ++-
src/backend/optimizer/plan/initsplan.c | 2 +-
src/backend/optimizer/plan/planmain.c | 103 +--
src/backend/optimizer/plan/planner.c | 49 +-
src/backend/optimizer/plan/subselect.c | 11 +-
src/backend/optimizer/prep/prepjointree.c | 796 ++++++++++++++++--------
src/backend/optimizer/util/clauses.c | 19 +-
src/backend/optimizer/util/pathnode.c | 59 +-
src/backend/optimizer/util/plancat.c | 1 +
src/backend/optimizer/util/relnode.c | 43 +-
src/backend/parser/analyze.c | 3 +
src/backend/parser/parse_relation.c | 20 +
src/backend/parser/parse_target.c | 2 +
src/backend/utils/adt/ruleutils.c | 1 +
src/include/nodes/nodes.h | 2 +-
src/include/nodes/parsenodes.h | 5 +-
src/include/nodes/relation.h | 11 +-
src/include/optimizer/cost.h | 3 +
src/include/optimizer/pathnode.h | 9 +-
src/include/optimizer/prep.h | 2 +
src/test/isolation/expected/eval-plan-qual.out | 45 +-
src/test/isolation/specs/eval-plan-qual.spec | 22 +-
src/test/regress/expected/join.out | 95 ++-
src/test/regress/expected/subselect.out | 4 +-
src/test/regress/sql/join.sql | 48 +-
35 files changed, 1171 insertions(+), 446 deletions(-)
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: In the planner, replace an empty FROM clause with a dummy RTE.
In-Reply-To: <E1goFnW-0001rq-KG@gemulon.postgresql.org>
References: <E1goFnW-0001rq-KG@gemulon.postgresql.org>
* 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