From 882b87722ad92cb6cedbba675b33c6df4513defe Mon Sep 17 00:00:00 2001
From: Sho Ito <i.sho.0628@gmail.com>
Date: Mon, 23 Nov 2026 00:03:17 -0500
Subject: [PATCH v5 2/2] psql: Add regression tests for REPACK tab completion
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Cover INDEX after explicitly typed USING and table-specific index
completion with and without column lists. Use another indexed table to
detect fallback to the generic index list. Check opening-parenthesis and
USING INDEX suggestions with ANALYZE enabled, disabled, or omitted.

Author: Sho Ito <i.sho.0628@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Ishjaap Singh <ishjaap.singh07@gmail.com>
Suggested-by: Álvaro Herrera <alvherre@kurilemu.de>
Backpatch-through: 19
Discussion: https://postgr.es/m/CAG7L5+7Xmus+JLa0m=Lpw49wU=secPm5dzNd2ibmfvhdSYEfqA@mail.gmail.com
---
 src/bin/psql/t/010_tab_completion.pl | 89 ++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl
index 1d2e5f5b92..43c33f651e 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -422,6 +422,95 @@ check_completion(
 
 clear_line();
 
+# REPACK must recognise USING as a keyword, including after a column list.
+check_completion(
+	"REPACK USING \t",
+	qr/REPACK USING INDEX /,
+	"complete INDEX after REPACK USING");
+
+clear_query();
+
+check_completion(
+	"REPACK (VERBOSE) USING \t",
+	qr/REPACK \(VERBOSE\) USING INDEX /,
+	"complete INDEX after REPACK (VERBOSE) USING");
+
+clear_query();
+
+check_completion(
+	"REPACK tab1 USING \t",
+	qr/REPACK tab1 USING INDEX /,
+	"complete INDEX after REPACK tab1 USING");
+
+clear_query();
+
+check_completion(
+	"REPACK (ANALYZE) tab1 USING \t",
+	qr/REPACK \(ANALYZE\) tab1 USING INDEX /,
+	"complete INDEX after REPACK (ANALYZE) tab1 USING");
+
+clear_query();
+
+check_completion(
+	"REPACK (ANALYZE) tab1 (c1) USING \t",
+	qr/REPACK \(ANALYZE\) tab1 \(c1\) USING INDEX /,
+	"complete INDEX after REPACK (ANALYZE) tab1 (c1) USING");
+
+clear_query();
+
+# Column-list suggestions need not validate the ANALYZE option.  Once a
+# column list is present, USING INDEX introduces no further requirement.
+foreach my $options ('ANALYZE', 'VERBOSE', 'ANALYZE OFF')
+{
+	check_completion(
+		"REPACK ($options) tab1 \t\t",
+		qr/\( +USING INDEX/,
+		"offer a column list after REPACK ($options) tab1");
+
+	clear_query();
+
+	check_completion(
+		"REPACK ($options) tab1 (c1) \t",
+		qr/REPACK \(\Q$options\E\) tab1 \(c1\) USING INDEX /,
+		"complete USING INDEX after REPACK ($options) tab1 (c1)");
+
+	clear_query();
+}
+
+# The table reference must skip the optional column list.  repack_other is an
+# unrelated table with a primary key too, so falling back to all indexes
+# cannot uniquely complete the index name to tab1_pkey.
+$node->safe_psql('postgres',
+	'CREATE TABLE repack_other (id int PRIMARY KEY)');
+check_completion(
+	"REPACK tab1 USING INDEX \t",
+	qr/REPACK tab1 USING INDEX tab1_pkey /,
+	"complete only the target table index after REPACK tab1");
+
+clear_query();
+
+check_completion(
+	"REPACK (ANALYZE) tab1 USING INDEX \t",
+	qr/REPACK \(ANALYZE\) tab1 USING INDEX tab1_pkey /,
+	"complete only the target table index after REPACK (ANALYZE) tab1");
+
+clear_query();
+
+check_completion(
+	"REPACK (ANALYZE) tab1 (c1) USING INDEX \t",
+	qr/REPACK \(ANALYZE\) tab1 \(c1\) USING INDEX tab1_pkey /,
+	"complete only the target table index after REPACK (ANALYZE) tab1 (c1)");
+
+clear_query();
+
+check_completion(
+	"REPACK (ANALYZE) tab1 (c1, c2) USING INDEX \t",
+	qr/REPACK \(ANALYZE\) tab1 \(c1, c2\) USING INDEX tab1_pkey /,
+	"complete only the target table index after REPACK (ANALYZE) tab1 (c1, c2)"
+);
+
+clear_query();
+
 # send psql an explicit \q to shut it down, else pty won't close properly
 $h->quit or die "psql returned $?";
 
-- 
2.48.1

