postgres.git / summary / log / commit / refs
commit e321faaaef54cd1b93186046d4c98b43a59a8f1e
Author: Peter Geoghegan <pg@bowt.ie>
Date: Sat Jul 25 16:01:28 2026 +0000
First released in: 14.24, 15.19, 16.15, 17.11, 18.6, 19.0
Fix another empty nbtree index SSI race.
Commit f9b7fc65 fixed a race when predicate-locking completely empty
btrees: without a buffer lock held, a matching key could be inserted
between _bt_search and the PredicateLockRelation call, so the scan would
miss concurrently inserted tuples while the writer wouldn't see the
reader's predicate lock. That commit only fixed _bt_first's _bt_search
path, though. Scans without useful insertion scan keys return early
from _bt_first via _bt_endpoint, which still didn't recheck if the
relation was empty.
To fix, add handling to _bt_endpoint that is analogous to the handling
added to _bt_search by commit f9b7fc65.
Author: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/CAH2-WzkNoTn3yXY0iGkSuavJ+sL8EROf+kitW+_2v2tJVWuKmA@mail.gmail.com
Backpatch-through: 14
src/backend/access/nbtree/nbtsearch.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 3230b3b8940..b4d28313c8e 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -2431,12 +2431,21 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
if (!BufferIsValid(buf))
{
/*
- * Empty index. Lock the whole relation, as nothing finer to lock
- * exists.
+ * Empty index. Lock the whole relation using the approach explained
+ * at the same point in the _bt_first path.
*/
- PredicateLockRelation(rel, scan->xs_snapshot);
- BTScanPosInvalidate(so->currPos);
- return false;
+ if (IsolationIsSerializable())
+ {
+ PredicateLockRelation(rel, scan->xs_snapshot);
+ buf = _bt_get_endpoint(rel, 0, ScanDirectionIsBackward(dir),
+ scan->xs_snapshot);
+ }
+
+ if (!BufferIsValid(buf))
+ {
+ BTScanPosInvalidate(so->currPos);
+ return false;
+ }
}
PredicateLockPage(rel, BufferGetBlockNumber(buf), scan->xs_snapshot);
[parent: 844bc718ac39]