Avoid some overhead with open and close of catalog indexes
authorMichael Paquier <michael@paquier.xyz>
Wed, 16 Nov 2022 01:49:05 +0000 (10:49 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 16 Nov 2022 01:49:05 +0000 (10:49 +0900)
commit09a72188cd5cdd179dc814c6729f8a3785b23c36
treece062bd69c670c1f05fcfa128be0b1e80e50af49
parent006b69fd912b8fa0e9d80df3f61829a5c976c3d3
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
src/backend/catalog/heap.c
src/backend/commands/analyze.c