postgres.git / summary / log / commit / refs
commit 0e58f6d737515f6d7f5c910472ffe28dc6fa9f7b
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 95fd87b5a6c..e5e8c55c8f2 100644
--- a/src/backend/optimizer/path/joinpath.c
+++ b/src/backend/optimizer/path/joinpath.c
@@ -538,6 +538,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);
+
/*
* 'expr' may already exist as a parameter from a previous item in
* ppi_clauses. No need to include it again, however we'd better
diff --git a/src/test/regress/expected/collate.icu.utf8.out b/src/test/regress/expected/collate.icu.utf8.out
index 0e64bcde8e9..0133def0da0 100644
--- a/src/test/regress/expected/collate.icu.utf8.out
+++ b/src/test/regress/expected/collate.icu.utf8.out
@@ -2195,6 +2195,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;
-- These queries should be able to use the index on test1ci.x:
SET enable_seqscan = off;
diff --git a/src/test/regress/sql/collate.icu.utf8.sql b/src/test/regress/sql/collate.icu.utf8.sql
index 225d493d98f..77564319b6a 100644
--- a/src/test/regress/sql/collate.icu.utf8.sql
+++ b/src/test/regress/sql/collate.icu.utf8.sql
@@ -796,6 +796,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;
+
-- These queries should be able to use the index on test1ci.x:
SET enable_seqscan = off;
SET enable_indexonlyscan = off;
[parent: 6059658609c8]