Skip to content

Commit 05b6ec3

Browse files
Show partition info from psql \d+
Author: Amit Langote, Ashutosh Bapat Reviewed-by: Álvaro Herrera, Simon Riggs
1 parent 7e17a68 commit 05b6ec3

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

src/bin/psql/describe.c

+28-6
Original file line numberDiff line numberDiff line change
@@ -2870,7 +2870,9 @@ describeOneTableDetails(const char *schemaname,
28702870
/* print child tables (with additional info if partitions) */
28712871
if (pset.sversion >= 100000)
28722872
printfPQExpBuffer(&buf,
2873-
"SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)"
2873+
"SELECT c.oid::pg_catalog.regclass,"
2874+
" pg_catalog.pg_get_expr(c.relpartbound, c.oid),"
2875+
" c.relkind"
28742876
" FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
28752877
" WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
28762878
" ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
@@ -2893,7 +2895,18 @@ describeOneTableDetails(const char *schemaname,
28932895
else
28942896
tuples = PQntuples(result);
28952897

2896-
if (!verbose)
2898+
/*
2899+
* For a partitioned table with no partitions, always print the number
2900+
* of partitions as zero, even when verbose output is expected.
2901+
* Otherwise, we will not print "Partitions" section for a partitioned
2902+
* table without any partitions.
2903+
*/
2904+
if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE && tuples == 0)
2905+
{
2906+
printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
2907+
printTableAddFooter(&cont, buf.data);
2908+
}
2909+
else if (!verbose)
28972910
{
28982911
/* print the number of child tables, if any */
28992912
if (tuples > 0)
@@ -2925,12 +2938,21 @@ describeOneTableDetails(const char *schemaname,
29252938
}
29262939
else
29272940
{
2941+
char *partitioned_note;
2942+
2943+
if (*PQgetvalue(result, i, 2) == RELKIND_PARTITIONED_TABLE)
2944+
partitioned_note = ", PARTITIONED";
2945+
else
2946+
partitioned_note = "";
2947+
29282948
if (i == 0)
2929-
printfPQExpBuffer(&buf, "%s: %s %s",
2930-
ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
2949+
printfPQExpBuffer(&buf, "%s: %s %s%s",
2950+
ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2951+
partitioned_note);
29312952
else
2932-
printfPQExpBuffer(&buf, "%*s %s %s",
2933-
ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1));
2953+
printfPQExpBuffer(&buf, "%*s %s %s%s",
2954+
ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
2955+
partitioned_note);
29342956
}
29352957
if (i < tuples - 1)
29362958
appendPQExpBufferChar(&buf, ',');

src/test/regress/expected/create_table.out

+8-5
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,15 @@ ERROR: cannot inherit from partitioned table "partitioned2"
428428
c | text | | |
429429
d | text | | |
430430
Partition key: RANGE (a oid_ops, plusone(b), c, d COLLATE "C")
431+
Number of partitions: 0
431432

432-
\d partitioned2
433-
Table "public.partitioned2"
434-
Column | Type | Collation | Nullable | Default
435-
--------+---------+-----------+----------+---------
436-
a | integer | | |
433+
\d+ partitioned2
434+
Table "public.partitioned2"
435+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
436+
--------+---------+-----------+----------+---------+---------+--------------+-------------
437+
a | integer | | | | plain | |
437438
Partition key: LIST (((a + 1)))
439+
Number of partitions: 0
438440

439441
DROP TABLE partitioned, partitioned2;
440442
--
@@ -858,5 +860,6 @@ SELECT obj_description('parted_col_comment'::regclass);
858860
a | integer | | | | plain | | Partition key
859861
b | text | | | | extended | |
860862
Partition key: LIST (a)
863+
Number of partitions: 0
861864

862865
DROP TABLE parted_col_comment;

src/test/regress/expected/foreign_data.out

+3
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,7 @@ DROP FOREIGN TABLE pt2_1;
18981898
c2 | text | | | | extended | |
18991899
c3 | date | | | | plain | |
19001900
Partition key: LIST (c1)
1901+
Number of partitions: 0
19011902

19021903
CREATE FOREIGN TABLE pt2_1 (
19031904
c1 integer NOT NULL,
@@ -1982,6 +1983,7 @@ ALTER TABLE pt2 ALTER c2 SET NOT NULL;
19821983
c2 | text | | not null | | extended | |
19831984
c3 | date | | | | plain | |
19841985
Partition key: LIST (c1)
1986+
Number of partitions: 0
19851987

19861988
\d+ pt2_1
19871989
Foreign table "public.pt2_1"
@@ -2011,6 +2013,7 @@ ALTER TABLE pt2 ADD CONSTRAINT pt2chk1 CHECK (c1 > 0);
20112013
Partition key: LIST (c1)
20122014
Check constraints:
20132015
"pt2chk1" CHECK (c1 > 0)
2016+
Number of partitions: 0
20142017

20152018
\d+ pt2_1
20162019
Foreign table "public.pt2_1"

src/test/regress/expected/insert.out

+17
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ from hash_parted order by part;
425425
hpart3 | 11 | 3
426426
(13 rows)
427427

428+
-- test \d+ output on a table which has both partitioned and unpartitioned
429+
-- partitions
430+
\d+ list_parted
431+
Table "public.list_parted"
432+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
433+
--------+---------+-----------+----------+---------+----------+--------------+-------------
434+
a | text | | | | extended | |
435+
b | integer | | | | plain | |
436+
Partition key: LIST (lower(a))
437+
Partitions: part_aa_bb FOR VALUES IN ('aa', 'bb'),
438+
part_cc_dd FOR VALUES IN ('cc', 'dd'),
439+
part_default DEFAULT, PARTITIONED,
440+
part_ee_ff FOR VALUES IN ('ee', 'ff'), PARTITIONED,
441+
part_gg FOR VALUES IN ('gg'), PARTITIONED,
442+
part_null FOR VALUES IN (NULL),
443+
part_xx_yy FOR VALUES IN ('xx', 'yy'), PARTITIONED
444+
428445
-- cleanup
429446
drop table range_parted, list_parted;
430447
drop table hash_parted;

src/test/regress/sql/create_table.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ CREATE TABLE fail () INHERITS (partitioned2);
421421

422422
-- Partition key in describe output
423423
\d partitioned
424-
\d partitioned2
424+
\d+ partitioned2
425425

426426
DROP TABLE partitioned, partitioned2;
427427

src/test/regress/sql/insert.sql

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ insert into hpart3 values(11);
252252
select tableoid::regclass as part, a, a%4 as "remainder = a % 4"
253253
from hash_parted order by part;
254254

255+
-- test \d+ output on a table which has both partitioned and unpartitioned
256+
-- partitions
257+
\d+ list_parted
258+
255259
-- cleanup
256260
drop table range_parted, list_parted;
257261
drop table hash_parted;

0 commit comments

Comments
 (0)