1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

Fix incorrect targetlist in dummy UNIONs

The prior code, added in 03d40e4b5 attempted to use the targetlist of the
first UNION child when all UNION children were proven as dummy rels.
That's not going to work when some operation atop of the Result node must
find target entries within the Result's targetlist.  This could have been
something as simple as trying to sort the results of the UNION operation,
which would lead to:

ERROR:  could not find pathkey item to sort

Instead, use the top-level UNION's targetlist and fix the varnos in
setrefs.c.  Because set operation targetlists always use varno==0, we
can rewrite those to become varno==1, i.e. use the Vars from the first
UNION child.  This does result in showing Vars from relations that are
not present in the final plan, but that's no different to what we see
when normal base relations are proven dummy.

Without this fix it would be possible to see the following error in
EXPLAIN VERBOSE when all UNION inputs were proven empty.

ERROR:  bogus varno: 0

Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CAApHDvrUASy9sfULMEsM2udvZJP6AoBRCZvHYXYxZTy2tX9FYw@mail.gmail.com
This commit is contained in:
David Rowley
2025-10-07 14:15:04 +13:00
parent 771cfe22a0
commit 928df067d1
4 changed files with 36 additions and 13 deletions

View File

@@ -1258,14 +1258,18 @@ SELECT two FROM tenk1 WHERE 1=2
UNION
SELECT four FROM tenk1 WHERE 1=2
UNION
SELECT ten FROM tenk1 WHERE 1=2;
QUERY PLAN
--------------------------------
Result
SELECT ten FROM tenk1 WHERE 1=2
ORDER BY 1;
QUERY PLAN
--------------------------------------
Sort
Output: unnamed_subquery.two
Replaces: Aggregate
One-Time Filter: false
(4 rows)
Sort Key: unnamed_subquery.two
-> Result
Output: unnamed_subquery.two
Replaces: Aggregate
One-Time Filter: false
(7 rows)
-- Test constraint exclusion of UNION ALL subqueries
explain (costs off)

View File

@@ -484,7 +484,8 @@ SELECT two FROM tenk1 WHERE 1=2
UNION
SELECT four FROM tenk1 WHERE 1=2
UNION
SELECT ten FROM tenk1 WHERE 1=2;
SELECT ten FROM tenk1 WHERE 1=2
ORDER BY 1;
-- Test constraint exclusion of UNION ALL subqueries
explain (costs off)