pgsql: Rewrite some RI code to avoid using SPI - Mailing list pgsql-committers

From Alvaro Herrera
Subject pgsql: Rewrite some RI code to avoid using SPI
Date
Msg-id E1ncXX2-000mFt-Pe@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Rewrite some RI code to avoid using SPI  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Corey Huinker <corey.huinker@gmail.com>
Reviewed-by: Li Japin <japinli@hotmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Zhihong Yu <zyu@yugabyte.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(-)


pgsql-committers by date:

Previous
From: Andres Freund
Date:
Subject: pgsql: Fix test instability introduced in e349c95d3e9 due to async dedu
Next
From: Andres Freund
Date:
Subject: pgsql: pgstat: add tests for handling of restarts, including crashes.