Monday, July 14, 2008

Re: [HACKERS] TODO item: Have psql show current values for a sequence

Index: src/bin/psql/describe.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/describe.c,v
retrieving revision 1.179
diff -c -c -r1.179 describe.c
*** src/bin/psql/describe.c 14 Jul 2008 23:13:04 -0000 1.179
--- src/bin/psql/describe.c 15 Jul 2008 03:06:24 -0000
***************
*** 811,817 ****
printTableContent cont;
int i;
char *view_def = NULL;
! char *headers[5];
char **modifiers = NULL;
char **ptr;
PQExpBufferData title;
--- 811,818 ----
printTableContent cont;
int i;
char *view_def = NULL;
! char *headers[6];
! char **seq_values = NULL;
char **modifiers = NULL;
char **ptr;
PQExpBufferData title;
***************
*** 869,874 ****
--- 870,904 ----
tableinfo.tablespace = (pset.sversion >= 80000) ?
atooid(PQgetvalue(res, 0, 6)) : 0;
PQclear(res);
+
+ /*
+ * This is used to get the values of a sequence and store it in an
+ * array that will be used later.
+ */
+ if (tableinfo.relkind == 'S')
+ {
+ PGresult *result;
+
+ #define SEQ_NUM_COLS 10
+ printfPQExpBuffer(&buf,
+ "SELECT sequence_name, last_value, \n"
+ " start_value, increment_by, \n"
+ " max_value, min_value, cache_value, \n"
+ " log_cnt, is_cycled, is_called \n"
+ "FROM \"%s\"",
+ relationname);
+
+ result = PSQLexec(buf.data, false);
+ if (!result)
+ goto error_return;
+
+ seq_values = pg_malloc_zero((SEQ_NUM_COLS+1) * sizeof(*seq_values));
+
+ for (i = 0; i < SEQ_NUM_COLS; i++)
+ seq_values[i] = pg_strdup(PQgetvalue(result, 0, i));
+
+ PQclear(result);
+ }

/* Get column info (index requires additional checks) */
printfPQExpBuffer(&buf, "SELECT a.attname,");
***************
*** 932,938 ****
}

/* Set the number of columns, and their names */
! cols = 2;
headers[0] = gettext_noop("Column");
headers[1] = gettext_noop("Type");

--- 962,968 ----
}

/* Set the number of columns, and their names */
! cols += 2;
headers[0] = gettext_noop("Column");
headers[1] = gettext_noop("Type");

***************
*** 943,948 ****
--- 973,981 ----
modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
}

+ if (tableinfo.relkind == 'S')
+ headers[cols++] = gettext_noop("Value");
+
if (verbose)
{
headers[cols++] = gettext_noop("Storage");
***************
*** 980,986 ****

/* Type */
printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
!
/* Extra: not null and default */
if (show_modifiers)
{
--- 1013,1023 ----

/* Type */
printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
!
! /* A special 'Value' column for sequences */
! if (tableinfo.relkind == 'S')
! printTableAddCell(&cont, seq_values[i], false);
!
/* Extra: not null and default */
if (show_modifiers)
{
***************
*** 1543,1549 ****
termPQExpBuffer(&buf);
termPQExpBuffer(&title);
termPQExpBuffer(&tmpbuf);
!
if (show_modifiers)
{
for (ptr = modifiers; *ptr; ptr++)
--- 1580,1593 ----
termPQExpBuffer(&buf);
termPQExpBuffer(&title);
termPQExpBuffer(&tmpbuf);
!
! if (tableinfo.relkind == 'S')
! {
! for (ptr = seq_values; *ptr; ptr++)
! free(*ptr);
! free(seq_values);
! }
!
if (show_modifiers)
{
for (ptr = modifiers; *ptr; ptr++)
Wow. I adjusted the patch slightly and applied it; the updated version
is attached. We have been waiting for this to be done for quite some
time. Thanks.

---------------------------------------------------------------------------


Dickson S. Guedes wrote:
> Hi all,
>
> These patch implements the TODO item: Have psql show current values
> for a sequence.
> Comments are welcome.
>
> * Credits
>
> The original patch were developed by Euler Taveira de Oliveira
> <euler@timbira.com>
> but how he is a little busy, he sends it to me and I made some changes
> to satisfy
> the TODO item above.
>
> * Discussions
>
> http://archives.postgresql.org/pgsql-hackers/2007-12/msg00102.php
> http://archives.postgresql.org/pgsql-hackers/2007-12/msg00605.php
>
> * Outputs
>
> # \d foo_bar_seq
> Sequence "public.foo_bar_seq"
> +---------------+---------+---------------------+
> | Column | Type | Value |
> +---------------+---------+---------------------+
> | sequence_name | name | foo_bar_seq |
> | last_value | bigint | 11 |
> | start_value | bigint | 1 |
> | increment_by | bigint | 1 |
> | max_value | bigint | 9223372036854775807 |
> | min_value | bigint | 1 |
> | cache_value | bigint | 1 |
> | log_cnt | bigint | 31 |
> | is_cycled | boolean | f |
> | is_called | boolean | t |
> +---------------+---------+---------------------+
>
> # \d+ foo_bar_seq
> Sequence "public.foo_bar_seq"
> +---------------+---------+---------------------+-------------+
> | Column | Type | Value | Description |
> +---------------+---------+---------------------+-------------+
> | sequence_name | name | foo_bar_seq | |
> | last_value | bigint | 11 | |
> | start_value | bigint | 1 | |
> | increment_by | bigint | 1 | |
> | max_value | bigint | 9223372036854775807 | |
> | min_value | bigint | 1 | |
> | cache_value | bigint | 1 | |
> | log_cnt | bigint | 31 | |
> | is_cycled | boolean | f | |
> | is_called | boolean | t | |
> +---------------+---------+---------------------+-------------+
>
> --
> []s
> Dickson S. Guedes
> --------------------------------------
> Projeto Colmeia - Curitiba - PR
> +55 (41) 3254-7130 ramal: 27
> http://makeall.wordpress.com/
> http://pgcon.postgresql.org.br/
> http://planeta.postgresql.org.br/

[ Attachment, skipping... ]

>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce@momjian.us>

http://momjian.us

EnterpriseDB

http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

1 comment:

UsamaAk said...

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! PostgreSQL trigger