From 871cc92c70f1ec3a38abcaf943ea7bc2fc56dcff 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 v32 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 | 67 ++++++++++++++++++++++++++----
 src/include/utils/rel.h            |  2 +
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 0ac72572e3..7632165ca9 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_isdropped = 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_isdropped)
 		{
 			/*
 			 * 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_isdropped);
 
 	/* 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_isdropped)
 	{
 		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 "dropped" and
+		 * leave it live invalid.
+		 */
+		if (relation->rd_createSubid != InvalidSubTransactionId ||
+			relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+		{
+			relation->rd_isdropped = 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 "dropped" and
+			 * leave it live invalid.
+			 */
+			if (relation->rd_createSubid != InvalidSubTransactionId ||
+				relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
+			{
+				relation->rd_isdropped = 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_isdropped)
+		{
+			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_isdropped)
+		{} /* 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 dropped, subxact rollback
+	 * cancels the uncommitted drop, and commit propagates it to the parent.
+	 */
+	if (relation->rd_isdropped)
+	{
+		Assert (!relation->rd_isvalid &&
+				(relation->rd_createSubid != InvalidSubTransactionId ||
+				 relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId));
+		if (!isCommit)
+			relation->rd_isdropped = 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..da0c197dcf 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 */
+	bool			 rd_isdropped;				/* has the coressponding catalog
+												 * entry been dropped? */
 
 	Form_pg_class rd_rel;		/* RELATION tuple */
 	TupleDesc	rd_att;			/* tuple descriptor */
-- 
2.23.0

