From c825d2132b39fc022985d8f741e46e0137b5ad76 Mon Sep 17 00:00:00 2001
From: Neil Chen <carpenter.nail.cz@gmail.com>
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 <carpenter.nail.cz@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
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

