Re: Revert RI fast-path batching from REL_19_STABLE

From: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
To: Amit Langote <amitlangote09(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, rmt(at)lists(dot)postgresql(dot)org
Subject: Re: Revert RI fast-path batching from REL_19_STABLE
Date: 2026-09-08 09:52:20
Message-ID: CAA4eK1Lk1DfgiFPL-HccZm-Nm+exg+FcsODH8KPw6BOBuzfHtA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Sep 7, 2026 at 6:45 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
>
> On Thu, Sep 3, 2026 at 10:36 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
>
> Updated 0001 also pushes/pops the snapshot and adds a
> regression test.
>

One more point about divergence in fast-patch versus SPI path:
ri_FastPathCheck()
{
...
/*
* Advance the command counter so the snapshot sees the effects of prior
* triggers in this statement. Mirrors what the SPI path does in
* ri_PerformCheck().
*/
CommandCounterIncrement();

Related to above, IIUC, ri_PerformCheck() doesn't seem to be calling
CommandCounterIncrement() when called via RI_FKey_check() as it passes
detectNewRows as false for non-partitioned tables. It seems to be
calling somewhere in the SPI code path which makes the above comment
misleading. The other related point is that, in SPI path we call CCI
after acquiring lock on pk_rel which means local invalidations are
processed (CCI->AtCCI_LocalCache) after acquiring the LOCK. I am not
able to see any problem with it but maybe a comment reflecting that
difference is worth it or we can change the order to keep both paths
consistent.

Few other comments on 0001:
========================
*
ri_LockPKTuple()
{
...
+ /*
+ * In READ COMMITTED, FIND_LAST_VERSION should have chased the
+ * chain and returned TM_Ok. Getting here means something
+ * unexpected -- fall through to error.
+ */
+ elog(ERROR, "unexpected table_tuple_lock status: %u", result);
+ break;
+
+ case TM_SelfModified:
+
+ /*
+ * The current command or a later command in this transaction
+ * modified the PK row. This shouldn't normally happen during an
+ * FK check (we're not modifying pk_rel), but handle it safely by
+ * treating the tuple as not found.
+ */
+ return false;

Why did the above two cases dealt differently? I mean if something is
not expected in the FK path, silently treating it as not found will
let the caller report an FK violation.

* RI fast path uses the wrong collation for the index probe.
build_index_scankeys() takes the scan key collation from the
referenced index. The SPI check query
resolves equality under the referenced column's collation (See
ri_GenerateQualCollation()'s header comment).

I used Claude to generate the test for the above (note you need to use
--with-icu build option) and verified that it shows the problem:
CREATE COLLATION fkfp_ci (provider = icu, locale =
'@colStrength=secondary', deterministic = false);
CREATE TABLE fkfp_pk (x text COLLATE fkfp_ci);
CREATE UNIQUE INDEX fkfp_pk_x_c ON fkfp_pk (x COLLATE "C");
INSERT INTO fkfp_pk VALUES ('ABC');

-- a non-superuser with REFERENCES but deliberately NOT SELECT on fkfp_pk,
-- so RI_Initial_Check() returns false and validation falls back to the
-- per-row RI_FKey_check_ins() loop
CREATE ROLE fkfp_r;
GRANT CREATE ON SCHEMA public TO fkfp_r;
GRANT REFERENCES ON fkfp_pk TO fkfp_r;

CREATE TABLE fkfp_fk_val (x text COLLATE fkfp_ci);
INSERT INTO fkfp_fk_val VALUES ('abc');
ALTER TABLE fkfp_fk_val OWNER TO fkfp_r;

SET ROLE fkfp_r;
-- expect: has_table_privilege = f, confirming the bulk path is unavailable
SELECT has_table_privilege('fkfp_pk', 'SELECT') AS can_select,
has_table_privilege('fkfp_pk', 'REFERENCES') AS can_reference;

-- this is the per-row fast path.
ALTER TABLE fkfp_fk_val ADD FOREIGN KEY (x) REFERENCES fkfp_pk (x);

On HEAD, above statement gives following ERROR:
ERROR: insert or update on table "fkfp_fk_val" violates foreign key
constraint "fkfp_fk_val_x_fkey"
DETAIL: Key (x)=(abc) is not present in table "fkfp_pk".

If I change code to skip fast-path and use SPI then the ALTER
statement is successful.

--
With Regards,
Amit Kapila.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Matthias van de Meent 2026-09-08 09:55:36 Re: Reducing relcache memory usage: deduping index shapes
Previous Message Clemenza Zhang 2026-09-08 09:23:30 Re: Bug: Whole-row var in indexes corrupts indexes after DDL