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

Commit 2be368a

Browse files
committed
Display incoming as well as outgoing foreign-key constraints in psql's
\d output for a table. Kenneth D'Souza, some changes by myself.
1 parent 2169e42 commit 2be368a

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

src/bin/psql/describe.c

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.164 2008/01/01 19:45:56 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.165 2008/03/30 17:50:11 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -1106,12 +1106,14 @@ describeOneTableDetails(const char *schemaname,
11061106
*result3 = NULL,
11071107
*result4 = NULL,
11081108
*result5 = NULL,
1109-
*result6 = NULL;
1109+
*result6 = NULL,
1110+
*result7 = NULL;
11101111
int check_count = 0,
11111112
index_count = 0,
11121113
foreignkey_count = 0,
11131114
rule_count = 0,
11141115
trigger_count = 0,
1116+
referencedby_count = 0,
11151117
inherits_count = 0;
11161118
int count_footers = 0;
11171119

@@ -1228,24 +1230,47 @@ describeOneTableDetails(const char *schemaname,
12281230
foreignkey_count = PQntuples(result5);
12291231
}
12301232

1233+
/* count incoming foreign-key references (none if no triggers) */
1234+
if (tableinfo.triggers)
1235+
{
1236+
printfPQExpBuffer(&buf,
1237+
"SELECT conname, conrelid::pg_catalog.regclass,\n"
1238+
" pg_catalog.pg_get_constraintdef(oid, true) as condef\n"
1239+
"FROM pg_catalog.pg_constraint c\n"
1240+
"WHERE c.confrelid = '%s' AND c.contype = 'f' ORDER BY 1",
1241+
oid);
1242+
result6 = PSQLexec(buf.data, false);
1243+
if (!result6)
1244+
{
1245+
PQclear(result1);
1246+
PQclear(result2);
1247+
PQclear(result3);
1248+
PQclear(result4);
1249+
PQclear(result5);
1250+
goto error_return;
1251+
}
1252+
else
1253+
referencedby_count = PQntuples(result6);
1254+
}
1255+
12311256
/* count inherited tables */
1232-
printfPQExpBuffer(&buf, "SELECT c.oid::regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno ASC", oid);
1257+
printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '%s' ORDER BY inhseqno", oid);
12331258

1234-
result6 = PSQLexec(buf.data, false);
1235-
if (!result6)
1236-
{
1259+
result7 = PSQLexec(buf.data, false);
1260+
if (!result7)
1261+
{
12371262
PQclear(result1);
12381263
PQclear(result2);
12391264
PQclear(result3);
12401265
PQclear(result4);
12411266
PQclear(result5);
1267+
PQclear(result6);
12421268
goto error_return;
12431269
}
12441270
else
1245-
inherits_count = PQntuples(result6);
1271+
inherits_count = PQntuples(result7);
12461272

1247-
footers = pg_malloc_zero((index_count + check_count + rule_count + trigger_count + foreignkey_count + inherits_count + 7 + 1)
1248-
* sizeof(*footers));
1273+
footers = pg_malloc_zero((index_count + check_count + rule_count + trigger_count + foreignkey_count + referencedby_count + inherits_count + 8 + 1) * sizeof(*footers));
12491274

12501275
/* print indexes */
12511276
if (index_count > 0)
@@ -1333,6 +1358,22 @@ describeOneTableDetails(const char *schemaname,
13331358
}
13341359
}
13351360

1361+
/* print incoming foreign-key constraints */
1362+
if (referencedby_count > 0)
1363+
{
1364+
printfPQExpBuffer(&buf, _("Referenced by:"));
1365+
footers[count_footers++] = pg_strdup(buf.data);
1366+
for (i = 0; i < referencedby_count; i++)
1367+
{
1368+
printfPQExpBuffer(&buf, _(" \"%s\" IN %s %s"),
1369+
PQgetvalue(result6, i, 0),
1370+
PQgetvalue(result6, i, 1),
1371+
PQgetvalue(result6, i, 2));
1372+
1373+
footers[count_footers++] = pg_strdup(buf.data);
1374+
}
1375+
}
1376+
13361377
/* print rules */
13371378
if (rule_count > 0)
13381379
{
@@ -1487,9 +1528,9 @@ describeOneTableDetails(const char *schemaname,
14871528
const char *s = _("Inherits");
14881529

14891530
if (i == 0)
1490-
printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result6, i, 0));
1531+
printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result7, i, 0));
14911532
else
1492-
printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result6, i, 0));
1533+
printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result7, i, 0));
14931534
if (i < inherits_count - 1)
14941535
appendPQExpBuffer(&buf, ",");
14951536

@@ -1516,6 +1557,7 @@ describeOneTableDetails(const char *schemaname,
15161557
PQclear(result4);
15171558
PQclear(result5);
15181559
PQclear(result6);
1560+
PQclear(result7);
15191561
}
15201562

15211563
printTable(title.data, headers,

0 commit comments

Comments
 (0)