From 3462ca980c47d7bb6c93573e05f740eb3c89ded9 Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Mon, 21 Sep 2026 20:53:25 -0300
Subject: [PATCH v1 1/5] Fix blocks_done of an index build's heap scan on its
 first block

heapam_scan_get_blocks_done() computes how many blocks a scan has done
from the block it is on and the block it started at, allowing for a
synchronized scan that wrapped around the end of the relation.  When the
current block is the start block, which only happens on the first block,
it took the wrap-around branch and returned the number of blocks in the
relation.  So CREATE INDEX reported blocks_done = blocks_total as soon as
the scan started, and then went back to 1 on the next block.

Found with the PROGRESS_DEBUG tracing proposed in the same thread.

Oversight in ab0dfc961b6.
---
 src/backend/access/heap/heapam_handler.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 6adb760b54f..499e426479c 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -1965,9 +1965,10 @@ heapam_scan_get_blocks_done(HeapScanDesc hscan)
 
 	/*
 	 * Might have wrapped around the end of the relation, if startblock was
-	 * not zero.
+	 * not zero.  The scan ends before coming back to startblock, so the
+	 * current block is only startblock at the start, when no block is done.
 	 */
-	if (hscan->rs_cblock > startblock)
+	if (hscan->rs_cblock >= startblock)
 		blocks_done = hscan->rs_cblock - startblock;
 	else
 	{
-- 
2.55.0

