From 3732441c7037b614464f739ad748aacb6ee4b805 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 26 Nov 2026 13:40:03 -0500 Subject: [PATCH 1/2] Fix merging of sort keys under nondeterministic collations Under a nondeterministic collation, give a sort key using a pattern_ops opfamily its own EquivalenceClass. Reported-by: chunling qin <303677365@qq.com> Discussion: https://postgr.es/m/19873-2b5e1e0dd7b9eac0@postgresql.org Discussion: https://postgr.es/m/19869-6cb4aeabc803c3a6@postgresql.org --- src/backend/optimizer/path/equivclass.c | 3 +- src/backend/optimizer/path/pathkeys.c | 13 ++++ .../regress/expected/collate.icu.utf8.out | 60 +++++++++++++++++++ src/test/regress/sql/collate.icu.utf8.sql | 22 +++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c index 248386a6298..33cfb100e87 100644 --- a/src/backend/optimizer/path/equivclass.c +++ b/src/backend/optimizer/path/equivclass.c @@ -731,7 +731,8 @@ add_child_eq_member(PlannerInfo *root, EquivalenceClass *ec, int ec_index, * Note: opfamilies must be chosen consistently with the way * process_equivalence() would do; that is, generated from a mergejoinable * equality operator. Else we might fail to detect valid equivalences, - * generating poor (but not incorrect) plans. + * generating poor (but not incorrect) plans. make_pathkey_from_sortinfo + * makes one exception. */ EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c index 50651813a11..eb3137ec86e 100644 --- a/src/backend/optimizer/path/pathkeys.c +++ b/src/backend/optimizer/path/pathkeys.c @@ -231,6 +231,19 @@ make_pathkey_from_sortinfo(PlannerInfo *root, elog(ERROR, "could not find opfamilies for equality operator %u", equality_op); + /* + * The pattern_ops opfamilies sort by bytes, but their equality operator + * follows the collation. Under a nondeterministic collation they thus + * sort apart values that are equal. Give such a sort key an EC of its + * own, so that it is not merged with equality clauses or other sort keys. + * Like index_create(), we have no better way to identify them. + */ + if ((opfamily == TEXT_PATTERN_BTREE_FAM_OID || + opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) && + OidIsValid(collation) && + !get_collation_isdeterministic(collation)) + opfamilies = list_make1_oid(opfamily); + /* Now find or (optionally) create a matching EquivalenceClass */ eclass = get_eclass_for_sort_expr(root, expr, opfamilies, opcintype, collation, diff --git a/src/test/regress/expected/collate.icu.utf8.out b/src/test/regress/expected/collate.icu.utf8.out index 636f0dfefc3..d2f6fcf12ea 100644 --- a/src/test/regress/expected/collate.icu.utf8.out +++ b/src/test/regress/expected/collate.icu.utf8.out @@ -3985,6 +3985,66 @@ SELECT count(*) FROM (SELECT NULLIF(a, NULL::text COLLATE case_insensitive) FROM (1 row) DROP TABLE simplify_collation_t; +-- Under a nondeterministic collation, a sort key using ~<~ must not be +-- merged with other sort keys or clauses that use "=" +CREATE TABLE sortkey_ci_t (x text COLLATE case_insensitive); +INSERT INTO sortkey_ci_t VALUES ('b'), ('a'), ('B'), ('A'), ('a'), ('A'); +EXPLAIN (COSTS OFF) +SELECT x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; + QUERY PLAN +------------------------------------------------------------------------------ + Sort + Sort Key: x COLLATE case_insensitive, x COLLATE case_insensitive USING ~<~ + -> Seq Scan on sortkey_ci_t +(3 rows) + +SELECT x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; + x +--- + A + A + a + a + B + b +(6 rows) + +SELECT DISTINCT ON (x) x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; + x +--- + A + B +(2 rows) + +SELECT string_agg(x, ',' ORDER BY x USING <, x USING ~<~) FROM sortkey_ci_t; + string_agg +------------- + A,A,a,a,B,b +(1 row) + +-- an equality with a constant must not remove the sort +SELECT x FROM sortkey_ci_t WHERE x = 'a' ORDER BY x USING ~<~; + x +--- + A + A + a + a +(4 rows) + +-- a merge join must not use the ~<~ ordering +SET enable_hashjoin = off; +SET enable_nestloop = off; +SELECT count(*) FROM (SELECT t1.x FROM sortkey_ci_t t1 + JOIN sortkey_ci_t t2 ON t1.x = t2.x ORDER BY t1.x USING ~<~) s; + count +------- + 20 +(1 row) + +RESET enable_hashjoin; +RESET enable_nestloop; +DROP TABLE sortkey_ci_t; -- virtual generated columns CREATE TABLE t5 ( a int, diff --git a/src/test/regress/sql/collate.icu.utf8.sql b/src/test/regress/sql/collate.icu.utf8.sql index b2d6f7d64ca..684257365c5 100644 --- a/src/test/regress/sql/collate.icu.utf8.sql +++ b/src/test/regress/sql/collate.icu.utf8.sql @@ -1571,6 +1571,28 @@ SELECT count(*) FROM (SELECT NULLIF(a, NULL::text COLLATE case_insensitive) FROM DROP TABLE simplify_collation_t; +-- Under a nondeterministic collation, a sort key using ~<~ must not be +-- merged with other sort keys or clauses that use "=" +CREATE TABLE sortkey_ci_t (x text COLLATE case_insensitive); +INSERT INTO sortkey_ci_t VALUES ('b'), ('a'), ('B'), ('A'), ('a'), ('A'); + +EXPLAIN (COSTS OFF) +SELECT x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; +SELECT x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; +SELECT DISTINCT ON (x) x FROM sortkey_ci_t ORDER BY x USING <, x USING ~<~; +SELECT string_agg(x, ',' ORDER BY x USING <, x USING ~<~) FROM sortkey_ci_t; +-- an equality with a constant must not remove the sort +SELECT x FROM sortkey_ci_t WHERE x = 'a' ORDER BY x USING ~<~; +-- a merge join must not use the ~<~ ordering +SET enable_hashjoin = off; +SET enable_nestloop = off; +SELECT count(*) FROM (SELECT t1.x FROM sortkey_ci_t t1 + JOIN sortkey_ci_t t2 ON t1.x = t2.x ORDER BY t1.x USING ~<~) s; +RESET enable_hashjoin; +RESET enable_nestloop; + +DROP TABLE sortkey_ci_t; + -- virtual generated columns CREATE TABLE t5 ( a int, -- 2.37.1 (Apple Git-137.1)