agora inbox for [email protected]  
help / color / mirror / Atom feed
From: Kyotaro Horiguchi <[email protected]>
Subject: [PATCH v31 3/3] Fix the defect 2
Date: Wed, 15 Jan 2020 17:00:39 +0900

ALTER TABLE ALTER TYPE 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.
---
 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..9c7e2d48e4 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 newness hints to new relation iff the relcache entry
+		 * of the already removed relation is avaiable.
+		 */
+		if (oldirel != NULL)
+		{
+			Assert(!oldirel->rd_isvalid && oldirel->rd_isremoved &&
+				   (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


----Next_Part(Wed_Jan_15_17_18_57_2020_742)----





view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [PATCH v31 3/3] Fix the defect 2
  In-Reply-To: <no-message-id-192807@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox