33 *
44 * Copyright (c) 2000-2005, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.116 2005/06/1402:57:41 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.117 2005/06/1423:59:31 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "describe.h"
@@ -37,8 +37,8 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern,
3737const char * schemavar ,const char * namevar ,
3838const char * altnamevar ,const char * visibilityrule );
3939
40- static void add_tablespace_footer (char relkind ,Oid tablespace ,
41- char * * footers , int * count ,PQExpBufferData buf );
40+ static bool add_tablespace_footer (char relkind ,Oid tablespace ,char * * footers ,
41+ int * count ,PQExpBufferData buf , bool newline );
4242
4343/*----------------
4444 * Handlers for various slash commands displaying some sort of list
@@ -942,7 +942,7 @@ describeOneTableDetails(const char *schemaname,
942942footers = pg_malloc_zero (4 * sizeof (* footers ));
943943footers [count_footers ++ ]= pg_strdup (tmpbuf .data );
944944add_tablespace_footer (tableinfo .relkind ,tableinfo .tablespace ,
945- footers ,& count_footers ,tmpbuf );
945+ footers ,& count_footers ,tmpbuf , true );
946946footers [count_footers ]= NULL ;
947947
948948}
@@ -1022,7 +1022,7 @@ describeOneTableDetails(const char *schemaname,
10221022{
10231023printfPQExpBuffer (& buf ,
10241024"SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, "
1025- "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n"
1025+ "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), c2.reltablespace \n"
10261026"FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
10271027"WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
10281028"ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname" ,
@@ -1142,6 +1142,7 @@ describeOneTableDetails(const char *schemaname,
11421142{
11431143const char * indexdef ;
11441144const char * usingpos ;
1145+ PQExpBufferData tmpbuf ;
11451146
11461147/* Output index name */
11471148printfPQExpBuffer (& buf ,_ (" \"%s\"" ),
@@ -1165,6 +1166,22 @@ describeOneTableDetails(const char *schemaname,
11651166if (strcmp (PQgetvalue (result1 ,i ,3 ),"t" )== 0 )
11661167appendPQExpBuffer (& buf ," CLUSTER" );
11671168
1169+ /* Print tablespace of the index on the same line */
1170+ count_footers += 1 ;
1171+ initPQExpBuffer (& tmpbuf );
1172+ if (add_tablespace_footer ('i' ,
1173+ atooid (PQgetvalue (result1 ,i ,5 )),
1174+ footers ,& count_footers ,tmpbuf , false))
1175+ {
1176+ appendPQExpBuffer (& buf ,", " );
1177+ appendPQExpBuffer (& buf ,tmpbuf .data );
1178+
1179+ count_footers -= 2 ;
1180+ }
1181+ else
1182+ count_footers -= 1 ;
1183+ termPQExpBuffer (& tmpbuf );
1184+
11681185footers [count_footers ++ ]= pg_strdup (buf .data );
11691186}
11701187}
@@ -1265,7 +1282,7 @@ describeOneTableDetails(const char *schemaname,
12651282}
12661283
12671284add_tablespace_footer (tableinfo .relkind ,tableinfo .tablespace ,
1268- footers ,& count_footers ,buf );
1285+ footers ,& count_footers ,buf , true );
12691286/* end of list marker */
12701287footers [count_footers ]= NULL ;
12711288
@@ -1317,9 +1334,13 @@ describeOneTableDetails(const char *schemaname,
13171334}
13181335
13191336
1320- static void
1337+ /*
1338+ * Return true if the relation uses non default tablespace;
1339+ * otherwise return false
1340+ */
1341+ static bool
13211342add_tablespace_footer (char relkind ,Oid tablespace ,char * * footers ,
1322- int * count ,PQExpBufferData buf )
1343+ int * count ,PQExpBufferData buf , bool newline )
13231344{
13241345/* relkinds for which we support tablespaces */
13251346if (relkind == 'r' || relkind == 'i' )
@@ -1336,17 +1357,23 @@ add_tablespace_footer(char relkind, Oid tablespace, char **footers,
13361357"WHERE oid = '%u';" ,tablespace );
13371358result1 = PSQLexec (buf .data , false);
13381359if (!result1 )
1339- return ;
1360+ return false ;
13401361/* Should always be the case, but.... */
13411362if (PQntuples (result1 )> 0 )
13421363{
1343- printfPQExpBuffer (& buf ,_ ("Tablespace: \"%s\"" ),
1344- PQgetvalue (result1 ,0 ,0 ));
1364+ printfPQExpBuffer (& buf ,
1365+ newline ?_ ("Tablespace: \"%s\"" ):_ ("tablespace \"%s\"" ),
1366+ PQgetvalue (result1 ,0 ,0 ));
1367+
13451368footers [(* count )++ ]= pg_strdup (buf .data );
13461369}
13471370PQclear (result1 );
1371+
1372+ return true;
13481373}
13491374}
1375+
1376+ return false;
13501377}
13511378
13521379/*