diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 7ad10736d5..eba9a4716d 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -3467,7 +3467,7 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
 		(BM_DIRTY | BM_JUST_DIRTIED))
 	{
 		XLogRecPtr	lsn = InvalidXLogRecPtr;
-		bool		dirtied = false;
+		bool		update_stats = false;
 		bool		delayChkpt = false;
 		uint32		buf_state;
 
@@ -3524,26 +3524,28 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
 
 		Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
 
+		/*
+		 * This allows to update cost accounting below without keeping
+		 * the buffer header lock around.
+		 */
 		if (!(buf_state & BM_DIRTY))
-		{
-			dirtied = true;		/* Means "will be dirtied by this action" */
+			update_stats = true;
 
-			/*
-			 * Set the page LSN if we wrote a backup block. We aren't supposed
-			 * to set this when only holding a share lock but as long as we
-			 * serialise it somehow we're OK. We choose to set LSN while
-			 * holding the buffer header lock, which causes any reader of an
-			 * LSN who holds only a share lock to also obtain a buffer header
-			 * lock before using PageGetLSN(), which is enforced in
-			 * BufferGetLSNAtomic().
-			 *
-			 * If checksums are enabled, you might think we should reset the
-			 * checksum here. That will happen when the page is written
-			 * sometime later in this checkpoint cycle.
-			 */
-			if (!XLogRecPtrIsInvalid(lsn))
-				PageSetLSN(page, lsn);
-		}
+		/*
+		 * Set the page LSN if we wrote a backup block. We aren't supposed
+		 * to set this when only holding a share lock but as long as we
+		 * serialise it somehow we're OK. We choose to set LSN while holding
+		 * the buffer header lock, which causes any reader of an * LSN who
+		 * holds only a share lock to also obtain a buffer header * lock
+		 * before using PageGetLSN(), which is enforced in
+		 * BufferGetLSNAtomic().
+		 *
+		 * If checksums are enabled, you might think we should reset the
+		 * checksum here. That will happen when the page is written sometime
+		 * later in this checkpoint cycle.
+		 */
+		if (!XLogRecPtrIsInvalid(lsn))
+			PageSetLSN(page, lsn);
 
 		buf_state |= BM_DIRTY | BM_JUST_DIRTIED;
 		UnlockBufHdr(bufHdr, buf_state);
@@ -3551,8 +3553,9 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
 		if (delayChkpt)
 			MyPgXact->delayChkpt = false;
 
-		if (dirtied)
+		if (update_stats)
 		{
+			/* Do vacuum cost accounting */
 			VacuumPageDirty++;
 			pgBufferUsage.shared_blks_dirtied++;
 			if (VacuumCostActive)
