Re: GETTING INFO FROM SEQUENCE OBJECTS

From: Brian Modra <brian(at)zwartberg(dot)com>
To: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: GETTING INFO FROM SEQUENCE OBJECTS
Date: 2009-11-03 06:15:36
Message-ID: 5a9699850911022215g62aba111t651c358fe25ab7d1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

2009/11/2 JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>:
> How do I use the functions to get data from sequence objects?
> For example, I need to know what is the value of a serial field after
> inserting a new record; the one assigned to the record just inserted.
>
> I have tried the psql interfase with the following:
> select currval('restaurant.ordenes_clave_seq');
> but I get a message telling me that "currval of sequence "ordenes_clave_seq"
> is not yet defined in this session.

Thats a "per session" function request...

however, if you describe the sequence, you'll see something like this:

\d dt_messages_sequence
Sequence "public.dt_messages_sequence"
Column | Type
---------------+---------
sequence_name | name
last_value | bigint
increment_by | bigint
max_value | bigint
min_value | bigint
cache_value | bigint
log_cnt | bigint
is_cycled | boolean
is_called | boolean

and then you can get the current value:

trackerData=> select last_value from dt_messages_sequence;
last_value
------------
1208
(1 row)

... however, if the sequence is being used in the session, then you
must use currval rather than the select I described above... because
it is thread-safe. This mechanism I described above may give you
something less than the current value if the sequence is in use.

>
> Please advice.
>
> Respectfully,
> Jorge Maldonado

--
Brian Modra Land line: +27 23 5411 462
Mobile: +27 79 69 77 082
5 Jan Louw Str, Prince Albert, 6930
Postal: P.O. Box 2, Prince Albert 6930
South Africa
http://www.zwartberg.com/

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message A. Kretschmer 2009-11-03 06:22:40 Re: GETTING INFO FROM SEQUENCE OBJECTS
Previous Message Tom Lane 2009-11-03 04:35:54 Re: Install problems for postgresql-8.4.1