From 6883d4220e37376b3133994792ae3f7c408f01f8 Mon Sep 17 00:00:00 2001
From: Sho Ito <i.sho.0628@gmail.com>
Date: Mon, 9 Nov 2026 22:13:01 -0500
Subject: [PATCH v2 1/2] psql: Fix REPACK tab completion around USING INDEX

Complete INDEX after an explicitly typed USING, rather than treating USING as a table name and inserting USING INDEX again. Handle optional command options and column lists.

When completing an index name after a column list, skip the column list to find the target table instead of falling back to indexes from unrelated tables. Also complete USING INDEX after a closed column list.
---
 src/bin/psql/tab-complete.in.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78cd..5e4466ae 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -5129,9 +5129,17 @@ match_previous_words(int pattern_id,
 	else if (Matches("REPACK", "(*)"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
 										"USING INDEX");
-	else if (Matches("REPACK", MatchAnyExcept("(")))
-		COMPLETE_WITH("USING INDEX");
-	else if (Matches("REPACK", "(*)", MatchAnyExcept("(")))
+	else if (Matches("REPACK", "USING") ||
+			 Matches("REPACK", "(*)", "USING") ||
+			 Matches("REPACK", MatchAny, "USING") ||
+			 Matches("REPACK", "(*)", MatchAny, "USING") ||
+			 Matches("REPACK", MatchAny, "(*)", "USING") ||
+			 Matches("REPACK", "(*)", MatchAny, "(*)", "USING"))
+		COMPLETE_WITH("INDEX");
+	else if (Matches("REPACK", MatchAnyExcept("(")) ||
+			 Matches("REPACK", "(*)", MatchAnyExcept("(")) ||
+			 Matches("REPACK", MatchAny, "(*)") ||
+			 Matches("REPACK", "(*)", MatchAny, "(*)"))
 		COMPLETE_WITH("USING INDEX");
 	else if (Matches("REPACK", MatchAny, "USING", "INDEX") ||
 			 Matches("REPACK", "(*)", MatchAny, "USING", "INDEX"))
@@ -5139,14 +5147,10 @@ match_previous_words(int pattern_id,
 		set_completion_reference(prev3_wd);
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
 	}
-
-	/*
-	 * Complete ... [ (*) ] <sth> USING INDEX, with a list of indexes for
-	 * <sth>.
-	 */
-	else if (TailMatches(MatchAny, "USING", "INDEX"))
+	else if (Matches("REPACK", MatchAny, "(*)", "USING", "INDEX") ||
+			 Matches("REPACK", "(*)", MatchAny, "(*)", "USING", "INDEX"))
 	{
-		set_completion_reference(prev3_wd);
+		set_completion_reference(prev4_wd);
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
 	}
 	else if (HeadMatches("REPACK", "(*") &&
