1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-08 11:42:09 +03:00

Avoid some overhead with open and close of catalog indexes

This commit improves two code paths to open and close indexes a
minimum amount of times when doing a series of catalog updates or
inserts.  CatalogTupleInsert() is costly when using it for multiple
inserts or updates compared to CatalogTupleInsertWithInfo(), as it would
need to open and close the indexes of the catalog worked each time an
operation is done.

This commit updates the following places:
- REINDEX CONCURRENTLY when copying statistics from one index relation
to the other.  Multi-INSERTs are avoided here, as this would begin to
show benefits only for indexes with multiple expressions, for example,
which may not be the most common pattern.  This change is noticeable in
profiles with indexes having many expressions, for example, and it would
improve any callers of CopyStatistics().
- Update of statistics on ANALYZE, that mixes inserts and updates.
In each case, the catalog indexes are opened only if at least one
insertion and/or update is required, to minimize the cost of the
operation.  Like the previous coding, no indexes are opened as long as
at least one insert or update of pg_statistic has happened.

Author: Ranier Vilela
Reviewed-by: Kyotaro Horiguchi, Michael Paquier
Discussion: https://postgr.es/m/CAEudQAqh0F9y6Di_Wc8xW4zkWm_5SDd-nRfVsCn=h0Nm1C_mrg@mail.gmail.com
This commit is contained in:
Michael Paquier
2022-11-16 10:49:05 +09:00
parent 006b69fd91
commit 09a72188cd
2 changed files with 18 additions and 3 deletions

View File

@ -2856,6 +2856,7 @@ CopyStatistics(Oid fromrelid, Oid torelid)
SysScanDesc scan;
ScanKeyData key[1];
Relation statrel;
CatalogIndexState indstate = NULL;
statrel = table_open(StatisticRelationId, RowExclusiveLock);
@ -2878,13 +2879,20 @@ CopyStatistics(Oid fromrelid, Oid torelid)
/* update the copy of the tuple and insert it */
statform->starelid = torelid;
CatalogTupleInsert(statrel, tup);
/* fetch index information when we know we need it */
if (indstate == NULL)
indstate = CatalogOpenIndexes(statrel);
CatalogTupleInsertWithInfo(statrel, tup, indstate);
heap_freetuple(tup);
}
systable_endscan(scan);
if (indstate != NULL)
CatalogCloseIndexes(indstate);
table_close(statrel, RowExclusiveLock);
}

View File

@ -1624,6 +1624,7 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
{
Relation sd;
int attno;
CatalogIndexState indstate = NULL;
if (natts <= 0)
return; /* nothing to do */
@ -1725,6 +1726,10 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
Int16GetDatum(stats->attr->attnum),
BoolGetDatum(inh));
/* Open index information when we know we need it */
if (indstate == NULL)
indstate = CatalogOpenIndexes(sd);
if (HeapTupleIsValid(oldtup))
{
/* Yes, replace it */
@ -1734,18 +1739,20 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
nulls,
replaces);
ReleaseSysCache(oldtup);
CatalogTupleUpdate(sd, &stup->t_self, stup);
CatalogTupleUpdateWithInfo(sd, &stup->t_self, stup, indstate);
}
else
{
/* No, insert new tuple */
stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
CatalogTupleInsert(sd, stup);
CatalogTupleInsertWithInfo(sd, stup, indstate);
}
heap_freetuple(stup);
}
if (indstate != NULL)
CatalogCloseIndexes(indstate);
table_close(sd, RowExclusiveLock);
}