aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2024-05-21 12:54:09 -0400
committerRobert Haas <rhaas@postgresql.org>2024-05-21 12:54:09 -0400
commitc37267162e889fe783786b9e28d1b65b82365a00 (patch)
tree0cbb5e2dbf8351f919cdd355a9f0a198c0db5b75 /src/backend/optimizer
parent12933dc6048902ba891f9572cab96981f50ef669 (diff)
downloadpostgresql-REL_17_BETA1.tar.gz
postgresql-REL_17_BETA1.zip
Fix generate_union_paths for non-sortable types.REL_17_BETA1
The previous logic would fail to set groupList when grouping_is_sortable() returned false, which could cause queries to return wrong answers when some columns were not sortable. David Rowley, reviewed by Heikki Linnakangas and Álvaro Herrera. Reported by Hubert "depesz" Lubaczewski. Discussion: http://postgr.es/m/Zktzf926vslR35Fv@depesz.com Discussion: http://postgr.es/m/CAApHDvra=c8_zZT0J-TftByWN2Y+OJfnjNJFg4Dfdi2s+QzmqA@mail.gmail.com
Diffstat (limited to 'src/backend/optimizer')
-rw-r--r--src/backend/optimizer/prep/prepunion.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 30068c27a13..1c69c6e97e8 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -498,7 +498,7 @@ generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root,
* interesting_pathkeys: if not NIL, also include paths that suit these
* pathkeys, sorting any unsorted paths as required.
* *pNumGroups: if not NULL, we estimate the number of distinct groups
- * in the result, and store it there
+ * in the result, and store it there.
*/
static void
build_setop_child_paths(PlannerInfo *root, RelOptInfo *rel,
@@ -714,7 +714,7 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
List *groupList = NIL;
Path *apath;
Path *gpath = NULL;
- bool try_sorted;
+ bool try_sorted = false;
List *union_pathkeys = NIL;
/*
@@ -740,18 +740,21 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
tlist_list, refnames_tlist);
*pTargetList = tlist;
- /* For for UNIONs (not UNION ALL), try sorting, if sorting is possible */
- try_sorted = !op->all && grouping_is_sortable(op->groupClauses);
-
- if (try_sorted)
+ /* For UNIONs (not UNION ALL), try sorting, if sorting is possible */
+ if (!op->all)
{
/* Identify the grouping semantics */
groupList = generate_setop_grouplist(op, tlist);
- /* Determine the pathkeys for sorting by the whole target list */
- union_pathkeys = make_pathkeys_for_sortclauses(root, groupList, tlist);
+ if (grouping_is_sortable(op->groupClauses))
+ {
+ try_sorted = true;
+ /* Determine the pathkeys for sorting by the whole target list */
+ union_pathkeys = make_pathkeys_for_sortclauses(root, groupList,
+ tlist);
- root->query_pathkeys = union_pathkeys;
+ root->query_pathkeys = union_pathkeys;
+ }
}
/*