Re: RI fast path gets cross-type foreign keys wrong

From: Amit Langote <amitlangote09(at)gmail(dot)com>
To: Peter Geoghegan <pg(at)bowt(dot)ie>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: RI fast path gets cross-type foreign keys wrong
Date: 2026-08-01 15:59:46
Message-ID: CA+HiwqGQnXVDb+OMqAkkokH8LrQ6p2JT-rrMoe3i-jsC1af=tw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Aug 1, 2026 at 8:49 Amit Langote <amitlangote09(at)gmail(dot)com> wrote:

> On Sat, Aug 1, 2026 at 8:33 Peter Geoghegan <pg(at)bowt(dot)ie> wrote:
>
>> The attached isolation test patch (written by Claude code) shows that
>> the new RI fast path can get some things wrong when a foreign key uses
>> a cross-type equality operator.
>>
>> The test has two permutations differing only in the primary key's type
>> -- date against a timestamp FK in the first, timestamp against
>> timestamp in the second. The interleaving is identical and the
>> referenced key is present throughout, so both should behave the same
>> way. The second succeeds (which is correct), while the first fails
>> with a foreign key violation (which is incorrect).
>>
>> The fast path was introduced by b7b27eb41. This is an issue on 19 and
>> master only.
>
>
> Thanks Peter for the report. I’m on vacation atm, will take a look when I
> get back next week.
>

Still away, but couldn’t help reading the code on my phone.

The recheck block in ri_FastPathFlushArray() appears to put the key it has
just read out of the locked tuple (found_val) into sk_argument, and then
pass that same slot to recheck_matched_pk_tuple(). If I’m reading that
right, both operands come from the locked tuple, so it compares the key
with itself and since sk_argument is the right-hand operand, which on a
cross-type operator is the FK-typed side, the PK value there gets read as
an FK value. That would make the same-type case pass trivially and the
cross-type case fail always, which seems to fit what Peter is seeing.

If that’s really what’s going on, maybe the recheck can just go away,
because the loop a few lines further down in the same scan iteration already
compares found_val against the buffered FK values. I’ll look at it properly
next week.

- Amit

>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexander Lakhin 2026-08-01 16:00:00 xid_wraparound/002_limits.pl might fail due to transient warning
Previous Message Pierre Forstmann 2026-08-01 15:09:51 Re: [PATCH] Doc: Mention OFF as an alias for EXPLAIN SERIALIZE NONE