Re: Eliminating SPI / SQL from some RI triggers - take 3

From: Junwang Zhao <zhjwpku(at)gmail(dot)com>
To: Amit Langote <amitlangote09(at)gmail(dot)com>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Haibo Yan <tristan(dot)yim(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Tomas Vondra <tomas(at)vondra(dot)me>
Subject: Re: Eliminating SPI / SQL from some RI triggers - take 3
Date: 2026-03-31 10:57:46
Message-ID: CAEG8a3JWHkJSXe9nNcVK7wnYKUEqWuMGFDhy5BynB_9tEjmEUg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Mar 31, 2026 at 5:17 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Tue, Mar 31, 2026 at 6:09 PM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
> > > On Mar 30, 2026, at 19:15, Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
> > >
> > > On Mon, Mar 30, 2026 at 1:55 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
> > >> Junwang pointed out off-list that FK tuples added to
> > >> RI_FastPathEntry.batch[] were being copied into TopTransactionContext
> > >> rather than flush_cxt, so they would accumulate until the batch was
> > >> exhausted rather than being reclaimed per flush. Fixed in
> > >> ri_FastPathBatchAdd() in 0002.
> > >>
> > >> Also added a couple of comments in trigger.c that were missing: an
> > >> Assert and explanation in RegisterAfterTriggerBatchCallback()
> > >> clarifying the query_depth >= 0 precondition, a comment at the
> > >> AfterTriggerEndQuery call site explaining why
> > >> FireAfterTriggerBatchCallbacks() must precede the query_depth
> > >> decrement and AfterTriggerFreeQuery, and brief intent comments at the
> > >> AfterTriggerFireDeferred and AfterTriggerSetState call sites.
> > >>
> > >> Plan is to commit 0001 tomorrow barring objections and let it sit for
> > >> a bit before committing 0002. Feedback on 0002, particularly on the
> > >> AfterTriggerBatchCallback mechanism in trigger.c, welcome in the
> > >> meantime.
> > >
> > > Kept looking at 0002 and found a couple of things to improve or change
> > > my thoughts about. I decided to move the permission check from fast
> > > path cache entry creation into ri_FastPathBatchFlush(), alongside the
> > > snapshot, so that permission changes between flushes are respected
> > > rather than checked once at batch start; the check happens for every
> > > row in the SPI and non-batched fast path. Also, improved comments in
> > > a few places to mention design decisions better.
> > >
> > > 0001 is mostly unchanged from v11 except I updated its commit message
> > > to explain why only RI_FKey_check is covered and not the action
> > > triggers as the topic has come up in previous threads about this
> > > topic.
> > >
> > > Still planning to commit 0001 tomorrow.
> > >
> > > --
> > > Thanks, Amit Langote
> > > <v12-0001-Add-fast-path-for-foreign-key-constraint-checks.patch><v12-0002-Batch-FK-rows-and-use-SK_SEARCHARRAY-for-fast-pa.patch>
> >
> > Hi Amit,
> >
> > While reading the recent commits, I saw that 0001 has been pushed as 2da86c1ef9b5446e0e22c0b6a5846293e58d98e3. However, I also just noticed a use-after-free issue in ri_LoadConstraintInfo(). It dereferences conForm after ReleaseSysCache(tup), which is unsafe. I am attaching a tiny patch to fix that.
>
> Thanks. I noticed that too and pushed the fix an hour ago:
>
> https://www.postgresql.org/message-id/E1w7U6V-002H6n-0o%40gemulon.postgresql.org
>
> --
> Thanks, Amit Langote

prion is happy now, the fix works, thanks.

--
Regards
Junwang Zhao

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2026-03-31 10:57:49 Re: WIP - xmlvalidate implementation from TODO list
Previous Message Michael Paquier 2026-03-31 10:54:30 Re: [PROPOSAL] Termination of Background Workers for ALTER/DROP DATABASE