6060#include "commands/comment.h"
6161#include "commands/defrem.h"
6262#include "commands/event_trigger.h"
63+ #include "commands/progress.h"
6364#include "commands/sequence.h"
6465#include "commands/tablecmds.h"
6566#include "commands/tablespace.h"
@@ -5841,6 +5842,10 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
58415842 if (!RELKIND_HAS_STORAGE(tab->relkind))
58425843 continue;
58435844
5845+ /* Start progress reporting */
5846+ pgstat_progress_start_command(PROGRESS_COMMAND_CLUSTER, tab->relid);
5847+ pgstat_progress_update_param(PROGRESS_CLUSTER_COMMAND, PROGRESS_CLUSTER_COMMAND_ALTER_TABLE);
5848+
58445849 /*
58455850 * If we change column data types, the operation has to be propagated
58465851 * to tables that use this table's rowtype as a column type.
@@ -5981,6 +5986,9 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
59815986 */
59825987 ATRewriteTable(tab, OIDNewHeap);
59835988
5989+ /* Report that we are now swapping relation files */
5990+ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
5991+ PROGRESS_CLUSTER_PHASE_SWAP_REL_FILES);
59845992 /*
59855993 * Swap the physical files of the old and new heaps, then rebuild
59865994 * indexes and discard the old heap. We can use RecentXmin for
@@ -6092,6 +6100,10 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
60926100 table_close(rel, NoLock);
60936101 }
60946102
6103+ /* Report that we are now doing clean up */
6104+ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
6105+ PROGRESS_CLUSTER_PHASE_FINAL_CLEANUP);
6106+
60956107 /* Finally, run any afterStmts that were queued up */
60966108 foreach(ltab, *wqueue)
60976109 {
@@ -6106,6 +6118,8 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
61066118 CommandCounterIncrement();
61076119 }
61086120 }
6121+
6122+ pgstat_progress_end_command();
61096123}
61106124
61116125/*
@@ -6131,6 +6145,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
61316145 BulkInsertState bistate;
61326146 int ti_options;
61336147 ExprState *partqualstate = NULL;
6148+ int64 numTuples = 0;
61346149
61356150 /*
61366151 * Open the relation(s). We have surely already locked the existing
@@ -6140,6 +6155,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
61406155 oldTupDesc = tab->oldDesc;
61416156 newTupDesc = RelationGetDescr(oldrel); /* includes all mods */
61426157
6158+ /* Update progress reporting - we are actually scanning and possibly rewriting the table */
6159+ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE, PROGRESS_CLUSTER_PHASE_SEQ_SCAN_HEAP);
6160+
61436161 if (OidIsValid(OIDNewHeap))
61446162 {
61456163 Assert(CheckRelationOidLockedByMe(OIDNewHeap, AccessExclusiveLock,
@@ -6247,6 +6265,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
62476265 TupleTableSlot *oldslot;
62486266 TupleTableSlot *newslot;
62496267 TableScanDesc scan;
6268+ HeapScanDesc heapScan;
62506269 MemoryContext oldCxt;
62516270 List *dropped_attrs = NIL;
62526271 ListCell *lc;
@@ -6346,6 +6365,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
63466365 */
63476366 snapshot = RegisterSnapshot(GetLatestSnapshot());
63486367 scan = table_beginscan(oldrel, snapshot, 0, NULL);
6368+ heapScan = (HeapScanDesc) scan;
6369+ pgstat_progress_update_param(PROGRESS_CLUSTER_TOTAL_HEAP_BLKS,
6370+ heapScan->rs_nblocks);
63496371
63506372 /*
63516373 * Switch to per-tuple memory context and reset it for each tuple
@@ -6356,6 +6378,13 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
63566378 while (table_scan_getnextslot(scan, ForwardScanDirection, oldslot))
63576379 {
63586380 TupleTableSlot *insertslot;
6381+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
6382+ (heapScan->rs_cblock +
6383+ heapScan->rs_nblocks -
6384+ heapScan->rs_startblock
6385+ ) % heapScan->rs_nblocks + 1);
6386+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED,
6387+ ++numTuples);
63596388
63606389 if (tab->rewrite > 0)
63616390 {
@@ -6404,6 +6433,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
64046433
64056434 ExecStoreVirtualTuple(newslot);
64066435
6436+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_TUPLES_WRITTEN,
6437+ numTuples);
6438+
64076439 /*
64086440 * Now, evaluate any expressions whose inputs come from the
64096441 * new tuple. We assume these columns won't reference each
@@ -6524,6 +6556,8 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
65246556
65256557 CHECK_FOR_INTERRUPTS();
65266558 }
6559+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
6560+ heapScan->rs_nblocks);
65276561
65286562 MemoryContextSwitchTo(oldCxt);
65296563 table_endscan(scan);
@@ -13692,10 +13726,12 @@ validateForeignKeyConstraint(char *conname,
1369213726{
1369313727 TupleTableSlot *slot;
1369413728 TableScanDesc scan;
13729+ HeapScanDesc heapScan;
1369513730 Trigger trig = {0};
1369613731 Snapshot snapshot;
1369713732 MemoryContext oldcxt;
1369813733 MemoryContext perTupCxt;
13734+ int64 numTuples = 0;
1369913735
1370013736 ereport(DEBUG1,
1370113737 (errmsg_internal("validating foreign key constraint \"%s\"", conname)));
@@ -13714,6 +13750,10 @@ validateForeignKeyConstraint(char *conname,
1371413750 trig.tginitdeferred = false;
1371513751 /* we needn't fill in remaining fields */
1371613752
13753+ /* Report that we are now checking foreign keys */
13754+ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
13755+ PROGRESS_CLUSTER_PHASE_CHECK_FKEYS);
13756+
1371713757 /*
1371813758 * See if we can do it with a single LEFT JOIN query. A false result
1371913759 * indicates we must proceed with the fire-the-trigger method. We can't do
@@ -13731,6 +13771,7 @@ validateForeignKeyConstraint(char *conname,
1373113771 snapshot = RegisterSnapshot(GetLatestSnapshot());
1373213772 slot = table_slot_create(rel, NULL);
1373313773 scan = table_beginscan(rel, snapshot, 0, NULL);
13774+ heapScan = (HeapScanDesc) scan;
1373413775
1373513776 perTupCxt = AllocSetContextCreate(CurrentMemoryContext,
1373613777 "validateForeignKeyConstraint",
@@ -13766,6 +13807,14 @@ validateForeignKeyConstraint(char *conname,
1376613807 RI_FKey_check_ins(fcinfo);
1376713808
1376813809 MemoryContextReset(perTupCxt);
13810+
13811+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
13812+ (heapScan->rs_cblock +
13813+ heapScan->rs_nblocks -
13814+ heapScan->rs_startblock
13815+ ) % heapScan->rs_nblocks + 1);
13816+ pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED,
13817+ ++numTuples);
1376913818 }
1377013819
1377113820 MemoryContextSwitchTo(oldcxt);
@@ -16856,6 +16905,10 @@ ATExecSetTableSpace(Oid tableOid, Oid newTableSpace, LOCKMODE lockmode)
1685616905 */
1685716906 rel = relation_open(tableOid, lockmode);
1685816907
16908+ /* Update progress reporting - we are copying the table */
16909+ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE, PROGRESS_CLUSTER_PHASE_WRITE_NEW_HEAP);
16910+ pgstat_progress_update_param(PROGRESS_CLUSTER_TOTAL_HEAP_BLKS, RelationGetNumberOfBlocks(rel));
16911+
1685916912 /* Check first if relation can be moved to new tablespace */
1686016913 if (!CheckRelationTableSpaceMove(rel, newTableSpace))
1686116914 {
0 commit comments