Skip to content

Commit 8516ce0

Browse files
committed
Have psql \d show the value of sequence columns.
Dickson S. Guedes
1 parent da0a9f1 commit 8516ce0

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

src/bin/psql/describe.c

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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,
811811
printTableContent cont;
812812
int i;
813813
char *view_def = NULL;
814-
char *headers[5];
814+
char *headers[6];
815+
char **seq_values = NULL;
815816
char **modifiers = NULL;
816817
char **ptr;
817818
PQExpBufferData title;
@@ -869,6 +870,35 @@ describeOneTableDetails(const char *schemaname,
869870
tableinfo.tablespace = (pset.sversion >= 80000) ?
870871
atooid(PQgetvalue(res, 0, 6)) : 0;
871872
PQclear(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+
goto error_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) */
874904
printfPQExpBuffer(&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;
936966
headers[0] = gettext_noop("Column");
937967
headers[1] = gettext_noop("Type");
938968

@@ -943,6 +973,9 @@ describeOneTableDetails(const char *schemaname,
943973
modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
944974
}
945975

976+
if (tableinfo.relkind == 'S')
977+
headers[cols++] = gettext_noop("Value");
978+
946979
if (verbose)
947980
{
948981
headers[cols++] = gettext_noop("Storage");
@@ -980,7 +1013,11 @@ describeOneTableDetails(const char *schemaname,
9801013

9811014
/* Type */
9821015
printTableAddCell(&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 */
9851022
if (show_modifiers)
9861023
{
@@ -1543,7 +1580,14 @@ describeOneTableDetails(const char *schemaname,
15431580
termPQExpBuffer(&buf);
15441581
termPQExpBuffer(&title);
15451582
termPQExpBuffer(&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+
15471591
if (show_modifiers)
15481592
{
15491593
for (ptr = modifiers; *ptr; ptr++)

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