From f631de03776f79b0d47ce49c34b221310616b448 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 21 Jan 2020 18:12:52 +0900
Subject: [PATCH v33 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 existing index and the reused index may be
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 | 32 +++++++++++++++++++++++++++++---
 src/backend/nodes/copyfuncs.c    |  1 +
 src/backend/nodes/equalfuncs.c   |  1 +
 src/backend/nodes/outfuncs.c     |  1 +
 src/include/nodes/parsenodes.h   |  1 +
 5 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 1b8e38d3aa..c84ba293d4 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -7676,12 +7676,35 @@ 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 = RelationIdGetRelationCache(stmt->oldRelId);
 
-		RelationPreserveStorage(irel->rd_node, true);
-		index_close(irel, NoLock);
+		RelationPreserveStorage(newirel->rd_node, true);
+
+		/*
+		 * oidirel is valid iff the old relation was created then dropped in
+		 * the current transaction.  We need to copy the newness hints other
+		 * than rd_droppedSubid corresponding to the reused relfilenode in that
+		 * case.
+		 */
+		if (oldirel != NULL)
+		{
+			Assert(!oldirel->rd_isvalid &&
+				   oldirel->rd_createSubid != InvalidSubTransactionId &&
+				   oldirel->rd_droppedSubid != InvalidSubTransactionId);
+
+			newirel->rd_createSubid = oldirel->rd_createSubid;
+			newirel->rd_firstRelfilenodeSubid =
+				oldirel->rd_firstRelfilenodeSubid;
+			newirel->rd_newRelfilenodeSubid =
+				oldirel->rd_newRelfilenodeSubid;
+
+			RelationClose(oldirel);
+		}
+		index_close(newirel, NoLock);
 	}
 
 	return address;
@@ -11960,7 +11983,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/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 54ad62bb7f..0e621d74d4 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -3477,6 +3477,7 @@ _copyIndexStmt(const IndexStmt *from)
 	COPY_STRING_FIELD(idxcomment);
 	COPY_SCALAR_FIELD(indexOid);
 	COPY_SCALAR_FIELD(oldNode);
+	COPY_SCALAR_FIELD(oldRelId);
 	COPY_SCALAR_FIELD(unique);
 	COPY_SCALAR_FIELD(primary);
 	COPY_SCALAR_FIELD(isconstraint);
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index 5b1ba143b1..5740b6890b 100644
--- a/src/backend/nodes/equalfuncs.c
+++ b/src/backend/nodes/equalfuncs.c
@@ -1345,6 +1345,7 @@ _equalIndexStmt(const IndexStmt *a, const IndexStmt *b)
 	COMPARE_STRING_FIELD(idxcomment);
 	COMPARE_SCALAR_FIELD(indexOid);
 	COMPARE_SCALAR_FIELD(oldNode);
+	COMPARE_SCALAR_FIELD(oldRelId);
 	COMPARE_SCALAR_FIELD(unique);
 	COMPARE_SCALAR_FIELD(primary);
 	COMPARE_SCALAR_FIELD(isconstraint);
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index d76fae44b8..bcbdb29ccb 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -2650,6 +2650,7 @@ _outIndexStmt(StringInfo str, const IndexStmt *node)
 	WRITE_STRING_FIELD(idxcomment);
 	WRITE_OID_FIELD(indexOid);
 	WRITE_OID_FIELD(oldNode);
+	WRITE_OID_FIELD(oldRelId);
 	WRITE_BOOL_FIELD(unique);
 	WRITE_BOOL_FIELD(primary);
 	WRITE_BOOL_FIELD(isconstraint);
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index da0706add5..b114d7a772 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -2782,6 +2782,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

