From 14974f272eaa5e9e6a0ae0862d6d32ca02196efb Mon Sep 17 00:00:00 2001 From: amit Date: Mon, 10 Jul 2017 13:57:47 +0900 Subject: [PATCH 2/2] Exclude partitions by default from the the psql \d listing Add a new modifier '!' to \d to request listing partitions. --- doc/src/sgml/ref/psql-ref.sgml | 14 +++++++++----- src/bin/psql/command.c | 14 ++++++++++---- src/bin/psql/describe.c | 12 ++++++++++-- src/bin/psql/describe.h | 4 ++-- src/bin/psql/help.c | 12 ++++++------ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index c592edac60..fef1aba963 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1096,7 +1096,7 @@ testdb=> - \d[S+] [ pattern ] + \d[S+!] [ pattern ] @@ -1132,7 +1132,9 @@ testdb=> By default, only user-created objects are shown; supply a pattern or the S modifier to include system - objects. + objects. Also, by default, only non-partition objects are shown; + supply a pattern or the ! modifier to include + partitions. @@ -1296,11 +1298,11 @@ testdb=> - \dE[S+] [ pattern ] + \dE[S+!] [ pattern ] \di[S+] [ pattern ] \dm[S+] [ pattern ] \ds[S+] [ pattern ] - \dt[S+] [ pattern ] + \dt[S+!] [ pattern ] \dv[S+] [ pattern ] @@ -1320,7 +1322,9 @@ testdb=> specified, only objects whose names match the pattern are listed. By default, only user-created objects are shown; supply a pattern or the S modifier to include system - objects. + objects. Also, by default, only non-partition objects are shown; + supply a pattern or the ! modifier to include + partitions. diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 14c64208ca..9c90414b49 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -703,7 +703,8 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) { char *pattern; bool show_verbose, - show_system; + show_system, + show_partitions; /* We don't do SQLID reduction on the pattern yet */ pattern = psql_scan_slash_option(scan_state, @@ -711,17 +712,21 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) show_verbose = strchr(cmd, '+') ? true : false; show_system = strchr(cmd, 'S') ? true : false; + show_partitions = strchr(cmd, '!') ? true : false; switch (cmd[1]) { case '\0': case '+': case 'S': + case '!': if (pattern) - success = describeTableDetails(pattern, show_verbose, show_system); + success = describeTableDetails(pattern, show_verbose, show_system, + show_partitions); else /* standard listing of interesting things */ - success = listTables("tvmsE", NULL, show_verbose, show_system); + success = listTables("tvmsE", NULL, show_verbose, show_system, + show_partitions); break; case 'A': success = describeAccessMethods(pattern, show_verbose); @@ -795,7 +800,8 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) case 'i': case 's': case 'E': - success = listTables(&cmd[1], pattern, show_verbose, show_system); + success = listTables(&cmd[1], pattern, show_verbose, show_system, + show_partitions); break; case 'r': if (cmd[2] == 'd' && cmd[3] == 's') diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index bbdac8d50d..abc569442f 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1284,7 +1284,8 @@ objectDescription(const char *pattern, bool showSystem) * verbose: if true, this is \d+ */ bool -describeTableDetails(const char *pattern, bool verbose, bool showSystem) +describeTableDetails(const char *pattern, bool verbose, bool showSystem, + bool showPartitions) { PQExpBufferData buf; PGresult *res; @@ -1303,6 +1304,9 @@ describeTableDetails(const char *pattern, bool verbose, bool showSystem) appendPQExpBufferStr(&buf, "WHERE n.nspname <> 'pg_catalog'\n" " AND n.nspname <> 'information_schema'\n"); + if (pset.sversion >= 100000 && !showPartitions && !pattern) + appendPQExpBufferStr(&buf, " AND relispartition = false\n"); + processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false, "n.nspname", "c.relname", NULL, "pg_catalog.pg_table_is_visible(c.oid)"); @@ -3294,7 +3298,8 @@ listDbRoleSettings(const char *pattern, const char *pattern2) * (any order of the above is fine) */ bool -listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem) +listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem, + bool showPartitions) { bool showTables = strchr(tabtypes, 't') != NULL; bool showIndexes = strchr(tabtypes, 'i') != NULL; @@ -3441,6 +3446,9 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys */ appendPQExpBufferStr(&buf, " AND n.nspname !~ '^pg_toast'\n"); + if (pset.sversion >= 100000 && !showPartitions) + appendPQExpBufferStr(&buf, " AND relispartition = 'false'\n"); + processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "c.relname", NULL, "pg_catalog.pg_table_is_visible(c.oid)"); diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index 14a5667f3e..f48c505798 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -43,7 +43,7 @@ extern bool listDefaultACLs(const char *pattern); extern bool objectDescription(const char *pattern, bool showSystem); /* \d foo */ -extern bool describeTableDetails(const char *pattern, bool verbose, bool showSystem); +extern bool describeTableDetails(const char *pattern, bool verbose, bool showSystem, bool showPartitions); /* \dF */ extern bool listTSConfigs(const char *pattern, bool verbose); @@ -61,7 +61,7 @@ extern bool listTSTemplates(const char *pattern, bool verbose); extern bool listAllDbs(const char *pattern, bool verbose); /* \dt, \di, \ds, \dS, etc. */ -extern bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem); +extern bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem, bool showPartitions); /* \dD */ extern bool listDomains(const char *pattern, bool verbose, bool showSystem); diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index b3dbb5946e..dc3c8e57cb 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -218,9 +218,9 @@ slashUsage(unsigned short int pager) fprintf(output, "\n"); fprintf(output, _("Informational\n")); - fprintf(output, _(" (options: S = show system objects, + = additional detail)\n")); - fprintf(output, _(" \\d[S+] list tables, views, and sequences\n")); - fprintf(output, _(" \\d[S+] NAME describe table, view, sequence, or index\n")); + fprintf(output, _(" (options: S = show system objects, + = additional detail, ! = show partitions)\n")); + fprintf(output, _(" \\d[S+!] list tables, views, and sequences\n")); + fprintf(output, _(" \\d[S+!] NAME describe table, view, sequence, or index\n")); fprintf(output, _(" \\da[S] [PATTERN] list aggregates\n")); fprintf(output, _(" \\dA[+] [PATTERN] list access methods\n")); fprintf(output, _(" \\db[+] [PATTERN] list tablespaces\n")); @@ -229,8 +229,8 @@ slashUsage(unsigned short int pager) fprintf(output, _(" \\dd[S] [PATTERN] show object descriptions not displayed elsewhere\n")); fprintf(output, _(" \\dD[S+] [PATTERN] list domains\n")); fprintf(output, _(" \\ddp [PATTERN] list default privileges\n")); - fprintf(output, _(" \\dE[S+] [PATTERN] list foreign tables\n")); - fprintf(output, _(" \\det[+] [PATTERN] list foreign tables\n")); + fprintf(output, _(" \\dE[S+!] [PATTERN] list foreign tables\n")); + fprintf(output, _(" \\det[+!] [PATTERN] list foreign tables\n")); fprintf(output, _(" \\des[+] [PATTERN] list foreign servers\n")); fprintf(output, _(" \\deu[+] [PATTERN] list user mappings\n")); fprintf(output, _(" \\dew[+] [PATTERN] list foreign-data wrappers\n")); @@ -252,7 +252,7 @@ slashUsage(unsigned short int pager) fprintf(output, _(" \\dRp[+] [PATTERN] list replication publications\n")); fprintf(output, _(" \\dRs[+] [PATTERN] list replication subscriptions\n")); fprintf(output, _(" \\ds[S+] [PATTERN] list sequences\n")); - fprintf(output, _(" \\dt[S+] [PATTERN] list tables\n")); + fprintf(output, _(" \\dt[S+!] [PATTERN] list tables\n")); fprintf(output, _(" \\dT[S+] [PATTERN] list data types\n")); fprintf(output, _(" \\du[S+] [PATTERN] list roles\n")); fprintf(output, _(" \\dv[S+] [PATTERN] list views\n")); -- 2.11.0