From 60e66c0d634d4f917827bf5693e0c3d07b03cbb8 Mon Sep 17 00:00:00 2001
From: Umar Hayat <postgresql.wizard@gmail.com>
Date: Thu, 6 Feb 2025 19:44:16 +0900
Subject: [PATCH] psql: Tab completion for VACUUM and ANALYZE ... ONLY option

Improve psql tab completion for VACUUM and ANALYZE with ONLY option introduced in 62ddf7e
---
 src/bin/psql/tab-complete.in.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 81cbf10aa2..4f36989864 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -3039,12 +3039,12 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
 
 /*
- * ANALYZE [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
- * ANALYZE [ VERBOSE ] [ table_and_columns [, ...] ]
+ * ANALYZE [ ( option [, ...] ) ] [ [ONLY] table_and_columns [, ...] ]
+ * ANALYZE [ VERBOSE ] [ [ONLY] table_and_columns [, ...] ]
  */
 	else if (Matches("ANALYZE"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
-										"VERBOSE");
+										"VERBOSE", "ONLY", "(");
 	else if (HeadMatches("ANALYZE", "(*") &&
 			 !HeadMatches("ANALYZE", "(*)"))
 	{
@@ -3061,8 +3061,10 @@ match_previous_words(int pattern_id,
 	else if (Matches("ANALYZE", MatchAnyN, "("))
 		/* "ANALYZE (" should be caught above, so assume we want columns */
 		COMPLETE_WITH_ATTR(prev2_wd);
-	else if (HeadMatches("ANALYZE"))
+	else if (HeadMatches("ANALYZE") && TailMatches("ONLY"))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables);
+	else if (HeadMatches("ANALYZE"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables, "ONLY");
 
 /* BEGIN */
 	else if (Matches("BEGIN"))
@@ -5096,30 +5098,35 @@ match_previous_words(int pattern_id,
 		COMPLETE_WITH("OPTIONS");
 
 /*
- * VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
- * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]
+ * VACUUM [ ( option [, ...] ) ] [ [ONLY] table_and_columns [, ...] ]
+ * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ [ONLY] table_and_columns [, ...] ]
  */
 	else if (Matches("VACUUM"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
 										"FULL",
 										"FREEZE",
 										"ANALYZE",
-										"VERBOSE");
+										"VERBOSE",
+										"ONLY",
+										"(");
 	else if (Matches("VACUUM", "FULL"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
 										"FREEZE",
 										"ANALYZE",
-										"VERBOSE");
+										"VERBOSE",
+										"ONLY");
 	else if (Matches("VACUUM", "FREEZE") ||
 			 Matches("VACUUM", "FULL", "FREEZE"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
 										"VERBOSE",
-										"ANALYZE");
+										"ANALYZE",
+										"ONLY");
 	else if (Matches("VACUUM", "VERBOSE") ||
 			 Matches("VACUUM", "FULL|FREEZE", "VERBOSE") ||
 			 Matches("VACUUM", "FULL", "FREEZE", "VERBOSE"))
 		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
-										"ANALYZE");
+										"ANALYZE",
+										"ONLY");
 	else if (HeadMatches("VACUUM", "(*") &&
 			 !HeadMatches("VACUUM", "(*)"))
 	{
@@ -5142,8 +5149,10 @@ match_previous_words(int pattern_id,
 	else if (Matches("VACUUM", MatchAnyN, "("))
 		/* "VACUUM (" should be caught above, so assume we want columns */
 		COMPLETE_WITH_ATTR(prev2_wd);
-	else if (HeadMatches("VACUUM"))
+	else if (HeadMatches("VACUUM") && TailMatches("ONLY"))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables);
+	else if (HeadMatches("VACUUM"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables, "ONLY");
 
 /* WITH [RECURSIVE] */
 
-- 
2.31.0

