From 2694cb39d1f6641bbc99bb9e1e6b43b22863af1b Mon Sep 17 00:00:00 2001
From: Sho Ito <i.sho.0628@gmail.com>
Date: Tue, 10 Nov 2026 23:48:32 -0500
Subject: [PATCH v3 1/2] psql: Fix REPACK tab completion around USING INDEX

Complete INDEX after an explicitly typed USING, including with command
options or a column list. 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.

Leave completion directly after a column list unchanged. Adding USING
INDEX there requires accounting for the ANALYZE option and is deferred
to a separate change.

Do not add column-list continuations without a command option list:
these forms cannot enable ANALYZE and are always invalid.
---
 src/bin/psql/tab-complete.in.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78cd..aedf065f 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -5129,9 +5129,14 @@ 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"))
+		COMPLETE_WITH("INDEX");
+	else if (Matches("REPACK", MatchAnyExcept("(")) ||
+			 Matches("REPACK", "(*)", MatchAnyExcept("(")))
 		COMPLETE_WITH("USING INDEX");
 	else if (Matches("REPACK", MatchAny, "USING", "INDEX") ||
 			 Matches("REPACK", "(*)", MatchAny, "USING", "INDEX"))
@@ -5139,14 +5144,9 @@ 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"))
 	{
-		set_completion_reference(prev3_wd);
+		set_completion_reference(prev4_wd);
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
 	}
 	else if (HeadMatches("REPACK", "(*") &&
-- 
2.48.1

