Improve psql's \d output for TOAST tables.
authorTom Lane
Tue, 23 Jul 2019 19:25:56 +0000 (15:25 -0400)
committerTom Lane
Tue, 23 Jul 2019 19:25:56 +0000 (15:25 -0400)
Add the name of the owning table to the footers for a TOAST table.
Also, show all the same footers as for a regular table (in practice,
this adds the index and perhaps the tablespace and access method).

Justin Pryzby, reviewed by Fabien Coelho

Discussion: https://postgr.es/m/20190422154902[email protected]

src/bin/psql/describe.c
src/test/regress/expected/psql.out
src/test/regress/sql/psql.sql

index 8b4cd53631c0f2c6fe77e4e83bf9c62ebf5b0377..b57bb8e831341131987a19cbb292eaf29169f6f7 100644 (file)
@@ -2172,6 +2172,33 @@ describeOneTableDetails(const char *schemaname,
        PQclear(result);
    }
 
+   if (tableinfo.relkind == RELKIND_TOASTVALUE)
+   {
+       /* For a TOAST table, print name of owning table */
+       PGresult   *result;
+
+       printfPQExpBuffer(&buf,
+                         "SELECT n.nspname, c.relname\n"
+                         "FROM pg_catalog.pg_class c"
+                         " JOIN pg_catalog.pg_namespace n"
+                         " ON n.oid = c.relnamespace\n"
+                         "WHERE reltoastrelid = '%s';", oid);
+       result = PSQLexec(buf.data);
+       if (!result)
+           goto error_return;
+
+       if (PQntuples(result) == 1)
+       {
+           char       *schemaname = PQgetvalue(result, 0, 0);
+           char       *relname = PQgetvalue(result, 0, 1);
+
+           printfPQExpBuffer(&tmpbuf, _("Owning table: \"%s.%s\""),
+                             schemaname, relname);
+           printTableAddFooter(&cont, tmpbuf.data);
+       }
+       PQclear(result);
+   }
+
    if (tableinfo.relkind == RELKIND_INDEX ||
        tableinfo.relkind == RELKIND_PARTITIONED_INDEX)
    {
@@ -2272,10 +2299,12 @@ describeOneTableDetails(const char *schemaname,
 
        PQclear(result);
    }
+   /* If you add relkinds here, see also "Finish printing..." stanza below */
    else if (tableinfo.relkind == RELKIND_RELATION ||
             tableinfo.relkind == RELKIND_MATVIEW ||
             tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
-            tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
+            tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
+            tableinfo.relkind == RELKIND_TOASTVALUE)
    {
        /* Footer information about a table */
        PGresult   *result = NULL;
@@ -3040,7 +3069,8 @@ describeOneTableDetails(const char *schemaname,
    if (tableinfo.relkind == RELKIND_RELATION ||
        tableinfo.relkind == RELKIND_MATVIEW ||
        tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
-       tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
+       tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
+       tableinfo.relkind == RELKIND_TOASTVALUE)
    {
        PGresult   *result;
        int         tuples;
@@ -3308,7 +3338,8 @@ add_tablespace_footer(printTableContent *const cont, char relkind,
        relkind == RELKIND_MATVIEW ||
        relkind == RELKIND_INDEX ||
        relkind == RELKIND_PARTITIONED_TABLE ||
-       relkind == RELKIND_PARTITIONED_INDEX)
+       relkind == RELKIND_PARTITIONED_INDEX ||
+       relkind == RELKIND_TOASTVALUE)
    {
        /*
         * We ignore the database default tablespace so that users not using
index 9021c808dcfe086aea9d995ae67b5a3c9e871084..ef534a36a06e204a52db0f0d400b7b74da121563 100644 (file)
@@ -4748,3 +4748,15 @@ drop schema testpart;
 set search_path to default;
 set role to default;
 drop role regress_partitioning_role;
+-- \d on toast table (use pg_statistic's toast table, which has a known name)
+\d pg_toast.pg_toast_2619
+TOAST table "pg_toast.pg_toast_2619"
+   Column   |  Type   
+------------+---------
+ chunk_id   | oid
+ chunk_seq  | integer
+ chunk_data | bytea
+Owning table: "pg_catalog.pg_statistic"
+Indexes:
+    "pg_toast_2619_index" PRIMARY KEY, btree (chunk_id, chunk_seq)
+
index cefe41bdc2e997d244faefb33c61f4dc59c70209..2e379849625b39f787d5e9680a126344c600d969 100644 (file)
@@ -1131,3 +1131,6 @@ set search_path to default;
 
 set role to default;
 drop role regress_partitioning_role;
+
+-- \d on toast table (use pg_statistic's toast table, which has a known name)
+\d pg_toast.pg_toast_2619