postgres.git / summary / log / commit / refs
commit 1e8a3033a9855076e9705b0f8f9f36d3f1bb9e34
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/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 ++++++++
4 files changed, 52 insertions(+)
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 9a8bf158daa..726aeaef017 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -3983,6 +3983,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.
*/
@@ -5376,6 +5388,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 d4c683b6b46..24d6e662641 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -891,6 +891,19 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
bool can_sort = grouping_is_sortable(groupList);
bool can_hash = grouping_is_hashable(groupList);
+ /*
+ * 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.")));
+
/*
* XXX for the moment, take the number of distinct groups as equal to
* the total input size, i.e., the worst case. This is too
diff --git a/src/test/regress/expected/union.out b/src/test/regress/expected/union.out
index 882b075b9a1..52d834ba1c4 100644
--- a/src/test/regress/expected/union.out
+++ b/src/test/regress/expected/union.out
@@ -829,6 +829,23 @@ explain (costs off) select '123'::xid union select '123'::xid;
(5 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 c52ca012b5c..ababc06b14c 100644
--- a/src/test/regress/sql/union.sql
+++ b/src/test/regress/sql/union.sql
@@ -252,6 +252,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: 43522ad56b4a]