Support tab-complete for TRUNCATE on foreign tables.
authorFujii Masao <fujii@postgresql.org>
Mon, 12 Apr 2021 12:34:23 +0000 (21:34 +0900)
committerFujii Masao <fujii@postgresql.org>
Mon, 12 Apr 2021 12:34:23 +0000 (21:34 +0900)
Commit 8ff1c94649 extended TRUNCATE command so that it can also truncate
foreign tables. But it forgot to support tab-complete for TRUNCATE on
foreign tables. That is, previously tab-complete for TRUNCATE displayed
only the names of regular tables.

This commit improves tab-complete for TRUNCATE so that it displays also
the names of foreign tables.

Author: Fujii Masao
Reviewed-by: Bharath Rupireddy
Discussion: https://postgr.es/m/551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com

src/bin/psql/tab-complete.c

index 26ac786c512dcee7ab9e7499ac41d3250a0a7e5b..d34271e3b87991e167dcbb28eeb19e8f00eebe2b 100644 (file)
@@ -549,6 +549,18 @@ static const SchemaQuery Query_for_list_of_selectables = {
    .result = "pg_catalog.quote_ident(c.relname)",
 };
 
+/* Relations supporting TRUNCATE */
+static const SchemaQuery Query_for_list_of_truncatables = {
+   .catname = "pg_catalog.pg_class c",
+   .selcondition =
+   "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
+   CppAsString2(RELKIND_FOREIGN_TABLE) ", "
+   CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
+   .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
+   .namespace = "c.relnamespace",
+   .result = "pg_catalog.quote_ident(c.relname)",
+};
+
 /* Relations supporting GRANT are currently same as those supporting SELECT */
 #define Query_for_list_of_grantables Query_for_list_of_selectables
 
@@ -3834,14 +3846,14 @@ psql_completion(const char *text, int start, int end)
 
 /* TRUNCATE */
    else if (Matches("TRUNCATE"))
-       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
+       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables,
                                   " UNION SELECT 'TABLE'"
                                   " UNION SELECT 'ONLY'");
    else if (Matches("TRUNCATE", "TABLE"))
-       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
+       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables,
                                   " UNION SELECT 'ONLY'");
    else if (HeadMatches("TRUNCATE") && TailMatches("ONLY"))
-       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
+       COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables, NULL);
    else if (Matches("TRUNCATE", MatchAny) ||
             Matches("TRUNCATE", "TABLE|ONLY", MatchAny) ||
             Matches("TRUNCATE", "TABLE", "ONLY", MatchAny))