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 1wXacS-003Lgf-36 for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Jun 2026 08:18:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wXacR-00F07m-2g for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Jun 2026 08:18:31 +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 1wXacR-00F07d-1P for pgsql-hackers@lists.postgresql.org; Thu, 11 Jun 2026 08:18:31 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wXacP-000000029Ee-2wGx for pgsql-hackers@postgresql.org; Thu, 11 Jun 2026 08:18:30 +0000 Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-69155ca09d8so6716687a12.3 for ; Thu, 11 Jun 2026 01:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1781165908; cv=none; d=google.com; s=arc-20240605; b=Byb5Lg3DIUPOJCQ6THQMBcYKWenjKzc6gvHmiEVEreTJuuwdj07Rm4+Y2zWOuXtLbz MhQ9OqMvLYx9ngPCUgE62hDKI2/NuLjvQ+Nvulb1PcJXoviL7KOSAXgKyJs9kKOTayVZ Pcl7Ph6ApkeWe+ABckyY9gAKxsvjzER/CqdsFjKFqL8k+46AM3cK+/sLOWJfv++eku7O SiMhi3wqK7tHxx6pxmBrD02VUsYUofyJ0AfIm3zZTSlXFqOoqfpaaeaDp5hAfRV6HJPd D9mApMLrn2pcB819Nr4UHZ4guFf6pdTduPQdruxtTBHYHGlMI5EhmOgijzWKv06g0ywF fM/Q== 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=/moTOCXjar+qzet9GsYmo+G0Elk/prNqdGD/fqhX54U=; fh=0bnJAtQN3HVan7IwWwzzVnsS+bEJV2hL73poCAkV4Dw=; b=SIX15Q/8O+EloqfOmW77OzIN4qDN8HjJkISY/4L7qien6WdPwrsWjQiPSzMc29IaZd Jf9mzZw7VVuWpVgSDsXRHnlFJAZB+lwkTcg8R5ywJAx2ZUocNfzvfk7c5Aa/gYcT7uhU c9hAs8EGDlhW88dOLs4Lzx0fYvGuXHBYVCX8Lbj0iAb13mXSZXDi99P1JIgiPuF+a6hX edqCl6Zd6kVBqpW1wwYo/gGwXHw2x5EhN2CaLNgUhJnSW4XTEBXDk3ItSbrtTTj91jEJ RfXZ1Kc2Eog65vMZBwVApW0yBOQz1Xp4rwBd1YeQ5yKiD5wrKlf1tM/QF/Cj0r8MhAbA Q5Cw==; 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=1781165908; x=1781770708; 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=/moTOCXjar+qzet9GsYmo+G0Elk/prNqdGD/fqhX54U=; b=XPLm2X4TCK98YKRcwppIIJTlSk5sv+xCdTcqbfNoccRSD1KhC/uGY1uXHlatHh3yY1 BXP1OveITFRyo5e0EyLKbxXfKhltCnpJBXhU5Wx49zoitbm8I5RgeLOdafTuPLWnzsoa AQ6UQY85rw/c9dU5ltfCuH3xGRc0R8wNZWBmK3sxqIHhNKfyvnzoZ4bX6vwMvDJp+nXu MZGZXxdC8Z6IP4Ih38Fg1khDEWdWlJCUuafX6Hlk66UJkc3ansrhrpT/qE+5KvRiYEp1 IowMvS9Mn/uyllN21CYSE7EBJqow8r5K4P7lGFna/tTTPiMQrEsQEhIHh9TePsX13rEh eSKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781165908; x=1781770708; 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=/moTOCXjar+qzet9GsYmo+G0Elk/prNqdGD/fqhX54U=; b=XU7l+6fVwqlh0qCTcdpMvA88ImBJvwgqr7cxLd/HNyWW2Ms49cSPWbqbSJdFQVQhr3 vaU5fKaP7KGHlNeG7CtPlx88N7HqjsMjqieawPHk9Ua+sb+Aw/plWvisw7ojRAv5sOIY yFAlzHHlM7G3sALklkD05Bn9v9uUi/yPNIcPkAP8933xk71Jh3xAN6XP7bX4iwOaie1Q q/xiv2lXJHlP/laJVlBBA1Zy2DUW/f6FzIIlOPfTOCZGYTCG+tL/v/ljsBKy+G1OMDWc MCFfKZ36D81qxh7NGoyKGOUyYcCj9q03Jl5b/Ddps5DsNx2qwytls0Me43ItUZHRk1XE Y+vw== X-Forwarded-Encrypted: i=1; AFNElJ/0YRuCFM24K8WaZ4gUDZsipMJKHQXKd36YOsGCYKAwdBZfInyh1M3SxI6jSR+ltMlHEVDMwlfzIYJGKF69@postgresql.org X-Gm-Message-State: AOJu0YwxSgusC0ygoVMd/IEN/Sd03SQDiLrTZ0FeeT5yo7VZr248hhJk 12fDrJMX6d46QqNNXsUrCxko3+ogYUNRgo4MvU99HO9R6AoUPnHtJtr+SRaAkzLl75G0mv97SIL 4lK1F2OcByzL53nHsjL+taO7HDQDs1l4= X-Gm-Gg: Acq92OHn9kqWmYGbvZy31862dT11/HWpmUBCkukw54+OXtRGR8QgylJj8iBAf03C3lc 9FsDx/9sQwAdzQpzq8T84yl0qFVutlMzvN9jDl2ysqcoWsfvBBnapgn4z9ST8ubsgHZW25Zxslr mjmzxFmu/n3QLO6f4yxCiUBidKOws6Afu0nQ2oEZO2PDskAbGxYk4eHWmjjtTnq0WLx3DEI8hd3 gk68X5xmL+61rm2uJGlOvZuNwVEbLCB653/J4q/R3HBhP9geAnAbRY0Q7D/dcpNBF6D76qWpIVU l47CfFIuaVYsEvtusTEYtmRluwJ+PNxj1nXxOmeeXb0sXH/ZfhlN9pqUpNPx22RscShLs6UVaGk IipVB X-Received: by 2002:a05:6402:540f:b0:691:b5aa:5a7b with SMTP id 4fb4d7f45d1cf-6930e2c3b90mr767022a12.17.1781165908011; Thu, 11 Jun 2026 01:18:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Junwang Zhao Date: Thu, 11 Jun 2026 16:18:15 +0800 X-Gm-Features: AVVi8CcYtPtAtuADK6wU77skscdTwXrizOZ2mmnt7k3qpHJhypQxdMhABFkcLWc Message-ID: Subject: Re: PG19 FK fast path: OOB write and missed FK checks during batched To: Amit Langote Cc: Ayush Tiwari , Nikolay Samokhvalov , pgsql-hackers mailing list , Andrey Borodin , Kirk Wolak 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 Hi Amit, On Wed, Jun 10, 2026 at 8:17=E2=80=AFPM Amit Langote wrote: > > Hi Ayush, > > Thanks for the review. > > On Wed, Jun 10, 2026 at 7:09=E2=80=AFPM Ayush Tiwari > wrote: > > On Wed, 10 Jun 2026 at 14:02, Amit Langote wr= ote > >> Thanks for checking. I will review them a bit more closely before > >> committing by Friday. Other reviews are welcome. > > > > Thanks for the patch! > > > > I read through v1-0001 and v1-0002 and tried them locally. I had a coup= le of > > things I wanted to ask about. > > > > 1. The per-entry "flushing" flag and test coverage. If I'm reading the= two > > patches together correctly, with both applied the 64-row re-entry test = in 0001 > > reaches the flush through ri_FastPathEndBatch(), where 0002's cache-wid= e > > ri_fastpath_flushing guard already routes the re-entrant check to the p= er-row > > path before it gets back into ri_FastPathBatchAdd(). Does that mean th= e > > per-entry flag from 0001 isn't really exercised by that test once 0002 = is in? > > As far as I can tell you'd need the flush to fire from ri_FastPathBatch= Add() > > itself (a 65th row) to reach it. I tried a 65-row variant (same FK, re= -entrant > > DML from the cast during the full-batch flush), including a case where = the > > re-entrant row was an orphan, and it seemed to do the right thing; the > > per-row fallback still raised the violation. Would it be worth switchi= ng the > > test to 65 rows, or adding that variant, so the per-entry guard is cove= red too? > > Or am I missing a path where the committed test already hits it? > > You're right. With 0002 applied, the 64-row test reaches the flush > through ri_FastPathEndBatch(), where the cache-wide > ri_fastpath_flushing guard catches the re-entry before it returns to > ri_FastPathBatchAdd(), so the per-entry flag is no longer exercised by > that test. To hit the per-entry flag the flush has to fire from > ri_FastPathBatchAdd() itself, which the 64-row case no longer does > once the add and flush are reordered. > > Rather than bump the test to 65 rows, I'd prefer to keep the flush > firing from ri_FastPathBatchAdd() at 64 by not reordering the add and > flush, and prevent the OOB write by bounds-checking the write instead, > as done in the attached updated 0001. A re-entrant add then can't > overrun the array regardless of the flag, the per-entry flushing guard > still routes the re-entry to the per-row path, and a 64-row statement > flushes from ri_FastPathBatchAdd() on the 64th row, so the existing > test exercises the per-entry guard. > > > 2. Resetting ri_fastpath_flushing. I noticed it's cleared only in the > > PG_FINALLY of ri_FastPathEndBatch(), which does seem to cover the cases= I could > > think of. Since ri_FastPathXactCallback already NULLs ri_fastpath_cach= e and > > clears ri_fastpath_callback_registered at transaction end, I wondered w= hether > > it might be worth clearing ri_fastpath_flushing there too, just as chea= p > > insurance against some future path that leaves it set across transactio= ns > > though maybe that's unnecessary given the PG_FINALLY. > > Agreed, it's cheap and matches the existing resets there, so I've > added it to ri_FastPathXactCallback() in v2-0002. > > > Other than the above queries, the patch looks good to me. > > Updated patches attached. I only reviewed and applied patch 0001 on my local machine, and it successfully fixed the crash. One minor comment: + if (fpentry->flushing) + { + ri_FastPathCheck(riinfo, fk_rel, newslot); + return; + } Would it be worth wrapping the condition with unlikely()? It seems this branch is expected to be false in most cases, not a strong opinion though. > > -- > Thanks, Amit Langote --=20 Regards Junwang Zhao