Skip to content

Commit fdda78e

Browse files
committed
Fix possible usage of incorrect UPPERREL_SETOP RelOptInfo
03d40e4 allowed dummy UNION [ALL] children to be removed from the plan by checking for is_dummy_rel(). That commit neglected to still account for the relids from the dummy rel so that the correct UPPERREL_SETOP RelOptInfo could be found and used for adding the Paths to. Not doing this could result in processing of subsequent UNIONs using the same RelOptInfo as a previously processed UNION, which could result in add_path() freeing old Paths that are needed by the previous UNION. The same fix was independently submitted (2 mins later) by Richard Guo. Reported-by: Alexander Lakhin <exclusion@gmail.com> Author: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/bee34aec-659c-46f1-9ab7-7bbae0b7616c@gmail.com
1 parent 0a3d27b commit fdda78e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/backend/optimizer/prep/prepunion.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,12 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
773773
RelOptInfo *rel = lfirst(lc);
774774
Path *ordered_path;
775775

776+
/*
777+
* Record the relids so that we can identify the correct
778+
* UPPERREL_SETOP RelOptInfo below.
779+
*/
780+
relids = bms_add_members(relids, rel->relids);
781+
776782
/* Skip any UNION children that are proven not to yield any rows */
777783
if (is_dummy_rel(rel))
778784
continue;
@@ -815,8 +821,6 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
815821
partial_pathlist = lappend(partial_pathlist,
816822
linitial(rel->partial_pathlist));
817823
}
818-
819-
relids = bms_add_members(relids, rel->relids);
820824
}
821825

822826
/* Build result relation. */

src/test/regress/expected/union.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,14 @@ SELECT four FROM tenk1 WHERE 1=2
12601260
UNION
12611261
SELECT ten FROM tenk1 WHERE 1=2
12621262
ORDER BY 1;
1263-
QUERY PLAN
1264-
--------------------------------------
1263+
QUERY PLAN
1264+
-----------------------------------------------------------------------------------------
12651265
Sort
12661266
Output: unnamed_subquery.two
12671267
Sort Key: unnamed_subquery.two
12681268
-> Result
12691269
Output: unnamed_subquery.two
1270-
Replaces: Aggregate
1270+
Replaces: Aggregate on unnamed_subquery, unnamed_subquery_1, unnamed_subquery_2
12711271
One-Time Filter: false
12721272
(7 rows)
12731273

0 commit comments

Comments
 (0)