psql: Fix tab completion for VACUUM option values

Started by Yugo Nagata21 days ago4 messages
#1Yugo Nagata
nagata@sraoss.co.jp
1 attachment(s)

Hi,

I noticed that tab completion for VACUUM option values is broken.
For example, ON or OFF should be suggested after
"VACUUM (VERBOSE", but this currently does not work.

I believe this issue was introduced by commit 8a3e4011, which added
tab completion for the ONLY option of VACUUM and ANALYZE, along with
some code simplification using MatchAnyN.

In addition, tab completion for the ONLY option is insufficient:
"ONLY" is not suggested immediately after a completed option list,
for example in "VACUUM (...) ONLY" or "ANALYZE (...) ONLY".

I've attached a patch to fix these issues.

Regards,
Yugo Nagata

--
Yugo Nagata <nagata@sraoss.co.jp>

Attachments:

0001-psql-Fix-tab-completion-for-VACUUM-option-values.patchtext/x-diff; name=0001-psql-Fix-tab-completion-for-VACUUM-option-values.patchDownload
From 2caee7fd14958bcb6109d8bfb879bee761cc2433 Mon Sep 17 00:00:00 2001
From: Yugo Nagata <nagata@sraoss.co.jp>
Date: Tue, 23 Dec 2025 01:28:10 +0900
Subject: [PATCH] psql: Fix tab completion for VACUUM option values

Commit 8a3e4011 introduced tab completion for the ONLY option of VACUUM
and ANALYZE, along with some code simplification using MatchAnyN.
However, it broke tab completion for VACUUM option values. For example,
neither ON nor OFF is suggested after "VACUUM (VERBOSE".

In addition, tab completion for the ONLY option is insufficient, and it
is not suggested immediately after a completed option list. This commit
fixes these issues.
---
 src/bin/psql/tab-complete.in.c | 42 +++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index b1ff6f6cd94..254b0af5cf5 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -3170,6 +3170,9 @@ match_previous_words(int pattern_id,
 		else if (TailMatches("VERBOSE|SKIP_LOCKED"))
 			COMPLETE_WITH("ON", "OFF");
 	}
+	else if (Matches("ANALYZE", "(*)"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
+										"ONLY");
 	else if (Matches("ANALYZE", MatchAnyN, "("))
 		/* "ANALYZE (" should be caught above, so assume we want columns */
 		COMPLETE_WITH_ATTR(prev2_wd);
@@ -5283,24 +5286,6 @@ match_previous_words(int pattern_id,
 										"VERBOSE",
 										"ANALYZE",
 										"ONLY");
-	else if (Matches("VACUUM", "FULL"))
-		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
-										"FREEZE",
-										"VERBOSE",
-										"ANALYZE",
-										"ONLY");
-	else if (Matches("VACUUM", MatchAnyN, "FREEZE"))
-		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
-										"VERBOSE",
-										"ANALYZE",
-										"ONLY");
-	else if (Matches("VACUUM", MatchAnyN, "VERBOSE"))
-		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
-										"ANALYZE",
-										"ONLY");
-	else if (Matches("VACUUM", MatchAnyN, "ANALYZE"))
-		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
-										"ONLY");
 	else if (HeadMatches("VACUUM", "(*") &&
 			 !HeadMatches("VACUUM", "(*)"))
 	{
@@ -5320,6 +5305,27 @@ match_previous_words(int pattern_id,
 		else if (TailMatches("INDEX_CLEANUP"))
 			COMPLETE_WITH("AUTO", "ON", "OFF");
 	}
+	else if (Matches("VACUUM", "(*)"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
+										"ONLY");
+	else if (Matches("VACUUM", "FULL"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
+										"FREEZE",
+										"VERBOSE",
+										"ANALYZE",
+										"ONLY");
+	else if (Matches("VACUUM", MatchAnyN, "FREEZE"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
+										"VERBOSE",
+										"ANALYZE",
+										"ONLY");
+	else if (Matches("VACUUM", MatchAnyN, "VERBOSE"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
+										"ANALYZE",
+										"ONLY");
+	else if (Matches("VACUUM", MatchAnyN, "ANALYZE"))
+		COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
+										"ONLY");
 	else if (Matches("VACUUM", MatchAnyN, "("))
 		/* "VACUUM (" should be caught above, so assume we want columns */
 		COMPLETE_WITH_ATTR(prev2_wd);
-- 
2.43.0

#2Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Yugo Nagata (#1)
Re: psql: Fix tab completion for VACUUM option values

Hi,

On Mon, Dec 22, 2025 at 9:15 AM Yugo Nagata <nagata@sraoss.co.jp> wrote:

Hi,

I noticed that tab completion for VACUUM option values is broken.
For example, ON or OFF should be suggested after
"VACUUM (VERBOSE", but this currently does not work.

I believe this issue was introduced by commit 8a3e4011, which added
tab completion for the ONLY option of VACUUM and ANALYZE, along with
some code simplification using MatchAnyN.

In addition, tab completion for the ONLY option is insufficient:
"ONLY" is not suggested immediately after a completed option list,
for example in "VACUUM (...) ONLY" or "ANALYZE (...) ONLY".

I've attached a patch to fix these issues.

I agree with your analysis. I think the patch should be backpatched to v18

FYI due to the simplification by using MatchAnyN, vacuum options are
suggested at inappropriate positions:

postgres(1:2376453)=# vacuum (verbose on) freeze
ANALYZE ONLY VERBOSE
information_schema. public. t

Given that it's not common usage, we can live with that.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#3Yugo Nagata
nagata@sraoss.co.jp
In reply to: Masahiko Sawada (#2)
Re: psql: Fix tab completion for VACUUM option values

On Mon, 22 Dec 2025 13:55:59 -0800
Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Hi,

On Mon, Dec 22, 2025 at 9:15 AM Yugo Nagata <nagata@sraoss.co.jp> wrote:

Hi,

I noticed that tab completion for VACUUM option values is broken.
For example, ON or OFF should be suggested after
"VACUUM (VERBOSE", but this currently does not work.

I believe this issue was introduced by commit 8a3e4011, which added
tab completion for the ONLY option of VACUUM and ANALYZE, along with
some code simplification using MatchAnyN.

In addition, tab completion for the ONLY option is insufficient:
"ONLY" is not suggested immediately after a completed option list,
for example in "VACUUM (...) ONLY" or "ANALYZE (...) ONLY".

I've attached a patch to fix these issues.

I agree with your analysis. I think the patch should be backpatched to v18

Thank you for your review! I agree that this should be backpatched to v18.

FYI due to the simplification by using MatchAnyN, vacuum options are
suggested at inappropriate positions:

postgres(1:2376453)=# vacuum (verbose on) freeze
ANALYZE ONLY VERBOSE
information_schema. public. t

Given that it's not common usage, we can live with that.

Similar inappropriate suggestions can occur in other cases where
MatchAnyN is used. For example:

postgres=# truncate only t cascade restart identity
CASCADE RESTRICT

However, I also agree that this behavior is acceptable, since it is
unlikely to be encountered when users follow the tab completion
suggested by psql.

Regards,
Yugo Nagata

--
Yugo Nagata <nagata@sraoss.co.jp>

#4Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Yugo Nagata (#3)
Re: psql: Fix tab completion for VACUUM option values

On Mon, Dec 22, 2025 at 5:37 PM Yugo Nagata <nagata@sraoss.co.jp> wrote:

On Mon, 22 Dec 2025 13:55:59 -0800
Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Hi,

On Mon, Dec 22, 2025 at 9:15 AM Yugo Nagata <nagata@sraoss.co.jp> wrote:

Hi,

I noticed that tab completion for VACUUM option values is broken.
For example, ON or OFF should be suggested after
"VACUUM (VERBOSE", but this currently does not work.

I believe this issue was introduced by commit 8a3e4011, which added
tab completion for the ONLY option of VACUUM and ANALYZE, along with
some code simplification using MatchAnyN.

In addition, tab completion for the ONLY option is insufficient:
"ONLY" is not suggested immediately after a completed option list,
for example in "VACUUM (...) ONLY" or "ANALYZE (...) ONLY".

I've attached a patch to fix these issues.

I agree with your analysis. I think the patch should be backpatched to v18

Thank you for your review! I agree that this should be backpatched to v18.

FYI due to the simplification by using MatchAnyN, vacuum options are
suggested at inappropriate positions:

postgres(1:2376453)=# vacuum (verbose on) freeze
ANALYZE ONLY VERBOSE
information_schema. public. t

Given that it's not common usage, we can live with that.

Similar inappropriate suggestions can occur in other cases where
MatchAnyN is used. For example:

postgres=# truncate only t cascade restart identity
CASCADE RESTRICT

Right.

I've pushed the patch.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com