From 14a49bc6d4ef577b3a6e65f3ac704430e1b5afde Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Fri, 27 Nov 2026 01:49:15 -0500 Subject: [PATCH v1] Fix GiST predicate locking for scans that prune at internal pages Once a downlink is widened, later inserts into that subtree no longer touch the page the scan locked. Reported-by: Michael Malis Discussion: https://postgr.es/m/CANVD+MB+0iJC4dqLk9gse2GPMYnZJYEa1cPy9-VyFxTJBhB6aw@mail.gmail.com Discussion: https://postgr.es/m/4CC0076C0200002500036BF8@gw.wicourts.gov --- src/backend/access/gist/gist.c | 15 +++++- src/backend/storage/lmgr/README-SSI | 13 +++-- .../expected/predicate-gist-prune.out | 22 +++++++++ src/test/isolation/isolation_schedule | 1 + .../isolation/specs/predicate-gist-prune.spec | 49 +++++++++++++++++++ 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/test/isolation/expected/predicate-gist-prune.out create mode 100644 src/test/isolation/specs/predicate-gist-prune.spec diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 44597793433..c9330dfe14f 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -751,6 +751,7 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace, IndexTuple newtup; GISTInsertStack *item; OffsetNumber downlinkoffnum; + bool is_split; downlinkoffnum = gistchoose(state.r, stack->page, itup, giststate); iid = PageGetItemId(stack->page, downlinkoffnum); @@ -803,8 +804,18 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace, * descend back to the half that's a better fit for the new * tuple. */ - if (gistinserttuple(&state, stack, giststate, newtup, - downlinkoffnum)) + is_split = gistinserttuple(&state, stack, giststate, newtup, + downlinkoffnum); + + /* + * Later inserts into the widened subtree will not modify this + * page, so the child inherits its predicate locks. We hold + * an exclusive lock on this page, as PredicateLockPageSplit() + * requires. + */ + PredicateLockPageSplit(state.r, stack->blkno, childblkno); + + if (is_split) { /* * If this was a root split, the root page continues to be diff --git a/src/backend/storage/lmgr/README-SSI b/src/backend/storage/lmgr/README-SSI index 50d2ecca9d7..bdf9a5aff2a 100644 --- a/src/backend/storage/lmgr/README-SSI +++ b/src/backend/storage/lmgr/README-SSI @@ -384,11 +384,14 @@ conflict with all scans. * GiST searches can determine that there are no matches at any level of the index, so we acquire predicate lock at each index -level during a GiST search. An index insert at the leaf level can -then be trusted to ripple up to all levels and locations where -conflicting predicate locks may exist. In case there is a page split, -we need to copy predicate lock from the original page to all the new -pages. +level during a GiST search. The first insert that falls in a subtree +pruned by the search has to widen the downlink in the internal page +where the search stopped, so it conflicts with the lock there. Later +inserts into the same subtree do not modify that page, though. So when +an insert widens a downlink, the child page inherits the predicate +locks of the parent page, and the locks follow the widened downlinks +down to the leaf level. In case there is a page split, we need to copy +predicate lock from the original page to all the new pages. * Hash index searches acquire predicate locks on the primary page of a bucket. It acquires a lock on both the old and new buckets diff --git a/src/test/isolation/expected/predicate-gist-prune.out b/src/test/isolation/expected/predicate-gist-prune.out new file mode 100644 index 00000000000..593dfffd2b7 --- /dev/null +++ b/src/test/isolation/expected/predicate-gist-prune.out @@ -0,0 +1,22 @@ +Parsed test spec with 2 sessions + +starting permutation: r1_hi r2_hi w1_hi w2_hi c1 c2 +step r1_hi: select count(*) from gist_prune_tbl + where span && '[50000, 50005)'::int4range; +count +----- + 0 +(1 row) + +step r2_hi: select count(*) from gist_prune_tbl + where span && '[50000, 50005)'::int4range; +count +----- + 0 +(1 row) + +step w1_hi: insert into gist_prune_tbl values (5001, '[50000, 50005)'); +step w2_hi: insert into gist_prune_tbl values (5002, '[50000, 50005)'); +step c1: commit; +step c2: commit; +ERROR: could not serialize access due to read/write dependencies among transactions diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index ae8e579b199..fdaabde9a37 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -118,6 +118,7 @@ test: horizons test: broken-hot-chain test: predicate-hash test: predicate-gist +test: predicate-gist-prune test: predicate-gin test: partition-concurrent-attach test: partition-drop-index-locking diff --git a/src/test/isolation/specs/predicate-gist-prune.spec b/src/test/isolation/specs/predicate-gist-prune.spec new file mode 100644 index 00000000000..38ed5a91bda --- /dev/null +++ b/src/test/isolation/specs/predicate-gist-prune.spec @@ -0,0 +1,49 @@ +# Predicate locking in a multi-level gist index, when a scan prunes a subtree +# +# A serializable scan that prunes a subtree locks only the internal page where +# it stopped. Inserts into the subtree must still conflict with it, including +# those that find the downlink already widened. +# +# A buffering build with a low fillfactor gives a three-level index, so the +# locks have to pass down through two levels. + +setup +{ + create table gist_prune_tbl (id int, span int4range); + insert into gist_prune_tbl + select g, int4range(g * 10, g * 10 + 5) from generate_series(1, 2000) g; + create index gist_prune_idx on gist_prune_tbl using gist (span) + with (fillfactor = 10, buffering = on); +} + +teardown +{ + drop table gist_prune_tbl; +} + +session s1 +setup +{ + begin isolation level serializable; + set enable_seqscan = off; + set enable_bitmapscan = off; +} +step r1_hi { select count(*) from gist_prune_tbl + where span && '[50000, 50005)'::int4range; } +step w1_hi { insert into gist_prune_tbl values (5001, '[50000, 50005)'); } +step c1 { commit; } + +session s2 +setup +{ + begin isolation level serializable; + set enable_seqscan = off; + set enable_bitmapscan = off; +} +step r2_hi { select count(*) from gist_prune_tbl + where span && '[50000, 50005)'::int4range; } +step w2_hi { insert into gist_prune_tbl values (5002, '[50000, 50005)'); } +step c2 { commit; } + +# Both scans prune the same subtree, then both insert into it. +permutation r1_hi r2_hi w1_hi w2_hi c1 c2 -- 2.37.1 (Apple Git-137.1)