Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 09a7218

Browse files
committed
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
1 parent 006b69f commit 09a7218

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/backend/catalog/heap.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,7 @@ CopyStatistics(Oid fromrelid, Oid torelid)
28562856
SysScanDesc scan;
28572857
ScanKeyData key[1];
28582858
Relation statrel;
2859+
CatalogIndexState indstate = NULL;
28592860

28602861
statrel = table_open(StatisticRelationId, RowExclusiveLock);
28612862

@@ -2878,13 +2879,20 @@ CopyStatistics(Oid fromrelid, Oid torelid)
28782879

28792880
/* update the copy of the tuple and insert it */
28802881
statform->starelid = torelid;
2881-
CatalogTupleInsert(statrel, tup);
2882+
2883+
/* fetch index information when we know we need it */
2884+
if (indstate == NULL)
2885+
indstate = CatalogOpenIndexes(statrel);
2886+
2887+
CatalogTupleInsertWithInfo(statrel, tup, indstate);
28822888

28832889
heap_freetuple(tup);
28842890
}
28852891

28862892
systable_endscan(scan);
28872893

2894+
if (indstate != NULL)
2895+
CatalogCloseIndexes(indstate);
28882896
table_close(statrel, RowExclusiveLock);
28892897
}
28902898

src/backend/commands/analyze.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,7 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
16241624
{
16251625
Relation sd;
16261626
int attno;
1627+
CatalogIndexState indstate = NULL;
16271628

16281629
if (natts <= 0)
16291630
return; /* nothing to do */
@@ -1725,6 +1726,10 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
17251726
Int16GetDatum(stats->attr->attnum),
17261727
BoolGetDatum(inh));
17271728

1729+
/* Open index information when we know we need it */
1730+
if (indstate == NULL)
1731+
indstate = CatalogOpenIndexes(sd);
1732+
17281733
if (HeapTupleIsValid(oldtup))
17291734
{
17301735
/* Yes, replace it */
@@ -1734,18 +1739,20 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
17341739
nulls,
17351740
replaces);
17361741
ReleaseSysCache(oldtup);
1737-
CatalogTupleUpdate(sd, &stup->t_self, stup);
1742+
CatalogTupleUpdateWithInfo(sd, &stup->t_self, stup, indstate);
17381743
}
17391744
else
17401745
{
17411746
/* No, insert new tuple */
17421747
stup = heap_form_tuple(RelationGetDescr(sd), values, nulls);
1743-
CatalogTupleInsert(sd, stup);
1748+
CatalogTupleInsertWithInfo(sd, stup, indstate);
17441749
}
17451750

17461751
heap_freetuple(stup);
17471752
}
17481753

1754+
if (indstate != NULL)
1755+
CatalogCloseIndexes(indstate);
17491756
table_close(sd, RowExclusiveLock);
17501757
}
17511758

0 commit comments

Comments
 (0)