agora inbox for pgsql-hackers@postgresql.org  
help / color / mirror / Atom feed
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Subject: [PATCH v32 3/4] Fix the defect 2
Date: Wed, 15 Jan 2020 17:00:39 +0900

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


----Next_Part(Thu_Jan_16_14_20_57_2020_980)--
Content-Type: Text/X-Patch; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="v32-0004-Fix-the-defect-3.patch"



view thread (3+ messages)  latest in thread

Message-ID: <no-message-id-713307@localhost>
Permalink:  ../../no-message-id-713307@localhost/
Also on:    postgresql.org/message-id/no-message-id-713307@localhost

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: pgsql-hackers@postgresql.org
  Cc: horikyota.ntt@gmail.com
  Subject: Re: [PATCH v32 3/4] Fix the defect 2
  In-Reply-To: <no-message-id-713307@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