Convert SetOp to read its inputs as outerPlan and innerPlan.
The original design for set operations involved appending the two
input relations into one and adding a flag column that allows
distinguishing which side each row came from. Then the SetOp node
pries them apart again based on the flag. This is bizarre. The
only apparent reason to do it is that when sorting, we'd only need
one Sort node not two. But since sorting is at least O(N log N),
sorting all the data is actually worse than sorting each side
separately --- plus, we have no chance of taking advantage of
presorted input. On top of that, adding the flag column frequently
requires an additional projection step that adds cycles, and then
the Append node isn't free either. Let's get rid of all of that
and make the SetOp node have two separate children, using the
existing outerPlan/innerPlan infrastructure.
This initial patch re-implements nodeSetop.c and does a bare minimum
of work on the planner side to generate correctly-shaped plans.
In particular, I've tried not to change the cost estimates here,
so that the visible changes in the regression test results will only
involve removal of useless projection steps and not any changes in
whether to use sorted vs hashed mode.
For SORTED mode, we combine successive identical tuples from each
input into groups, and then merge-join the groups. The tuple
comparisons now use SortSupport instead of simple equality, but
the group-formation part should involve roughly the same number of
tuple comparisons as before. The cross-comparisons between left and
right groups probably add to that, but I'm not sure to quantify how
many more comparisons we might need.
For HASHED mode, nodeSetop's logic is almost the same as before,
just refactored into two separate loops instead of one loop that
has an assumption that it will see all the left-hand inputs first.
In both modes, I added early-exit logic to not bother reading the
right-hand relation if the left-hand input is empty, since neither
INTERSECT nor EXCEPT modes can produce any output if the left input
is empty. This could have been done before in the hashed mode, but
not in sorted mode. Sorted mode can also stop as soon as it exhausts
the left input; any remaining right-hand tuples cannot have matches.
Also, this patch adds some infrastructure for detecting whether
child plan nodes all output the same type of tuple table slot.
If they do, the hash table logic can use slightly more efficient
code based on assuming that that's the input slot type it will see.
We'll make use of that infrastructure in other plan node types later.
Patch by me; thanks to Richard Guo and David Rowley for review.
Discussion: https://postgr.es/m/1850138.1731549611@sss.pgh.pa.us
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/27627929528e24a547d1058a5444b35491057a56
Modified Files
--------------
src/backend/executor/execUtils.c | 43 +++
src/backend/executor/nodeSetOp.c | 537 ++++++++++++++++++--------------
src/backend/optimizer/README | 2 +-
src/backend/optimizer/plan/createplan.c | 81 ++---
src/backend/optimizer/prep/prepunion.c | 156 ++++++----
src/backend/optimizer/util/pathnode.c | 50 +--
src/include/executor/executor.h | 3 +
src/include/nodes/execnodes.h | 31 +-
src/include/nodes/pathnodes.h | 9 +-
src/include/nodes/plannodes.h | 19 +-
src/include/optimizer/pathnode.h | 7 +-
src/test/regress/expected/subselect.out | 20 +-
src/test/regress/expected/union.out | 254 +++++++--------
src/test/regress/sql/union.sql | 6 +
src/tools/pgindent/typedefs.list | 2 +
15 files changed, 696 insertions(+), 524 deletions(-)