Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Unpin buffer before inplace update waits for an XID to end.
authorNoah Misch <noah@leadboat.com>
Tue, 29 Oct 2024 16:39:55 +0000 (09:39 -0700)
committerNoah Misch <noah@leadboat.com>
Tue, 29 Oct 2024 16:39:58 +0000 (09:39 -0700)
Commit a07e03fd8fa7daf4d1356f7cb501ffe784ea6257 changed inplace updates
to wait for heap_update() commands like GRANT TABLE and GRANT DATABASE.
By keeping the pin during that wait, a sequence of autovacuum workers
and an uncommitted GRANT starved one foreground LockBufferForCleanup()
for six minutes, on buildfarm member sarus.  Prevent, at the cost of a
bit of complexity.  Back-patch to v12, like the earlier commit.  That
commit and heap_inplace_lock() have not yet appeared in any release.

Discussion: https://postgr.es/m/20241026184936.ae.nmisch@google.com

src/backend/access/heap/heapam.c
src/backend/access/index/genam.c
src/include/access/heapam.h

index e392810d76c0f130940edcc120640294f819e75c..3ebba39db8776006bf6612bc85b47a58cea51f99 100644 (file)
@@ -6183,8 +6183,8 @@ heap_abort_speculative(Relation relation, ItemPointer tid)
  * transaction.  If compatible, return true with the buffer exclusive-locked,
  * and the caller must release that by calling
  * heap_inplace_update_and_unlock(), calling heap_inplace_unlock(), or raising
- * an error.  Otherwise, return false after blocking transactions, if any,
- * have ended.
+ * an error.  Otherwise, call release_callback(arg), wait for blocking
+ * transactions to end, and return false.
  *
  * Since this is intended for system catalogs and SERIALIZABLE doesn't cover
  * DDL, this doesn't guarantee any particular predicate locking.
@@ -6218,7 +6218,8 @@ heap_abort_speculative(Relation relation, ItemPointer tid)
  */
 bool
 heap_inplace_lock(Relation relation,
-                 HeapTuple oldtup_ptr, Buffer buffer)
+                 HeapTuple oldtup_ptr, Buffer buffer,
+                 void (*release_callback) (void *), void *arg)
 {
    HeapTupleData oldtup = *oldtup_ptr; /* minimize diff vs. heap_update() */
    TM_Result   result;
@@ -6283,6 +6284,7 @@ heap_inplace_lock(Relation relation,
                                        lockmode, NULL))
            {
                LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+               release_callback(arg);
                ret = false;
                MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask,
                                relation, &oldtup.t_self, XLTW_Update,
@@ -6298,6 +6300,7 @@ heap_inplace_lock(Relation relation,
        else
        {
            LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+           release_callback(arg);
            ret = false;
            XactLockTableWait(xwait, relation, &oldtup.t_self,
                              XLTW_Update);
@@ -6309,6 +6312,7 @@ heap_inplace_lock(Relation relation,
        if (!ret)
        {
            LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+           release_callback(arg);
        }
    }
 
index 58f33555f97ea3d8a5cff8529a8e1015dae1b8b0..b123acc5a609146fd828462210c9e06120188a7d 100644 (file)
@@ -804,6 +804,7 @@ systable_inplace_update_begin(Relation relation,
    int         retries = 0;
    SysScanDesc scan;
    HeapTuple   oldtup;
+   BufferHeapTupleTableSlot *bslot;
 
    /*
     * For now, we don't allow parallel updates.  Unlike a regular update,
@@ -825,10 +826,9 @@ systable_inplace_update_begin(Relation relation,
    Assert(IsInplaceUpdateRelation(relation) || !IsSystemRelation(relation));
 
    /* Loop for an exclusive-locked buffer of a non-updated tuple. */
-   for (;;)
+   do
    {
        TupleTableSlot *slot;
-       BufferHeapTupleTableSlot *bslot;
 
        CHECK_FOR_INTERRUPTS();
 
@@ -855,11 +855,9 @@ systable_inplace_update_begin(Relation relation,
        slot = scan->slot;
        Assert(TTS_IS_BUFFERTUPLE(slot));
        bslot = (BufferHeapTupleTableSlot *) slot;
-       if (heap_inplace_lock(scan->heap_rel,
-                             bslot->base.tuple, bslot->buffer))
-           break;
-       systable_endscan(scan);
-   };
+   } while (!heap_inplace_lock(scan->heap_rel,
+                               bslot->base.tuple, bslot->buffer,
+                               (void (*) (void *)) systable_endscan, scan));
 
    *oldtupcopy = heap_copytuple(oldtup);
    *state = scan;
index 4e87a795b1aab8120c220739eac26c7f606d1cc4..65999dd64e16054ec19aa10c2f72d3ff1e478469 100644 (file)
@@ -337,7 +337,8 @@ extern TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple,
                                 Buffer *buffer, struct TM_FailureData *tmfd);
 
 extern bool heap_inplace_lock(Relation relation,
-                             HeapTuple oldtup_ptr, Buffer buffer);
+                             HeapTuple oldtup_ptr, Buffer buffer,
+                             void (*release_callback) (void *), void *arg);
 extern void heap_inplace_update_and_unlock(Relation relation,
                                           HeapTuple oldtup, HeapTuple tuple,
                                           Buffer buffer);