From 9b0eab2442647960a3bf94e5b9c2a82d17042f11 Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Wed, 16 Sep 2026 06:48:55 -0300
Subject: [PATCH] Test index_rebuild_count of REPACK (CONCURRENTLY)

Commits 4b445479f9e and 0765b48874a fixed index_rebuild_count in
pg_stat_progress_repack: creating the TOAST index of the new heap no
longer writes the CREATE INDEX phase into the same progress slot, and
the concurrent path now counts the indexes it builds.  Neither added a
test.

repack_toast.spec already stops REPACK (CONCURRENTLY) at
repack-concurrently-before-lock, right after build_new_indexes() and
with a TOAST table on the new heap.  Read the progress view from the
other session there: with the table's single index rebuilt, the count
must be 1.  Reverting 4b445479f9e makes it 3, and reverting 0765b48874a
makes it 0.
---
 .../modules/injection_points/expected/repack_toast.out | 10 +++++++++-
 .../modules/injection_points/specs/repack_toast.spec   |  9 +++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/test/modules/injection_points/expected/repack_toast.out b/src/test/modules/injection_points/expected/repack_toast.out
index 95e7b19893e..ee8cf1dfa4b 100644
--- a/src/test/modules/injection_points/expected/repack_toast.out
+++ b/src/test/modules/injection_points/expected/repack_toast.out
@@ -1,6 +1,6 @@
 Parsed test spec with 2 sessions
 
-starting permutation: s1_wait_before_lock s2_updates s2_check s2_wakeup_before_lock s1_check
+starting permutation: s1_wait_before_lock s2_progress s2_updates s2_check s2_wakeup_before_lock s1_check
 injection_points_attach
 -----------------------
                        
@@ -9,6 +9,14 @@ injection_points_attach
 step s1_wait_before_lock: 
 	REPACK (CONCURRENTLY) repack_toast;
  <waiting ...>
+step s2_progress: 
+	SELECT phase, index_rebuild_count FROM pg_stat_progress_repack;
+
+phase           |index_rebuild_count
+----------------+-------------------
+rebuilding index|                  1
+(1 row)
+
 step s2_updates: 
 	DELETE FROM repack_toast WHERE i=1;
 	INSERT INTO repack_toast(i, j, k) VALUES (1, gen_external(), gen_compressible(1));
diff --git a/src/test/modules/injection_points/specs/repack_toast.spec b/src/test/modules/injection_points/specs/repack_toast.spec
index cc8f034d016..03f5f9086c7 100644
--- a/src/test/modules/injection_points/specs/repack_toast.spec
+++ b/src/test/modules/injection_points/specs/repack_toast.spec
@@ -125,6 +125,14 @@ teardown
 
 session s2
 
+# By now the one index of repack_toast has been built on the new heap, so
+# index_rebuild_count must be 1: the index built must be counted, and the
+# TOAST index of the new heap must not leave anything behind.
+step s2_progress
+{
+	SELECT phase, index_rebuild_count FROM pg_stat_progress_repack;
+}
+
 # Test different kinds of toast data changes.
 step s2_updates
 {
@@ -174,6 +182,7 @@ step s2_wakeup_before_lock
 # CONCURRENTLY find their way into the table.
 permutation
 	s1_wait_before_lock
+	s2_progress
 	s2_updates
 	s2_check
 	s2_wakeup_before_lock
-- 
2.55.0

