Skip to content

Commit 90434e6

Browse files
committed
Fix tab completion of "SET variable TO|=" to not offer bogus completions.
Don't think that the context "UPDATE tab SET var =" is a GUC-setting command. If we have "SET var =" but the "var" is not a known GUC variable, don't offer any completions. The most likely explanation is that we've misparsed the context and it's not really a GUC-setting command. Per gripe from Ken Tanzer. Back-patch to 9.6. The issue exists further back, but before 9.6 the code looks very different and it doesn't actually know whether the "var" name matches anything, so I desisted from trying to fix it. Discussion: https://postgr.es/m/CAD3a31XpXzrZA9TT3BqLSHghdTK+=cXjNCE+oL2Zn4+oWoc=qA@mail.gmail.com
1 parent cd9d489 commit 90434e6

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/bin/psql/tab-complete.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,8 +3249,13 @@ psql_completion(const char *text, int start, int end)
32493249
else if (HeadMatches2("ALTER", "DATABASE|FUNCTION|ROLE|USER") &&
32503250
TailMatches2("SET", MatchAny))
32513251
COMPLETE_WITH_LIST2("FROM CURRENT", "TO");
3252-
/* Suggest possible variable values */
3253-
else if (TailMatches3("SET", MatchAny, "TO|="))
3252+
3253+
/*
3254+
* Suggest possible variable values in SET variable TO|=, along with the
3255+
* preceding ALTER syntaxes.
3256+
*/
3257+
else if (TailMatches3("SET", MatchAny, "TO|=") &&
3258+
!TailMatches5("UPDATE", MatchAny, "SET", MatchAny, "TO|="))
32543259
{
32553260
/* special cased code for individual GUCs */
32563261
if (TailMatches2("DateStyle", "TO|="))
@@ -3273,21 +3278,29 @@ psql_completion(const char *text, int start, int end)
32733278
/* generic, type based, GUC support */
32743279
char *guctype = get_guctype(prev2_wd);
32753280

3276-
if (guctype && strcmp(guctype, "enum") == 0)
3281+
/*
3282+
* Note: if we don't recognize the GUC name, it's important to not
3283+
* offer any completions, as most likely we've misinterpreted the
3284+
* context and this isn't a GUC-setting command at all.
3285+
*/
3286+
if (guctype)
32773287
{
3278-
char querybuf[1024];
3288+
if (strcmp(guctype, "enum") == 0)
3289+
{
3290+
char querybuf[1024];
32793291

3280-
snprintf(querybuf, sizeof(querybuf), Query_for_enum, prev2_wd);
3281-
COMPLETE_WITH_QUERY(querybuf);
3282-
}
3283-
else if (guctype && strcmp(guctype, "bool") == 0)
3284-
COMPLETE_WITH_LIST9("on", "off", "true", "false", "yes", "no",
3285-
"1", "0", "DEFAULT");
3286-
else
3287-
COMPLETE_WITH_CONST("DEFAULT");
3292+
snprintf(querybuf, sizeof(querybuf),
3293+
Query_for_enum, prev2_wd);
3294+
COMPLETE_WITH_QUERY(querybuf);
3295+
}
3296+
else if (strcmp(guctype, "bool") == 0)
3297+
COMPLETE_WITH_LIST9("on", "off", "true", "false",
3298+
"yes", "no", "1", "0", "DEFAULT");
3299+
else
3300+
COMPLETE_WITH_CONST("DEFAULT");
32883301

3289-
if (guctype)
32903302
free(guctype);
3303+
}
32913304
}
32923305
}
32933306

0 commit comments

Comments
 (0)