Hi Chao,

Thanks for the review.

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

>
> After “GRANTED BY x”, [ CASCADE | RESTRICT ] can be specified, so we
> should support them as well.
>
>
I added both after the grantor and alongside GRANTED BY after the
grantee, since the grantor clause is optional.


>
> Is the check of comma really needed? At least for the latter 2 matches,
> prev2_wd would be OPTION|TRUE|FALSE, impossible to be comma.
>
> In the immediate next branch, the check on prev3_wd has a similar problem.
>
>
You're right about the WITH forms. The GROUP and comma checks are
now part of the grantee matches, where granted can be a role name,
as in FROM alice, granted. After the full GRANTED BY, a single rule
looks up the grantor for either command, even after a list. I removed
the second comma check there. The lookup leaves out PUBLIC, which
can't be a brantor.

I kept keyword completion to one grantee word or one WITH option,
following the existing GRANT completion rules.
For role membership options, the name can be quoted, so the rule
matches the value and uses a wildcard for the name.

Attached is v2.

Thanks,

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

After WITH GRANT OPTION, psql treats the trailing GRANT OPTION as a
generic GRANT followed by a role name, so Tab inserts TO.  REVOKE
also lacks completion for GRANTED BY and its grantor.  Object
privileges have supported explicit grantors since dd1398f1378.

Match WITH GRANT OPTION before the generic rule, and 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.

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 | 17 +++++++++++++++++
 src/bin/psql/tab-complete.in.c       | 24 +++++++++++++++++++++---
 2 files changed, 38 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..55c22a06e18 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -206,6 +206,23 @@ check_completion(
 
 clear_query();
 
+check_completion(
+	"grant select on tab1 to r with grant option \t",
+	qr/with grant option granted by /i,
+	"complete GRANTED BY after WITH GRANT OPTION");
+clear_query();
+
+check_completion(
+	"revoke 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..b8002ce9dad 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -4607,6 +4607,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 +4744,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|*,")))
+		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|*,"), "GRANTED") ||
+			 Matches("REVOKE", MatchAnyN, "FROM", MatchAnyExcept("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