From e769d75ed56ff3971d9b5cda63c68cad52ed74a9 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 v31 2/3] Fix the defect 1

Pending sync is lost by the followig sequence. Fix it.

  begin;
  create table t (c int);
  savepoint q; drop table t; rollback to q;  -- forgets table is skipping wal
  commit;  -- assertion failure
---
 src/backend/utils/cache/relcache.c | 67 ++++++++++++++++++++++++++----
 src/include/utils/rel.h            |  1 +
 2 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 0ac72572e3..551a7d40bd 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -1094,6 +1094,7 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
 	relation->rd_createSubid = InvalidSubTransactionId;
 	relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
 	relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
+	relation->rd_isremoved = false;
 	switch (relation->rd_rel->relpersistence)
 	{
 		case RELPERSISTENCE_UNLOGGED:
@@ -1993,7 +1994,7 @@ RelationIdGetRelation(Oid relationId)
 	{
 		RelationIncrementReferenceCount(rd);
 		/* revalidate cache entry if necessary */
-		if (!rd->rd_isvalid)
+		if (!rd->rd_isvalid && !rd->rd_isremoved)
 		{
 			/*
 			 * Indexes only have a limited number of possible schema changes,
@@ -2137,7 +2138,7 @@ RelationReloadIndexInfo(Relation relation)
 	/* Should be called only for invalidated indexes */
 	Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
 			relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
-		   !relation->rd_isvalid);
+		   !relation->rd_isvalid && !relation->rd_isremoved);
 
 	/* Ensure it's closed at smgr level */
 	RelationCloseSmgr(relation);
@@ -2443,7 +2444,7 @@ 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_isremoved)
 	{
 		relation->rd_isvalid = false;	/* needs to be revalidated */
 		if (IsTransactionState())
@@ -2462,6 +2463,18 @@ 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 "removed" and
+		 * leave it live invalid.
+		 */
+		if (relation->rd_createSubid != InvalidSubTransactionId ||
+			relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+		{
+			relation->rd_isremoved = true;
+			return;
+		}
+
 		/* Remove it from the hash table */
 		RelationCacheDelete(relation);
 
@@ -2546,6 +2559,19 @@ 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 "removed" and
+			 * leave it live invalid.
+			 */
+			if (relation->rd_createSubid != InvalidSubTransactionId ||
+				relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+			{
+				relation->rd_isremoved = true;
+				return;
+			}
+
 			/*
 			 * This shouldn't happen as dropping a relation is intended to be
 			 * impossible if still referenced (cf. CheckTableNotInUse()). But
@@ -2991,7 +3017,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_isremoved)
+		{
+			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 +3153,10 @@ AtEOXact_cleanup(Relation relation, bool isCommit)
 	 */
 	if (relation->rd_createSubid != InvalidSubTransactionId)
 	{
-		if (isCommit)
-			relation->rd_createSubid = InvalidSubTransactionId;
+		relation->rd_createSubid = InvalidSubTransactionId;
+
+		if (isCommit && !relation->rd_isremoved)
+		{} /* Nothing to do */
 		else if (RelationHasReferenceCountZero(relation))
 		{
 			RelationClearRelation(relation, false);
@@ -3131,7 +3172,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));
 		}
@@ -3232,6 +3272,19 @@ AtEOSubXact_cleanup(Relation relation, bool isCommit,
 		}
 	}
 
+	/*
+	 * If this relation registered pending sync then removed, subxact rollback
+	 * cancels pending remove. Subxact commit propagates it to the parent.
+	 */
+	if (relation->rd_isremoved)
+	{
+		Assert (!relation->rd_isvalid &&
+				(relation->rd_createSubid != InvalidSubTransactionId ||
+				 relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId));
+		if (!isCommit)
+			relation->rd_isremoved = false;
+	}
+
 	/*
 	 * 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..970f20b82a 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -98,6 +98,7 @@ typedef struct RelationData
 												 * rd_node to current value */
 	SubTransactionId rd_firstRelfilenodeSubid;	/* highest subxact changing
 												 * rd_node to any value */
+	bool			 rd_isremoved;				/* is to be removed?  */
 
 	Form_pg_class rd_rel;		/* RELATION tuple */
 	TupleDesc	rd_att;			/* tuple descriptor */
-- 
2.23.0

