From 82beb3f17ca7c9fc5b9260b1e19e138a059e2b77 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Thu, 16 Jan 2020 13:24:27 +0900
Subject: [PATCH v34 4/4] Fix the defect 3

Force file sync if the file has been truncated.

The previous verision of the patch allowed to choose WAL when main
fork is larger than ever.  But there's a case where FSM fork gets
shorter while main fork is larger than ever.  Force file sync always
when the file has experienced a truncation.
---
 src/backend/catalog/storage.c | 56 +++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 25 deletions(-)

diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index 8253c420ef..447fb606e5 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -68,7 +68,7 @@ typedef struct PendingRelDelete
 typedef struct pendingSync
 {
 	RelFileNode rnode;
-	BlockNumber max_truncated;
+	bool		is_truncated;	/* Has the file experienced truncation? */
 } pendingSync;
 
 static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
@@ -154,7 +154,7 @@ RelationCreateStorage(RelFileNode rnode, char relpersistence)
 
 		pending = hash_search(pendingSyncHash, &rnode, HASH_ENTER, &found);
 		Assert(!found);
-		pending->max_truncated = 0;
+		pending->is_truncated = false;
 	}
 
 	return srel;
@@ -388,13 +388,7 @@ RelationPreTruncate(Relation rel)
 	/* Record largest maybe-unsynced block of files under tracking  */
 	pending = hash_search(pendingSyncHash, &(rel->rd_smgr->smgr_rnode.node),
 						  HASH_FIND, NULL);
-	if (pending)
-	{
-		BlockNumber nblocks = smgrnblocks(rel->rd_smgr, MAIN_FORKNUM);
-
-		if (pending->max_truncated < nblocks)
-			pending->max_truncated = nblocks;
-	}
+	pending->is_truncated = true;
 }
 
 /*
@@ -637,31 +631,43 @@ smgrDoPendingSyncs(bool isCommit)
 		 * Small WAL records have a chance to be emitted along with other
 		 * backends' WAL records.  We emit WAL records instead of syncing for
 		 * files that are smaller than a certain threshold, expecting faster
-		 * commit.  The threshold is defined by the GUC wal_skip_threshold.
+		 * commit.  The threshold is defined by the GUC wal_skip_threshold.  We
+		 * don't bother counting the pages when the file has experienced a
+		 * truncation.
 		 */
-		for (fork = 0; fork <= MAX_FORKNUM; fork++)
+		if (!pendingsync->is_truncated)
 		{
-			if (smgrexists(srel, fork))
+			for (fork = 0; fork <= MAX_FORKNUM; fork++)
 			{
-				BlockNumber n = smgrnblocks(srel, fork);
+				if (smgrexists(srel, fork))
+				{
+					BlockNumber n = smgrnblocks(srel, fork);
 
-				/* we shouldn't come here for unlogged relations */
-				Assert(fork != INIT_FORKNUM);
-
-				nblocks[fork] = n;
-				total_blocks += n;
+					/* we shouldn't come here for unlogged relations */
+					Assert(fork != INIT_FORKNUM);
+					nblocks[fork] = n;
+					total_blocks += n;
+				}
+				else
+					nblocks[fork] = InvalidBlockNumber;
 			}
-			else
-				nblocks[fork] = InvalidBlockNumber;
 		}
 
 		/*
-		 * Sync file or emit WAL records for its contents.  Do file sync if
-		 * the size is larger than the threshold or truncates may have removed
-		 * blocks beyond the current size.
+		 * Sync file or emit WAL records for its contents.
+		 *
+		 * Alghough we emit WAL record if the file is small enough, do file
+		 * sync regardless of the size if the file has experienced a
+		 * truncation. It is because the file would be followed by trailing
+		 * garbage blocks after a crash recovery if, while a past longer file
+		 * had been flushed out, we omitted syncing-out of the file and emit
+		 * WAL instead.  You might think that we could choose WAL if the
+		 * current main fork is longer than ever, but there's a case where main
+		 * fork is longer than ever but FSM fork gets shorter. We don't bother
+		 * checking that for every fork.
 		 */
-		if (total_blocks * BLCKSZ / 1024 >= wal_skip_threshold ||
-			nblocks[MAIN_FORKNUM] < pendingsync->max_truncated)
+		if (pendingsync->is_truncated ||
+			total_blocks * BLCKSZ / 1024 >= wal_skip_threshold)
 		{
 			/* allocate the initial array, or extend it, if needed */
 			if (maxrels == 0)
-- 
2.18.2

