[PATCH] psql: Complete GRANTED BY in GRANT and REVOKE
Thread notes
1 note#feature#minor#enhancement
hackorum-bot#feature#minor#enhancement
@global #feature #minor #enhancement
Hi hackers,
While trying REVOKE ... GRANTED BY in PostgreSQL 19, I noticed that
psql doesn't complete the clause or the grantor's name.
I tried the corresponding GRANT and found that pressing Tab after
a trailing space on this line inserts TO, making the statement invalid.
GRANT SELECT ON t TO r WITH GRANT OPTION
Before the patch, the generic GRANT rule treats the trailing GRANT
OPTION as GRANT followed by a role name. The patch matches WITH
GRANT OPTION first, so Tab inserts GRANTED BY. It also completes
the clause and grantor name in both commands.
Thanks,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/
On Nov 20, 2026, at 16:57, Neil Chen <carpenter.nail.cz@gmail.com> wrote:
Hi hackers,
While trying REVOKE ... GRANTED BY in PostgreSQL 19, I noticed that
psql doesn't complete the clause or the grantor's name.I tried the corresponding GRANT and found that pressing Tab after
a trailing space on this line inserts TO, making the statement invalid.GRANT SELECT ON t TO r WITH GRANT OPTION
Before the patch, the generic GRANT rule treats the trailing GRANT
OPTION as GRANT followed by a role name. The patch matches WITH
GRANT OPTION first, so Tab inserts GRANTED BY. It also completes
the clause and grantor name in both commands.
Hi Neil,
Thanks for the patch. I reviewed and tested this patch, and I have a few comments.
1
```
evantest=# revoke select ON all functions in schema public FROM chaol GRANTED BY chaol
```
After “GRANTED BY x”, [ CASCADE | RESTRICT ] can be specified, so we should support them as well.
2
```
+ /* GRANTED can itself be a role name, including after GROUP or a comma. */
+ else if (Matches("GRANT", MatchAnyN, "TO", MatchAnyExcept("GROUP"), "GRANTED") ||
+ Matches("REVOKE", MatchAnyN, "FROM", MatchAnyExcept("GROUP"), "GRANTED") ||
+ Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH",
+ "GRANT", "OPTION", "GRANTED") ||
+ Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH",
+ "ADMIN|INHERIT|SET", "OPTION|TRUE|FALSE", "GRANTED"))
+ {
+ if (!ends_with(prev2_wd, ','))
+ COMPLETE_WITH("BY");
+ }
```
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.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Hi Chao,
Thanks for the review.
On Wed, Nov 25, 2026 at 7:36 AM Chao Li <li.evan.chao@gmail.com> 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/
On Nov 25, 2026, at 09:34, Neil Chen <carpenter.nail.cz@gmail.com> wrote:
Hi Chao,
Thanks for the review.
On Wed, Nov 25, 2026 at 7:36 AM Chao Li <li.evan.chao@gmail.com> 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/
<v2-0001-psql-Complete-GRANTED-BY-in-GRANT-and-REVOKE.patch>
I just tested v2, and there are still something feels incorrect.
1
```
evantest=# grant select on t1 to r with grant
ALL pg_checkpoint pg_read_server_files SET
ALTER SYSTEM pg_create_subscription pg_signal_autovacuum_worker TEMPORARY
chaol pg_database_owner pg_signal_backend TRIGGER
CONNECT pg_execute_server_program pg_stat_scan_tables TRUNCATE
CREATE pg_maintain pg_use_reserved_connections UPDATE
DELETE pg_monitor pg_write_all_data USAGE
EXECUTE pg_read_all_data pg_write_server_files
INSERT pg_read_all_settings REFERENCES
MAINTAIN pg_read_all_stats SELECT
```
After “grant”, type in a white space then tab, it prompts a list of options to choose, but I only expect “OPTION”.
2
```
evantest=# revoke select on t1 from r1,r2
CASCADE GRANTED BY RESTRICT
evantest=# revoke select on t1 from r1, r2
```
If there is no whitespace between r1 and r2, it correctly prompts options, but a whitespace makes it to not work.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Hi Chao,
Thanks for testing v2.
On Wed, Nov 25, 2026 at 10:30 AM Chao Li <li.evan.chao@gmail.com> 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]https://www.postgresql.org/docs/devel/sql-revoke.html
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]https://www.postgresql.org/docs/devel/sql-droptable.html
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]https://www.postgresql.org/docs/devel/app-psql.html#APP-PSQL-COMMAND-LINE-EDITING 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]/messages/by-id/E1wDbnx-001MS2-25@gemulon.postgresql.org
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]https://www.postgresql.org/docs/devel/sql-createforeigntable.html
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
https://www.postgresql.org/docs/devel/app-psql.html#APP-PSQL-COMMAND-LINE-EDITING
[4]: /messages/by-id/E1wDbnx-001MS2-25@gemulon.postgresql.org
/messages/by-id/E1wDbnx-001MS2-25@gemulon.postgresql.org
[5]: https://www.postgresql.org/docs/devel/sql-createforeigntable.html
Thanks,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/
On Nov 25, 2026, at 15:04, Neil Chen <carpenter.nail.cz@gmail.com> wrote:
Hi Chao,
Thanks for testing v2.
Thanks for updating the patch.
On Wed, Nov 25, 2026 at 10:30 AM Chao Li <li.evan.chao@gmail.com> 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 agree with these 3 criteria.
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?
I’m fine with leaving those issues for a separate discussion.
[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] /messages/by-id/E1wDbnx-001MS2-25@gemulon.postgresql.org
[5] https://www.postgresql.org/docs/devel/sql-createforeigntable.htmlThanks,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/
<v3-0001-psql-Complete-GRANTED-BY-in-GRANT-and-REVOKE.patch>
V3 looks good to me.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Hi,
Thanks for the reviews!
I've added this patch to the next CommitFest.
https://commitfest.postgresql.org/patch/7702/
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/