From: Kyotaro Horiguchi Date: Wed, 15 Jan 2020 17:00:39 +0900 Subject: [PATCH v31 3/3] Fix the defect 2 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)----