postgres.git / summary / log / commit / refs

commit    2da9b7f9b22dbcc64bffeab3612fc9f3ca063db9
Author:   David Rowley <drowley@postgresql.org>
Date:     Wed Nov 04 00:43:17 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix Memoize to correctly use clause's input collation
    
    When building Memoize cache keys from a parameterized path's join clauses,
    paraminfo_get_equal_hashops() used the outer side expression of each
    clause verbatim, which meant the cache was hashed and probed using that
    expression's collation rather than the input collation of the join
    condition.
    
    If the expression's collation is nondeterministic and the clause compares
    using a different collation, values that the clause can distinguish, such
    as 'abc' and 'ABC' under a case-insensitive collation, could be treated as
    equal by the cache.  This could cause a lookup to return the cached
    results for another value, giving wrong query results.
    
    Here we fix this by relabeling the cache key expression with the join
    clause's input collation, so that cache lookups match values the same way
    as the join clause does.
    
    Author: shihao zhong <zhong950419@gmail.com>
    Reviewed-by: Manu <manuelreyesbravo@gmail.com>
    Reviewed-by: David Rowley <dgrowleyml@gmail.com>
    Discussion: https://postgr.es/m/CAGRkXqTBsBegZ8rNMYjwkeu1V5wRQ06zR6jTVickqP_3E7mb8g@mail.gmail.com
    Backpatch-through: 14


src/backend/optimizer/path/joinpath.c | 11 +++++++ src/test/regress/expected/collate.icu.utf8.out | 42 ++++++++++++++++++++++++++ src/test/regress/sql/collate.icu.utf8.sql | 25 +++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index 069cabc7418..c170c5cf5a4 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -456,6 +456,17 @@ paraminfo_get_equal_hashops(PlannerInfo *root, ParamPathInfo *param_info, return false; } + /* + * Ensure we use the clause's input collation so that cache + * lookups are performed using that collation rather than the + * expr's collation. This is not required for binary mode, which + * compares keys bit-by-bit, but we don't yet know which mode + * we'll end up in. + */ + expr = (Node *) canonicalize_ec_expression((Expr *) expr, + exprType(expr), + opexpr->inputcollid); + *operators = lappend_oid(*operators, hasheqoperator); *param_exprs = lappend(*param_exprs, expr); diff --git a/src/test/regress/expected/collate.icu.utf8.out b/src/test/regress/expected/collate.icu.utf8.out index 473f38026b5..0ef476cdcca 100644 --- a/src/test/regress/expected/collate.icu.utf8.out +++ b/src/test/regress/expected/collate.icu.utf8.out @@ -1865,6 +1865,48 @@ WHERE x COLLATE case_insensitive IN (SELECT x FROM test3cs); 3 (1 row) +ROLLBACK; +-- Memoize must not share a cache entry between 'abc' and 'ABC' when the join +-- clause can tell them apart. The planner won't choose Memoize without +-- a valid ndistinct estimate, so create a new table and analyze it. +BEGIN; +CREATE TABLE test_memoize_ci (x text COLLATE case_insensitive); +INSERT INTO test_memoize_ci SELECT x FROM test3ci, generate_series(1, 10); +ANALYZE test_memoize_ci; +SET LOCAL enable_hashjoin TO off; +SET LOCAL enable_mergejoin TO off; +-- Ensure we get a Memoize plan +EXPLAIN (COSTS OFF) +SELECT t2.x, count(*) FROM test_memoize_ci t1 + LEFT JOIN test3cs t2 ON t2.x COLLATE case_sensitive = t1.x +GROUP BY t2.x ORDER BY t2.x; + QUERY PLAN +--------------------------------------------------------------------------- + Sort + Sort Key: t2.x COLLATE case_sensitive + -> HashAggregate + Group Key: t2.x + -> Nested Loop Left Join + -> Seq Scan on test_memoize_ci t1 + -> Memoize + Cache Key: t1.x + Cache Mode: logical + -> Index Only Scan using test3cs_x_idx on test3cs t2 + Index Cond: (x = t1.x) +(11 rows) + +-- Ensure we get the expected results +SELECT t2.x, count(*) FROM test_memoize_ci t1 + LEFT JOIN test3cs t2 ON t2.x COLLATE case_sensitive = t1.x +GROUP BY t2.x ORDER BY t2.x; + x | count +-----+------- + abc | 10 + ABC | 10 + def | 10 + ghi | 10 +(4 rows) + ROLLBACK; -- bpchar CREATE TABLE test1bpci (x char(3) COLLATE case_insensitive); diff --git a/src/test/regress/sql/collate.icu.utf8.sql b/src/test/regress/sql/collate.icu.utf8.sql index 917ff524565..030c265b3d8 100644 --- a/src/test/regress/sql/collate.icu.utf8.sql +++ b/src/test/regress/sql/collate.icu.utf8.sql @@ -688,6 +688,31 @@ WHERE x COLLATE case_insensitive IN (SELECT x FROM test3cs); ROLLBACK; +-- Memoize must not share a cache entry between 'abc' and 'ABC' when the join +-- clause can tell them apart. The planner won't choose Memoize without +-- a valid ndistinct estimate, so create a new table and analyze it. +BEGIN; + +CREATE TABLE test_memoize_ci (x text COLLATE case_insensitive); +INSERT INTO test_memoize_ci SELECT x FROM test3ci, generate_series(1, 10); +ANALYZE test_memoize_ci; + +SET LOCAL enable_hashjoin TO off; +SET LOCAL enable_mergejoin TO off; + +-- Ensure we get a Memoize plan +EXPLAIN (COSTS OFF) +SELECT t2.x, count(*) FROM test_memoize_ci t1 + LEFT JOIN test3cs t2 ON t2.x COLLATE case_sensitive = t1.x +GROUP BY t2.x ORDER BY t2.x; + +-- Ensure we get the expected results +SELECT t2.x, count(*) FROM test_memoize_ci t1 + LEFT JOIN test3cs t2 ON t2.x COLLATE case_sensitive = t1.x +GROUP BY t2.x ORDER BY t2.x; + +ROLLBACK; + -- bpchar CREATE TABLE test1bpci (x char(3) COLLATE case_insensitive); CREATE TABLE test2bpci (x char(3) COLLATE case_insensitive); [parent: c859616f14b5]