Skip to content

Commit 788d8e5

Browse files
committed
Code review for patch to show definition of index columns in \d on index.
Safely schema-qualify the pg_get_indexdef call, make the query a bit prettier in -E mode, remove useless join to pg_index, make it more obvious that the header[] array is not overrun.
1 parent 47386fe commit 788d8e5

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/bin/psql/describe.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* Copyright (c) 2000-2009, PostgreSQL Global Development Group
1010
*
11-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.220 2009/07/06 17:01:42 petere Exp $
11+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.221 2009/07/07 16:28:38 tgl Exp $
1212
*/
1313
#include "postgres_fe.h"
1414

@@ -1028,7 +1028,7 @@ describeOneTableDetails(const char *schemaname,
10281028
char **ptr;
10291029
PQExpBufferData title;
10301030
PQExpBufferData tmpbuf;
1031-
int cols = 0;
1031+
int cols;
10321032
int numrows = 0;
10331033
struct
10341034
{
@@ -1156,23 +1156,19 @@ describeOneTableDetails(const char *schemaname,
11561156
PQclear(result);
11571157
}
11581158

1159-
/* Get column info (index requires additional checks) */
1159+
/* Get column info */
11601160
printfPQExpBuffer(&buf, "SELECT a.attname,");
11611161
appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
11621162
"\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
11631163
"\n FROM pg_catalog.pg_attrdef d"
11641164
"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
11651165
"\n a.attnotnull, a.attnum");
11661166
if (tableinfo.relkind == 'i')
1167-
appendPQExpBuffer(&buf, ", pg_get_indexdef(i.indexrelid,a.attnum, TRUE) AS indexdef");
1167+
appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
11681168
if (verbose)
1169-
appendPQExpBuffer(&buf, ", a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
1169+
appendPQExpBuffer(&buf, ",\n a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)");
11701170
appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_attribute a");
1171-
if (tableinfo.relkind == 'i')
1172-
appendPQExpBuffer(&buf, ", pg_catalog.pg_index i");
11731171
appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid);
1174-
if (tableinfo.relkind == 'i')
1175-
appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
11761172
appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
11771173

11781174
res = PSQLexec(buf.data, false);
@@ -1220,9 +1216,9 @@ describeOneTableDetails(const char *schemaname,
12201216
}
12211217

12221218
/* Set the number of columns, and their names */
1223-
cols += 2;
12241219
headers[0] = gettext_noop("Column");
12251220
headers[1] = gettext_noop("Type");
1221+
cols = 2;
12261222

12271223
if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v')
12281224
{
@@ -1302,15 +1298,15 @@ describeOneTableDetails(const char *schemaname,
13021298
if (tableinfo.relkind == 'S')
13031299
printTableAddCell(&cont, seq_values[i], false);
13041300

1305-
/* Expression for index */
1301+
/* Expression for index column */
13061302
if (tableinfo.relkind == 'i')
13071303
printTableAddCell(&cont, PQgetvalue(res, i, 5), false);
13081304

13091305
/* Storage and Description */
13101306
if (verbose)
13111307
{
1312-
int fnum = (tableinfo.relkind == 'i' ? 6 : 5);
1313-
char *storage = PQgetvalue(res, i, fnum);
1308+
int firstvcol = (tableinfo.relkind == 'i' ? 6 : 5);
1309+
char *storage = PQgetvalue(res, i, firstvcol);
13141310

13151311
/* these strings are literal in our syntax, so not translated. */
13161312
printTableAddCell(&cont, (storage[0] == 'p' ? "plain" :
@@ -1319,7 +1315,7 @@ describeOneTableDetails(const char *schemaname,
13191315
(storage[0] == 'e' ? "external" :
13201316
"???")))),
13211317
false);
1322-
printTableAddCell(&cont, PQgetvalue(res, i, fnum + 1), false);
1318+
printTableAddCell(&cont, PQgetvalue(res, i, firstvcol + 1), false);
13231319
}
13241320
}
13251321

@@ -1844,20 +1840,23 @@ describeOneTableDetails(const char *schemaname,
18441840
}
18451841
else
18461842
{
1847-
/* display the list of child tables*/
1843+
/* display the list of child tables */
1844+
const char *ct = _("Child tables");
1845+
18481846
for (i = 0; i < tuples; i++)
1849-
{
1850-
const char *ct = _("Child tables");
1851-
1852-
if (i == 0)
1853-
printfPQExpBuffer(&buf, "%s: %s", ct, PQgetvalue(result, i, 0));
1854-
else
1855-
printfPQExpBuffer(&buf, "%*s %s", (int) strlen(ct), "", PQgetvalue(result, i, 0));
1856-
if (i < tuples - 1)
1857-
appendPQExpBuffer(&buf, ",");
1858-
1859-
printTableAddFooter(&cont, buf.data);
1860-
}
1847+
{
1848+
if (i == 0)
1849+
printfPQExpBuffer(&buf, "%s: %s",
1850+
ct, PQgetvalue(result, i, 0));
1851+
else
1852+
printfPQExpBuffer(&buf, "%*s %s",
1853+
(int) strlen(ct), "",
1854+
PQgetvalue(result, i, 0));
1855+
if (i < tuples - 1)
1856+
appendPQExpBuffer(&buf, ",");
1857+
1858+
printTableAddFooter(&cont, buf.data);
1859+
}
18611860
}
18621861
PQclear(result);
18631862

0 commit comments

Comments
 (0)