pgsql: Rewrite some RI code to avoid using SPI

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Rewrite some RI code to avoid using SPI
Date: 2022-04-07 19:11:01
Message-ID: E1ncXX2-000mFt-Pe@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Rewrite some RI code to avoid using SPI

Modify the subroutines called by RI trigger functions that want to check
if a given referenced value exists in the referenced relation to simply
scan the foreign key constraint's unique index, instead of using SPI to
execute
SELECT 1 FROM referenced_relation WHERE ref_key = $1
This saves a lot of work, especially when inserting into or updating a
referencing relation.

This rewrite allows to fix a PK row visibility bug caused by a partition
descriptor hack which requires ActiveSnapshot to be set to come up with
the correct set of partitions for the RI query running under REPEATABLE
READ isolation. We now set that snapshot indepedently of the snapshot
to be used by the PK index scan, so the two no longer interfere. The
buggy output in src/test/isolation/expected/fk-snapshot.out of the
relevant test case added by commit 00cb86e75d6d has been corrected.
(The bug still exists in branch 14, however, but this fix is too
invasive to backpatch.)

Author: Amit Langote <amitlangote09(at)gmail(dot)com>
Reviewed-by: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
Reviewed-by: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
Reviewed-by: Li Japin <japinli(at)hotmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: Zhihong Yu <zyu(at)yugabyte(dot)com>
Discussion: https://postgr.es/m/CA+HiwqGkfJfYdeq5vHPh6eqPKjSbfpDDY+j-kXYFePQedtSLeg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/99392cdd78b788295e52b9f4942fa11992fd5ba9

Modified Files
--------------
src/backend/executor/execPartition.c | 174 ++++++++-
src/backend/executor/nodeLockRows.c | 161 ++++----
src/backend/utils/adt/ri_triggers.c | 564 ++++++++++++++++------------
src/include/executor/execPartition.h | 6 +
src/include/executor/executor.h | 8 +
src/test/isolation/expected/fk-snapshot.out | 4 +-
src/test/isolation/specs/fk-snapshot.spec | 5 +-
7 files changed, 605 insertions(+), 317 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2022-04-07 19:31:02 pgsql: pgstat: add tests for handling of restarts, including crashes.
Previous Message Andres Freund 2022-04-07 19:03:58 pgsql: Fix test instability introduced in e349c95d3e9 due to async dedu