Skip to content

Commit bcf6058

Browse files
Standardize cleanup lock terminology.
The term "super-exclusive lock" is a synonym for "buffer cleanup lock" that first appeared in nbtree many years ago. Standardize things by consistently using the term cleanup lock. This finishes work started by commit 276db87. There is no good reason to have two terms. But there is a good reason to only have one: to avoid confusion around why VACUUM acquires a full cleanup lock (not just an ordinary exclusive lock) in index AMs, during ambulkdelete calls. This has nothing to do with protecting the physical index data structure itself. It is needed to implement a locking protocol that ensures that TIDs pointing to the heap/table structure cannot get marked for recycling by VACUUM before it is safe (which is somewhat similar to how VACUUM uses cleanup locks during its first heap pass). Note that it isn't strictly necessary for index AMs to implement this locking protocol -- several index AMs use an MVCC snapshot as their sole interlock to prevent unsafe TID recycling. In passing, update the nbtree README. Cleanly separate discussion of the aforementioned index vacuuming locking protocol from discussion of the "drop leaf page pin" optimization added by commit 2ed5b87. We now structure discussion of the latter by describing how individual index scans may safely opt out of applying the standard locking protocol (and so can avoid blocking progress by VACUUM). Also document why the optimization is not safe to apply during nbtree index-only scans. Author: Peter Geoghegan <pg@bowt.ie> Discussion: https://postgr.es/m/CAH2-WzngHgQa92tz6NQihf4nxJwRzCV36yMJO_i8dS+2mgEVKw@mail.gmail.com Discussion: https://postgr.es/m/CAH2-WzkHPgsBBvGWjz=8PjNhDefy7XRkDKiT5NxMs-n5ZCf2dA@mail.gmail.com
1 parent 6f0e6ab commit bcf6058

File tree

9 files changed

+108
-96
lines changed

9 files changed

+108
-96
lines changed

src/backend/access/gin/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ leafs. If first stage detects at least one empty page, then at the second stage
396396
ginScanToDelete() deletes empty pages.
397397

398398
ginScanToDelete() traverses the whole tree in depth-first manner. It starts
399-
from the super-exclusive lock on the tree root. This lock prevents all the
399+
from the full cleanup lock on the tree root. This lock prevents all the
400400
concurrent insertions into this tree while we're deleting pages. However,
401401
there are still might be some in-progress readers, who traversed root before
402402
we locked it.

src/backend/access/heap/heapam.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8448,7 +8448,7 @@ ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_changed,
84488448
/*
84498449
* Handles XLOG_HEAP2_PRUNE record type.
84508450
*
8451-
* Acquires a super-exclusive lock.
8451+
* Acquires a full cleanup lock.
84528452
*/
84538453
static void
84548454
heap_xlog_prune(XLogReaderState *record)
@@ -8534,7 +8534,7 @@ heap_xlog_prune(XLogReaderState *record)
85348534
/*
85358535
* Handles XLOG_HEAP2_VACUUM record type.
85368536
*
8537-
* Acquires an exclusive lock only.
8537+
* Acquires an ordinary exclusive lock only.
85388538
*/
85398539
static void
85408540
heap_xlog_vacuum(XLogReaderState *record)

src/backend/access/heap/pruneheap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum)
844844

845845
/*
846846
* Perform the actual page changes needed by heap_page_prune.
847-
* It is expected that the caller has a super-exclusive lock on the
847+
* It is expected that the caller has a full cleanup lock on the
848848
* buffer.
849849
*/
850850
void

src/backend/access/nbtree/README

Lines changed: 89 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -166,53 +166,40 @@ that the incoming item doesn't fit on the split page where it needs to go!
166166
Deleting index tuples during VACUUM
167167
-----------------------------------
168168

169-
Before deleting a leaf item, we get a super-exclusive lock on the target
169+
Before deleting a leaf item, we get a full cleanup lock on the target
170170
page, so that no other backend has a pin on the page when the deletion
171171
starts. This is not necessary for correctness in terms of the btree index
172172
operations themselves; as explained above, index scans logically stop
173173
"between" pages and so can't lose their place. The reason we do it is to
174-
provide an interlock between VACUUM and indexscans. Since VACUUM deletes
175-
index entries before reclaiming heap tuple line pointers, the
176-
super-exclusive lock guarantees that VACUUM can't reclaim for re-use a
177-
line pointer that an indexscanning process might be about to visit. This
178-
guarantee works only for simple indexscans that visit the heap in sync
179-
with the index scan, not for bitmap scans. We only need the guarantee
180-
when using non-MVCC snapshot rules; when using an MVCC snapshot, it
181-
doesn't matter if the heap tuple is replaced with an unrelated tuple at
182-
the same TID, because the new tuple won't be visible to our scan anyway.
183-
Therefore, a scan using an MVCC snapshot which has no other confounding
184-
factors will not hold the pin after the page contents are read. The
185-
current reasons for exceptions, where a pin is still needed, are if the
186-
index is not WAL-logged or if the scan is an index-only scan. If later
187-
work allows the pin to be dropped for all cases we will be able to
188-
simplify the vacuum code, since the concept of a super-exclusive lock
189-
for btree indexes will no longer be needed.
174+
provide an interlock between VACUUM and index scans that are not prepared
175+
to deal with concurrent TID recycling when visiting the heap. Since only
176+
VACUUM can ever mark pointed-to items LP_UNUSED in the heap, and since
177+
this only ever happens _after_ btbulkdelete returns, having index scans
178+
hold on to the pin (used when reading from the leaf page) until _after_
179+
they're done visiting the heap (for TIDs from pinned leaf page) prevents
180+
concurrent TID recycling. VACUUM cannot get a conflicting cleanup lock
181+
until the index scan is totally finished processing its leaf page.
182+
183+
This approach is fairly coarse, so we avoid it whenever possible. In
184+
practice most index scans won't hold onto their pin, and so won't block
185+
VACUUM. These index scans must deal with TID recycling directly, which is
186+
more complicated and not always possible. See later section on making
187+
concurrent TID recycling safe.
188+
189+
Opportunistic index tuple deletion performs almost the same page-level
190+
modifications while only holding an exclusive lock. This is safe because
191+
there is no question of TID recycling taking place later on -- only VACUUM
192+
can make TIDs recyclable. See also simple deletion and bottom-up
193+
deletion, below.
190194

191195
Because a pin is not always held, and a page can be split even while
192196
someone does hold a pin on it, it is possible that an indexscan will
193197
return items that are no longer stored on the page it has a pin on, but
194198
rather somewhere to the right of that page. To ensure that VACUUM can't
195-
prematurely remove such heap tuples, we require btbulkdelete to obtain a
196-
super-exclusive lock on every leaf page in the index, even pages that
197-
don't contain any deletable tuples. Any scan which could yield incorrect
198-
results if the tuple at a TID matching the scan's range and filter
199-
conditions were replaced by a different tuple while the scan is in
200-
progress must hold the pin on each index page until all index entries read
201-
from the page have been processed. This guarantees that the btbulkdelete
202-
call cannot return while any indexscan is still holding a copy of a
203-
deleted index tuple if the scan could be confused by that. Note that this
204-
requirement does not say that btbulkdelete must visit the pages in any
205-
particular order. (See also simple deletion and bottom-up deletion,
206-
below.)
207-
208-
There is no such interlocking for deletion of items in internal pages,
209-
since backends keep no lock nor pin on a page they have descended past.
210-
Hence, when a backend is ascending the tree using its stack, it must
211-
be prepared for the possibility that the item it wants is to the left of
212-
the recorded position (but it can't have moved left out of the recorded
213-
page). Since we hold a lock on the lower page (per L&Y) until we have
214-
re-found the parent item that links to it, we can be assured that the
215-
parent item does still exist and can't have been deleted.
199+
prematurely make TIDs recyclable in this scenario, we require btbulkdelete
200+
to obtain a cleanup lock on every leaf page in the index, even pages that
201+
don't contain any deletable tuples. Note that this requirement does not
202+
say that btbulkdelete must visit the pages in any particular order.
216203

217204
VACUUM's linear scan, concurrent page splits
218205
--------------------------------------------
@@ -453,6 +440,55 @@ whenever it is subsequently taken from the FSM for reuse. The deleted
453440
page's contents will be overwritten by the split operation (it will become
454441
the new right sibling page).
455442

443+
Making concurrent TID recycling safe
444+
------------------------------------
445+
446+
As explained in the earlier section about deleting index tuples during
447+
VACUUM, we implement a locking protocol that allows individual index scans
448+
to avoid concurrent TID recycling. Index scans opt-out (and so drop their
449+
leaf page pin when visiting the heap) whenever it's safe to do so, though.
450+
Dropping the pin early is useful because it avoids blocking progress by
451+
VACUUM. This is particularly important with index scans used by cursors,
452+
since idle cursors sometimes stop for relatively long periods of time. In
453+
extreme cases, a client application may hold on to an idle cursors for
454+
hours or even days. Blocking VACUUM for that long could be disastrous.
455+
456+
Index scans that don't hold on to a buffer pin are protected by holding an
457+
MVCC snapshot instead. This more limited interlock prevents wrong answers
458+
to queries, but it does not prevent concurrent TID recycling itself (only
459+
holding onto the leaf page pin while accessing the heap ensures that).
460+
461+
Index-only scans can never drop their buffer pin, since they are unable to
462+
tolerate having a referenced TID become recyclable. Index-only scans
463+
typically just visit the visibility map (not the heap proper), and so will
464+
not reliably notice that any stale TID reference (for a TID that pointed
465+
to a dead-to-all heap item at first) was concurrently marked LP_UNUSED in
466+
the heap by VACUUM. This could easily allow VACUUM to set the whole heap
467+
page to all-visible in the visibility map immediately afterwards. An MVCC
468+
snapshot is only sufficient to avoid problems during plain index scans
469+
because they must access granular visibility information from the heap
470+
proper. A plain index scan will even recognize LP_UNUSED items in the
471+
heap (items that could be recycled but haven't been just yet) as "not
472+
visible" -- even when the heap page is generally considered all-visible.
473+
474+
LP_DEAD setting of index tuples by the kill_prior_tuple optimization
475+
(described in full in simple deletion, below) is also more complicated for
476+
index scans that drop their leaf page pins. We must be careful to avoid
477+
LP_DEAD-marking any new index tuple that looks like a known-dead index
478+
tuple because it happens to share the same TID, following concurrent TID
479+
recycling. It's just about possible that some other session inserted a
480+
new, unrelated index tuple, on the same leaf page, which has the same
481+
original TID. It would be totally wrong to LP_DEAD-set this new,
482+
unrelated index tuple.
483+
484+
We handle this kill_prior_tuple race condition by having affected index
485+
scans conservatively assume that any change to the leaf page at all
486+
implies that it was reached by btbulkdelete in the interim period when no
487+
buffer pin was held. This is implemented by not setting any LP_DEAD bits
488+
on the leaf page at all when the page's LSN has changed. (That won't work
489+
with an unlogged index, so for now we don't ever apply the "don't hold
490+
onto pin" optimization there.)
491+
456492
Fastpath For Index Insertion
457493
----------------------------
458494

@@ -518,22 +554,6 @@ that's required for the deletion process to perform granular removal of
518554
groups of dead TIDs from posting list tuples (without the situation ever
519555
being allowed to get out of hand).
520556

521-
It's sufficient to have an exclusive lock on the index page, not a
522-
super-exclusive lock, to do deletion of LP_DEAD items. It might seem
523-
that this breaks the interlock between VACUUM and indexscans, but that is
524-
not so: as long as an indexscanning process has a pin on the page where
525-
the index item used to be, VACUUM cannot complete its btbulkdelete scan
526-
and so cannot remove the heap tuple. This is another reason why
527-
btbulkdelete has to get a super-exclusive lock on every leaf page, not only
528-
the ones where it actually sees items to delete.
529-
530-
LP_DEAD setting by index scans cannot be sure that a TID whose index tuple
531-
it had planned on LP_DEAD-setting has not been recycled by VACUUM if it
532-
drops its pin in the meantime. It must conservatively also remember the
533-
LSN of the page, and only act to set LP_DEAD bits when the LSN has not
534-
changed at all. (Avoiding dropping the pin entirely also makes it safe, of
535-
course.)
536-
537557
Bottom-Up deletion
538558
------------------
539559

@@ -733,23 +753,21 @@ because it allows running applications to continue while the standby
733753
changes state into a normally running server.
734754

735755
The interlocking required to avoid returning incorrect results from
736-
non-MVCC scans is not required on standby nodes. We still get a
737-
super-exclusive lock ("cleanup lock") when replaying VACUUM records
738-
during recovery, but recovery does not need to lock every leaf page
739-
(only those leaf pages that have items to delete). That is safe because
740-
HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesSelf(),
741-
HeapTupleSatisfiesDirty() and HeapTupleSatisfiesVacuum() are only ever
742-
used during write transactions, which cannot exist on the standby. MVCC
743-
scans are already protected by definition, so HeapTupleSatisfiesMVCC()
744-
is not a problem. The optimizer looks at the boundaries of value ranges
745-
using HeapTupleSatisfiesNonVacuumable() with an index-only scan, which
746-
is also safe. That leaves concern only for HeapTupleSatisfiesToast().
747-
748-
HeapTupleSatisfiesToast() doesn't use MVCC semantics, though that's
749-
because it doesn't need to - if the main heap row is visible then the
750-
toast rows will also be visible. So as long as we follow a toast
751-
pointer from a visible (live) tuple the corresponding toast rows
752-
will also be visible, so we do not need to recheck MVCC on them.
756+
non-MVCC scans is not required on standby nodes. We still get a full
757+
cleanup lock when replaying VACUUM records during recovery, but recovery
758+
does not need to lock every leaf page (only those leaf pages that have
759+
items to delete) -- that's sufficient to avoid breaking index-only scans
760+
during recovery (see section above about making TID recycling safe). That
761+
leaves concern only for plain index scans. (XXX: Not actually clear why
762+
this is totally unnecessary during recovery.)
763+
764+
MVCC snapshot plain index scans are always safe, for the same reasons that
765+
they're safe during original execution. HeapTupleSatisfiesToast() doesn't
766+
use MVCC semantics, though that's because it doesn't need to - if the main
767+
heap row is visible then the toast rows will also be visible. So as long
768+
as we follow a toast pointer from a visible (live) tuple the corresponding
769+
toast rows will also be visible, so we do not need to recheck MVCC on
770+
them.
753771

754772
Other Things That Are Handy to Know
755773
-----------------------------------

src/backend/access/nbtree/nbtpage.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,7 @@ _bt_conditionallockbuf(Relation rel, Buffer buf)
11151115
}
11161116

11171117
/*
1118-
* _bt_upgradelockbufcleanup() -- upgrade lock to super-exclusive/cleanup
1119-
* lock.
1118+
* _bt_upgradelockbufcleanup() -- upgrade lock to a full cleanup lock.
11201119
*/
11211120
void
11221121
_bt_upgradelockbufcleanup(Relation rel, Buffer buf)
@@ -1147,7 +1146,7 @@ _bt_pageinit(Page page, Size size)
11471146
/*
11481147
* Delete item(s) from a btree leaf page during VACUUM.
11491148
*
1150-
* This routine assumes that the caller has a super-exclusive write lock on
1149+
* This routine assumes that the caller already has a full cleanup lock on
11511150
* the buffer. Also, the given deletable and updatable arrays *must* be
11521151
* sorted in ascending order.
11531152
*

src/backend/access/nbtree/nbtree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,9 @@ btvacuumpage(BTVacState *vstate, BlockNumber scanblkno)
11611161
nhtidslive;
11621162

11631163
/*
1164-
* Trade in the initial read lock for a super-exclusive write lock on
1165-
* this page. We must get such a lock on every leaf page over the
1166-
* course of the vacuum scan, whether or not it actually contains any
1164+
* Trade in the initial read lock for a full cleanup lock on this
1165+
* page. We must get such a lock on every leaf page over the course
1166+
* of the vacuum scan, whether or not it actually contains any
11671167
* deletable tuples --- see nbtree/README.
11681168
*/
11691169
_bt_upgradelockbufcleanup(rel, buf);

src/backend/access/nbtree/nbtsearch.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ static inline void _bt_initialize_more_data(BTScanOpaque so, ScanDirection dir);
5050
/*
5151
* _bt_drop_lock_and_maybe_pin()
5252
*
53-
* Unlock the buffer; and if it is safe to release the pin, do that, too. It
54-
* is safe if the scan is using an MVCC snapshot and the index is WAL-logged.
53+
* Unlock the buffer; and if it is safe to release the pin, do that, too.
5554
* This will prevent vacuum from stalling in a blocked state trying to read a
56-
* page when a cursor is sitting on it -- at least in many important cases.
55+
* page when a cursor is sitting on it.
5756
*
58-
* Set the buffer to invalid if the pin is released, since the buffer may be
59-
* re-used. If we need to go back to this block (for example, to apply
60-
* LP_DEAD hints) we must get a fresh reference to the buffer. Hopefully it
61-
* will remain in shared memory for as long as it takes to scan the index
62-
* buffer page.
57+
* See nbtree/README section on making concurrent TID recycling safe.
6358
*/
6459
static void
6560
_bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp)

src/backend/storage/page/bufpage.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ compactify_tuples(itemIdCompact itemidbase, int nitems, Page page, bool presorte
701701
* there is, in general, a good chance that even large groups of unused line
702702
* pointers that we see here will be recycled quickly.
703703
*
704-
* Caller had better have a super-exclusive lock on page's buffer. As a side
704+
* Caller had better have a full cleanup lock on page's buffer. As a side
705705
* effect the page's PD_HAS_FREE_LINES hint bit will be set or unset as
706706
* needed.
707707
*/
@@ -820,9 +820,9 @@ PageRepairFragmentation(Page page)
820820
* arbitrary, but it seems like a good idea to avoid leaving a PageIsEmpty()
821821
* page behind.
822822
*
823-
* Caller can have either an exclusive lock or a super-exclusive lock on
824-
* page's buffer. The page's PD_HAS_FREE_LINES hint bit will be set or unset
825-
* based on whether or not we leave behind any remaining LP_UNUSED items.
823+
* Caller can have either an exclusive lock or a full cleanup lock on page's
824+
* buffer. The page's PD_HAS_FREE_LINES hint bit will be set or unset based
825+
* on whether or not we leave behind any remaining LP_UNUSED items.
826826
*/
827827
void
828828
PageTruncateLinePointerArray(Page page)

src/include/access/heapam_xlog.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ typedef struct xl_heap_update
238238
* Note that nunused is not explicitly stored, but may be found by reference
239239
* to the total record length.
240240
*
241-
* Requires a super-exclusive lock.
241+
* Acquires a full cleanup lock.
242242
*/
243243
typedef struct xl_heap_prune
244244
{
@@ -252,9 +252,9 @@ typedef struct xl_heap_prune
252252

253253
/*
254254
* The vacuum page record is similar to the prune record, but can only mark
255-
* already dead items as unused
255+
* already LP_DEAD items LP_UNUSED (during VACUUM's second heap pass)
256256
*
257-
* Used by heap vacuuming only. Does not require a super-exclusive lock.
257+
* Acquires an ordinary exclusive lock only.
258258
*/
259259
typedef struct xl_heap_vacuum
260260
{

0 commit comments

Comments
 (0)