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,
3939const char * schemavar ,const char * namevar ,
4040const 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,
682685bool hasindex ;
683686bool hasrules ;
684687bool hasoids ;
688+ Oid tablespace ;
685689}tableinfo ;
686690bool show_modifiers = false;
687691bool retval ;
@@ -694,7 +698,8 @@ describeOneTableDetails(const char *schemaname,
694698
695699/* Get general table info */
696700printfPQExpBuffer (& 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'" ,
699704oid );
700705res = PSQLexec (buf .data , false);
@@ -717,6 +722,7 @@ describeOneTableDetails(const char *schemaname,
717722tableinfo .hasindex = strcmp (PQgetvalue (res ,0 ,0 ),"t" )== 0 ;
718723tableinfo .hasrules = strcmp (PQgetvalue (res ,0 ,4 ),"t" )== 0 ;
719724tableinfo .hasoids = strcmp (PQgetvalue (res ,0 ,5 ),"t" )== 0 ;
725+ tableinfo .tablespace = atooid (PQgetvalue (res ,0 ,6 ));
720726PQclear (res );
721727
722728headers [0 ]= _ ("Column" );
@@ -897,6 +903,7 @@ describeOneTableDetails(const char *schemaname,
897903char * indamname = PQgetvalue (result ,0 ,3 );
898904char * indtable = PQgetvalue (result ,0 ,4 );
899905char * indpred = PQgetvalue (result ,0 ,5 );
906+ int count_footers = 0 ;
900907
901908if (strcmp (indisprimary ,"t" )== 0 )
902909printfPQExpBuffer (& tmpbuf ,_ ("PRIMARY KEY, " ));
@@ -916,9 +923,12 @@ describeOneTableDetails(const char *schemaname,
916923if (strcmp (indisclustered ,"t" )== 0 )
917924appendPQExpBuffer (& 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
924934PQclear (result );
@@ -1103,7 +1113,7 @@ describeOneTableDetails(const char *schemaname,
11031113else
11041114inherits_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,
12361246footers [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 */
12401252footers [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 *