aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-01-03 18:14:01 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2022-01-03 18:14:01 -0500
commitdfe67c0e85a5a613f802f78641ccc1c48845076e (patch)
tree5965c39302437ec6662026b0c33a160f4228a446
parent8a2e323f208557acbfdc911516e84ec017f5a6ca (diff)
downloadpostgresql-dfe67c0e85a5a613f802f78641ccc1c48845076e.tar.gz
postgresql-dfe67c0e85a5a613f802f78641ccc1c48845076e.zip
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
-rw-r--r--src/bin/psql/tab-complete.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index cf30239f6db..b81a04c93b5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -788,6 +788,15 @@ Query_for_index_of_table \
" and pg_catalog.quote_ident(c1.relname)='%s'"\
" and pg_catalog.pg_table_is_visible(c1.oid)"
+/* the silly-looking length condition is just to eat up the current word */
+#define Query_for_constraint_of_table_not_validated \
+"SELECT pg_catalog.quote_ident(conname) "\
+" FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\
+" WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\
+" and pg_catalog.quote_ident(c1.relname)='%s'"\
+" and pg_catalog.pg_table_is_visible(c1.oid)" \
+" and not con.convalidated"
+
#define Query_for_all_table_constraints \
"SELECT pg_catalog.quote_ident(conname) "\
" FROM pg_catalog.pg_constraint c "\
@@ -2165,16 +2174,18 @@ psql_completion(const char *text, int start, int end)
/* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
COMPLETE_WITH_ATTR(prev3_wd, "");
-
- /*
- * If we have ALTER TABLE <sth> ALTER|DROP|RENAME|VALIDATE CONSTRAINT,
- * provide list of constraints
- */
- else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME|VALIDATE", "CONSTRAINT"))
+ /* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
+ else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
{
completion_info_charp = prev3_wd;
COMPLETE_WITH_QUERY(Query_for_constraint_of_table);
}
+ /* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
+ else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
+ {
+ completion_info_charp = prev3_wd;
+ COMPLETE_WITH_QUERY(Query_for_constraint_of_table_not_validated);
+ }
/* ALTER TABLE ALTER [COLUMN] <foo> */
else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))