postgres.git / summary / log / commit / refs

commit    514d9ce3078a7610186d8bdf1f5991925000a0ab
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 d7959a479f8..f997d191097 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -4075,6 +4075,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. */ @@ -5434,6 +5446,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 eab44da65b8..eb19b161207 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -871,6 +871,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 984db95608c..694d05d0d8f 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 bc50bc40ded..6bb9c0f3d6e 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: 36ff18f94841]