From d86a4004ad3121071b8748ffdb423e8f65290756 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Wed, 15 Jan 2020 17:00:39 +0900
Subject: [PATCH v32 3/4] Fix the defect 2

Pass newness flags to new index relation inherits the old relfilenode
whie ALTER TABLE ALTER TYPE.

The command may reuse old indexes that are created in the current
transaction.  Pass the information to the relcache of the new index
relation so that pending sync correctly works. This relies on the
relcache preserving feature introduced by the previos fix.
---
 src/backend/commands/tablecmds.c | 29 ++++++++++++++++++++++++++---
 src/include/nodes/parsenodes.h   |  1 +
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 0edb474118..59ff5979ad 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -7420,12 +7420,32 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
 	 * this index will have scheduled the storage for deletion at commit, so
 	 * cancel that pending deletion.
 	 */
+	Assert (OidIsValid(stmt->oldNode) == OidIsValid(stmt->oldRelId));
 	if (OidIsValid(stmt->oldNode))
 	{
-		Relation	irel = index_open(address.objectId, NoLock);
+		Relation	newirel = index_open(address.objectId, NoLock);
+		Relation	oldirel = RelationIdGetRelation(stmt->oldRelId);
 
-		RelationPreserveStorage(irel->rd_node, true);
-		index_close(irel, NoLock);
+		RelationPreserveStorage(newirel->rd_node, true);
+
+		/*
+		 * We need to copy the newness hints iff the relation cache entry is
+		 * available for the already dropped relation.
+		 */
+		if (oldirel != NULL)
+		{
+			Assert(!oldirel->rd_isvalid && oldirel->rd_isdropped &&
+				   (oldirel->rd_createSubid != InvalidSubTransactionId ||
+					oldirel->rd_firstRelfilenodeSubid !=
+					InvalidSubTransactionId));
+
+			newirel->rd_createSubid = oldirel->rd_createSubid;
+			newirel->rd_firstRelfilenodeSubid =
+				oldirel->rd_firstRelfilenodeSubid;
+
+			RelationClose(oldirel);
+		}
+		index_close(newirel, NoLock);
 	}
 
 	return address;
@@ -11680,7 +11700,10 @@ TryReuseIndex(Oid oldId, IndexStmt *stmt)
 
 		/* If it's a partitioned index, there is no storage to share. */
 		if (irel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
+		{
 			stmt->oldNode = irel->rd_node.relNode;
+			stmt->oldRelId = irel->rd_id;
+		}
 		index_close(irel, NoLock);
 	}
 }
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 28d837b8fa..c4bdf7ccc9 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -2784,6 +2784,7 @@ typedef struct IndexStmt
 	char	   *idxcomment;		/* comment to apply to index, or NULL */
 	Oid			indexOid;		/* OID of an existing index, if any */
 	Oid			oldNode;		/* relfilenode of existing storage, if any */
+	Oid			oldRelId;		/* relid of the old index, if any */
 	bool		unique;			/* is index unique? */
 	bool		primary;		/* is index a primary key? */
 	bool		isconstraint;	/* is it for a pkey/unique constraint? */
-- 
2.23.0

