From 549ca4de6d9f4136f2a0104c11121b46b2fb1aae Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Wed, 18 Aug 2021 01:09:12 +0300 Subject: [PATCH] Use RelationGetIndexList instead of scanning pg_index --- src/backend/distributed/commands/index.c | 40 +++++++------------ .../distributed/operations/node_protocol.c | 6 +++ 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/backend/distributed/commands/index.c b/src/backend/distributed/commands/index.c index 6a0e39739..bd1b9eb88 100644 --- a/src/backend/distributed/commands/index.c +++ b/src/backend/distributed/commands/index.c @@ -306,37 +306,25 @@ ExecuteFunctionOnEachTableIndex(Oid relationId, PGIndexProcessor pgIndexProcesso int indexFlags) { List *result = NIL; - ScanKeyData scanKey[1]; - int scanKeyCount = 1; - PushOverrideEmptySearchPath(CurrentMemoryContext); - - /* open system catalog and scan all indexes that belong to this table */ - Relation pgIndex = table_open(IndexRelationId, AccessShareLock); - - ScanKeyInit(&scanKey[0], Anum_pg_index_indrelid, - BTEqualStrategyNumber, F_OIDEQ, relationId); - - SysScanDesc scanDescriptor = systable_beginscan(pgIndex, - IndexIndrelidIndexId, true, /* indexOK */ - NULL, scanKeyCount, scanKey); - - HeapTuple heapTuple = systable_getnext(scanDescriptor); - while (HeapTupleIsValid(heapTuple)) + Relation relation = RelationIdGetRelation(relationId); + List *indexIdList = RelationGetIndexList(relation); + Oid indexId = InvalidOid; + foreach_oid(indexId, indexIdList) { - Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(heapTuple); - pgIndexProcessor(indexForm, &result, indexFlags); + HeapTuple indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId)); + if (!HeapTupleIsValid(indexTuple)) + { + ereport(ERROR, (errmsg("cache lookup failed for index with oid %u", + indexId))); + } - heapTuple = systable_getnext(scanDescriptor); + Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(indexTuple); + pgIndexProcessor(indexForm, &result, indexFlags); + ReleaseSysCache(indexTuple); } - /* clean up scan and close system catalog */ - systable_endscan(scanDescriptor); - table_close(pgIndex, AccessShareLock); - - /* revert back to original search_path */ - PopOverrideSearchPath(); - + RelationClose(relation); return result; } diff --git a/src/backend/distributed/operations/node_protocol.c b/src/backend/distributed/operations/node_protocol.c index f0ff1c82b..1638489e7 100644 --- a/src/backend/distributed/operations/node_protocol.c +++ b/src/backend/distributed/operations/node_protocol.c @@ -798,6 +798,9 @@ void GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLEventList, int indexFlags) { + /* generate fully-qualified names */ + PushOverrideEmptySearchPath(CurrentMemoryContext); + Oid indexId = indexForm->indexrelid; bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm); @@ -845,6 +848,9 @@ GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLE *indexDDLEventList = list_concat(*indexDDLEventList, alterIndexStatisticsCommands); } + + /* revert back to original search_path */ + PopOverrideSearchPath(); }