diff options
author | David Rowley <drowley@postgresql.org> | 2024-03-25 14:31:14 +1300 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-03-25 14:31:14 +1300 |
commit | 66c0185a3d14bbbf51d0fc9d267093ffec735231 (patch) | |
tree | ed16cb0999652ad23efef6b5e025554f4136020c /src/backend/parser/analyze.c | |
parent | 47f99a407de224df6f9c43697d0a9c0a5598b250 (diff) | |
download | postgresql-66c0185a3d14bbbf51d0fc9d267093ffec735231.tar.gz postgresql-66c0185a3d14bbbf51d0fc9d267093ffec735231.zip |
Allow planner to use Merge Append to efficiently implement UNION
Until now, UNION queries have often been suboptimal as the planner has
only ever considered using an Append node and making the results unique
by either using a Hash Aggregate, or by Sorting the entire Append result
and running it through the Unique operator. Both of these methods
always require reading all rows from the union subqueries.
Here we adjust the union planner so that it can request that each subquery
produce results in target list order so that these can be Merge Appended
together and made unique with a Unique node. This can improve performance
significantly as the union child can make use of the likes of btree
indexes and/or Merge Joins to provide the top-level UNION with presorted
input. This is especially good if the top-level UNION contains a LIMIT
node that limits the output rows to a small subset of the unioned rows as
cheap startup plans can be used.
Author: David Rowley
Reviewed-by: Richard Guo, Andy Fan
Discussion: https://postgr.es/m/CAApHDvpb_63XQodmxKUF8vb9M7CxyUyT4sWvEgqeQU-GB7QFoQ@mail.gmail.com
Diffstat (limited to 'src/backend/parser/analyze.c')
-rw-r--r-- | src/backend/parser/analyze.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 40ea19e6f10..28fed9d87f6 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -1890,7 +1890,8 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt) * For now, we don't support resjunk sort clauses on the output of a * setOperation tree --- you can only use the SQL92-spec options of * selecting an output column by name or number. Enforce by checking that - * transformSortClause doesn't add any items to tlist. + * transformSortClause doesn't add any items to tlist. Note, if changing + * this, add_setop_child_rel_equivalences() will need to be updated. */ tllen = list_length(qry->targetList); |