Thread: How to find all current sequence IDs

How to find all current sequence IDs

From
"Campbell, Lance"
Date:

Is there a way to get a list of all sequences, the schema it is used in, and the current sequence number in use?

 

The below SQL will give me the schema and sequences. It does not give me the current sequence number in use.

 

SELECT sequence_schema, sequence_name

FROM information_schema.sequences

ORDER BY sequence_name ;

 

Thanks,

 

Lance Campbell

University of Illinois

Re: How to find all current sequence IDs

From
Tom Lane
Date:
"Campbell, Lance" <lance@illinois.edu> writes:
> Is there a way to get a list of all sequences, the schema it is used in, and the current sequence number in use?

Try something like

select relname, relnamespace::regnamespace, pg_sequence_last_value(oid)
from pg_class where relkind = 'S';

            regards, tom lane



Re: How to find all current sequence IDs

From
Nisarg Patel
Date:
This has worked for me in the past:
select schemaname, sequencename, last_value
from pg_sequences;

Thank you,
Nisarg

On Wed, Feb 21, 2024, 5:14 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"Campbell, Lance" <lance@illinois.edu> writes:
> Is there a way to get a list of all sequences, the schema it is used in, and the current sequence number in use?

Try something like

select relname, relnamespace::regnamespace, pg_sequence_last_value(oid)
from pg_class where relkind = 'S';

                        regards, tom lane


RE: How to find all current sequence IDs

From
"Campbell, Lance"
Date:

That worked great. Thanks so much.

 

From: Nisarg Patel <er.nisarg@gmail.com>
Sent: Wednesday, February 21, 2024 5:17 PM
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Campbell, Lance <lance@illinois.edu>; pgsql-sql@lists.postgresql.org
Subject: Re: How to find all current sequence IDs

 

This has worked for me in the past:

select schemaname, sequencename, last_value

from pg_sequences;

 

Thank you,

Nisarg

On Wed, Feb 21, 2024, 5:14PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Campbell, Lance" <lance@illinois.edu> writes:
> Is there a way to get a list of all sequences, the schema it is used in, and the current sequence number in use?

Try something like

select relname, relnamespace::regnamespace, pg_sequence_last_value(oid)
from pg_class where relkind = 'S';

                        regards, tom lane