Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9vvh-001tzR-1P for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 02:12:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9vvf-00DU52-2u for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 02:12:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9vvf-00DU4u-1c for pgsql-hackers@lists.postgresql.org; Tue, 07 Apr 2026 02:12:36 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w9vvd-00000000xBP-2cM7 for pgsql-hackers@postgresql.org; Tue, 07 Apr 2026 02:12:34 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2ad617d5b80so23662945ad.1 for ; Mon, 06 Apr 2026 19:12:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775527952; cv=none; d=google.com; s=arc-20240605; b=NABkeKw7goSyxwpfwgCyh5jGvEYMjOWzFSZIAE8Ez2DY6GDuqv6ZTWd9k5eAjs0MJZ nV6VW5aTfMkjZx3KZzdbqjBZ5qukZrUS/f6vUDBkCak/+rvO1/fuh5pRJt03a6AL8C/c AqC61DXIHUFljTeUvMSTNat6QexA9cNIAGtA7t2Vobr5WN5Ct1XYS1twBLVkDgTMr6ax LdUmweb1p3Y8D3+avAW0zNH488LdfkE5FycahRDbfB+w6+kp2GzL98VBLYIywDMk2UA0 OugTlam/oRGOkGkKD+s7JYYcKB4PukZYqDrlAOzGhY3O63A8Gdi/G6GsqKo2+l2qdSLy 4XiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=0gnv5zrP9Bjio0+iHDveMBP7qR5rxqLTGn0UHM+XlLk=; fh=MfLoS87hB/9onhGtIW6cw7N0eTwTV7UdKp2aM10w4KY=; b=Qt73ihw3nFlzzj2tBfvRVV6GoJwUOQ4IEEsh5NP7YF1LX+s0NSAjUTVmY646qOL/En DagOmXKSA5q9a+PP6KdO4V2E6LqOdnxkwNzM7NUrECHycfC2C/k3883Z4N+9xksRBHC+ ZajjYRIHRipNkCb3pLrW91VlfSBoW5g7PHTZzXrHw7qOwCO4YcTzgxE/2xf91DhIp3mL paLSgZ8aKAcbVb7chbOL3/xmedQXOKqRHoJugrh5dUYVBhpx0LD6EMqz2D3TWXA7o7Qn 2ijOOr8agLSRJq8qRZmXMO2uNZc19EL2Yxjexsula1EikpR2MCNKDcMqdiEPFEGd2VY/ K+GA==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775527952; x=1776132752; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0gnv5zrP9Bjio0+iHDveMBP7qR5rxqLTGn0UHM+XlLk=; b=Y8Ux0NfCEVij2dL+/7sWhHIkAz1UfaaELXpF9wA9ZvI9yLycjNTpPoWnl9ZvWExwrU LkFwDs+mAK1eFmXtEtDAkgcTGdpTQsoNbMkpKgKQWf0BUeE4ElW4+kMARhjDT8P9XGFU TtQ8PlOJdi2oisRl9HdA2bw4oiLAKYnWDoFKERlVTvrVTIdtw+iR2ByL3ynaVKsSpJ4Y PEN8x9AfbxG5piFG3nNVR0Ornoi6H4/7jKY9axjhwEewtrhU6uM3ZqiyVj7kT7e6kl5D 2TNVWKvU0E8gi0kX2UKn3Ph68zjocfxx39vzTznPAXiQq/Tiydwqrykzo49MH7S2zT5X CAKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775527952; x=1776132752; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=0gnv5zrP9Bjio0+iHDveMBP7qR5rxqLTGn0UHM+XlLk=; b=JIvlUbpWRe9xRoeI2TcvZcznR6pOxLnmET5QNc3yVc8tcVpmKL8DwgzCi5ufIusnFX gDxIpu/BV7QKjKPopvSSLQsZ/I6BwHEnEG5gznKyUi0JfN92X67Qs7v8Y9mvZPtqpOTw lENIFvGcw1txweKDX6POfYYBd4z7bbzohyztdQAU963GxEL8Arw0F4Ef6q8b3J/YY0Au 2Qs/eXzblN9e4tk71tWna78/RwwJVS7TuVcbjI7xWjbDyDWJSWfDpBv4Ib2yYJ9TcHgV neKyjixVPYdlAOXlgPSA2zmqaPHV2Oh/FsFR684ROmobCpiHRmhXHAUVp0iexCYwqnL6 jo4A== X-Forwarded-Encrypted: i=1; AJvYcCV0rzRTRTtmbCstYZu67L5XBdpjXcc5wRfHVO/VxjAyPMYQCbr9VNlvq/JTAzfB8HS7kWYqy69P9if898dM@postgresql.org X-Gm-Message-State: AOJu0YwssXZ0Ye8VXYTzMKhRkoY+Dxwmuxwob702zJFLwcXVk1Jey1nf 01orfSF2KsEp57UZEOFdTVBrn9BBmKDZxby7MwoZur9wBC9lIPbrXNMOGhc67acbrjWSvIBfOM3 vM+X+L6iMoqmPYKqyUxSSmhDD5umA1UA= X-Gm-Gg: AeBDiesP15/PyS5xEfgyyBWzSBLobQaYRiKasl6yVl/6L/sXfYWxvdz3X7R5Bo6aRbL VARwcBC07K9Bji04CdWP4EgSYcuLMyNgVSxCfUgVrXYUF/ioNdtqwZNrxHnDoR4HI9n6+ujlQLH wdkLu2eqWKqTC/7EV2Fm4v6QAvPD33sudM0iipI5V5wmfHSZ6qsLJ9CC8NUd6TXa8G3ClAqeeOi /TmI+jlYOzgGb0jgwo7+aLzFKLgcSZqXcxGXOzEZR1TqlIPw6P08ZxA4Ygrs0P8SqyMqAIRyT/q EU4IfwwW X-Received: by 2002:a17:903:907:b0:2b0:af2f:b27a with SMTP id d9443c01a7336-2b281667455mr163476375ad.11.1775527952323; Mon, 06 Apr 2026 19:12:32 -0700 (PDT) MIME-Version: 1.0 References: <2BE661BA-D909-4093-BF78-DB9B0C099337@gmail.com> <77FA04FE-1F84-4DA1-8855-8BBFD8CC889A@gmail.com> <72AA2663-B642-4FB1-BDC2-5FAFF2D2DF15@gmail.com> In-Reply-To: <72AA2663-B642-4FB1-BDC2-5FAFF2D2DF15@gmail.com> From: Amit Langote Date: Tue, 7 Apr 2026 11:12:15 +0900 X-Gm-Features: AQROBzDiuG-Qq5-G_SqEBwRlrWK98J9104i3dFQeX3r4AdR66tePg_35HNOqdtI Message-ID: Subject: Re: Eliminating SPI / SQL from some RI triggers - take 3 To: Chao Li Cc: Junwang Zhao , Haibo Yan , Pavel Stehule , PostgreSQL-development , Tomas Vondra Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 7, 2026 at 10:46=E2=80=AFAM Chao Li wr= ote: > > On Apr 6, 2026, at 17:45, Amit Langote wrote: > > On Fri, Apr 3, 2026 at 6:39=E2=80=AFPM Amit Langote wrote: > >> On Fri, Apr 3, 2026 at 5:58=E2=80=AFPM Chao Li wrote: > >>> I spent several hours debugging this patch today, and I found a probl= em where the batch mode doesn't seem to handle deferred RI triggers, althou= gh the commit message suggests that it should. > >>> > >>> I traced this scenario: > >>> ``` > >>> CREATE TABLE pk (a int primary key); > >>> CREATE TABLE fk (a int references pk(a) DEFERRABLE INITIALLY DEFERRED= ); > >>> BEGIN; > >>> INSERT INTO fk VALUES (1); > >>> INSERT INTO pk VALUES (1); > >>> COMMIT; > >>> ``` > >>> > >>> When COMMIT is executed, it reaches RI_FKey_check(), where AfterTrigg= erIsActive() checks whether afterTriggers.query_depth >=3D 0. But in the de= ferred case, afterTriggers.query_depth is -1. > >>> > >>> From the code: > >>> ``` > >>> if (ri_fastpath_is_applicable(riinfo)) > >>> { > >>> if (AfterTriggerIsActive()) > >>> { > >>> /* Batched path: buffer and probe in groups */ > >>> ri_FastPathBatchAdd(riinfo, fk_rel, newslot); > >>> } > >>> else > >>> { > >>> /* ALTER TABLE validation: per-row, no cache *= / > >>> ri_FastPathCheck(riinfo, fk_rel, newslot); > >>> } > >>> return PointerGetDatum(NULL); > >>> } > >>> ``` > >>> > >>> So this ends up falling back to the per-row path for deferred RI chec= ks at COMMIT, even though the intent here seems to be only to bypass the AL= TER TABLE validation case, where batch callbacks would never fire, and MyTr= iggerDepth is 0. So, maybe we can just check MyTriggerDepth>0 in AfterTrigg= erIsActive(). > >>> > >>> I tried the attached fix. With it, deferred triggers go through the b= atch mode, and all existing tests still pass. > >> > >> I think you might be right. Thanks for the patch. It looks correct > >> to me at a glance, but I will need to check it a bit more closely > >> before committing. > > > > Thinking about this some more, your fix is on the right track but > > needs a bit more work -- MyTriggerDepth > 0 is too broad since it > > fires for BEFORE triggers too. I have a revised version using a new > > afterTriggerFiringDepth counter that I'll push shortly. > > > > Added an open item for tracking in the meantime: > > https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items#Open_Issues > > V2 looks good to me. Besides the normal cases, I also traced an abnormal = case to verify that afterTriggerFiringDepth is always reset to 0: > ``` > evantest=3D# begin; > BEGIN > evantest=3D*# INSERT INTO fk VALUES (2); > INSERT 0 1 > evantest=3D*# commit; > ERROR: insert or update on table "fk" violates foreign key constraint "f= k_a_fkey" > DETAIL: Key (a)=3D(2) is not present in table "pk". > ``` Thanks for checking. Pushed. --=20 Thanks, Amit Langote