From 1ccd187820b20378059e4c0346ee5c8623c007d5 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 14 Jan 2020 19:24:04 +0900
Subject: [PATCH v34 2/4] Fix the defect 1

Pending sync is lost by the followig sequence.

  begin;
  create table t (c int);
  savepoint q; drop table t; rollback to q;  -- forgets table is skipping wal
  commit;  -- assertion failure

Relcache entry for a dropped relation is deleted right out. On the
other hand we need the newness information holded in the dropped entry
if the subtransaction is rolled back later. So this patch makes
relcache entry preserved after dropping of a relation that any newness
flag is active, so that it is available later in the current
transaction.
---
 src/backend/utils/cache/relcache.c | 170 +++++++++++++++++++++++++----
 src/include/utils/rel.h            |   2 +
 src/include/utils/relcache.h       |   1 +
 3 files changed, 150 insertions(+), 23 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 0ac72572e3..ec1e501b4d 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -18,6 +18,7 @@
  *		RelationCacheInitializePhase2	- initialize shared-catalog entries
  *		RelationCacheInitializePhase3	- finish initializing relcache
  *		RelationIdGetRelation			- get a reldesc by relation id
+ *		RelationIdGetRelationCache		- get a relcache entry by relation id
  *		RelationClose					- close an open relation
  *
  * NOTES
@@ -1094,6 +1095,7 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
 	relation->rd_createSubid = InvalidSubTransactionId;
 	relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
 	relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
+	relation->rd_droppedSubid = InvalidSubTransactionId;
 	switch (relation->rd_rel->relpersistence)
 	{
 		case RELPERSISTENCE_UNLOGGED:
@@ -1991,6 +1993,13 @@ RelationIdGetRelation(Oid relationId)
 
 	if (RelationIsValid(rd))
 	{
+		/* return NULL for dropped relatoins */
+		if (rd->rd_droppedSubid != InvalidSubTransactionId)
+		{
+			Assert (!rd->rd_isvalid);
+			return NULL;
+		}
+
 		RelationIncrementReferenceCount(rd);
 		/* revalidate cache entry if necessary */
 		if (!rd->rd_isvalid)
@@ -2030,6 +2039,31 @@ RelationIdGetRelation(Oid relationId)
 	return rd;
 }
 
+/*
+ * RelationIdGetRelationCache: returns an entry exists in relcache.
+ *
+ * This function returns NULL not building new one if no existing entries
+ * found, and may return an invalid or dropped-but-not-commited entry if any.
+ *
+ * This function is intended to be used to lookup the relcache entriy for a
+ * dropped relation that entry is preserved for pending sync.
+ */
+Relation
+RelationIdGetRelationCache(Oid relationId)
+{
+	Relation	rd;
+
+	/* Make sure we're in an xact, even if this ends up being a cache hit */
+	Assert(IsTransactionState());
+
+	RelationIdCacheLookup(relationId, rd);
+
+	if (RelationIsValid(rd))
+		RelationIncrementReferenceCount(rd);
+
+	return rd;
+}
+
 /* ----------------------------------------------------------------
  *				cache invalidation support routines
  * ----------------------------------------------------------------
@@ -2134,10 +2168,11 @@ RelationReloadIndexInfo(Relation relation)
 	HeapTuple	pg_class_tuple;
 	Form_pg_class relp;
 
-	/* Should be called only for invalidated indexes */
+	/* Should be called only for invalidated living indexes */
 	Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
 			relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
-		   !relation->rd_isvalid);
+		   !relation->rd_isvalid &&
+		   relation->rd_droppedSubid == InvalidSubTransactionId);
 
 	/* Ensure it's closed at smgr level */
 	RelationCloseSmgr(relation);
@@ -2438,12 +2473,14 @@ RelationClearRelation(Relation relation, bool rebuild)
 	 * have valid index support information.  This avoids problems with active
 	 * use of the index support information.  As with nailed indexes, we
 	 * re-read the pg_class row to handle possible physical relocation of the
-	 * index, and we check for pg_index updates too.
+	 * index, and we check for pg_index updates too.  Relations with valid
+	 * rd_droppedSubid doesn't have the corresponding catalog entry,
 	 */
 	if ((relation->rd_rel->relkind == RELKIND_INDEX ||
 		 relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
 		relation->rd_refcnt > 0 &&
-		relation->rd_indexcxt != NULL)
+		relation->rd_indexcxt != NULL &&
+		relation->rd_droppedSubid == InvalidSubTransactionId)
 	{
 		relation->rd_isvalid = false;	/* needs to be revalidated */
 		if (IsTransactionState())
@@ -2462,6 +2499,25 @@ RelationClearRelation(Relation relation, bool rebuild)
 	 */
 	if (!rebuild)
 	{
+		/*
+		 * If pending sync is active, the entry is still needed.  Mark the
+		 * relcache as "dropped" and leave it live invalid.
+		 */
+		if (relation->rd_createSubid != InvalidSubTransactionId ||
+			relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+		{
+			if (relation->rd_droppedSubid == InvalidSubTransactionId)
+				relation->rd_droppedSubid = GetCurrentSubTransactionId();
+			else
+			{
+				/* shouldn't try to change it */
+				Assert (relation->rd_droppedSubid ==
+						GetCurrentSubTransactionId());
+			}
+
+			return;
+		}
+
 		/* Remove it from the hash table */
 		RelationCacheDelete(relation);
 
@@ -2546,6 +2602,26 @@ RelationClearRelation(Relation relation, bool rebuild)
 			if (HistoricSnapshotActive())
 				return;
 
+			/*
+			 * Although this relation is already dropped from the catalog, the
+			 * relcache entry is still needed if pending sync is active.  Mark
+			 * the relcache as "dropped" and leave it live invalid.
+			 */
+			if (relation->rd_createSubid != InvalidSubTransactionId ||
+				relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+			{
+				if (relation->rd_droppedSubid == InvalidSubTransactionId)
+					relation->rd_droppedSubid = GetCurrentSubTransactionId();
+				else
+				{
+					/* shouldn't try to change it */
+					Assert (relation->rd_droppedSubid ==
+							GetCurrentSubTransactionId());
+				}
+
+				return;
+			}
+
 			/*
 			 * This shouldn't happen as dropping a relation is intended to be
 			 * impossible if still referenced (cf. CheckTableNotInUse()). But
@@ -2598,6 +2674,7 @@ RelationClearRelation(Relation relation, bool rebuild)
 		SWAPFIELD(SubTransactionId, rd_createSubid);
 		SWAPFIELD(SubTransactionId, rd_newRelfilenodeSubid);
 		SWAPFIELD(SubTransactionId, rd_firstRelfilenodeSubid);
+		SWAPFIELD(SubTransactionId, rd_droppedSubid);
 		/* un-swap rd_rel pointers, swap contents instead */
 		SWAPFIELD(Form_pg_class, rd_rel);
 		/* ... but actually, we don't have to update newrel->rd_rel */
@@ -2991,7 +3068,20 @@ AssertPendingSyncs_RelationCache(void)
 
 	hash_seq_init(&status, RelationIdCache);
 	while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
-		AssertPendingSyncConsistency(idhentry->reldesc);
+	{
+		Relation r = idhentry->reldesc;
+
+		/* Ignore relcache entries of deleted relations */
+		if (r->rd_droppedSubid != InvalidSubTransactionId)
+		{
+			Assert(!r->rd_isvalid &&
+				   (r->rd_createSubid != InvalidSubTransactionId ||
+					r->rd_firstRelfilenodeSubid != InvalidSubTransactionId));
+			continue;
+		}
+
+		AssertPendingSyncConsistency(r);
+	}
 
 	for (i = 0; i < nrels; i++)
 		RelationClose(rels[i]);
@@ -3081,6 +3171,8 @@ AtEOXact_RelationCache(bool isCommit)
 static void
 AtEOXact_cleanup(Relation relation, bool isCommit)
 {
+	bool clear_relcache = false;
+
 	/*
 	 * The relcache entry's ref count should be back to its normal
 	 * not-in-a-transaction state: 0 unless it's nailed in cache.
@@ -3106,17 +3198,31 @@ AtEOXact_cleanup(Relation relation, bool isCommit)
 #endif
 
 	/*
-	 * Is it a relation created in the current transaction?
+	 * Is the relation lives after this transction ends?
 	 *
-	 * During commit, reset the flag to zero, since we are now out of the
-	 * creating transaction.  During abort, simply delete the relcache entry
-	 * --- it isn't interesting any longer.
+	 * During commit, clear the relcache entry if it is preserved after
+	 * relation drop, in order not to make the entry left orphaned. During
+	 * rollback, clear the relacache entry if the relation is created in the
+	 * current transaction since it isn't interesting any longer once we are
+	 * out of the transaction.
 	 */
-	if (relation->rd_createSubid != InvalidSubTransactionId)
+	clear_relcache =
+		(isCommit ?
+		 relation->rd_droppedSubid != InvalidSubTransactionId :
+		 relation->rd_createSubid != InvalidSubTransactionId);
+			
+	/*
+	 * Since we are now out of the transaction, reset the flags to zero.
+	 * That also lets RelationClearRelation drop the relcache entry.
+	 */
+	relation->rd_createSubid = InvalidSubTransactionId;
+	relation->rd_droppedSubid = InvalidSubTransactionId;
+	relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
+	relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
+
+	if (clear_relcache)
 	{
-		if (isCommit)
-			relation->rd_createSubid = InvalidSubTransactionId;
-		else if (RelationHasReferenceCountZero(relation))
+		if (RelationHasReferenceCountZero(relation))
 		{
 			RelationClearRelation(relation, false);
 			return;
@@ -3131,17 +3237,10 @@ AtEOXact_cleanup(Relation relation, bool isCommit)
 			 * eventually.  This must be just a WARNING to avoid
 			 * error-during-error-recovery loops.
 			 */
-			relation->rd_createSubid = InvalidSubTransactionId;
 			elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
 				 RelationGetRelationName(relation));
 		}
 	}
-
-	/*
-	 * Likewise, reset any record of the relfilenode being new.
-	 */
-	relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
-	relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
 }
 
 /*
@@ -3205,15 +3304,24 @@ AtEOSubXact_cleanup(Relation relation, bool isCommit,
 	/*
 	 * Is it a relation created in the current subtransaction?
 	 *
-	 * During subcommit, mark it as belonging to the parent, instead. During
-	 * subabort, simply delete the relcache entry.
+	 * During subcommit, mark it as belonging to the parent, instead, as long
+	 * as it has not been dropped. Otherwise simply delete the relcache entry.
+	 * --- it isn't interesting any longer.
 	 */
 	if (relation->rd_createSubid == mySubid)
 	{
-		if (isCommit)
+		/*
+		 * Valid rd_droppedSubid means the corresponding relation is dropped
+		 * but the relcache entry is preserved for at-commit pending sync.
+		 * We need to drop it explicitly here not to make the entry orphan.
+		 */
+		if (isCommit && relation->rd_droppedSubid != mySubid)
 			relation->rd_createSubid = parentSubid;
 		else if (RelationHasReferenceCountZero(relation))
 		{
+			/* allow the entry to be removed */
+			relation->rd_createSubid = InvalidSubTransactionId;
+			relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
 			RelationClearRelation(relation, false);
 			return;
 		}
@@ -3232,6 +3340,22 @@ AtEOSubXact_cleanup(Relation relation, bool isCommit,
 		}
 	}
 
+	/*
+	 * Have the relation that got a new relfilenode in the current transaction
+	 * been dropped?
+	 *
+	 * If this relation registered pending sync then dropped, subxact rollback
+	 * cancels the uncommitted drop, and commit propagates it to the parent.
+	 */
+	if (relation->rd_droppedSubid == mySubid)
+	{
+		if (isCommit)
+			relation->rd_droppedSubid = parentSubid;
+		else
+			relation->rd_droppedSubid = InvalidSubTransactionId;
+	}
+
+
 	/*
 	 * Likewise, update or drop any new-relfilenode-in-subtransaction record.
 	 */
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index ad72a8b910..0b87bc3222 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -98,6 +98,8 @@ typedef struct RelationData
 												 * rd_node to current value */
 	SubTransactionId rd_firstRelfilenodeSubid;	/* highest subxact changing
 												 * rd_node to any value */
+	SubTransactionId rd_droppedSubid;	/* in-transaction created rel has been
+										 * dropped */
 
 	Form_pg_class rd_rel;		/* RELATION tuple */
 	TupleDesc	rd_att;			/* tuple descriptor */
diff --git a/src/include/utils/relcache.h b/src/include/utils/relcache.h
index 62239a09e8..0362b6f6ff 100644
--- a/src/include/utils/relcache.h
+++ b/src/include/utils/relcache.h
@@ -37,6 +37,7 @@ typedef Relation *RelationPtr;
  * Routines to open (lookup) and close a relcache entry
  */
 extern Relation RelationIdGetRelation(Oid relationId);
+extern Relation RelationIdGetRelationCache(Oid relationId);
 extern void RelationClose(Relation relation);
 
 /*
-- 
2.18.2

