How to Check Sequence Details in PostgreSQL

In PostgreSQL, sequences are used to create an auto-incremented series of integers. Sequences can be associated with a table’s column to define an auto-increment column. Various options/parameters are available in Postgres that can be used with the CREATE SEQUENCE command to create a customized sequence. However, while working with an already existing sequence, it's important to check its details before proceeding. To do this, a couple of approaches are used in Postgres.

This write-up will demonstrate how to check the sequence details in Postgres using:

- “\d” Command
- Information_schema

So, let’s start with the “\d” command.

How to Check Sequence Details Using “\d” Command?

“\d” is a metadata command that is usually used to describe a specific table, however, it can also be used to check the details of a particular sequence. For this purpose, the “\d” command must be followed by the sequence name:

\d seq_name;

Example: Describing a Sequence

First, let’s execute the “\ds” command to list the available sequences:

\ds;
img

Now, utilize the “\d” along with the sequence name, let’s say “example_seq”:

\d example_seq;
img

The stated command retrieves all the details of the selected sequence, including type, start value, minimum value, etc.

How to Check Sequence Details Using information_schema?

Another useful way of checking the sequence details is the “information_schema” which can be executed with the help of the SELECT command from any interface like CLI or GUI:

SELECT sequence_name, data_type, start_value, minimum_value, maximum_value, increment, cycle_option
FROM information_schema.sequences 
WHERE sequence_name='example_seq';

In the above query:

- All the columns to be fetched are specified within the SELECT statement.
- The “information_schema” is used with the “sequences” view in the FROM clause.
- While the name of the selected sequence is specified in the WHERE clause.

img

The output proved that the “information_schema” successfully retrieves all the sequence details.

Conclusion

In PostgreSQL, the “\d” command and “information_schema” are used to describe a specific sequence. Run the “\d” command followed by the sequence name to get sequence details using SQL Shell. While the “information_schema” can be used with the help of the SELECT command to get all the necessary details regarding the selected sequence. This post has explained a couple of methods to describe the sequence details in Postgres.