]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix incorrect targetlist in dummy UNIONs
authorDavid Rowley <drowley@postgresql.org>
Tue, 7 Oct 2025 01:15:04 +0000 (14:15 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 7 Oct 2025 01:15:04 +0000 (14:15 +1300)
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

src/backend/optimizer/plan/setrefs.c
src/backend/optimizer/prep/prepunion.c
src/test/regress/expected/union.out
src/test/regress/sql/union.sql

index 6950eff2c5ba71b5eec675dfd5704ff74f62ba39..ccdc9bc264ab93ed4fc06b6c853362c584d89d42 100644 (file)
@@ -1034,16 +1034,35 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
                                         * expected to occur here, it seems safer to special-case
                                         * it here and keep the assertions that ROWID_VARs
                                         * shouldn't be seen by fix_scan_expr.
+                                        *
+                                        * We also must handle the case where set operations have
+                                        * been short-circuited resulting in a dummy Result node.
+                                        * prepunion.c uses varno==0 for the set op targetlist.
+                                        * See generate_setop_tlist() and generate_setop_tlist().
+                                        * Here we rewrite these to use varno==1, which is the
+                                        * varno of the first set-op child.  Without this, EXPLAIN
+                                        * will have trouble displaying targetlists of dummy set
+                                        * operations.
                                         */
                                        foreach(l, splan->plan.targetlist)
                                        {
                                                TargetEntry *tle = (TargetEntry *) lfirst(l);
                                                Var                *var = (Var *) tle->expr;
 
-                                               if (var && IsA(var, Var) && var->varno == ROWID_VAR)
-                                                       tle->expr = (Expr *) makeNullConst(var->vartype,
-                                                                                                                          var->vartypmod,
-                                                                                                                          var->varcollid);
+                                               if (var && IsA(var, Var))
+                                               {
+                                                       if (var->varno == ROWID_VAR)
+                                                               tle->expr = (Expr *) makeNullConst(var->vartype,
+                                                                                                                                  var->vartypmod,
+                                                                                                                                  var->varcollid);
+                                                       else if (var->varno == 0)
+                                                               tle->expr = (Expr *) makeVar(1,
+                                                                                                                        var->varattno,
+                                                                                                                        var->vartype,
+                                                                                                                        var->vartypmod,
+                                                                                                                        var->varcollid,
+                                                                                                                        var->varlevelsup);
+                                               }
                                        }
 
                                        splan->plan.targetlist =
index 547dbd5354047dc5b32f25110b16cdb9a3aa1aad..da9431108a2a2035fa5928959ffd279d4a410a1f 100644 (file)
@@ -826,7 +826,6 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
        /* If all UNION children were dummy rels, make the resulting rel dummy */
        if (cheapest_pathlist == NIL)
        {
-               result_rel->reltarget = create_pathtarget(root, list_nth(tlist_list, 0));
                mark_dummy_rel(result_rel);
 
                return result_rel;
index 7c089e0d598807c269a4686464b4f1fc16f13c02..15931beea3a7822fa76afcb04fa5ffa85f6a7452 100644 (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)
index 56bd20e741cc6ecea31382e18aaaa93e03d11f23..e252316f69ba4b5bfd9df69798b6454aa388257b 100644 (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)