Use RelationGetIndexList instead of scanning pg_index

pull/5192/head
Onur Tirtir 2021-08-18 01:09:12 +03:00
parent fa9933daf3
commit 549ca4de6d
2 changed files with 20 additions and 26 deletions

View File

@ -306,37 +306,25 @@ ExecuteFunctionOnEachTableIndex(Oid relationId, PGIndexProcessor pgIndexProcesso
int indexFlags) int indexFlags)
{ {
List *result = NIL; List *result = NIL;
ScanKeyData scanKey[1];
int scanKeyCount = 1;
PushOverrideEmptySearchPath(CurrentMemoryContext); Relation relation = RelationIdGetRelation(relationId);
List *indexIdList = RelationGetIndexList(relation);
/* open system catalog and scan all indexes that belong to this table */ Oid indexId = InvalidOid;
Relation pgIndex = table_open(IndexRelationId, AccessShareLock); foreach_oid(indexId, indexIdList)
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))
{ {
Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(heapTuple); HeapTuple indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
pgIndexProcessor(indexForm, &result, indexFlags); 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 */ RelationClose(relation);
systable_endscan(scanDescriptor);
table_close(pgIndex, AccessShareLock);
/* revert back to original search_path */
PopOverrideSearchPath();
return result; return result;
} }

View File

@ -798,6 +798,9 @@ void
GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLEventList, GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLEventList,
int indexFlags) int indexFlags)
{ {
/* generate fully-qualified names */
PushOverrideEmptySearchPath(CurrentMemoryContext);
Oid indexId = indexForm->indexrelid; Oid indexId = indexForm->indexrelid;
bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm); bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm);
@ -845,6 +848,9 @@ GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLE
*indexDDLEventList = list_concat(*indexDDLEventList, *indexDDLEventList = list_concat(*indexDDLEventList,
alterIndexStatisticsCommands); alterIndexStatisticsCommands);
} }
/* revert back to original search_path */
PopOverrideSearchPath();
} }