Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 24f62e9

Browse files
committed
Improve psql's \d output for partitioned indexes.
Include partitioning information much as we do for partitioned tables. (However, \d+ doesn't show the partition bounds, because those are not stored for indexes.) In passing, fix a couple of queries to look less messy in -E output. Also, add some tests for \d on tables with nondefault tablespaces. (Somebody previously added a rather silly number of tests for \d on partitioned indexes, yet completely neglected other cases.) Justin Pryzby, reviewed by Fabien Coelho Discussion: https://postgr.es/m/20190422154902.GH14223@telsasoft.com
1 parent eb5472d commit 24f62e9

File tree

3 files changed

+155
-57
lines changed

3 files changed

+155
-57
lines changed

src/bin/psql/describe.c

+57-57
Original file line numberDiff line numberDiff line change
@@ -2293,8 +2293,13 @@ describeOneTableDetails(const char *schemaname,
22932293
appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
22942294

22952295
printTableAddFooter(&cont, tmpbuf.data);
2296-
add_tablespace_footer(&cont, tableinfo.relkind,
2297-
tableinfo.tablespace, true);
2296+
2297+
/*
2298+
* If it's a partitioned index, we'll print the tablespace below
2299+
*/
2300+
if (tableinfo.relkind == RELKIND_INDEX)
2301+
add_tablespace_footer(&cont, tableinfo.relkind,
2302+
tableinfo.tablespace, true);
22982303
}
22992304

23002305
PQclear(result);
@@ -2304,6 +2309,7 @@ describeOneTableDetails(const char *schemaname,
23042309
tableinfo.relkind == RELKIND_MATVIEW ||
23052310
tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
23062311
tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
2312+
tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
23072313
tableinfo.relkind == RELKIND_TOASTVALUE)
23082314
{
23092315
/* Footer information about a table */
@@ -3070,11 +3076,17 @@ describeOneTableDetails(const char *schemaname,
30703076
tableinfo.relkind == RELKIND_MATVIEW ||
30713077
tableinfo.relkind == RELKIND_FOREIGN_TABLE ||
30723078
tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3079+
tableinfo.relkind == RELKIND_PARTITIONED_INDEX ||
30733080
tableinfo.relkind == RELKIND_TOASTVALUE)
30743081
{
3082+
bool is_partitioned;
30753083
PGresult *result;
30763084
int tuples;
30773085

3086+
/* simplify some repeated tests below */
3087+
is_partitioned = (tableinfo.relkind == RELKIND_PARTITIONED_TABLE ||
3088+
tableinfo.relkind == RELKIND_PARTITIONED_INDEX);
3089+
30783090
/* print foreign server name */
30793091
if (tableinfo.relkind == RELKIND_FOREIGN_TABLE)
30803092
{
@@ -3115,13 +3127,15 @@ describeOneTableDetails(const char *schemaname,
31153127
PQclear(result);
31163128
}
31173129

3118-
/* print inherited tables (exclude, if parent is a partitioned table) */
3130+
/* print tables inherited from (exclude partitioned parents) */
31193131
printfPQExpBuffer(&buf,
3120-
"SELECT c.oid::pg_catalog.regclass"
3121-
" FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
3122-
" WHERE c.oid=i.inhparent AND i.inhrelid = '%s'"
3123-
" AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
3124-
" ORDER BY inhseqno;", oid);
3132+
"SELECT c.oid::pg_catalog.regclass\n"
3133+
"FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3134+
"WHERE c.oid = i.inhparent AND i.inhrelid = '%s'\n"
3135+
" AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_TABLE)
3136+
" AND c.relkind != " CppAsString2(RELKIND_PARTITIONED_INDEX)
3137+
"\nORDER BY inhseqno;",
3138+
oid);
31253139

31263140
result = PSQLexec(buf.data);
31273141
if (!result)
@@ -3153,39 +3167,40 @@ describeOneTableDetails(const char *schemaname,
31533167
/* print child tables (with additional info if partitions) */
31543168
if (pset.sversion >= 100000)
31553169
printfPQExpBuffer(&buf,
3156-
"SELECT c.oid::pg_catalog.regclass,"
3157-
" pg_catalog.pg_get_expr(c.relpartbound, c.oid),"
3158-
" c.relkind"
3159-
" FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
3160-
" WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
3161-
" ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3162-
" c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
3170+
"SELECT c.oid::pg_catalog.regclass, c.relkind,"
3171+
" pg_catalog.pg_get_expr(c.relpartbound, c.oid)\n"
3172+
"FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3173+
"WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3174+
"ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT',"
3175+
" c.oid::pg_catalog.regclass::pg_catalog.text;",
3176+
oid);
31633177
else if (pset.sversion >= 80300)
31643178
printfPQExpBuffer(&buf,
3165-
"SELECT c.oid::pg_catalog.regclass"
3166-
" FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
3167-
" WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
3168-
" ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;", oid);
3179+
"SELECT c.oid::pg_catalog.regclass, c.relkind, NULL\n"
3180+
"FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3181+
"WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3182+
"ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;",
3183+
oid);
31693184
else
31703185
printfPQExpBuffer(&buf,
3171-
"SELECT c.oid::pg_catalog.regclass"
3172-
" FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i"
3173-
" WHERE c.oid=i.inhrelid AND i.inhparent = '%s'"
3174-
" ORDER BY c.relname;", oid);
3186+
"SELECT c.oid::pg_catalog.regclass, c.relkind, NULL\n"
3187+
"FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i\n"
3188+
"WHERE c.oid = i.inhrelid AND i.inhparent = '%s'\n"
3189+
"ORDER BY c.relname;",
3190+
oid);
31753191

31763192
result = PSQLexec(buf.data);
31773193
if (!result)
31783194
goto error_return;
3179-
else
3180-
tuples = PQntuples(result);
3195+
tuples = PQntuples(result);
31813196

31823197
/*
31833198
* For a partitioned table with no partitions, always print the number
31843199
* of partitions as zero, even when verbose output is expected.
31853200
* Otherwise, we will not print "Partitions" section for a partitioned
31863201
* table without any partitions.
31873202
*/
3188-
if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE && tuples == 0)
3203+
if (is_partitioned && tuples == 0)
31893204
{
31903205
printfPQExpBuffer(&buf, _("Number of partitions: %d"), tuples);
31913206
printTableAddFooter(&cont, buf.data);
@@ -3195,49 +3210,34 @@ describeOneTableDetails(const char *schemaname,
31953210
/* print the number of child tables, if any */
31963211
if (tuples > 0)
31973212
{
3198-
if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
3199-
printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
3200-
else
3213+
if (is_partitioned)
32013214
printfPQExpBuffer(&buf, _("Number of partitions: %d (Use \\d+ to list them.)"), tuples);
3215+
else
3216+
printfPQExpBuffer(&buf, _("Number of child tables: %d (Use \\d+ to list them.)"), tuples);
32023217
printTableAddFooter(&cont, buf.data);
32033218
}
32043219
}
32053220
else
32063221
{
32073222
/* display the list of child tables */
3208-
const char *ct = (tableinfo.relkind != RELKIND_PARTITIONED_TABLE) ?
3209-
_("Child tables") : _("Partitions");
3223+
const char *ct = is_partitioned ? _("Partitions") : _("Child tables");
32103224
int ctw = pg_wcswidth(ct, strlen(ct), pset.encoding);
32113225

32123226
for (i = 0; i < tuples; i++)
32133227
{
3214-
if (tableinfo.relkind != RELKIND_PARTITIONED_TABLE)
3215-
{
3216-
if (i == 0)
3217-
printfPQExpBuffer(&buf, "%s: %s",
3218-
ct, PQgetvalue(result, i, 0));
3219-
else
3220-
printfPQExpBuffer(&buf, "%*s %s",
3221-
ctw, "", PQgetvalue(result, i, 0));
3222-
}
3223-
else
3224-
{
3225-
char *partitioned_note;
3226-
3227-
if (*PQgetvalue(result, i, 2) == RELKIND_PARTITIONED_TABLE)
3228-
partitioned_note = ", PARTITIONED";
3229-
else
3230-
partitioned_note = "";
3228+
char child_relkind = *PQgetvalue(result, i, 1);
32313229

3232-
if (i == 0)
3233-
printfPQExpBuffer(&buf, "%s: %s %s%s",
3234-
ct, PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
3235-
partitioned_note);
3236-
else
3237-
printfPQExpBuffer(&buf, "%*s %s %s%s",
3238-
ctw, "", PQgetvalue(result, i, 0), PQgetvalue(result, i, 1),
3239-
partitioned_note);
3240-
}
3230+
if (i == 0)
3231+
printfPQExpBuffer(&buf, "%s: %s",
3232+
ct, PQgetvalue(result, i, 0));
3233+
else
3234+
printfPQExpBuffer(&buf, "%*s %s",
3235+
ctw, "", PQgetvalue(result, i, 0));
3236+
if (!PQgetisnull(result, i, 2))
3237+
appendPQExpBuffer(&buf, " %s", PQgetvalue(result, i, 2));
3238+
if (child_relkind == RELKIND_PARTITIONED_TABLE ||
3239+
child_relkind == RELKIND_PARTITIONED_INDEX)
3240+
appendPQExpBufferStr(&buf, ", PARTITIONED");
32413241
if (i < tuples - 1)
32423242
appendPQExpBufferChar(&buf, ',');
32433243

src/test/regress/input/tablespace.source

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace;
4444
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
4545
where c.reltablespace = t.oid AND c.relname = 'foo_idx';
4646

47+
-- check \d output
48+
\d testschema.foo
49+
\d testschema.foo_idx
50+
4751
--
4852
-- partitioned table
4953
--
@@ -85,7 +89,12 @@ CREATE INDEX part_a_idx ON testschema.part (a) TABLESPACE regress_tblspace;
8589
CREATE TABLE testschema.part2 PARTITION OF testschema.part FOR VALUES IN (2);
8690
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
8791
where c.reltablespace = t.oid AND c.relname LIKE 'part%_idx';
92+
\d testschema.part
93+
\d+ testschema.part
94+
\d testschema.part1
95+
\d+ testschema.part1
8896
\d testschema.part_a_idx
97+
\d+ testschema.part_a_idx
8998

9099
-- partitioned rels cannot specify the default tablespace. These fail:
91100
CREATE TABLE testschema.dflt (a int PRIMARY KEY) PARTITION BY LIST (a) TABLESPACE pg_default;

0 commit comments

Comments
 (0)