From 11e6a21ddbc847128e204dac6ba0f62734dd141c 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 v33 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 | 138 ++++++++++++++++++++++++++---
 src/include/utils/rel.h            |   2 +
 src/include/utils/relcache.h       |   1 +
 3 files changed, 131 insertions(+), 10 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 0ac72572e3..a223cc0f94 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
+ * relation that have been created then dropped in the current transaction.
+ */
+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);
@@ -2443,7 +2478,8 @@ RelationClearRelation(Relation relation, bool rebuild)
 	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 +2498,25 @@ RelationClearRelation(Relation relation, bool rebuild)
 	 */
 	if (!rebuild)
 	{
+		/*
+		 * The relcache entry is still needed to perform at-commit sync if the
+		 * subtransaction aborts later.  Mark the relcache as "dropped" and
+		 * leave it live invalid.
+		 */
+		if (relation->rd_createSubid != InvalidSubTransactionId)
+		{
+			if (relation->rd_droppedSubid == InvalidSubTransactionId)
+				relation->rd_droppedSubid = GetCurrentSubTransactionId();
+			else
+			{
+				/* shouldn't try to update it */
+				Assert (relation->rd_droppedSubid ==
+						GetCurrentSubTransactionId());
+			}
+
+			return;
+		}
+
 		/* Remove it from the hash table */
 		RelationCacheDelete(relation);
 
@@ -2546,6 +2601,26 @@ RelationClearRelation(Relation relation, bool rebuild)
 			if (HistoricSnapshotActive())
 				return;
 
+			/*
+			 * Although this relation is already dropped from catalog, the
+			 * relcache entry is still needed to perform at-commit sync if the
+			 * subtransaction aborts later.  Mark the relcache as "dropped" and
+			 * leave it live invalid.
+			 */
+			if (relation->rd_createSubid != InvalidSubTransactionId)
+			{
+				if (relation->rd_droppedSubid == InvalidSubTransactionId)
+					relation->rd_droppedSubid = GetCurrentSubTransactionId();
+				else
+				{
+					/* shouldn't try to update 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 +2673,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 */
@@ -2978,8 +3054,8 @@ AssertPendingSyncs_RelationCache(void)
 			LOCKTAG_RELATION)
 			continue;
 		relid = ObjectIdGetDatum(locallock->tag.lock.locktag_field2);
-		r = RelationIdGetRelation(relid);
-		if (r == NULL)
+		r = RelationIdGetRelationCache(relid);
+		if (!RelationIsValid(r))
 			continue;
 		if (nrels >= maxrels)
 		{
@@ -2991,7 +3067,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]);
@@ -3114,8 +3203,23 @@ AtEOXact_cleanup(Relation relation, bool isCommit)
 	 */
 	if (relation->rd_createSubid != InvalidSubTransactionId)
 	{
-		if (isCommit)
-			relation->rd_createSubid = InvalidSubTransactionId;
+		/* dropped subid must be the same with carete subid if any */
+		Assert (relation->rd_droppedSubid == InvalidSubTransactionId ||
+				relation->rd_createSubid == relation->rd_droppedSubid);
+
+		/*
+		 * cancel rd_createSubid to let RelationClearRelation drop the relcache
+		 * entry.
+		 */
+		relation->rd_createSubid = InvalidSubTransactionId;
+
+		/*
+		 * Besides at rollback, if we have dropped an in-transaction-created
+		 * relation, the corresponding relcache entry is preserved for pending
+		 * sync. We must remove such entries at commit.
+		 */
+		if (isCommit && relation->rd_droppedSubid == InvalidSubTransactionId)
+		{} /* Nothing to do*/
 		else if (RelationHasReferenceCountZero(relation))
 		{
 			RelationClearRelation(relation, false);
@@ -3131,7 +3235,6 @@ 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));
 		}
@@ -3210,7 +3313,7 @@ AtEOSubXact_cleanup(Relation relation, bool isCommit,
 	 */
 	if (relation->rd_createSubid == mySubid)
 	{
-		if (isCommit)
+		if (isCommit && relation->rd_droppedSubid != mySubid)
 			relation->rd_createSubid = parentSubid;
 		else if (RelationHasReferenceCountZero(relation))
 		{
@@ -3232,6 +3335,21 @@ AtEOSubXact_cleanup(Relation relation, bool isCommit,
 		}
 	}
 
+	/*
+	 * Is it a relation create then dropped in the current subtransaction?
+	 *
+	 * 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.23.0

