Merge pull request #5192 from citusdata/use-pg-functions

Use relcache utils instead of scanning catalog tables for indexes and ext stats
pull/5195/head
Onur Tirtir 2021-08-18 17:56:56 +03:00 committed by GitHub
commit 262f89359e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 140 deletions

View File

@ -888,17 +888,12 @@ GetIndexAccessMethodName(Oid indexId)
Oid indexAMId = indexForm->relam;
ReleaseSysCache(indexTuple);
/* fetch pg_am tuple of index' access method */
HeapTuple indexAMTuple = SearchSysCache1(AMOID, ObjectIdGetDatum(indexAMId));
if (!HeapTupleIsValid(indexAMTuple))
char *indexAmName = get_am_name(indexAMId);
if (!indexAmName)
{
ereport(ERROR, (errmsg("access method with oid %u does not exist", indexAMId)));
}
Form_pg_am indexAMForm = (Form_pg_am) GETSTRUCT(indexAMTuple);
char *indexAmName = pstrdup(indexAMForm->amname.data);
ReleaseSysCache(indexAMTuple);
return indexAmName;
}

View File

@ -69,6 +69,9 @@ static char * GetRenameShardTriggerCommand(Oid shardRelationId, char *triggerNam
static void DropRelationTruncateTriggers(Oid relationId);
static char * GetDropTriggerCommand(Oid relationId, char *triggerName);
static List * GetRenameStatsCommandList(List *statsOidList, uint64 shardId);
static void AppendExplicitIndexIdsToList(Form_pg_index indexForm,
List **explicitIndexIdList,
int flags);
static void DropDefaultExpressionsAndMoveOwnedSequenceOwnerships(Oid sourceRelationId,
Oid targetRelationId);
static void DropDefaultColumnDefinition(Oid relationId, char *columnName);
@ -684,7 +687,10 @@ GetRenameShardIndexCommand(Oid indexOid, uint64 shardId)
static void
RenameShardRelationStatistics(Oid shardRelationId, uint64 shardId)
{
List *statsOidList = GetExplicitStatisticsIdList(shardRelationId);
Relation shardRelation = RelationIdGetRelation(shardRelationId);
List *statsOidList = RelationGetStatExtList(shardRelation);
RelationClose(shardRelation);
List *statsCommandList = GetRenameStatsCommandList(statsOidList, shardId);
char *command = NULL;
@ -834,51 +840,25 @@ GetDropTriggerCommand(Oid relationId, char *triggerName)
List *
GetExplicitIndexOidList(Oid relationId)
{
int scanKeyCount = 1;
ScanKeyData scanKey[1];
/* flags are not applicable for AppendExplicitIndexIdsToList */
int flags = 0;
return ExecuteFunctionOnEachTableIndex(relationId, AppendExplicitIndexIdsToList,
flags);
}
PushOverrideEmptySearchPath(CurrentMemoryContext);
Relation pgIndex = table_open(IndexRelationId, AccessShareLock);
ScanKeyInit(&scanKey[0], Anum_pg_index_indrelid,
BTEqualStrategyNumber, F_OIDEQ, relationId);
bool useIndex = true;
SysScanDesc scanDescriptor = systable_beginscan(pgIndex, IndexIndrelidIndexId,
useIndex, NULL, scanKeyCount,
scanKey);
List *indexOidList = NIL;
HeapTuple heapTuple = systable_getnext(scanDescriptor);
while (HeapTupleIsValid(heapTuple))
/*
* AppendExplicitIndexIdsToList adds the given index oid if it is
* explicitly created on its relation.
*/
static void
AppendExplicitIndexIdsToList(Form_pg_index indexForm, List **explicitIndexIdList,
int flags)
{
if (!IndexImpliedByAConstraint(indexForm))
{
Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(heapTuple);
Oid indexId = indexForm->indexrelid;
bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm);
/*
* Skip the indexes that are not implied by explicitly executing
* a CREATE INDEX command.
*/
if (!indexImpliedByConstraint)
{
indexOidList = lappend_oid(indexOidList, indexId);
}
heapTuple = systable_getnext(scanDescriptor);
*explicitIndexIdList = lappend_oid(*explicitIndexIdList, indexForm->indexrelid);
}
systable_endscan(scanDescriptor);
table_close(pgIndex, NoLock);
/* revert back to original search_path */
PopOverrideSearchPath();
return indexOidList;
}

View File

@ -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;
}

View File

@ -425,16 +425,18 @@ PreprocessAlterStatisticsOwnerStmt(Node *node, const char *queryString,
* GetExplicitStatisticsCommandList returns the list of DDL commands to create
* or alter statistics that are explicitly created for the table with relationId.
* This function gets called when distributing the table with relationId.
* See comment of GetExplicitStatisticsIdList function.
*/
List *
GetExplicitStatisticsCommandList(Oid relationId)
{
List *explicitStatisticsCommandList = NIL;
PushOverrideEmptySearchPath(CurrentMemoryContext);
Relation relation = RelationIdGetRelation(relationId);
List *statisticsIdList = RelationGetStatExtList(relation);
RelationClose(relation);
List *statisticsIdList = GetExplicitStatisticsIdList(relationId);
/* generate fully-qualified names */
PushOverrideEmptySearchPath(CurrentMemoryContext);
Oid statisticsId = InvalidOid;
foreach_oid(statisticsId, statisticsIdList)
@ -488,23 +490,19 @@ GetExplicitStatisticsSchemaIdList(Oid relationId)
{
List *schemaIdList = NIL;
Relation pgStatistics = table_open(StatisticExtRelationId, AccessShareLock);
Relation relation = RelationIdGetRelation(relationId);
List *statsIdList = RelationGetStatExtList(relation);
RelationClose(relation);
int scanKeyCount = 1;
ScanKeyData scanKey[1];
ScanKeyInit(&scanKey[0], Anum_pg_statistic_ext_stxrelid,
BTEqualStrategyNumber, F_OIDEQ, relationId);
bool useIndex = true;
SysScanDesc scanDescriptor = systable_beginscan(pgStatistics,
StatisticExtRelidIndexId,
useIndex, NULL, scanKeyCount,
scanKey);
HeapTuple heapTuple = systable_getnext(scanDescriptor);
while (HeapTupleIsValid(heapTuple))
Oid statsId = InvalidOid;
foreach_oid(statsId, statsIdList)
{
HeapTuple heapTuple = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statsId));
if (!HeapTupleIsValid(heapTuple))
{
ereport(ERROR, (errmsg("cache lookup failed for statistics "
"object with oid %u", statsId)));
}
FormData_pg_statistic_ext *statisticsForm =
(FormData_pg_statistic_ext *) GETSTRUCT(heapTuple);
@ -513,12 +511,9 @@ GetExplicitStatisticsSchemaIdList(Oid relationId)
{
schemaIdList = lappend_oid(schemaIdList, schemaId);
}
heapTuple = systable_getnext(scanDescriptor);
ReleaseSysCache(heapTuple);
}
systable_endscan(scanDescriptor);
table_close(pgStatistics, NoLock);
return schemaIdList;
}
@ -567,48 +562,6 @@ GetAlterIndexStatisticsCommands(Oid indexOid)
}
/*
* GetExplicitStatisticsIdList returns a list of OIDs corresponding to the statistics
* that are explicitly created on the relation with relationId. That means,
* this function discards internal statistics implicitly created by postgres.
*/
List *
GetExplicitStatisticsIdList(Oid relationId)
{
List *statisticsIdList = NIL;
Relation pgStatistics = table_open(StatisticExtRelationId, AccessShareLock);
int scanKeyCount = 1;
ScanKeyData scanKey[1];
ScanKeyInit(&scanKey[0], Anum_pg_statistic_ext_stxrelid,
BTEqualStrategyNumber, F_OIDEQ, relationId);
bool useIndex = true;
SysScanDesc scanDescriptor = systable_beginscan(pgStatistics,
StatisticExtRelidIndexId,
useIndex, NULL, scanKeyCount,
scanKey);
HeapTuple heapTuple = systable_getnext(scanDescriptor);
while (HeapTupleIsValid(heapTuple))
{
FormData_pg_statistic_ext *statisticsForm =
(FormData_pg_statistic_ext *) GETSTRUCT(heapTuple);
Oid statisticsId = statisticsForm->oid;
statisticsIdList = lappend_oid(statisticsIdList, statisticsId);
heapTuple = systable_getnext(scanDescriptor);
}
systable_endscan(scanDescriptor);
table_close(pgStatistics, NoLock);
return statisticsIdList;
}
/*
* GenerateAlterIndexColumnSetStatsCommand returns a string in form of 'ALTER INDEX ..
* ALTER COLUMN .. SET STATISTICS ..' which will be used to create a DDL command to

View File

@ -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();
}

View File

@ -388,7 +388,6 @@ extern List * PreprocessAlterStatisticsOwnerStmt(Node *node, const char *querySt
extern List * GetExplicitStatisticsCommandList(Oid relationId);
extern List * GetExplicitStatisticsSchemaIdList(Oid relationId);
extern List * GetAlterIndexStatisticsCommands(Oid indexOid);
extern List * GetExplicitStatisticsIdList(Oid relationId);
/* subscription.c - forward declarations */
extern Node * ProcessCreateSubscriptionStmt(CreateSubscriptionStmt *createSubStmt);