From d42522fb19d74a4154d3310a667a82ca888e0bf9 Mon Sep 17 00:00:00 2001 From: amit Date: Wed, 12 Jul 2017 15:37:29 +0900 Subject: [PATCH 4/4] Fix \d+ output for yet empty partitioned tables Currently it only shows the partition key line, but says nothing about partitions. Instead add a line saying it has 0 partitions. Authors: Amit Langote, Ashutosh Bapat --- src/bin/psql/describe.c | 16 +++++++++++++++- src/test/regress/expected/create_table.out | 13 ++++++++----- src/test/regress/expected/foreign_data.out | 3 +++ src/test/regress/sql/create_table.sql | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index faf69b95c6..28b0f2b62a 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2863,7 +2863,20 @@ 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) + { + + /* print the number of child tables, if any */ + printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples); + printTableAddFooter(&cont, buf.data); + } + else if (!verbose) { const char *ct = (tableinfo.relkind != RELKIND_PARTITIONED_TABLE) ? _("child tables") @@ -2921,6 +2934,7 @@ describeOneTableDetails(const char *schemaname, printTableAddFooter(&cont, buf.data); } } + PQclear(result); /* Table type */ diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out index b6f794e1c2..f6c6a514fa 100644 --- a/src/test/regress/expected/create_table.out +++ b/src/test/regress/expected/create_table.out @@ -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; -- @@ -773,5 +775,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; diff --git a/src/test/regress/expected/foreign_data.out b/src/test/regress/expected/foreign_data.out index 7f2f529393..51da5e91e2 100644 --- a/src/test/regress/expected/foreign_data.out +++ b/src/test/regress/expected/foreign_data.out @@ -1879,6 +1879,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, @@ -1963,6 +1964,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" @@ -1992,6 +1994,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" diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql index cb7aa5bbc6..80106dd7d9 100644 --- a/src/test/regress/sql/create_table.sql +++ b/src/test/regress/sql/create_table.sql @@ -421,7 +421,7 @@ CREATE TABLE fail () INHERITS (partitioned2); -- Partition key in describe output \d partitioned -\d partitioned2 +\d+ partitioned2 DROP TABLE partitioned, partitioned2; -- 2.11.0