[PG19] Wrong results from Memoize with a nondeterministic collation
Hi hackers,
I used Opus to analyze the new features in PG19, and it found a query
that 18 gets right and 19 gets wrong.
CREATE COLLATION case_insensitive (provider = icu,
locale = 'und-u-ks-level2', deterministic = false);
CREATE TABLE orders (code text COLLATE case_insensitive);
CREATE TABLE products (code text COLLATE "C" PRIMARY KEY);
INSERT INTO orders
SELECT CASE WHEN g % 2 = 1 THEN 'p1' ELSE 'P1' END
FROM generate_series(1, 2000) g;
INSERT INTO products
SELECT 'p' || g FROM generate_series(1, 50000) g;
ANALYZE orders, products;
SELECT count(*) FROM orders o WHERE NOT EXISTS
(SELECT 1 FROM products p WHERE p.code COLLATE "C" = o.code);
18 gives 1000, the 'P1' orders. 19 gives 0.
Since 0da29e4cb16 an anti join can use Memoize. Memoize compares its
cache key with the collation of o.code, so 'P1' hits the cache entry
of 'p1'. The clause compares under "C", where they differ.
The same clause in a LEFT JOIN is wrong on 14 and up, there the 'P1'
orders come out joined to 'p1'. So it is an old bug with a new way
in. It needs a nondeterministic collation and a COLLATE on the inner
side of the clause, so I doubt many people hit it. The attached
memoize-collation.sql runs both queries.
0001 labels the cache key with the clause's input collation in
paraminfo_get_equal_hashops(), like d237a7a8366 did for a semijoin's
RHS.Inner joins already get this from equivalence classes. EXPLAIN
output stays the same. Switching to binary mode on a mismatch would
also work, but it changes the cache mode of plans that are right
today. 0002 is a test, it is optional.
0001 applies to 17 through 19. On 14 to 16 the hunk has to be placed
by hand. The NOT EXISTS case is a regression from 18, since anti
joins could not use Memoize there. It is a narrow one, so I will
leave the open items list to you.
0002 is a test only patch.
Thanks,
Shihao
Attachments:
t254100_1v1-0001-Use-the-join-clause-s-collation-for-Memoize-cache.patchapplication/octet-stream; name=v1-0001-Use-the-join-clause-s-collation-for-Memoize-cache.patchDownload+9-1
v1-0002-Add-a-test-for-Memoize-with-mismatched-collations.patchapplication/octet-stream; name=v1-0002-Add-a-test-for-Memoize-with-mismatched-collations.patchDownload+62-1
Hi Shihao,
So it is an old bug with a new way in.
Confirmed, and the split is clean when measured on the current tip of
each branch: the LEFT JOIN wrong results are there from 14, while the
NOT EXISTS (anti join) case appears only from 19, where 0da29e4cb16
let anti joins use Memoize -- matching your "regression from 18".
0001 fixes it and make check passes on all of 14 through 19 and
master. I also checked it holds across several nondeterministic
collations, a two-column key, a varchar outer column, a non-Var outer
expression, a partitioned inner rel, and LEFT JOIN LATERAL (already
right, since lateral_vars go into binary mode).
One caveat for the back branches: on 14-16 the new block must go
before the lappend into *param_exprs -- there is no list_member test
there. patch/git apply with fuzz instead places it after, which
still compiles and passes make check but leaves the bug unfixed (I
measured that on 16). A mechanical back-patch would look fine and
fix nothing; I can post the by-hand hunks for 14-16 if that helps.
Manu
Hi Manu,
Yes, please post the hand-placed hunks for 14 to 16.
I also add a new commitfeast entry
https://commitfest.postgresql.org/patch/7421/
Thanks,
Shihao
Hi Shihao,
Yes, please post the hand-placed hunks for 14 to 16.
Attached, one per branch. In 14-16 paraminfo_get_equal_hashops has no
list_member() dedup, so the block goes right after the clause's outer
expr is chosen and before it is appended to *param_exprs, so the
canonicalized expr is what becomes the cache key.
I re-verified each on the current tip of its branch (14.24, 15.19,
16.15): make check passes, and the stress oracle (enable_memoize on vs
off over a matrix of nondeterministic-collation shapes) goes from 39/52
wrong cases to 0/52, with identical row counts. For contrast I also
built the mechanical fuzz-placed hunk: it compiles and make check still
passes, but it puts the block after the append and leaves all 39 cases
wrong, which is why these are placed by hand.
The LEFT JOIN case is the one reachable on 14-16; the anti-join path is
19+ only.
Regards,
Manu
Attachments:
v1-PG14-0001-Use-the-join-clause-s-collation-for-Memoize-cache-key.patchtext/x-patchDownload+9-0
v1-PG15-0001-Use-the-join-clause-s-collation-for-Memoize-cache-key.patchtext/x-patchDownload+9-0
v1-PG16-0001-Use-the-join-clause-s-collation-for-Memoize-cache-key.patchtext/x-patchDownload+9-0
On Thu, Oct 8, 2026 at 12:33 PM shihao zhong <zhong950419@gmail.com> wrote:
Since 0da29e4cb16 an anti join can use Memoize. Memoize compares its
cache key with the collation of o.code, so 'P1' hits the cache entry
of 'p1'. The clause compares under "C", where they differ.
The root cause here is that Memoize hashes and compares its cache key
using the collation of the outer expression rather than the input
collation of the join operator. That code dates back to v14, and the
LEFT JOIN form of the same query gives wrong results on all supported
branches.
0da29e4cb16 doesn't change how Memoize builds its cache keys. It only
allows one more join type to reach the same code, and the proposed fix
doesn't touch anything that commit added. So I don't think this
belongs under Open Items for v19.
- Richard
Hi Richard,
Thanks for looking. Agreed, the cause is old and the fix does not
touch anything 0da29e4cb16 added. I will move the entry to "Older
bugs affecting stable branches".
Thanks,
Shihao
On Thu, Oct 29, 2026 at 10:45 AM shihao zhong <zhong950419@gmail.com> wrote:
I will move the entry to "Older
bugs affecting stable branches".
I've already done that.
- Richard
I've already done that.
I think we were doing it at the sametime and this is my first time
seeing conflict resolution on the wiki page, it is clear it does not have
page lock..
LOL
On Thu, 8 Oct 2026 at 16:33, shihao zhong <zhong950419@gmail.com> wrote:
Since 0da29e4cb16 an anti join can use Memoize. Memoize compares its
cache key with the collation of o.code, so 'P1' hits the cache entry
of 'p1'. The clause compares under "C", where they differ.
I made a few minor adjustments and pushed this. Thanks.
David