From c1e3389d3f6db95b67c77cde0f918b2ab89bdfc1 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 19 Nov 2026 00:00:09 -0500 Subject: [PATCH 1/2] Reject pattern_ops ordering operators for nondeterministic collations Their ordering disagrees with equality there, so ORDER BY gave wrong results. CREATE INDEX already rejects this combination. Reported-by: chunling qin <303677365@qq.com> Bug: #19873 Discussion: https://postgr.es/m/19873-2b5e1e0dd7b9eac0@postgresql.org Discussion: https://postgr.es/m/19869-6cb4aeabc803c3a6@postgresql.org --- src/backend/catalog/index.c | 9 ++-- src/backend/parser/parse_collate.c | 65 +++++++++++++++++++++++++++++ src/backend/utils/cache/lsyscache.c | 22 ++++++++++ src/include/utils/lsyscache.h | 1 + 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 99413556572..97f280b8e95 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -823,7 +823,8 @@ index_create(Relation heapRelation, * The same applies to varchar_pattern_ops and bpchar_pattern_ops. If we * find more cases, we might decide to create a real mechanism for marking * opclasses as incompatible with nondeterminism; but for now, this small - * hack suffices. + * hack suffices. opfamily_conflicts_with_collation() holds the list, + * which parse analysis also consults for ORDER BY ... USING. * * Another solution is to use a special operator, not texteq, as the * equality opclass member; but that is undesirable because it would @@ -836,10 +837,8 @@ index_create(Relation heapRelation, if (collation) { - if ((opclass == TEXT_BTREE_PATTERN_OPS_OID || - opclass == VARCHAR_BTREE_PATTERN_OPS_OID || - opclass == BPCHAR_BTREE_PATTERN_OPS_OID) && - !get_collation_isdeterministic(collation)) + if (opfamily_conflicts_with_collation(get_opclass_family(opclass), + collation)) { HeapTuple classtup; diff --git a/src/backend/parser/parse_collate.c b/src/backend/parser/parse_collate.c index ba7df2a7789..51041d736c1 100644 --- a/src/backend/parser/parse_collate.c +++ b/src/backend/parser/parse_collate.c @@ -44,6 +44,7 @@ #include "catalog/pg_collation.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" +#include "optimizer/optimizer.h" #include "parser/parse_collate.h" #include "utils/lsyscache.h" @@ -87,6 +88,8 @@ static void assign_ordered_set_collations(Aggref *aggref, assign_collations_context *loccontext); static void assign_hypothetical_collations(Aggref *aggref, assign_collations_context *loccontext); +static void check_sort_collations(ParseState *pstate, List *sortlist, + List *targetlist); /* @@ -100,6 +103,8 @@ static void assign_hypothetical_collations(Aggref *aggref, void assign_query_collations(ParseState *pstate, Query *query) { + ListCell *lc; + /* * We just use query_tree_walker() to visit all the contained expressions. * We can skip the rangetable and CTE subqueries, though, since RTEs and @@ -111,6 +116,20 @@ assign_query_collations(ParseState *pstate, Query *query) pstate, QTW_IGNORE_RANGE_TABLE | QTW_IGNORE_CTE_SUBQUERIES); + + /* + * Now that the sort keys have collations, check that their ordering + * operators can be used with them. We need not look at GROUP BY, + * DISTINCT or PARTITION BY items, since those can only have borrowed a + * non-default ordering operator from the associated ORDER BY list. + */ + check_sort_collations(pstate, query->sortClause, query->targetList); + foreach(lc, query->windowClause) + { + WindowClause *wc = lfirst_node(WindowClause, lc); + + check_sort_collations(pstate, wc->orderClause, query->targetList); + } } /* @@ -618,6 +637,11 @@ assign_collations_walker(Node *node, assign_collations_context *context) assign_expr_collations(context->pstate, (Node *) aggref->aggfilter); + + /* Now we can check the ORDER BY operators */ + check_sort_collations(context->pstate, + aggref->aggorder, + aggref->args); } break; case T_WindowFunc: @@ -1056,3 +1080,44 @@ assign_hypothetical_collations(Aggref *aggref, } Assert(h_cell == NULL && s_cell == NULL); } + +/* + * check_sort_collations + * Complain if any ordering operator in the sort list can't be used with + * the collation of its sort key. + * + * sortlist is a list of SortGroupClauses that reference targetlist. This + * can't be done when the SortGroupClauses are made, because the sort keys + * don't have their collations yet. + */ +static void +check_sort_collations(ParseState *pstate, List *sortlist, List *targetlist) +{ + ListCell *lc; + + foreach(lc, sortlist) + { + SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc); + TargetEntry *tle = get_sortgroupclause_tle(sortcl, targetlist); + Oid collation = exprCollation((Node *) tle->expr); + Oid opfamily; + Oid opcintype; + CompareType cmptype; + + /* Only nondeterministic collations can be a problem */ + if (!OidIsValid(collation) || + get_collation_isdeterministic(collation)) + continue; + + if (get_ordering_op_properties(sortcl->sortop, + &opfamily, &opcintype, &cmptype) && + opfamily_conflicts_with_collation(opfamily, collation)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("nondeterministic collations are not supported for ordering operator %s", + get_opname(sortcl->sortop)), + errhint("You can sort in byte order by applying COLLATE \"C\" to the expression."), + parser_errposition(pstate, + exprLocation((Node *) tle->expr)))); + } +} diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c index 65b2d64529a..dfcd66f032c 100644 --- a/src/backend/utils/cache/lsyscache.c +++ b/src/backend/utils/cache/lsyscache.c @@ -1290,6 +1290,28 @@ get_collation_isdeterministic(Oid colloid) return result; } +/* + * opfamily_conflicts_with_collation + * Is this btree opfamily's ordering inconsistent with its equality + * operator under the given collation? + * + * The pattern_ops opfamilies sort bitwise, but their equality member is the + * datatype's regular equality operator, which follows the collation. Under + * a nondeterministic collation, values that are equal per that operator can + * sort apart, so the opfamily must not be used with such a collation. + * + * The list lives here so that CREATE INDEX and ORDER BY ... USING reject the + * same opfamilies. + */ +bool +opfamily_conflicts_with_collation(Oid opfamily, Oid collation) +{ + if (opfamily != TEXT_PATTERN_BTREE_FAM_OID && + opfamily != BPCHAR_PATTERN_BTREE_FAM_OID) + return false; + return OidIsValid(collation) && !get_collation_isdeterministic(collation); +} + /* ---------- CONSTRAINT CACHE ---------- */ /* diff --git a/src/include/utils/lsyscache.h b/src/include/utils/lsyscache.h index 171fbda9a71..efdcf548296 100644 --- a/src/include/utils/lsyscache.h +++ b/src/include/utils/lsyscache.h @@ -105,6 +105,7 @@ extern Datum get_attoptions(Oid relid, int16 attnum); extern Oid get_cast_oid(Oid sourcetypeid, Oid targettypeid, bool missing_ok); extern char *get_collation_name(Oid colloid); extern bool get_collation_isdeterministic(Oid colloid); +extern bool opfamily_conflicts_with_collation(Oid opfamily, Oid collation); extern char *get_constraint_name(Oid conoid); extern Oid get_constraint_index(Oid conoid); extern char get_constraint_type(Oid conoid); -- 2.37.1 (Apple Git-137.1)