pg_get_serial_sequence()

A system function to determine the name of a sequence used by a particular table column

pg_get_serial_sequence() is a system function to determine the name of a sequence used by a particular table column.

pg_get_serial_sequence() was added in PostgreSQL 8.0 (commit a0e842d8).

Examples

postgres=# CREATE TABLE foo (id SERIAL, val TEXT);
CREATE TABLE

postgres=# \d foo
                            Table "public.foo"
 Column |  Type   | Collation | Nullable |             Default             
--------+---------+-----------+----------+---------------------------------
 id     | integer |           | not null | nextval('foo_id_seq'::regclass)
 val    | text    |           |          | 

postgres=# SELECT pg_get_serial_sequence('foo', 'id');
 pg_get_serial_sequence 
------------------------
 public.foo_id_seq
(1 row)

postgres=# SELECT currval(pg_get_serial_sequence('foo','id'));
ERROR:  currval of sequence "foo_id_seq" is not yet defined in this session

postgres=# INSERT INTO foo VALUES(DEFAULT, 'bar');
INSERT 0 1

postgres=# SELECT currval(pg_get_serial_sequence('foo','id'));
 currval 
---------
       1
(1 row)

Categories

DDL, Sequences, System function