88 *
99 * Copyright (c) 2000-2008, PostgreSQL Global Development Group
1010 *
11- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.179 2008/07/14 23:13:04 momjian Exp $
11+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.180 2008/07/15 03:16:03 momjian Exp $
1212 */
1313#include "postgres_fe.h"
1414
@@ -811,7 +811,8 @@ describeOneTableDetails(const char *schemaname,
811811printTableContent cont ;
812812int i ;
813813char * view_def = NULL ;
814- char * headers [5 ];
814+ char * headers [6 ];
815+ char * * seq_values = NULL ;
815816char * * modifiers = NULL ;
816817char * * ptr ;
817818PQExpBufferData title ;
@@ -869,6 +870,35 @@ describeOneTableDetails(const char *schemaname,
869870tableinfo .tablespace = (pset .sversion >=80000 ) ?
870871atooid (PQgetvalue (res ,0 ,6 )) :0 ;
871872PQclear (res );
873+
874+ /*
875+ * This is used to get the values of a sequence and store it in an
876+ * array that will be used later.
877+ */
878+ if (tableinfo .relkind == 'S' )
879+ {
880+ PGresult * result ;
881+
882+ #define SEQ_NUM_COLS 10
883+ printfPQExpBuffer (& buf ,
884+ "SELECT sequence_name, last_value, \n"
885+ "start_value, increment_by, \n"
886+ "max_value, min_value, cache_value, \n"
887+ "log_cnt, is_cycled, is_called \n"
888+ "FROM \"%s\"" ,
889+ relationname );
890+
891+ result = PSQLexec (buf .data , false);
892+ if (!result )
893+ gotoerror_return ;
894+
895+ seq_values = pg_malloc_zero ((SEQ_NUM_COLS + 1 )* sizeof (* seq_values ));
896+
897+ for (i = 0 ;i < SEQ_NUM_COLS ;i ++ )
898+ seq_values [i ]= pg_strdup (PQgetvalue (result ,0 ,i ));
899+
900+ PQclear (result );
901+ }
872902
873903/* Get column info (index requires additional checks) */
874904printfPQExpBuffer (& buf ,"SELECT a.attname," );
@@ -932,7 +962,7 @@ describeOneTableDetails(const char *schemaname,
932962}
933963
934964/* Set the number of columns, and their names */
935- cols = 2 ;
965+ cols + =2 ;
936966headers [0 ]= gettext_noop ("Column" );
937967headers [1 ]= gettext_noop ("Type" );
938968
@@ -943,6 +973,9 @@ describeOneTableDetails(const char *schemaname,
943973modifiers = pg_malloc_zero ((numrows + 1 )* sizeof (* modifiers ));
944974}
945975
976+ if (tableinfo .relkind == 'S' )
977+ headers [cols ++ ]= gettext_noop ("Value" );
978+
946979if (verbose )
947980{
948981headers [cols ++ ]= gettext_noop ("Storage" );
@@ -980,7 +1013,11 @@ describeOneTableDetails(const char *schemaname,
9801013
9811014/* Type */
9821015printTableAddCell (& cont ,PQgetvalue (res ,i ,1 ), false);
983-
1016+
1017+ /* A special 'Value' column for sequences */
1018+ if (tableinfo .relkind == 'S' )
1019+ printTableAddCell (& cont ,seq_values [i ], false);
1020+
9841021/* Extra: not null and default */
9851022if (show_modifiers )
9861023{
@@ -1543,7 +1580,14 @@ describeOneTableDetails(const char *schemaname,
15431580termPQExpBuffer (& buf );
15441581termPQExpBuffer (& title );
15451582termPQExpBuffer (& tmpbuf );
1546-
1583+
1584+ if (tableinfo .relkind == 'S' )
1585+ {
1586+ for (ptr = seq_values ;* ptr ;ptr ++ )
1587+ free (* ptr );
1588+ free (seq_values );
1589+ }
1590+
15471591if (show_modifiers )
15481592{
15491593for (ptr = modifiers ;* ptr ;ptr ++ )