From c26b513e72811058ddf729854d54f4084670208b Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Wed, 8 Mar 2017 11:39:51 +0530
Subject: [PATCH 2/2] Number of partitions for a partitioned table

For a partitioned table show the number of partitions even if it's 0.

Patch by Amit Langote, with some code and test changes by Ashutosh Bapat
---
 src/bin/psql/describe.c                    |   19 +++++++++++++++----
 src/test/regress/expected/create_table.out |   12 +++++++-----
 src/test/regress/sql/create_table.sql      |    2 +-
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index f8617cf..40549c9 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -2707,13 +2707,23 @@ 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 == 'P' && tuples == 0)
 		{
-			const char *ct = tableinfo.relkind != 'P' ? _("child tables") : _("partitions");
-
-			/* print the number of child tables, if any */
+			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)
 			{
+				const char *ct = tableinfo.relkind != 'P' ? _("child tables") : _("partitions");
 				printfPQExpBuffer(&buf, _("Number of %s: %d (Use \\d+ to list them.)"), ct, tuples);
 				printTableAddFooter(&cont, buf.data);
 			}
@@ -2759,6 +2769,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 c07a474..f518cec 100644
--- a/src/test/regress/expected/create_table.out
+++ b/src/test/regress/expected/create_table.out
@@ -440,13 +440,15 @@ ERROR:  cannot inherit from partitioned table "partitioned2"
  c      | text    |           | not null | 
  d      | text    |           | not null | 
 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;
 --
diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql
index 1f0fa8e..15e3274 100644
--- a/src/test/regress/sql/create_table.sql
+++ b/src/test/regress/sql/create_table.sql
@@ -426,7 +426,7 @@ CREATE TABLE fail () INHERITS (partitioned2);
 
 -- Partition key in describe output
 \d partitioned
-\d partitioned2
+\d+ partitioned2
 
 DROP TABLE partitioned, partitioned2;
 
-- 
1.7.9.5

