]> granicus.if.org Git - postgresql/commitdiff
Show partition info from psql \d+
authorSimon Riggs <simon@2ndQuadrant.com>
Wed, 22 Nov 2017 18:10:39 +0000 (05:10 +1100)
committerSimon Riggs <simon@2ndQuadrant.com>
Wed, 22 Nov 2017 18:10:39 +0000 (05:10 +1100)
Author: Amit Langote, Ashutosh Bapat
Reviewed-by: Álvaro Herrera, Simon Riggs
src/bin/psql/describe.c
src/test/regress/expected/create_table.out
src/test/regress/expected/foreign_data.out
src/test/regress/expected/insert.out
src/test/regress/sql/create_table.sql
src/test/regress/sql/insert.sql

index b7b978a36147deb1a0eb37266434ca5391caaa01..44c508971a0761522d803772fe0200ca6f5d8781 100644 (file)
@@ -2870,7 +2870,9 @@ describeOneTableDetails(const char *schemaname,
                /* print child tables (with additional info if partitions) */
                if (pset.sversion >= 100000)
                        printfPQExpBuffer(&buf,
-                                                         "SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)"
+                                                         "SELECT c.oid::pg_catalog.regclass,"
+                                                         "       pg_catalog.pg_get_expr(c.relpartbound, c.oid),"
+                                                         "       c.relkind"
                                                          " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
                                                          " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
                                                          " ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
@@ -2893,7 +2895,18 @@ describeOneTableDetails(const char *schemaname,
                else
                        tuples = PQntuples(result);
 
-               if (!verbose)
+               /*
+                * For a partitioned table with no partitions, always print the number
+                * of partitions as zero, even when verbose output is expected.
+                * Otherwise, we will not print "Partitions" section for a partitioned
+                * table without any partitions.
+                */
+               if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE && tuples == 0)
+               {
+                       printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
+                       printTableAddFooter(&cont, buf.data);
+               }
+               else if (!verbose)
                {
                        /* print the number of child tables, if any */
                        if (tuples > 0)
@@ -2925,12 +2938,21 @@ describeOneTableDetails(const char *schemaname,
                                }
                                else
                                {
+                                       char *partitioned_note;
+
+                                       if (*PQgetvalue(result, i, 2) == RELKIND_PARTITIONED_TABLE)
+                                               partitioned_note = ", PARTITIONED";
+                                       else
+                                               partitioned_note = "";
+
                                        if (i == 0)
-                                               printfPQExpBuffer(&buf, "%s: %s %s",
-                                                                                 ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
+                                               printfPQExpBuffer(&buf, "%s: %s %s%s",
+                                                                                 ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
+                                                                                 partitioned_note);
                                        else
-                                               printfPQExpBuffer(&buf, "%*s  %s %s",
-                                                                                 ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
+                                               printfPQExpBuffer(&buf, "%*s  %s %s%s",
+                                                                                 ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
+                                                                                 partitioned_note);
                                }
                                if (i < tuples - 1)
                                        appendPQExpBufferChar(&buf, ',');
index 335cd37e18464a2af0226d8ea2b7e76f978f0ed6..8e745402ae4dbef82b14798eb06ca147b3746a51 100644 (file)
@@ -428,13 +428,15 @@ ERROR:  cannot inherit from partitioned table "partitioned2"
  c      | text    |           |          | 
  d      | text    |           |          | 
 Partition key: RANGE (a oid_ops, plusone(b), c, d COLLATE "C")
+Number of partitions: 0
 
-\d partitioned2
-            Table "public.partitioned2"
- Column |  Type   | Collation | Nullable | Default 
---------+---------+-----------+----------+---------
- a      | integer |           |          | 
+\d+ partitioned2
+                               Table "public.partitioned2"
+ Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
+--------+---------+-----------+----------+---------+---------+--------------+-------------
+ a      | integer |           |          |         | plain   |              | 
 Partition key: LIST (((a + 1)))
+Number of partitions: 0
 
 DROP TABLE partitioned, partitioned2;
 --
@@ -858,5 +860,6 @@ SELECT obj_description('parted_col_comment'::regclass);
  a      | integer |           |          |         | plain    |              | Partition key
  b      | text    |           |          |         | extended |              | 
 Partition key: LIST (a)
+Number of partitions: 0
 
 DROP TABLE parted_col_comment;
index 331f7a911fcc848d2226c1b5d181fc96e444afe1..d2c184f2cfe771e591faecff905f54279bd9bf30 100644 (file)
@@ -1898,6 +1898,7 @@ DROP FOREIGN TABLE pt2_1;
  c2     | text    |           |          |         | extended |              | 
  c3     | date    |           |          |         | plain    |              | 
 Partition key: LIST (c1)
+Number of partitions: 0
 
 CREATE FOREIGN TABLE pt2_1 (
        c1 integer NOT NULL,
@@ -1982,6 +1983,7 @@ ALTER TABLE pt2 ALTER c2 SET NOT NULL;
  c2     | text    |           | not null |         | extended |              | 
  c3     | date    |           |          |         | plain    |              | 
 Partition key: LIST (c1)
+Number of partitions: 0
 
 \d+ pt2_1
                                       Foreign table "public.pt2_1"
@@ -2011,6 +2013,7 @@ ALTER TABLE pt2 ADD CONSTRAINT pt2chk1 CHECK (c1 > 0);
 Partition key: LIST (c1)
 Check constraints:
     "pt2chk1" CHECK (c1 > 0)
+Number of partitions: 0
 
 \d+ pt2_1
                                       Foreign table "public.pt2_1"
index 9d84ba46583a77235fdb45fa7b40a379ab255784..1116b3a8d244f96d0ee38a5b228185a3d4bb414a 100644 (file)
@@ -425,6 +425,23 @@ from hash_parted order by part;
  hpart3 | 11 |                 3
 (13 rows)
 
+-- test \d+ output on a table which has both partitioned and unpartitioned
+-- partitions
+\d+ list_parted
+                                Table "public.list_parted"
+ Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description 
+--------+---------+-----------+----------+---------+----------+--------------+-------------
+ a      | text    |           |          |         | extended |              | 
+ b      | integer |           |          |         | plain    |              | 
+Partition key: LIST (lower(a))
+Partitions: part_aa_bb FOR VALUES IN ('aa', 'bb'),
+            part_cc_dd FOR VALUES IN ('cc', 'dd'),
+            part_default DEFAULT, PARTITIONED,
+            part_ee_ff FOR VALUES IN ('ee', 'ff'), PARTITIONED,
+            part_gg FOR VALUES IN ('gg'), PARTITIONED,
+            part_null FOR VALUES IN (NULL),
+            part_xx_yy FOR VALUES IN ('xx', 'yy'), PARTITIONED
+
 -- cleanup
 drop table range_parted, list_parted;
 drop table hash_parted;
index b77b4764365b86d7649b6ce19b9bc11b4b0b809e..8f9991ef18673701bcdc7c5935d5e87985e2731a 100644 (file)
@@ -421,7 +421,7 @@ CREATE TABLE fail () INHERITS (partitioned2);
 
 -- Partition key in describe output
 \d partitioned
-\d partitioned2
+\d+ partitioned2
 
 DROP TABLE partitioned, partitioned2;
 
index 791817ba50737c2927c635f13bb4348648a63345..f22ab41ae300d55e2768bb161c2150e2f2184171 100644 (file)
@@ -252,6 +252,10 @@ insert into hpart3 values(11);
 select tableoid::regclass as part, a, a%4 as "remainder = a % 4"
 from hash_parted order by part;
 
+-- test \d+ output on a table which has both partitioned and unpartitioned
+-- partitions
+\d+ list_parted
+
 -- cleanup
 drop table range_parted, list_parted;
 drop table hash_parted;