Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit928df06

Browse files
committed
Fix incorrect targetlist in dummy UNIONs
The prior code, added in03d40e4 attempted to use the targetlist of thefirst 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 mustfind target entries within the Result's targetlist. This could have beensomething as simple as trying to sort the results of the UNION operation,which would lead to:ERROR: could not find pathkey item to sortInstead, use the top-level UNION's targetlist and fix the varnos insetrefs.c. Because set operation targetlists always use varno==0, wecan rewrite those to become varno==1, i.e. use the Vars from the firstUNION child. This does result in showing Vars from relations that arenot present in the final plan, but that's no different to what we seewhen normal base relations are proven dummy.Without this fix it would be possible to see the following error inEXPLAIN VERBOSE when all UNION inputs were proven empty.ERROR: bogus varno: 0Author: David Rowley <dgrowleyml@gmail.com>Discussion:https://postgr.es/m/CAApHDvrUASy9sfULMEsM2udvZJP6AoBRCZvHYXYxZTy2tX9FYw@mail.gmail.com
1 parent771cfe2 commit928df06

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

‎src/backend/optimizer/plan/setrefs.c‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,16 +1034,35 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
10341034
* expected to occur here, it seems safer to special-case
10351035
* it here and keep the assertions that ROWID_VARs
10361036
* shouldn't be seen by fix_scan_expr.
1037+
*
1038+
* We also must handle the case where set operations have
1039+
* been short-circuited resulting in a dummy Result node.
1040+
* prepunion.c uses varno==0 for the set op targetlist.
1041+
* See generate_setop_tlist() and generate_setop_tlist().
1042+
* Here we rewrite these to use varno==1, which is the
1043+
* varno of the first set-op child. Without this, EXPLAIN
1044+
* will have trouble displaying targetlists of dummy set
1045+
* operations.
10371046
*/
10381047
foreach(l,splan->plan.targetlist)
10391048
{
10401049
TargetEntry*tle= (TargetEntry*)lfirst(l);
10411050
Var*var= (Var*)tle->expr;
10421051

1043-
if (var&&IsA(var,Var)&&var->varno==ROWID_VAR)
1044-
tle->expr= (Expr*)makeNullConst(var->vartype,
1045-
var->vartypmod,
1046-
var->varcollid);
1052+
if (var&&IsA(var,Var))
1053+
{
1054+
if (var->varno==ROWID_VAR)
1055+
tle->expr= (Expr*)makeNullConst(var->vartype,
1056+
var->vartypmod,
1057+
var->varcollid);
1058+
elseif (var->varno==0)
1059+
tle->expr= (Expr*)makeVar(1,
1060+
var->varattno,
1061+
var->vartype,
1062+
var->vartypmod,
1063+
var->varcollid,
1064+
var->varlevelsup);
1065+
}
10471066
}
10481067

10491068
splan->plan.targetlist=

‎src/backend/optimizer/prep/prepunion.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,6 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
826826
/* If all UNION children were dummy rels, make the resulting rel dummy */
827827
if (cheapest_pathlist==NIL)
828828
{
829-
result_rel->reltarget=create_pathtarget(root,list_nth(tlist_list,0));
830829
mark_dummy_rel(result_rel);
831830

832831
returnresult_rel;

‎src/test/regress/expected/union.out‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,14 +1258,18 @@ SELECT two FROM tenk1 WHERE 1=2
12581258
UNION
12591259
SELECT four FROM tenk1 WHERE 1=2
12601260
UNION
1261-
SELECT ten FROM tenk1 WHERE 1=2;
1262-
QUERY PLAN
1263-
--------------------------------
1264-
Result
1261+
SELECT ten FROM tenk1 WHERE 1=2
1262+
ORDER BY 1;
1263+
QUERY PLAN
1264+
--------------------------------------
1265+
Sort
12651266
Output: unnamed_subquery.two
1266-
Replaces: Aggregate
1267-
One-Time Filter: false
1268-
(4 rows)
1267+
Sort Key: unnamed_subquery.two
1268+
-> Result
1269+
Output: unnamed_subquery.two
1270+
Replaces: Aggregate
1271+
One-Time Filter: false
1272+
(7 rows)
12691273

12701274
-- Test constraint exclusion of UNION ALL subqueries
12711275
explain (costs off)

‎src/test/regress/sql/union.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ SELECT two FROM tenk1 WHERE 1=2
484484
UNION
485485
SELECT fourFROM tenk1WHERE1=2
486486
UNION
487-
SELECT tenFROM tenk1WHERE1=2;
487+
SELECT tenFROM tenk1WHERE1=2
488+
ORDER BY1;
488489

489490
-- Test constraint exclusion of UNION ALL subqueries
490491
explain (costs off)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp