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-14 12:00:54
Message-ID: CA+HiwqFK+rGhFT33wxkBYiM4qdpGaFmCwBjHjz+8UGNvNkwJGw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Aug 13, 2026 at 1:22 Peter Geoghegan <pg(at)bowt(dot)ie> wrote:

> On Fri, Jul 31, 2026 at 11:03 PM 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.
>
> Opus 5 flagged an independent problem in the RI fast path work. This
> one appears to be a resource management issue.
>
> When I run the foreign_key tests with the attached patch applied, I
> see these failures:
>
> # END$$;
> # CREATE TRIGGER fp_expand_kit_trg AFTER INSERT ON fp_order
> # FOR EACH ROW EXECUTE FUNCTION fp_expand_kit();
> # INSERT INTO fp_order VALUES (1, 1, 1);
> # +WARNING: resource was not closed: relation "fp_product"
> # +WARNING: resource was not closed: relation "fp_product_pkey"
> # +WARNING: resource was not closed: TupleDesc 0x7f7e109a3448 (34085,-1)
> # +WARNING: resource was not closed: TupleDesc 0x7f7e109a2cd8 (34068,-1)
> # NOTICE: order 1 expanded into 3 order items
>
> Right afterwards, "Assert(rel->rd_refcnt > 0)" fails within
> RelationDecrementReferenceCount().

Thanks, Peter. This is another bug, and a hole in the April fix for nested
C-level SPI [1].

That fix made the batch callback lists per query level, but left
ri_fastpath_callback_registered global. If an outer FK check has already
set the flag, the nested level does not register its own
ri_FastPathEndBatch() callback. When the nested portal ends, its
ResourceOwner warns about and releases the still-cached relation references
and TupleDesc pins. The outer callback later calls ri_FastPathTeardown() on
the same entries, accounting for the refcount assertion.

This exact case should be addressed by 0001 from the subtransaction series
[2], which registers the callback per query level and tears down only that
level’s entries. I am checking whether it also handles nested firing
involving the same constraint before committing that series.

[1]
https://postgr.es/m/CAEg7pwcKf01FmDqFAf-Hzu_pYnMYScY_Otid-pe9uw3BJ6gq9g@mail.gmail.com

[2]
https://postgr.es/m/CA+HiwqGHa3tc6MZFSLyLrvwySdrmpkb1TqkH2jGd3HtKGGZ6cQ@mail.gmail.com

- Amit

>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message wenhui qiu 2026-08-14 12:36:00 Re: Compression of bigger WAL records
Previous Message jian he 2026-08-14 11:56:05 Re: 2026-08-13 release announcement draft