PSQL - improve tab completion for pub/sub options
Thread notes
1 note#feature#minor#enhancement
hackorum-bot#feature#minor#enhancement
@global #feature #minor #enhancement
Here is a new thread/patch to improve the tab-completion of
publication/subscription options.
For commands:
CREATE PUBLICATION ... WITH (options)
ALTER PUBLICATION ... SET (options)
CREATE SUBSCRIPTION ... WITH (options)
ALTER SUBSCRIPTION ... SET (options)
Now:
- handles multiple options of a WITH/SET comma-separated list
- enum options show their allowed values
~~~
PSA v1.
======
Kind Regards,
Peter Smith.
Fujitsu Australia
Dear Peter,
Here is a new thread/patch to improve the tab-completion of
publication/subscription options.
Thanks for the proposal. Few comments:
01.
```
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -2341,9 +2341,20 @@ match_previous_words(int pattern_id,
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
" AND nspname NOT LIKE E'pg\\\\_%%'",
"CURRENT_SCHEMA");
- /* ALTER PUBLICATION <name> SET ( */
- else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "("))
- COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
+ /* ALTER PUBLICATION <name> SET ( <opt> */
+ else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "("))
+ {
+ if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
```
According to others, the else-if statement should be slightly changed like:
```
else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "(*") &&
!HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "(*)"))
```
02.
```
+ COMPLETE_WITH("publish", "publish_generated_columns =",
+ "publish_via_partition_root");
```
Let me confirm the policy here: the parameter "publish" won't be accepted alone,
but " =" is not completed for it. Is there a reason?
03.
```
COMPLETE_WITH("binary", "conflict_log_destination =",
+ "disable_on_error", "failover", "max_retention_duration",
+ "origin =", "password_required", "retain_dead_tuples",
+ "run_as_owner", "slot_name", "streaming =",
+ "synchronous_commit =", "two_phase",
```
Same as 02. max_retention_duration and slot_name cannot be alone.
04.
While testing, I recognized that the word "PUBLICATION" cannot be suggested
if users input till below. Can you also fix?
```
postgres=# CREATE SUBSCRIPTION sub CONNECTION 'dbname=tmp'
```
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Hi Kuroda-San
Thanks for your review comments!
On Tue, Jul 28, 2026 at 4:46 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
...
01. ``` --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -2341,9 +2341,20 @@ match_previous_words(int pattern_id, COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas " AND nspname NOT LIKE E'pg\\\\_%%'", "CURRENT_SCHEMA"); - /* ALTER PUBLICATION <name> SET ( */ - else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "(")) - COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root"); + /* ALTER PUBLICATION <name> SET ( <opt> */ + else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "(")) + { + if (ends_with(prev_wd, '(') || ends_with(prev_wd, ',')) ```According to others, the else-if statement should be slightly changed like:
```
else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "(*") &&
!HeadMatches("ALTER", "PUBLICATION", MatchAny, "SET", "(*)"))
```
Done as suggested.
02. ``` + COMPLETE_WITH("publish", "publish_generated_columns =", + "publish_via_partition_root"); ```Let me confirm the policy here: the parameter "publish" won't be accepted alone,
but " =" is not completed for it. Is there a reason?03. ``` COMPLETE_WITH("binary", "conflict_log_destination =", + "disable_on_error", "failover", "max_retention_duration", + "origin =", "password_required", "retain_dead_tuples", + "run_as_owner", "slot_name", "streaming =", + "synchronous_commit =", "two_phase", ```Same as 02. max_retention_duration and slot_name cannot be alone.
I initially included " =" only for the enum options. But you are
correct; it could/should have been added for some of the other options
too. Fixed now.
04.
While testing, I recognized that the word "PUBLICATION" cannot be suggested
if users input till below. Can you also fix?```
postgres=# CREATE SUBSCRIPTION sub CONNECTION 'dbname=tmp'
```
Hmm, I don't think you have to try very hard to find lots of examples
where tab-completion could be improved.
The scope of this particular thread/patch is *only* the pub/sub
options. I am happy to investigate what you reported, but that belongs
in another thread.
~~~
PSA patch v2.
======
Kind Regards,
Peter Smith.
Fujitsu Australia
PSA patch v2.
Hi,
Created a v3 and issued the following topics I found.
- Fixed typo in CREATE SUBSCRIPTION block: max_rentention_duration →
max_retention_duration (was causing the TailMatches check to never
fire for that option)
- Applied pgindent to clean up indentation in the ALTER PUBLICATION
and ALTER SUBSCRIPTION SET blocks (reproduces the same fix pattern
identified in v1 review)
While reviewing, I noticed you applied the "(*)" idiom to ALTER
PUBLICATION/SUBSCRIPTION SET blocks (great fix), but the CREATE
PUBLICATION/SUBSCRIPTION blocks still use the seen_with scan. For
consistency, the same pattern could be applied there too. What is your
opinion about it?
Please see the attached v3.
On Sun, Aug 9, 2026 at 6:55 PM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
Hi,
Created a v3 and issued the following topics I found.
- Fixed typo in CREATE SUBSCRIPTION block: max_rentention_duration →
max_retention_duration (was causing the TailMatches check to never
fire for that option)
- Applied pgindent to clean up indentation in the ALTER PUBLICATION
and ALTER SUBSCRIPTION SET blocks (reproduces the same fix pattern
identified in v1 review)
Thanks for your review, and catching that typo.
While reviewing, I noticed you applied the "(*)" idiom to ALTER
PUBLICATION/SUBSCRIPTION SET blocks (great fix), but the CREATE
PUBLICATION/SUBSCRIPTION blocks still use the seen_with scan. For
consistency, the same pattern could be applied there too. What is your
opinion about it?
Yeah, IIRC, I already tried to do that but came to grief.
e.g. Compared to ALTER, the CREATE PUBLICATION/SUBSCRIPION has a lot
more flexibility before the "WITH (", like multiple publication
clauses or multiple subscribed publications, so I'd need MatchManyN
for those...
However, MatchAnyN is only supported inside Matches() -- not HeadMatches().
But if we try to use Matches like:
else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*") &&
!Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*)"))
... then it just doesn't work for the comma "," separators. IIUC it's
because the comma is another token.
The following response from AI explains it better:
------
"(*" means: one word that starts with (. It matches "(", "(a",
"(publish", but it only ever matches a single token from
previous_words[].
That's actually what makes the HeadMatches approach work —
get_previous_words() groups a completed parenthesized expression into
one token (e.g. "(publish = insert)"), which matches "(*)" (starts
with (, ends with )). An uncompleted one like "(" matches "(*" but not
"(*)". Words typed inside the open parens appear at lower indices and
don't interfere with the position of the ( token when checked from the
head.
So the multi-word wildcard you'd need is MatchAnyN (""), which is only
supported by Matches/MatchesCS — and that's exactly why Matches(...,
MatchAnyN, "WITH", "(*") breaks after a comma: by the time options are
typed, previous_words[0] is something like "insert,", not "(".
------
So, it's catch-22:
syntax flexiblity of CREATE means I want MatchAnyN
--> but MatchAnyN is not compatible with HeadMatches
--> need to use Matches
--> but Matches cannot work, because logic needs needs HeadMatches
;-(
~~
I've experimented with other things like:
else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*") ||
Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(", MatchAnyN))
... but, apparently the Matches() logic only recognizes the first MatchAnyN.
To cut a long story short, the only approach that I found that works
was the `seen_with` scan.
~~~
PSA v4. It has unchanged content from v3, but restores the original
commit message of my patch.
======
Kind Regards,
Peter Smith.
Fujitsu Australia
--> but MatchAnyN is not compatible with HeadMatches
--> need to use Matches
--> but Matches cannot work, because logic needs needs HeadMatches
;-(~~
I've experimented with other things like:
else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*") ||
Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(", MatchAnyN))... but, apparently the Matches() logic only recognizes the first MatchAnyN.
To cut a long story short, the only approach that I found that works
was the `seen_with` scan.
Thank you for shedding light on this.
v4 LGTM. If no barring objections marked this patch as ready for committer.
Regards,
Demir.
Hi all,
On Mon, Oct 26, 2026 at 5:33 PM Peter Smith <smithpb2250@gmail.com> wrote:
On Sun, Aug 9, 2026 at 6:55 PM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
Hi,
Created a v3 and issued the following topics I found.
- Fixed typo in CREATE SUBSCRIPTION block: max_rentention_duration →
max_retention_duration (was causing the TailMatches check to never
fire for that option)
- Applied pgindent to clean up indentation in the ALTER PUBLICATION
and ALTER SUBSCRIPTION SET blocks (reproduces the same fix pattern
identified in v1 review)Thanks for your review, and catching that typo.
While reviewing, I noticed you applied the "(*)" idiom to ALTER
PUBLICATION/SUBSCRIPTION SET blocks (great fix), but the CREATE
PUBLICATION/SUBSCRIPTION blocks still use the seen_with scan. For
consistency, the same pattern could be applied there too. What is your
opinion about it?Yeah, IIRC, I already tried to do that but came to grief.
e.g. Compared to ALTER, the CREATE PUBLICATION/SUBSCRIPION has a lot
more flexibility before the "WITH (", like multiple publication
clauses or multiple subscribed publications, so I'd need MatchManyN
for those...However, MatchAnyN is only supported inside Matches() -- not HeadMatches().
But if we try to use Matches like:
else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*") &&
!Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*)"))... then it just doesn't work for the comma "," separators. IIUC it's
because the comma is another token.The following response from AI explains it better:
------
"(*" means: one word that starts with (. It matches "(", "(a",
"(publish", but it only ever matches a single token from
previous_words[].That's actually what makes the HeadMatches approach work —
get_previous_words() groups a completed parenthesized expression into
one token (e.g. "(publish = insert)"), which matches "(*)" (starts
with (, ends with )). An uncompleted one like "(" matches "(*" but not
"(*)". Words typed inside the open parens appear at lower indices and
don't interfere with the position of the ( token when checked from the
head.So the multi-word wildcard you'd need is MatchAnyN (""), which is only
supported by Matches/MatchesCS — and that's exactly why Matches(...,
MatchAnyN, "WITH", "(*") breaks after a comma: by the time options are
typed, previous_words[0] is something like "insert,", not "(".
------So, it's catch-22:
syntax flexiblity of CREATE means I want MatchAnyN
--> but MatchAnyN is not compatible with HeadMatches
--> need to use Matches
--> but Matches cannot work, because logic needs needs HeadMatches
;-(~~
I've experimented with other things like:
else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(*") ||
Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WITH", "(", MatchAnyN))... but, apparently the Matches() logic only recognizes the first MatchAnyN.
To cut a long story short, the only approach that I found that works
was the `seen_with` scan.~~~
PSA v4. It has unchanged content from v3, but restores the original
commit message of my patch.
Thank you for the updated patch. I tested the tab-completion behaviour
for publication and subscription options and inspected the relevant
completion logic in src/bin/psql/tab-complete.in.c. The completion
suggestions are displayed correctly when the cursor is immediately
after the opening parenthesis. For example: ALTER PUBLICATION test_pub
SET (
However, the suggestions are not displayed when entering subsequent
options after a comma. For example: ALTER PUBLICATION test_pub SET
(publish = 'insert',
I investigated the relevant completion conditions and the
complete_from_list() implementation in src/bin/psql/tab-complete.c.
The current completion logic attempts to handle this case using
text[0] == ','. However, complete_from_list() uses the current text as
a prefix when filtering the completion candidates. Since the prefix is
a comma, it does not match the available option names, and
consequently no suggestions are displayed.
I also checked the WORD_BREAKS definition and observed that the comma
is not currently included in it. Could you please look into this
behaviour and consider an appropriate way to support completion after
commas without affecting other existing psql completion contexts?
Regards
Solai
Hi Solai,
Thanks for testing my patch!
On Fri, Oct 30, 2026 at 4:36 PM solai v <solai.cdac@gmail.com> wrote:
...
Thank you for the updated patch. I tested the tab-completion behaviour
for publication and subscription options and inspected the relevant
completion logic in src/bin/psql/tab-complete.in.c. The completion
suggestions are displayed correctly when the cursor is immediately
after the opening parenthesis. For example: ALTER PUBLICATION test_pub
SET (
However, the suggestions are not displayed when entering subsequent
options after a comma. For example: ALTER PUBLICATION test_pub SET
(publish = 'insert',
IIUC, the problem is only that there was no space after the comma. If
you add a space, it works fine:
ALTER PUBLICATION test_pub SET (publish = 'insert', <TAB>
I investigated the relevant completion conditions and the
complete_from_list() implementation in src/bin/psql/tab-complete.c.
The current completion logic attempts to handle this case using
text[0] == ','. However, complete_from_list() uses the current text as
a prefix when filtering the completion candidates. Since the prefix is
a comma, it does not match the available option names, and
consequently no suggestions are displayed.
I'm not sure what "text[0] == ','" check you're referring to. The
comma handling is only this:
if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
With the space, the word being completed is empty and prev_wd is
"'insert',", so the above matches and the options are offered. The
other comma-separated lists in tab-complete.in.c also expect a space
after the comma. e.g. the existing "CREATE PUBLICATION ... FOR ALL
TABLES EXCEPT ( ..." uses (!ends_with(prev_wd, ',')) in the same way.
I also checked the WORD_BREAKS definition and observed that the comma
is not currently included in it. Could you please look into this
behaviour and consider an appropriate way to support completion after
commas without affecting other existing psql completion contexts?
AFAIK, that is a known limitation of psql tab-completion in general;
it is not specific to this patch.
The WORD_BREAKS question has come up a few times before:
* Actually, I proposed adding the comma to WORD_BREAKS back in 2022
[1]: /messages/by-id/CAHut+PvUShR5SHgm2pUj_8xcH=8CmqBjFcOdvjx+1j6b=_K2DQ@mail.gmail.com
random, and said he would like to see it populated more fully, with
the reasons for any omissions documented.
* In April 2024, Tom Lane wondered again why comma isn't in there [3]/messages/by-id/3870833.1712696581@sss.pgh.pa.us.
So, changing WORD_BREAKS might be worthwhile, but it would need its
own thread (and ideally more tab-complete test coverage), because it
affects all psql tab-completion, not just pub/sub options. Therefore,
I think it is out of scope for this patch.
======
[1]: /messages/by-id/CAHut+PvUShR5SHgm2pUj_8xcH=8CmqBjFcOdvjx+1j6b=_K2DQ@mail.gmail.com
[2]: /messages/by-id/431600.1642554676@sss.pgh.pa.us
[3]: /messages/by-id/3870833.1712696581@sss.pgh.pa.us
Kind Regards,
Peter Smith.
Fujitsu Australia