Skip to content

Commit b6335b4

Browse files
committed
Add tablespace location display for psql \d.
Gavin Sherry
1 parent e47cbb3 commit b6335b4

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.116 2004/06/18 06:13:05 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.117 2004/07/12 20:41:08 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -784,7 +784,7 @@ testdb=>
784784
<para>
785785
For each relation (table, view, index, or sequence) matching the
786786
<replaceable class="parameter">pattern</replaceable>, show all
787-
columns, their types, and any special
787+
columns, their types, the tablespace (if not the default) and any special
788788
attributes such as <literal>NOT NULL</literal> or defaults, if
789789
any. Associated indexes, constraints, rules, and triggers are
790790
also shown, as is the view definition if the relation is a view.

src/bin/psql/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.37 2004/07/11 13:29:15 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.38 2004/07/12 20:41:13 momjian Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
@@ -20,6 +20,9 @@
2020
#define psql_assert(p)
2121
#endif
2222

23+
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
24+
25+
2326
/*
2427
* Safer versions of some standard C library functions. If an
2528
* out-of-memory condition occurs, these functions will bail out

src/bin/psql/describe.c

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.99 2004/06/18 06:14:04 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.100 2004/07/12 20:41:13 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -39,6 +39,9 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern,
3939
const char *schemavar, const char *namevar,
4040
const char *altnamevar, const char *visibilityrule);
4141

42+
static void add_tablespace_footer(char relkind, Oid tablespace,
43+
char **footers, int *count, PQExpBufferData buf);
44+
4245
/*----------------
4346
* Handlers for various slash commands displaying some sort of list
4447
* of things in the database.
@@ -682,6 +685,7 @@ describeOneTableDetails(const char *schemaname,
682685
bool hasindex;
683686
bool hasrules;
684687
bool hasoids;
688+
Oid tablespace;
685689
} tableinfo;
686690
bool show_modifiers = false;
687691
bool retval;
@@ -694,7 +698,8 @@ describeOneTableDetails(const char *schemaname,
694698

695699
/* Get general table info */
696700
printfPQExpBuffer(&buf,
697-
"SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, relhasoids\n"
701+
"SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules, \n"
702+
"relhasoids, reltablespace \n"
698703
"FROM pg_catalog.pg_class WHERE oid = '%s'",
699704
oid);
700705
res = PSQLexec(buf.data, false);
@@ -717,6 +722,7 @@ describeOneTableDetails(const char *schemaname,
717722
tableinfo.hasindex = strcmp(PQgetvalue(res, 0, 0), "t") == 0;
718723
tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
719724
tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
725+
tableinfo.tablespace = atooid(PQgetvalue(res, 0, 6));
720726
PQclear(res);
721727

722728
headers[0] = _("Column");
@@ -897,6 +903,7 @@ describeOneTableDetails(const char *schemaname,
897903
char *indamname = PQgetvalue(result, 0, 3);
898904
char *indtable = PQgetvalue(result, 0, 4);
899905
char *indpred = PQgetvalue(result, 0, 5);
906+
int count_footers = 0;
900907

901908
if (strcmp(indisprimary, "t") == 0)
902909
printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, "));
@@ -916,9 +923,12 @@ describeOneTableDetails(const char *schemaname,
916923
if (strcmp(indisclustered, "t") == 0)
917924
appendPQExpBuffer(&tmpbuf, _(", CLUSTER"));
918925

919-
footers = pg_malloc_zero(2 * sizeof(*footers));
920-
footers[0] = pg_strdup(tmpbuf.data);
921-
footers[1] = NULL;
926+
footers = pg_malloc_zero(4 * sizeof(*footers));
927+
footers[count_footers++] = pg_strdup(tmpbuf.data);
928+
add_tablespace_footer(tableinfo.relkind, tableinfo.tablespace,
929+
footers, &count_footers, tmpbuf);
930+
footers[count_footers] = NULL;
931+
922932
}
923933

924934
PQclear(result);
@@ -1103,7 +1113,7 @@ describeOneTableDetails(const char *schemaname,
11031113
else
11041114
inherits_count = PQntuples(result6);
11051115

1106-
footers = pg_malloc_zero((index_count + check_count + rule_count + trigger_count + foreignkey_count + inherits_count + 6)
1116+
footers = pg_malloc_zero((index_count + check_count + rule_count + trigger_count + foreignkey_count + inherits_count + 7 + 1)
11071117
* sizeof(*footers));
11081118

11091119
/* print indexes */
@@ -1236,6 +1246,8 @@ describeOneTableDetails(const char *schemaname,
12361246
footers[count_footers++] = pg_strdup(buf.data);
12371247
}
12381248

1249+
add_tablespace_footer(tableinfo.relkind, tableinfo.tablespace,
1250+
footers, &count_footers, buf);
12391251
/* end of list marker */
12401252
footers[count_footers] = NULL;
12411253

@@ -1287,6 +1299,40 @@ describeOneTableDetails(const char *schemaname,
12871299
}
12881300

12891301

1302+
static void
1303+
add_tablespace_footer(char relkind, Oid tablespace, char **footers,
1304+
int *count, PQExpBufferData buf)
1305+
{
1306+
/* relkinds for which we support tablespaces */
1307+
if(relkind == 'r' || relkind == 'i')
1308+
{
1309+
/*
1310+
* We ignore the database default tablespace so that users not
1311+
* using tablespaces don't need to know about them.
1312+
*/
1313+
if(tablespace != 0)
1314+
{
1315+
PGresult *result1 = NULL;
1316+
printfPQExpBuffer(&buf, "SELECT spcname FROM pg_tablespace \n"
1317+
"WHERE oid = '%u';", tablespace);
1318+
result1 = PSQLexec(buf.data, false);
1319+
if (!result1)
1320+
return;
1321+
/* Should always be the case, but.... */
1322+
if(PQntuples(result1) > 0)
1323+
{
1324+
printfPQExpBuffer(&buf, _("Tablespace:"));
1325+
footers[(*count)++] = pg_strdup(buf.data);
1326+
printfPQExpBuffer(&buf, _(" \"%s\""),
1327+
PQgetvalue(result1, 0, 0));
1328+
1329+
footers[(*count)++] = pg_strdup(buf.data);
1330+
}
1331+
PQclear(result1);
1332+
}
1333+
}
1334+
}
1335+
12901336
/*
12911337
* \du
12921338
*

src/bin/psql/large_obj.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.31 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.32 2004/07/12 20:41:13 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "large_obj.h"
@@ -16,9 +16,6 @@
1616
#include "print.h"
1717

1818

19-
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
20-
21-
2219
/*
2320
* Prepare to do a large-object operation. We *must* be inside a transaction
2421
* block for all these operations, so start one if needed.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy