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 1w7V6l-005Ncb-1e for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 09:09:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7V6j-0091j5-2S for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 09:09:58 +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 1w7V6j-0091ix-0y for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2026 09:09:57 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7V6h-00000001wV5-41Xr for pgsql-hackers@postgresql.org; Tue, 31 Mar 2026 09:09:56 +0000 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-35da9692ec3so1525542a91.1 for ; Tue, 31 Mar 2026 02:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774948195; x=1775552995; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=GRIIk6PiZsY3/bWxg0pJapcsRiLhjwPQDG5GZHp0L0M=; b=eoTacIy1R6euKFdS0YOVSxTtMkHnC1lxg9zKurt/zlAwk5oikc0pYEBjT8mq2N7h2r ZBuQaNsETvNY3etLlvAoX4ScqqgGMyWZS0LSjQaCZOEDEUSQOoO1Q0RccCZgSFVKiOuv UdhuVFH021BxVJYKGT371zBBo5u8k9cZb7qvkbcZh+B9Tvu+a1GVD4jK1NFmaAmod3NP 9U0icDs2m1FkAh3EdZx6cf05CtdG475UR5XihGvQchOhC1Zglx0LupDx9jEN8yyEVDDK ni8T67vKxVJJvLGh71awQDzo9vV47DMZYfxBaB/v9vPUd9/mCIzeWHIgNXBmmPpjVIEV /gFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774948195; x=1775552995; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GRIIk6PiZsY3/bWxg0pJapcsRiLhjwPQDG5GZHp0L0M=; b=ICvfqYXFRoh7boEVLXCp1NktL8nSTVzs1NCi905Fw4BGOA+Iu7qOcTfKsDtha0Vxhm 0yYXRq3i/Q9pRmCuFK2D3QS/nfSwPRoRe9/Fx2+gGcEFNT8LPKvdv3bnQ8ycHuhZ9TAi Z8A7eDcHmPkyx159bJBucTFGALWS+4WYQ9yC/11GKu2vChWzS6kScg1lLXe7wodMsQ0g i6ba8JJBakMk9DlO6ZzOhrbpMzm0FNkswWwp9B2XOxfdqOrunU316zk7n0OiiXS0LGQs 0tMwWfDBbvT8jW7y6wVOTFZy5dTrN/HrxFB+ZqB4bSZHHibPb9PxB/APvHqpe+/jrynf DlIA== X-Forwarded-Encrypted: i=1; AJvYcCUbL/Dh9L52Satw3BW6N/7JRcv6o5kG3NLgfVLT7B1mTrEZPuaynLY5qIm8qM5ahPnvGNbwGD+XRgy/0sjy@postgresql.org X-Gm-Message-State: AOJu0Yw3lSFeJQoZqN3huOmjGwkO1obxQ0z3Q9C4RG+cHYTg+qBAbXaq zZz48ZSOvOPK6E9icx4w49XIs2tl4bw4cucqDFLq/IOv6ldwPVQDZx4z X-Gm-Gg: ATEYQzwQckqi/NUyeXIGCKS1XtXx8uFsexLXfa+aNms/DBnCXKOdPi1T+wUO7Zz4EsA J5rFXDPLX5GZvkJHBQ7WCLyzk2BNlz/TJO2orV1pTPU4ZYHxv+nkmEpHOvUkwtrQtkTvq0Nch+4 xK2XtyaOXC6/uYPwZtc1yiQT3xPhlSNPj9cKec1nQvJDHuadIjcVzjbRi2Oo0ikrUOZ3woyaC83 pHPIGOX/ulbVEFYmWYJtNxZpt9H6gbzoiKtVzDdx5q0MKfY41gzF+opU0sk0X5nMDwj391u+YxH ODF460ouleZYZ1U+fLf6Be4tKUXqobbJ60H6zpyfvRDXKP5JDCAn7TCKFxmqGfh6R3bBg2MKzW7 P9IgM5VrFGIoo5343oZUE06t5oZNgOSUT9cwJmIe2Z3dzVIUEmj9nVue2D1Fjs9nz593HGtKVHy LrobBBxDT5xK3H5+9PKZGviE+b/O1pQQ== X-Received: by 2002:a17:902:ef44:b0:2b2:58c7:2cd6 with SMTP id d9443c01a7336-2b258c73377mr53853375ad.5.1774948194956; Tue, 31 Mar 2026 02:09:54 -0700 (PDT) Received: from smtpclient.apple ([203.10.98.27]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2427c4737sm123942455ad.80.2026.03.31.02.09.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Mar 2026 02:09:54 -0700 (PDT) From: Chao Li Message-Id: <77FA04FE-1F84-4DA1-8855-8BBFD8CC889A@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_1AB14FA3-7520-4FAA-9E08-DF39B58C6C0D" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Eliminating SPI / SQL from some RI triggers - take 3 Date: Tue, 31 Mar 2026 17:09:19 +0800 In-Reply-To: Cc: Junwang Zhao , Haibo Yan , Pavel Stehule , PostgreSQL-development , Tomas Vondra To: Amit Langote References: <2BE661BA-D909-4093-BF78-DB9B0C099337@gmail.com> X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_1AB14FA3-7520-4FAA-9E08-DF39B58C6C0D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Mar 30, 2026, at 19:15, Amit Langote = wrote: >=20 > On Mon, Mar 30, 2026 at 1:55=E2=80=AFPM Amit Langote = 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. >>=20 >> Also added a couple of comments in trigger.c that were missing: an >> Assert and explanation in RegisterAfterTriggerBatchCallback() >> clarifying the query_depth >=3D 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. >>=20 >> 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. >=20 > 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. >=20 > 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. >=20 > Still planning to commit 0001 tomorrow. >=20 > --=20 > Thanks, Amit Langote > = 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. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_1AB14FA3-7520-4FAA-9E08-DF39B58C6C0D Content-Disposition: attachment; filename=v1-0001-Fix-a-use-after-problem-in-ri_LoadConstraintInfo.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Fix-a-use-after-problem-in-ri_LoadConstraintInfo.patch" Content-Transfer-Encoding: quoted-printable =46rom=2085898f7825631f9a46b057ea486a766484c77f9b=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Tue,=2031=20Mar=202026=2017:06:12=20+0800=0ASubject:=20[PATCH=20= v1]=20Fix=20a=20use-after-problem=20in=20ri_LoadConstraintInfo()=0A=0A= Author:=20Chao=20Li=20=0A---=0A=20= src/backend/utils/adt/ri_triggers.c=20|=204=20++--=0A=201=20file=20= changed,=202=20insertions(+),=202=20deletions(-)=0A=0Adiff=20--git=20= a/src/backend/utils/adt/ri_triggers.c=20= b/src/backend/utils/adt/ri_triggers.c=0Aindex=20da7640a8005..94bb180325b=20= 100644=0A---=20a/src/backend/utils/adt/ri_triggers.c=0A+++=20= b/src/backend/utils/adt/ri_triggers.c=0A@@=20-2396,8=20+2396,6=20@@=20= ri_LoadConstraintInfo(Oid=20constraintOid)=0A=20=09=09=09=09=09=09=20=20= &riinfo->period_intersect_oper);=0A=20=09}=0A=20=0A-=09= ReleaseSysCache(tup);=0A-=0A=20=09/*=0A=20=09=20*=20For=20efficient=20= processing=20of=20invalidation=20messages=20below,=20we=20keep=20a=0A=20=09= =20*=20doubly-linked=20count=20list=20of=20all=20currently=20valid=20= entries.=0A@@=20-2412,6=20+2410,8=20@@=20ri_LoadConstraintInfo(Oid=20= constraintOid)=0A=20=0A=20=09riinfo->fpmeta=20=3D=20NULL;=0A=20=0A+=09= ReleaseSysCache(tup);=0A+=0A=20=09return=20riinfo;=0A=20}=0A=20=0A--=20=0A= 2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_1AB14FA3-7520-4FAA-9E08-DF39B58C6C0D--