Skip to content

Commit dfe67c0

Browse files
committed
Tab completion: don't offer valid constraints in VALIDATE CONSTRAINT.
Improve psql so that "ALTER TABLE foo VALIDATE CONSTRAINT <TAB>" only offers not-convalidated entries. While it's not formally wrong to offer validated ones, there's not much point either, and it can save some typing if we incorporate this knowledge. David Fetter, reviewed by Aleksander Alekseev Discussion: https://postgr.es/m/20210427002433.GB17834@fetter.org
1 parent 8a2e323 commit dfe67c0

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/bin/psql/tab-complete.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,15 @@ Query_for_index_of_table \
788788
" and pg_catalog.quote_ident(c1.relname)='%s'"\
789789
" and pg_catalog.pg_table_is_visible(c1.oid)"
790790

791+
/* the silly-looking length condition is just to eat up the current word */
792+
#define Query_for_constraint_of_table_not_validated \
793+
"SELECT pg_catalog.quote_ident(conname) "\
794+
" FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\
795+
" WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\
796+
" and pg_catalog.quote_ident(c1.relname)='%s'"\
797+
" and pg_catalog.pg_table_is_visible(c1.oid)" \
798+
" and not con.convalidated"
799+
791800
#define Query_for_all_table_constraints \
792801
"SELECT pg_catalog.quote_ident(conname) "\
793802
" FROM pg_catalog.pg_constraint c "\
@@ -2165,16 +2174,18 @@ psql_completion(const char *text, int start, int end)
21652174
/* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
21662175
else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
21672176
COMPLETE_WITH_ATTR(prev3_wd, "");
2168-
2169-
/*
2170-
* If we have ALTER TABLE <sth> ALTER|DROP|RENAME|VALIDATE CONSTRAINT,
2171-
* provide list of constraints
2172-
*/
2173-
else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME|VALIDATE", "CONSTRAINT"))
2177+
/* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
2178+
else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
21742179
{
21752180
completion_info_charp = prev3_wd;
21762181
COMPLETE_WITH_QUERY(Query_for_constraint_of_table);
21772182
}
2183+
/* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
2184+
else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
2185+
{
2186+
completion_info_charp = prev3_wd;
2187+
COMPLETE_WITH_QUERY(Query_for_constraint_of_table_not_validated);
2188+
}
21782189
/* ALTER TABLE ALTER [COLUMN] <foo> */
21792190
else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
21802191
Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))

0 commit comments

Comments
 (0)