Allow index_create to suppress index_build progress reporting

A future REPACK patch wants a way to suppress index_build doing its
progress reports when building an index, because that would interfere
with repack's own reporting; so add an INDEX_CREATE_SUPPRESS_PROGRESS
bit that enables this.

Furthermore, change the index_create_copy() API so that it takes flag
bits for index_create() and passes them unchanged.  This gives its
callers more direct control, which eases the interface -- now its
callers can pass the INDEX_CREATE_SUPPRESS_PROGRESS bit directly.  We
use it for the current caller in REINDEX CONCURRENTLY, since it's also
not interested in progress reporting, since it doesn't want
index_build() to be called at all in the first place.

One thing to keep in mind, pointed out by Mihail, is that we're not
suppressing the index-AM-specific progress report updates which happen
during ambuild().  At present this is not a problem, because the values
updated by those don't overlap with those used by commands other than
CREATE INDEX; but maybe in the future we'll want the ability to suppress
them also.  (Alternatively we might want to display how each
index-build-subcommand progresses during REPACK and others.)

Author: Antonin Houska <ah@cybertec.at>
Author: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Mihail Nikalayeu <mihailnikalayeu@gmail.com>
Discussion: https://postgr.es/m/102906.1773668762@localhost
This commit is contained in:
Álvaro Herrera 2026-04-05 13:34:08 +02:00
parent de28140ded
commit caec9d9fad
No known key found for this signature in database
GPG key ID: 1C20ACB9D5C564AE
5 changed files with 26 additions and 16 deletions

View file

@ -1184,7 +1184,7 @@ build_indices(void)
heap = table_open(ILHead->il_heap, NoLock);
ind = index_open(ILHead->il_ind, NoLock);
index_build(heap, ind, ILHead->il_info, false, false);
index_build(heap, ind, ILHead->il_info, false, false, false);
index_close(ind, NoLock);
table_close(heap, NoLock);

View file

@ -3570,7 +3570,8 @@ RelationTruncateIndexes(Relation heapRelation)
/* Initialize the index and rebuild */
/* Note: we do not need to re-establish pkey setting */
index_build(heapRelation, currentIndex, indexInfo, true, false);
index_build(heapRelation, currentIndex, indexInfo, true, false,
true);
/* We're done with this index */
index_close(currentIndex, NoLock);

View file

@ -715,6 +715,9 @@ UpdateIndexRelation(Oid indexoid,
* already exists.
* INDEX_CREATE_PARTITIONED:
* create a partitioned index (table must be partitioned)
* INDEX_CREATE_SUPPRESS_PROGRESS:
* don't report progress during the index build.
*
* constr_flags: flags passed to index_constraint_create
* (only if INDEX_CREATE_ADD_CONSTRAINT is set)
* allow_system_table_mods: allow table to be a system catalog
@ -760,6 +763,7 @@ index_create(Relation heapRelation,
bool invalid = (flags & INDEX_CREATE_INVALID) != 0;
bool concurrent = (flags & INDEX_CREATE_CONCURRENT) != 0;
bool partitioned = (flags & INDEX_CREATE_PARTITIONED) != 0;
bool progress = (flags & INDEX_CREATE_SUPPRESS_PROGRESS) == 0;
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
@ -1276,7 +1280,8 @@ index_create(Relation heapRelation,
}
else
{
index_build(heapRelation, indexRelation, indexInfo, false, true);
index_build(heapRelation, indexRelation, indexInfo, false, true,
progress);
}
/*
@ -1292,19 +1297,20 @@ index_create(Relation heapRelation,
* index_create_copy
*
* Create an index based on the definition of the one provided by caller. The
* index is inserted into catalogs. If 'concurrently' is TRUE, it needs to be
* built later on; otherwise it's built immediately.
* index is inserted into catalogs. 'flags' are passed directly to
* index_create.
*
* "tablespaceOid" is the tablespace to use for this index.
*/
Oid
index_create_copy(Relation heapRelation, bool concurrently,
index_create_copy(Relation heapRelation, uint16 flags,
Oid oldIndexId, Oid tablespaceOid, const char *newName)
{
Relation indexRelation;
IndexInfo *oldInfo,
*newInfo;
Oid newIndexId = InvalidOid;
bool concurrently = (flags & INDEX_CREATE_CONCURRENT) != 0;
HeapTuple indexTuple,
classTuple;
Datum indclassDatum,
@ -1318,7 +1324,6 @@ index_create_copy(Relation heapRelation, bool concurrently,
List *indexColNames = NIL;
List *indexExprs = NIL;
List *indexPreds = NIL;
int flags = 0;
indexRelation = index_open(oldIndexId, RowExclusiveLock);
@ -1448,9 +1453,6 @@ index_create_copy(Relation heapRelation, bool concurrently,
stattargets[i].isnull = isnull;
}
if (concurrently)
flags = INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT;
/*
* Now create the new index.
*
@ -1538,7 +1540,7 @@ index_concurrently_build(Oid heapRelationId,
indexInfo->ii_BrokenHotChain = false;
/* Now build the index */
index_build(heapRel, indexRelation, indexInfo, false, true);
index_build(heapRel, indexRelation, indexInfo, false, true, true);
/* Roll back any GUC changes executed by index functions */
AtEOXact_GUC(false, save_nestlevel);
@ -3009,6 +3011,7 @@ index_update_stats(Relation rel,
*
* isreindex indicates we are recreating a previously-existing index.
* parallel indicates if parallelism may be useful.
* progress indicates if the backend should update its progress info.
*
* Note: before Postgres 8.2, the passed-in heap and index Relations
* were automatically closed by this routine. This is no longer the case.
@ -3019,7 +3022,8 @@ index_build(Relation heapRelation,
Relation indexRelation,
IndexInfo *indexInfo,
bool isreindex,
bool parallel)
bool parallel,
bool progress)
{
IndexBuildResult *stats;
Oid save_userid;
@ -3070,6 +3074,7 @@ index_build(Relation heapRelation,
RestrictSearchPath();
/* Set up initial progress report status */
if (progress)
{
const int progress_index[] = {
PROGRESS_CREATEIDX_PHASE,
@ -3827,7 +3832,7 @@ reindex_index(const ReindexStmt *stmt, Oid indexId,
/* Initialize the index and rebuild */
/* Note: we do not need to re-establish pkey setting */
index_build(heapRelation, iRel, indexInfo, true, true);
index_build(heapRelation, iRel, indexInfo, true, true, progress);
/* Re-allow use of target index */
ResetReindexProcessing();

View file

@ -3990,7 +3990,9 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
/* Create new index definition based on given index */
newIndexId = index_create_copy(heapRel,
true,
INDEX_CREATE_CONCURRENT |
INDEX_CREATE_SKIP_BUILD |
INDEX_CREATE_SUPPRESS_PROGRESS,
idx->indexId,
tablespaceid,
concurrentName);

View file

@ -71,6 +71,7 @@ extern void index_check_primary_key(Relation heapRel,
#define INDEX_CREATE_IF_NOT_EXISTS (1 << 4)
#define INDEX_CREATE_PARTITIONED (1 << 5)
#define INDEX_CREATE_INVALID (1 << 6)
#define INDEX_CREATE_SUPPRESS_PROGRESS (1 << 7)
extern Oid index_create(Relation heapRelation,
const char *indexRelationName,
@ -101,7 +102,7 @@ extern Oid index_create(Relation heapRelation,
#define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS (1 << 4)
#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS (1 << 5)
extern Oid index_create_copy(Relation heapRelation, bool concurrently,
extern Oid index_create_copy(Relation heapRelation, uint16 flags,
Oid oldIndexId, Oid tablespaceOid,
const char *newName);
@ -148,7 +149,8 @@ extern void index_build(Relation heapRelation,
Relation indexRelation,
IndexInfo *indexInfo,
bool isreindex,
bool parallel);
bool parallel,
bool progress);
extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);