Skip to content

Commit dcc2094

Browse files
committed
Fix CLUSTER progress reporting of number of blocks scanned.
Previously pg_stat_progress_cluster view reported the current block number in heap scan as the number of heap blocks scanned (i.e., heap_blks_scanned). This reported number could be incorrect when synchronize_seqscans is enabled, because it allowed the heap scan to start at block in middle. This could result in wraparounds in the heap_blks_scanned column when the heap scan wrapped around. This commit fixes the bug by calculating the number of blocks from the block that the heap scan starts at to the current block in scan, and reporting that number in the heap_blks_scanned column. Also, in pg_stat_progress_cluster view, previously heap_blks_scanned could not reach heap_blks_total at the end of heap scan phase if the last pages scanned were empty. This commit fixes the bug by manually updating heap_blks_scanned to the same value as heap_blks_total when the heap scan phase finishes. Back-patch to v12 where pg_stat_progress_cluster view was introduced. Reported-by: Matthias van de Meent Author: Matthias van de Meent Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CAEze2WjCBWSGkVfYag001Rc4+-nNLDpWM7QbyD6yPvuhKs-gYQ@mail.gmail.com
1 parent a0ef081 commit dcc2094

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/backend/access/heap/heapam_handler.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
696696
Datum *values;
697697
bool *isnull;
698698
BufferHeapTupleTableSlot *hslot;
699+
BlockNumber prev_cblock = InvalidBlockNumber;
699700

700701
/* Remember if it's a system catalog */
701702
is_system_catalog = IsSystemRelation(OldHeap);
@@ -791,14 +792,38 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
791792
else
792793
{
793794
if (!table_scan_getnextslot(tableScan, ForwardScanDirection, slot))
795+
{
796+
/*
797+
* If the last pages of the scan were empty, we would go to
798+
* the next phase while heap_blks_scanned != heap_blks_total.
799+
* Instead, to ensure that heap_blks_scanned is equivalent to
800+
* total_heap_blks after the table scan phase, this parameter
801+
* is manually updated to the correct value when the table
802+
* scan finishes.
803+
*/
804+
pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
805+
heapScan->rs_nblocks);
794806
break;
807+
}
795808

796809
/*
797810
* In scan-and-sort mode and also VACUUM FULL, set heap blocks
798811
* scanned
812+
*
813+
* Note that heapScan may start at an offset and wrap around, i.e.
814+
* rs_startblock may be >0, and rs_cblock may end with a number
815+
* below rs_startblock. To prevent showing this wraparound to the
816+
* user, we offset rs_cblock by rs_startblock (modulo rs_nblocks).
799817
*/
800-
pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
801-
heapScan->rs_cblock + 1);
818+
if (prev_cblock != heapScan->rs_cblock)
819+
{
820+
pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
821+
(heapScan->rs_cblock +
822+
heapScan->rs_nblocks -
823+
heapScan->rs_startblock
824+
) % heapScan->rs_nblocks + 1);
825+
prev_cblock = heapScan->rs_cblock;
826+
}
802827
}
803828

804829
tuple = ExecFetchSlotHeapTuple(slot, false, NULL);

0 commit comments

Comments
 (0)