8
8
*
9
9
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
10
10
*
11
- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.185 2008/09/23 09:20:38 heikki Exp $
11
+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.186 2008/11/03 19:08:56 tgl Exp $
12
12
*/
13
13
#include "postgres_fe.h"
14
14
@@ -826,6 +826,7 @@ describeOneTableDetails(const char *schemaname,
826
826
PGresult * res = NULL ;
827
827
printTableOpt myopt = pset .popt .topt ;
828
828
printTableContent cont ;
829
+ bool printTableInitialized = false;
829
830
int i ;
830
831
char * view_def = NULL ;
831
832
char * headers [6 ];
@@ -887,9 +888,10 @@ describeOneTableDetails(const char *schemaname,
887
888
tableinfo .tablespace = (pset .sversion >=80000 ) ?
888
889
atooid (PQgetvalue (res ,0 ,6 )) :0 ;
889
890
PQclear (res );
891
+ res = NULL ;
890
892
891
893
/*
892
- *This is used to get the valuesof a sequence and storeit in an
894
+ *If it's a sequence, fetch its values and storeinto an
893
895
* array that will be used later.
894
896
*/
895
897
if (tableinfo .relkind == 'S' )
@@ -898,12 +900,14 @@ describeOneTableDetails(const char *schemaname,
898
900
899
901
#define SEQ_NUM_COLS 10
900
902
printfPQExpBuffer (& buf ,
901
- "SELECT sequence_name, last_value, \n"
902
- "start_value, increment_by, \n"
903
- "max_value, min_value, cache_value, \n"
904
- "log_cnt, is_cycled, is_called \n"
905
- "FROM \"%s\"" ,
906
- relationname );
903
+ "SELECT sequence_name, last_value,\n"
904
+ " start_value, increment_by,\n"
905
+ " max_value, min_value, cache_value,\n"
906
+ " log_cnt, is_cycled, is_called\n"
907
+ "FROM %s" ,
908
+ fmtId (schemaname ));
909
+ /* must be separate because fmtId isn't reentrant */
910
+ appendPQExpBuffer (& buf ,".%s" ,fmtId (relationname ));
907
911
908
912
result = PSQLexec (buf .data , false);
909
913
if (!result )
@@ -1000,6 +1004,7 @@ describeOneTableDetails(const char *schemaname,
1000
1004
}
1001
1005
1002
1006
printTableInit (& cont ,& myopt ,title .data ,cols ,numrows );
1007
+ printTableInitialized = true;
1003
1008
1004
1009
for (i = 0 ;i < cols ;i ++ )
1005
1010
printTableAddHeader (& cont ,headers [i ], true,'l' );
@@ -1030,12 +1035,8 @@ describeOneTableDetails(const char *schemaname,
1030
1035
1031
1036
/* Type */
1032
1037
printTableAddCell (& cont ,PQgetvalue (res ,i ,1 ), false);
1033
-
1034
- /* A special 'Value' column for sequences */
1035
- if (tableinfo .relkind == 'S' )
1036
- printTableAddCell (& cont ,seq_values [i ], false);
1037
-
1038
- /* Extra: not null and default */
1038
+
1039
+ /* Modifiers: not null and default */
1039
1040
if (show_modifiers )
1040
1041
{
1041
1042
resetPQExpBuffer (& tmpbuf );
@@ -1057,10 +1058,15 @@ describeOneTableDetails(const char *schemaname,
1057
1058
printTableAddCell (& cont ,modifiers [i ], false);
1058
1059
}
1059
1060
1061
+ /* Value: for sequences only */
1062
+ if (tableinfo .relkind == 'S' )
1063
+ printTableAddCell (& cont ,seq_values [i ], false);
1064
+
1060
1065
/* Storage and Description */
1061
1066
if (verbose )
1062
1067
{
1063
1068
char * storage = PQgetvalue (res ,i ,5 );
1069
+
1064
1070
/* these strings are literal in our syntax, so not translated. */
1065
1071
printTableAddCell (& cont , (storage [0 ]== 'p' ?"plain" :
1066
1072
(storage [0 ]== 'm' ?"main" :
@@ -1593,7 +1599,8 @@ describeOneTableDetails(const char *schemaname,
1593
1599
error_return :
1594
1600
1595
1601
/* clean up */
1596
- printTableCleanup (& cont );
1602
+ if (printTableInitialized )
1603
+ printTableCleanup (& cont );
1597
1604
termPQExpBuffer (& buf );
1598
1605
termPQExpBuffer (& title );
1599
1606
termPQExpBuffer (& tmpbuf );