postgres.git / summary / log / commit / refs
commit 7b4f814b8042f8bf9d7e2943181ea825c7cdf5ce
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu Nov 19 20:49:00 2026 +0000
Give a good error message when we can neither hash nor sort a UNION.
Prior to v17 we said
ERROR: could not implement UNION
DETAIL: Some of the datatypes only support hashing, while others only support sorting.
but this check got lost in refactoring, leading to a much less helpful
message. Put it back (in generate_union_paths). Also add a couple of
comparable checks in other places that seem like they could be
vulnerable to this problem, and add some test coverage.
Bug: #19916
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19916-8097ca71b8aa4085@postgresql.org
Backpatch-through: 17
src/backend/optimizer/path/allpaths.c | 11 +++++++++++
src/backend/optimizer/plan/planner.c | 14 ++++++++++++++
src/backend/optimizer/prep/prepunion.c | 13 +++++++++++++
src/test/regress/expected/union.out | 17 +++++++++++++++++
src/test/regress/sql/union.sql | 8 ++++++++
5 files changed, 63 insertions(+)
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 8ac2d666fc4..8166a1cf217 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -3573,6 +3573,17 @@ generate_grouped_paths(PlannerInfo *root, RelOptInfo *grouped_rel,
can_hash = (agg_info->group_clauses != NIL &&
grouping_is_hashable(agg_info->group_clauses));
+ /*
+ * Check that the operators support sorting or hashing. The parser would
+ * have checked that individual grouping columns support at least one, but
+ * it doesn't verify that they all support the same method.
+ */
+ if (!can_sort && !can_hash)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("could not implement GROUP BY"),
+ errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
+
/*
* Consider whether we should generate partially aggregated non-partial
* paths. We can only do this if we have a non-partial path.
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 0e7a6a7354f..36a57f24148 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -4157,6 +4157,18 @@ create_grouping_paths(PlannerInfo *root,
(gd ? gd->any_hashable : grouping_is_hashable(root->processed_groupClause))))
flags |= GROUPING_CAN_USE_HASH;
+ /*
+ * Check that the operators support sorting or hashing. The parser
+ * would have checked that individual grouping columns support at
+ * least one, but it doesn't verify that they all support the same
+ * method.
+ */
+ if ((flags & (GROUPING_CAN_USE_SORT | GROUPING_CAN_USE_HASH)) == 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("could not implement GROUP BY"),
+ errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
+
/*
* Determine whether partial aggregation is possible.
*/
@@ -5509,6 +5521,8 @@ create_final_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
numDistinctRows));
}
+ /* Caller will complain if we made no paths */
+
return distinct_rel;
}
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index d2724ea28ce..a4b13d3dabf 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -918,6 +918,19 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
bool can_hash = grouping_is_hashable(groupList);
Path *first_path = linitial(cheapest.subpaths);
+ /*
+ * Check that the operators support sorting or hashing. The parser
+ * would have checked that individual grouping columns support at
+ * least one, but it doesn't verify that they all support the same
+ * method.
+ */
+ if (!can_sort && !can_hash)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ /* translator: %s is UNION */
+ errmsg("could not implement %s", "UNION"),
+ errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
+
/*
* Estimate the number of UNION output rows. In the case when only a
* single UNION child remains, we can use estimate_num_groups() on
diff --git a/src/test/regress/expected/union.out b/src/test/regress/expected/union.out
index 69d75933a6c..66f875c6e17 100644
--- a/src/test/regress/expected/union.out
+++ b/src/test/regress/expected/union.out
@@ -847,6 +847,23 @@ explain (costs off) select '123'::xid union select '123'::xid;
(6 rows)
reset enable_hashagg;
+-- check for clean failure with incompatible types
+SELECT NULL::tsvector, NULL::aclitem[] UNION SELECT NULL, NULL;
+ERROR: could not implement UNION
+DETAIL: Some of the datatypes only support hashing, while others only support sorting.
+SELECT NULL::tsvector, NULL::aclitem[] INTERSECT SELECT NULL, NULL;
+ERROR: could not implement INTERSECT
+DETAIL: Some of the datatypes only support hashing, while others only support sorting.
+SELECT NULL::tsvector, NULL::aclitem[] EXCEPT SELECT NULL, NULL;
+ERROR: could not implement EXCEPT
+DETAIL: Some of the datatypes only support hashing, while others only support sorting.
+-- in these examples, the sortable column has to be non-constant to get error
+SELECT random()::text::tsvector, NULL::aclitem[] GROUP BY 1,2;
+ERROR: could not implement GROUP BY
+DETAIL: Some of the datatypes only support hashing, while others only support sorting.
+SELECT DISTINCT random()::text::tsvector, NULL::aclitem[];
+ERROR: could not implement DISTINCT
+DETAIL: Some of the datatypes only support hashing, while others only support sorting.
--
-- Mixed types
--
diff --git a/src/test/regress/sql/union.sql b/src/test/regress/sql/union.sql
index 3803b1cd1e1..2b4b42327f8 100644
--- a/src/test/regress/sql/union.sql
+++ b/src/test/regress/sql/union.sql
@@ -267,6 +267,14 @@ explain (costs off) select '123'::xid union select '123'::xid;
reset enable_hashagg;
+-- check for clean failure with incompatible types
+SELECT NULL::tsvector, NULL::aclitem[] UNION SELECT NULL, NULL;
+SELECT NULL::tsvector, NULL::aclitem[] INTERSECT SELECT NULL, NULL;
+SELECT NULL::tsvector, NULL::aclitem[] EXCEPT SELECT NULL, NULL;
+-- in these examples, the sortable column has to be non-constant to get error
+SELECT random()::text::tsvector, NULL::aclitem[] GROUP BY 1,2;
+SELECT DISTINCT random()::text::tsvector, NULL::aclitem[];
+
--
-- Mixed types
--
[parent: 61b11a31bf44]