From 70d8236c375c6dc115e6023707b8a53a28f0b872 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 26 Nov 2019 21:25:09 +0900
Subject: [PATCH v26 6/6] Sync files shrinked by truncation

If truncation made a WAL-skipped file become smaller at commit than
the maximum size during the transaction, the file must not be
at-commit-WAL-logged and must be synced.
---
 src/backend/access/transam/xact.c |   5 +-
 src/backend/catalog/storage.c     | 161 +++++++++++++++++++-----------
 src/include/catalog/storage.h     |   2 +-
 3 files changed, 106 insertions(+), 62 deletions(-)

diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 750f95c482..f681cd3a23 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -2114,7 +2114,7 @@ CommitTransaction(void)
 	 * transaction. This must happen before AtEOXact_RelationMap(), so that we
 	 * don't see committed-but-broken files after a crash.
 	 */
-	smgrDoPendingSyncs();
+	smgrDoPendingSyncs(true);
 
 	/* close large objects before lower-level cleanup */
 	AtEOXact_LargeObject(true);
@@ -2354,7 +2354,7 @@ PrepareTransaction(void)
 	 * transaction. This must happen before EndPrepare(), so that we don't see
 	 * committed-but-broken files after a crash and COMMIT PREPARED.
 	 */
-	smgrDoPendingSyncs();
+	smgrDoPendingSyncs(true);
 
 	/* close large objects before lower-level cleanup */
 	AtEOXact_LargeObject(true);
@@ -2674,6 +2674,7 @@ AbortTransaction(void)
 	 */
 	AfterTriggerEndXact(false); /* 'false' means it's abort */
 	AtAbort_Portals();
+	smgrDoPendingSyncs(false);
 	AtEOXact_LargeObject(false);
 	AtAbort_Notify();
 	AtEOXact_RelationMap(false, is_parallel_worker);
diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index 65811b2a9e..aa68c77d44 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -62,11 +62,17 @@ typedef struct PendingRelDelete
 	BackendId	backend;		/* InvalidBackendId if not a temp rel */
 	bool		atCommit;		/* T=delete at commit; F=delete at abort */
 	int			nestLevel;		/* xact nesting level of request */
-	bool		sync;			/* whether to fsync at commit */
 	struct PendingRelDelete *next;	/* linked-list link */
 } PendingRelDelete;
 
+typedef struct pendingSync
+{
+	RelFileNode rnode;
+	BlockNumber max_truncated;
+} pendingSync;
+
 static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
+HTAB *pendingSyncHash = NULL;
 
 /*
  * RelationCreateStorage
@@ -119,11 +125,39 @@ RelationCreateStorage(RelFileNode rnode, char relpersistence)
 	pending->backend = backend;
 	pending->atCommit = false;	/* delete if abort */
 	pending->nestLevel = GetCurrentTransactionNestLevel();
-	pending->sync =
-		relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded();
 	pending->next = pendingDeletes;
 	pendingDeletes = pending;
 
+	/*
+	 * If the relation needs at-commit sync, we also need to track the maximum
+	 * unsynced truncated block used to decide whether we can WAL-logging or we
+	 * must sync the file in smgrDoPendingSyncs.
+	 */
+	if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
+	{
+		pendingSync *pending;
+		bool		 found;
+
+		/* we sync only permanent relations */
+		Assert(backend == InvalidBackendId);
+
+		if (!pendingSyncHash)
+		{
+			HASHCTL ctl;
+
+			ctl.keysize = sizeof(RelFileNode);
+			ctl.entrysize =  sizeof(pendingSync);
+			ctl.hcxt = TopTransactionContext;
+			pendingSyncHash =
+				hash_create("max truncatd block hash",
+							16, &ctl, HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
+		}
+
+		pending = hash_search(pendingSyncHash, &rnode, HASH_ENTER, &found);
+		Assert(!found);
+		pending->max_truncated = InvalidBlockNumber;
+	}
+
 	return srel;
 }
 
@@ -162,7 +196,6 @@ RelationDropStorage(Relation rel)
 	pending->backend = rel->rd_backend;
 	pending->atCommit = true;	/* delete if commit */
 	pending->nestLevel = GetCurrentTransactionNestLevel();
-	pending->sync = false;
 	pending->next = pendingDeletes;
 	pendingDeletes = pending;
 
@@ -320,6 +353,22 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
 		if (fsm || vm)
 			XLogFlush(lsn);
 	}
+	else if (pendingSyncHash)
+	{
+		pendingSync *pending;
+
+		/* 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 (!BlockNumberIsValid(pending->max_truncated) ||
+				pending->max_truncated < nblocks)
+				pending->max_truncated = nblocks;
+		}
+	}
 
 	/* Do the real work to truncate relation forks */
 	smgrtruncate(rel->rd_smgr, forks, nforks, blocks);
@@ -430,18 +479,17 @@ RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
 bool
 RelFileNodeSkippingWAL(RelFileNode rnode)
 {
-	PendingRelDelete *pending;
-
 	if (XLogIsNeeded())
 		return false;  /* no permanent relfilenode skips WAL */
 
-	for (pending = pendingDeletes; pending != NULL; pending = pending->next)
-	{
-		if (RelFileNodeEquals(pending->relnode, rnode) && pending->sync)
-			return true;
-	}
+	if (!pendingSyncHash)
+		return false;  /* we don't have a to-be-synced relation */
 
-	return false;
+	/* the relation is not tracked as to-be-synced */
+	if (hash_search(pendingSyncHash, &rnode, HASH_FIND, NULL) == NULL)
+		return false;
+
+	return true;
 }
 
 /*
@@ -529,13 +577,14 @@ smgrDoPendingDeletes(bool isCommit)
  * failure prevents commit.
  */
 void
-smgrDoPendingSyncs(void)
+smgrDoPendingSyncs(bool isCommit)
 {
 	PendingRelDelete *pending;
-	HTAB	*delhash = NULL;
 	int			nrels = 0,
 				maxrels = 0;
 	SMgrRelation *srels = NULL;
+	HASH_SEQ_STATUS scan;
+	pendingSync *pendingsync;
 
 	if (XLogIsNeeded())
 		return;  /* no relation can use this */
@@ -543,58 +592,44 @@ smgrDoPendingSyncs(void)
 	Assert(GetCurrentTransactionNestLevel() == 1);
 	AssertPendingSyncs_RelationCache();
 
+	if (!pendingSyncHash)
+		return; /* no relation needs sync */
+
+	/* Just throw away all pending syncs if any at rollback */
+	if (!isCommit)
+	{
+		if (pendingSyncHash)
+		{
+			hash_destroy(pendingSyncHash);
+			pendingSyncHash = NULL;
+		}
+		return;
+	}
+
 	/*
 	 * Pending syncs on the relation that are to be deleted in this
-	 * transaction-end should be ignored. Collect pending deletes that will
-	 * happen in the following call to smgrDoPendingDeletes().
+	 * transaction-end should be ignored. Remove sync hash entries entries for
+	 * relations that will be deleted in the following call to
+	 * smgrDoPendingDeletes().
 	 */
 	for (pending = pendingDeletes; pending != NULL; pending = pending->next)
 	{
-		bool found PG_USED_FOR_ASSERTS_ONLY;
-
 		if (!pending->atCommit)
 			continue;
 
-		/* create the hash if not yet */
-		if (delhash == NULL)
-		{
-			HASHCTL hash_ctl;
-
-			memset(&hash_ctl, 0, sizeof(hash_ctl));
-			hash_ctl.keysize = sizeof(RelFileNode);
-			hash_ctl.entrysize = sizeof(RelFileNode);
-			hash_ctl.hcxt = CurrentMemoryContext;
-			delhash =
-				hash_create("pending del temporary hash", 8, &hash_ctl,
-							HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
-		}
-
-		(void) hash_search(delhash, (void *) &pending->relnode,
-						   HASH_ENTER, &found);
-		Assert(!found);
+		(void) hash_search(pendingSyncHash, (void *) &pending->relnode,
+						   HASH_REMOVE, NULL);
 	}
 
-	for (pending = pendingDeletes; pending != NULL; pending = pending->next)
+	hash_seq_init(&scan, pendingSyncHash);
+	while ((pendingsync = (pendingSync *) hash_seq_search(&scan)))
 	{
-		bool to_be_removed = false;
-		ForkNumber fork;
-		BlockNumber nblocks[MAX_FORKNUM + 1];
-		BlockNumber total_blocks = 0;
-		SMgrRelation srel;
-
-		if (!pending->sync)
-			continue;
-		Assert(!pending->atCommit);
-
-		/* don't sync relnodes that is being deleted */
-		if (delhash)
-			hash_search(delhash, (void *) &pending->relnode,
-						HASH_FIND, &to_be_removed);
-		if (to_be_removed)
-			continue;
+		ForkNumber		fork;
+		BlockNumber		nblocks[MAX_FORKNUM + 1];
+		BlockNumber		total_blocks = 0;
+		SMgrRelation	srel;
 
-		/* Now the time to sync the rnode */
-		srel = smgropen(pending->relnode, pending->backend);
+		srel = smgropen(pendingsync->rnode, InvalidBackendId);
 
 		/*
 		 * We emit newpage WAL records for smaller relations.
@@ -622,9 +657,12 @@ smgrDoPendingSyncs(void)
 
 		/*
 		 * Sync file or emit WAL record for the file according to the total
-		 * size.
+		 * size. Do file sync if the size is larger than the threshold or
+		 * truncates may have left blocks beyond the current size.
 		 */
-		if (total_blocks * BLCKSZ >= wal_skip_threshold * 1024)
+		if (total_blocks * BLCKSZ >= wal_skip_threshold * 1024 ||
+			(BlockNumberIsValid(pendingsync->max_truncated) &&
+			 smgrnblocks(srel, MAIN_FORKNUM) < pendingsync->max_truncated))
 		{
 			/* relations to sync are passed to smgrdosyncall at once */
 
@@ -644,7 +682,11 @@ smgrDoPendingSyncs(void)
 		}
 		else
 		{
-			/* Emit WAL records for all blocks. The file is small enough. */
+			/*
+			 * Emit WAL records for all blocks.  We don't emit
+			 * XLOG_SMGR_TRUNCATE record because the past truncations haven't
+			 * left unlogged pages here.
+			 */
 			for (fork = 0 ; fork <= MAX_FORKNUM ; fork++)
 			{
 				int    n        = nblocks[fork];
@@ -666,8 +708,9 @@ smgrDoPendingSyncs(void)
 		}
 	}
 
-	if (delhash)
-		hash_destroy(delhash);
+	Assert (pendingSyncHash);
+	hash_destroy(pendingSyncHash);
+	pendingSyncHash = NULL;
 
 	if (nrels > 0)
 	{
diff --git a/src/include/catalog/storage.h b/src/include/catalog/storage.h
index 108115a023..bf076657e7 100644
--- a/src/include/catalog/storage.h
+++ b/src/include/catalog/storage.h
@@ -35,7 +35,7 @@ extern bool RelFileNodeSkippingWAL(RelFileNode rnode);
  * naming
  */
 extern void smgrDoPendingDeletes(bool isCommit);
-extern void smgrDoPendingSyncs(void);
+extern void smgrDoPendingSyncs(bool isCommit);
 extern int	smgrGetPendingDeletes(bool forCommit, RelFileNode **ptr);
 extern void AtSubCommit_smgr(void);
 extern void AtSubAbort_smgr(void);
-- 
2.23.0

