Hi Chao,

Thanks for testing v2.

On Wed, Nov 25, 2026 at 10:30 AM Chao Li <[email protected]> wrote:

>
> After “grant”, type in a white space then tab, it prompts a list of
> options to choose, but I only expect “OPTION”.
>
>
Fixed it.
I also found the same problem in standalone REVOKE. The generic GRANT
rules offered roles and privileges after REVOKE GRANT and inserted TO
after REVOKE GRANT OPTION. These now offer OPTION FOR and FOR,
respectively. [1]

I also tightened the GRANTED BY matching for FROM r1,GROUP granted,
where granted is a role name and Tab shouldn't insert BY.


>
> If there is no whitespace between r1 and r2, it correctly prompts options,
> but a whitespace makes it to not work.
>
>
I tried this example with DROP TABLE. A space and Tab after
DROP TABLE t1,t2 give me CASCADE and RESTRICT. With DROP TABLE t1, t2,
I get neither. [2]

I tried a few more commands and found both wrong suggestions and
missing ones. The psql manual describes completion in many contexts,
and tab-complete.in.c says the aim is to cover common cases. [3] I'd
use three questions to decide what's worth fixing.

1. Can the suggested keyword go here? This is what prompted the
patch. After GRANT ... WITH GRANT OPTION, the generic GRANT rule
took OPTION for a role name and inserted TO. The fix that removed
FROM SERVER after CREATE PUBLICATION ... EXCEPT (...) is another
example. [4]

2. Is a common next step missing? After CREATE FOREIGN TABLE f (id int),
psql doesn't suggest the required SERVER clause. Once you type SERVER,
it offers server names. It would be worth seeing whether a simple rule
can fill that gap. TRUNCATE already offers the full RESTART IDENTITY
phrase. If you type RESTART by hand, Tab gives no suggestions, so
I'd leave that case alone. [5]

3. Do equivalent name lists get the same candidates? Once the list
can end, spacing after a comma shouldn't change the suggestions
for the next clause. A trailing comma still needs another name.
With REASSIGN OWNED BY r1, pressing Tab after a space currently
inserts TO too early.

I left the REVOKE spacing issue for discussion. It might make sense
to look at REVOKE, DROP TABLE, and REASSIGN OWNED together and see
whether they can handle lists the same way. Do you think the remaining
completion issues are worth discussing in separate threads?

[1] https://www.postgresql.org/docs/devel/sql-revoke.html
[2] https://www.postgresql.org/docs/devel/sql-droptable.html
[3]
https://www.postgresql.org/docs/devel/app-psql.html#APP-PSQL-COMMAND-LINE-EDITING
[4]
https://www.postgresql.org/message-id/E1wDbnx-001MS2-25%40gemulon.postgresql.org
[5] https://www.postgresql.org/docs/devel/sql-createforeigntable.html

Thanks,

-- 
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/
From c825d2132b39fc022985d8f741e46e0137b5ad76 Mon Sep 17 00:00:00 2001
From: Neil Chen <[email protected]>
Date: Wed, 25 Nov 2026 14:32:40 +0800
Subject: [PATCH v3] psql: Complete GRANTED BY in GRANT and REVOKE

The generic GRANT rules offer roles and privileges after WITH GRANT
and insert TO after WITH GRANT OPTION. Match those prefixes first to
complete OPTION and GRANTED BY, respectively.

The same generic rules offer unrelated candidates after REVOKE GRANT
and insert TO after REVOKE GRANT OPTION. Complete the rest of the
fixed GRANT OPTION FOR prefix before those rules.

REVOKE also lacks completion for GRANTED BY and its grantor. Object
privileges have supported explicit grantors since dd1398f1378.

Also offer GRANTED BY after a single membership option or a REVOKE
grantee. Keep membership option name matching open so that quoted
option names continue to work. When completing BY, account for a
grantee named GRANTED after GROUP or a comma. GROUP can also share
a completion word with the preceding grantee, as in r1,GROUP.

Use one role lookup after GRANTED BY in both commands, without the
PUBLIC keyword.  In REVOKE, offer CASCADE and RESTRICT both directly
after the grantee and after an explicit grantor.

Author: Neil Chen <[email protected]>
Reviewed-by: Chao Li <[email protected]>
Discussion: https://postgr.es/m/CAA3qoJ%3DeA85GW5Enrc5h5tTEowxfUo-A3vaL-ebSY-5Zxqm%3DTQ%40mail.gmail.com
---
 src/bin/psql/t/010_tab_completion.pl | 25 ++++++++++++++++++++++
 src/bin/psql/tab-complete.in.c       | 32 +++++++++++++++++++++++++---
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl
index 1d2e5f5b92a..a7852112fda 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -206,6 +206,31 @@ check_completion(
 
 clear_query();
 
+check_completion(
+	"grant select on tab1 to r with grant \t",
+	qr/with grant option /i,
+	"complete OPTION after WITH GRANT");
+check_completion(
+	"\t",
+	qr/granted by /i,
+	"complete GRANTED BY after WITH GRANT OPTION");
+clear_query();
+
+check_completion(
+	"revoke grant option \t",
+	qr/revoke grant option for /i,
+	"complete FOR after REVOKE GRANT OPTION");
+check_completion(
+	"select on tab1 from r granted \t",
+	qr/from r granted by /i,
+	"complete BY after GRANTED in REVOKE");
+check_completion("pg_mon\t", qr/pg_monitor /, "complete the grantor role");
+check_completion(
+	"ca\t",
+	qr/cascade /i,
+	"complete CASCADE after the grantor in REVOKE");
+clear_query();
+
 # check interpretation of referenced names
 check_completion(
 	"alter table tab1 drop constraint t\t",
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78cd087..f5e22d14f3a 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -4542,6 +4542,14 @@ match_previous_words(int pattern_id,
  * GRANT and REVOKE are allowed inside CREATE SCHEMA and
  * ALTER DEFAULT PRIVILEGES, so use TailMatches
  */
+	/* Complete top-level GRANT OPTION phrases before the generic rules. */
+	else if (Matches("GRANT", MatchAnyN, "WITH", "GRANT"))
+		COMPLETE_WITH("OPTION");
+	else if (Matches("REVOKE", "GRANT"))
+		COMPLETE_WITH("OPTION FOR");
+	else if (Matches("REVOKE", "GRANT", "OPTION"))
+		COMPLETE_WITH("FOR");
+
 	/* Complete GRANT/REVOKE with a list of roles and privileges */
 	else if (TailMatches("GRANT|REVOKE") ||
 			 TailMatches("REVOKE", "ADMIN|GRANT|INHERIT|SET", "OPTION", "FOR"))
@@ -4607,6 +4615,10 @@ match_previous_words(int pattern_id,
 			 TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
 		COMPLETE_WITH("FROM");
 
+	/* Match WITH GRANT OPTION before the generic GRANT <word> rule. */
+	else if (Matches("GRANT", MatchAnyN, "WITH", "GRANT", "OPTION"))
+		COMPLETE_WITH("GRANTED BY");
+
 	/*
 	 * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
 	 * TO/FROM
@@ -4740,11 +4752,25 @@ match_previous_words(int pattern_id,
 					  "GRANT OPTION");
 	else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", "ADMIN|INHERIT|SET"))
 		COMPLETE_WITH("OPTION", "TRUE", "FALSE");
-	else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION"))
+	/* Role membership option names can be quoted. */
+	else if (Matches("GRANT", MatchAnyN, "WITH",
+					 MatchAnyExcept("GRANT"), "OPTION|TRUE|FALSE"))
 		COMPLETE_WITH("GRANTED BY");
-	else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))
+	else if (Matches("REVOKE", MatchAnyN, "FROM", MatchAnyExcept("GROUP|*,|*,GROUP")))
+		COMPLETE_WITH("GRANTED BY", "CASCADE", "RESTRICT");
+
+	/* GRANTED can also be a grantee name after GROUP or a comma. */
+	else if (Matches("GRANT", MatchAnyN, "TO", MatchAnyExcept("GROUP|*,|*,GROUP"), "GRANTED") ||
+			 Matches("REVOKE", MatchAnyN, "FROM", MatchAnyExcept("GROUP|*,|*,GROUP"), "GRANTED") ||
+			 Matches("GRANT", MatchAnyN, "WITH", "GRANT", "OPTION", "GRANTED") ||
+			 Matches("GRANT", MatchAnyN, "WITH",
+					 MatchAnyExcept("GRANT"), "OPTION|TRUE|FALSE", "GRANTED"))
+		COMPLETE_WITH("BY");
+	else if (Matches("GRANT|REVOKE", MatchAnyN, "GRANTED", "BY"))
 		COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
-								 Keywords_for_list_of_grant_roles);
+								 Keywords_for_list_of_owner_roles);
+	else if (Matches("REVOKE", MatchAnyN, "GRANTED", "BY", MatchAny))
+		COMPLETE_WITH("CASCADE", "RESTRICT");
 	/* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
 	else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO|FROM"))
 		COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,

base-commit: b4cc5e924b7a85f76637227002eb1a4be443d956
-- 
2.51.0

Reply via email to