hio: Release extension lock before initializing page / pinning VM
authorAndres Freund <[email protected]>
Sun, 2 Apr 2023 00:50:18 +0000 (17:50 -0700)
committerAndres Freund <[email protected]>
Sun, 2 Apr 2023 00:50:18 +0000 (17:50 -0700)
PageInit() while holding the extension lock is unnecessary after 0d1fe9f74e3
started to use RBM_ZERO_AND_LOCK - nobody can look at the new page before we
release the page lock. PageInit() zeroes the page, which isn't that cheap, so
deferring it until after the extension lock is released seems like a good idea.

Doing visibilitymap_pin() while holding the extension lock, introduced in
7db0cd2145f2, looks like an accident. Due to the restrictions on
HEAP_INSERT_FROZEN it's unlikely to be a performance issue, but it still seems
better to move it out.  We also are doing the visibilitymap_pin() while
holding the buffer lock, which will be fixed in a separate commit.

Reviewed-by: Heikki Linnakangas <[email protected]>
Discussion: http://postgr.es/m/419312fd-9255-078c-c3e3-f0525f911d7f@iki.fi

src/backend/access/heap/hio.c

index e152807d2dc9d41184d50b4b146653bc1cab9543..7479212d4e02739520d89f83ac6b93447564c9e1 100644 (file)
@@ -623,6 +623,13 @@ loop:
         */
        buffer = ReadBufferBI(relation, P_NEW, RBM_ZERO_AND_LOCK, bistate);
 
+       /*
+        * Release the file-extension lock; it's now OK for someone else to extend
+        * the relation some more.
+        */
+       if (needLock)
+               UnlockRelationForExtension(relation, ExclusiveLock);
+
        /*
         * We need to initialize the empty new page.  Double-check that it really
         * is empty (this should never happen, but if it does we don't want to
@@ -647,13 +654,6 @@ loop:
                visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer);
        }
 
-       /*
-        * Release the file-extension lock; it's now OK for someone else to extend
-        * the relation some more.
-        */
-       if (needLock)
-               UnlockRelationForExtension(relation, ExclusiveLock);
-
        /*
         * Lock the other buffer. It's guaranteed to be of a lower page number
         * than the new page. To conform with the deadlock prevent rules, we ought