A sequence in Postgres is nothing more than an ordered list of integers. In databases, sequences are used to generate a sequence/series of numeric values. However, while working with databases, developers may encounter a situation where they need to get the list of available sequences. For this purpose, Postgres supports various approaches, such as the “\ds” command, “information_schema”, etc.
This blog post will explain how to show the list of available sequences in Postgres:
- Using \ds Command
- Using information_schema
- Using pg_class
Let’s begin with the “\ds” command.
How Do I List Sequences in Postgres Using \ds Command?
“\ds” is a psql supported command that helps us get the list of available sequences:
\ds
The output shows the list of sequences containing sequence name, schema name, and owner.
How Do I List Sequences in Postgres Using information_schema?
In PostgreSQL, the “information_schema” is an inbuilt schema that assists us in getting information regarding database objects. The “information_schema” can be utilized with the “sequences” view to get the list of available sequences:
SELECT sequence_name FROM information_schema.sequences;
Here, “sequence_name” is a column that keeps the names of available sequences:
The output shows that the information_schema retrieves the list of available sequences. Moreover, you can specify the column names like “start_value”, “minimum_value”, and “maximum_value” to get additional information like the sequence’s start value, minimum value, maximum value, etc.
How Do I List Sequences in Postgres Using pg_class?
The "pg_class" catalog, which stores the information about the database objects, can also be used to find the list of available sequences:
SELECT relname FROM pg_class WHERE relkind = 'S';
The above-stated command will retrieve all the sequences available in the current database:
The output proved that the “pg_class” has successfully retrieved the list of available sequences.
Conclusion
In PostgreSQL, the “\ds” command, “information_schema”, and “pg_class” catalog are used to get the list of available sequences. The “\ds” can only be executed from the psql, however, the “pg_class” catalog and “information_schema” can be used along with the SELECT statement from any interface/tool like pgAdmin, psql, etc. This post has discussed various ways to get the list of available sequences in PostgreSQL.