]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix tab completion for ALTER ROLE|USER ... RESET
authorTomas Vondra <tomas.vondra@postgresql.org>
Thu, 31 Jul 2025 13:17:26 +0000 (15:17 +0200)
committerTomas Vondra <tomas.vondra@postgresql.org>
Thu, 31 Jul 2025 14:04:21 +0000 (16:04 +0200)
Commit c407d5426b87 added tab completion for ALTER ROLE|USER ... RESET,
with the intent to offer only the variables actually set on the role.
But as soon as the user started typing something, it would start to
offer all possible matching variables.

Fix this the same way ALTER DATABASE ... RESET does it, i.e. by
properly considering the prefix.

A second issue causing similar symptoms (offering variables that are not
actually set for a role) was caused by a match to another pattern. The
ALTER DATABASE ... RESET was already excluded, so do the same thing for
ROLE/USER.

Report and fix by Dagfinn Ilmari MannsÃ¥ker. Backpatch to 18, same as
c407d5426b87.

Author: Dagfinn Ilmari MannsÃ¥ker <ilmari@ilmari.org>
Reviewed-by: jian he <jian.universality@gmail.com>
Discussion: https://postgr.es/m/87qzyghw2x.fsf%40wibble.ilmari.org
Discussion: https://postgr.es/m/87tt4lumqz.fsf%40wibble.ilmari.org
Backpatch-through: 18

src/bin/psql/tab-complete.in.c

index 3c50847f958cd19b63f02fc67d671c4c82543fc3..1f2ca946fc500d8eed257f0b1d503a66ee38f874 100644 (file)
@@ -1086,9 +1086,12 @@ Keywords_for_list_of_owner_roles, "PUBLIC"
 "  WHERE usename LIKE '%s'"
 
 #define Query_for_list_of_user_vars \
-" SELECT pg_catalog.split_part(pg_catalog.unnest(rolconfig),'=',1) "\
-"   FROM pg_catalog.pg_roles "\
-"  WHERE rolname LIKE '%s'"
+"SELECT conf FROM ("\
+"       SELECT rolname, pg_catalog.split_part(pg_catalog.unnest(rolconfig),'=',1) conf"\
+"         FROM pg_catalog.pg_roles"\
+"       ) s"\
+"  WHERE s.conf like '%s' "\
+"    AND s.rolname LIKE '%s'"
 
 #define Query_for_list_of_access_methods \
 " SELECT amname "\
@@ -2517,7 +2520,10 @@ match_previous_words(int pattern_id,
 
        /* ALTER USER,ROLE <name> RESET */
        else if (Matches("ALTER", "USER|ROLE", MatchAny, "RESET"))
+       {
+               set_completion_reference(prev2_wd);
                COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_user_vars, "ALL");
+       }
 
        /* ALTER USER,ROLE <name> WITH */
        else if (Matches("ALTER", "USER|ROLE", MatchAny, "WITH"))
@@ -5015,7 +5021,7 @@ match_previous_words(int pattern_id,
        /* Complete with a variable name */
        else if (TailMatches("SET|RESET") &&
                         !TailMatches("UPDATE", MatchAny, "SET") &&
-                        !TailMatches("ALTER", "DATABASE", MatchAny, "RESET"))
+                        !TailMatches("ALTER", "DATABASE|USER|ROLE", MatchAny, "RESET"))
                COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
                                                                                  "CONSTRAINTS",
                                                                                  "TRANSACTION",