Skip to main content
Contact Us
Blog

Understanding Sequences in PostgreSQL

By Yogesh MishraNovember 12, 20262 min read

Sequences in PostgreSQL

In PostgreSQL, sequences are powerful objects designed to generate unique, ordered numeric values, commonly used for auto-generating primary key values within tables.

They offer a robust mechanism for managing incremental values, allowing for customization of starting points, increment steps, caching, and more. Understanding the intricacies of sequences enables efficient and controlled generation of identifiers within a database schema. Sequences are structured using various parameters like starting values, incrementation rules, and caching strategies, providing flexibility to tailor their behavior to specific database requirements. Leveraging sequences simplifies the process of assigning unique identifiers to records, ensuring data integrity and facilitating efficient retrieval.

In this blog post we’ll be understanding sequences more:

  • Create Sequence: CREATE SEQUENCE actor_id_seq; This statement creates a sequence named actor_actor_id_seq. By default, sequences start at 1 and increment by 1.

Screenshot of pgAdmin running CREATE SEQUENCE actor_id_seq successfully

Screenshot of pgAdmin returning 1 from nextval on actor_id_seq

Screenshot of pgAdmin showing an error because the actor_id_seq relation already exists

Screenshot of pgAdmin running DROP SEQUENCE actor_id_seq successfully

CREATE SEQUENCE actor_id_seq
    START WITH 10000
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 10;

Explanation of the parameters used:

  • actor_id_seq: This is the name of the sequence.
  • START WITH 10000: Specifies the starting value of the sequence.
  • INCREMENT BY 1: Specifies the increment value. In this case, it increments by 1.
  • NO MINVALUE: Indicates that there is no minimum value for the sequence.
  • NO MAXVALUE: Indicates that there is no maximum value for the sequence.
  • CACHE 10: Sets the number of sequence numbers that should be preallocated and stored in memory for faster access.
  • Using the Sequence:
CREATE TABLE actor_master (
    id INTEGER PRIMARY KEY DEFAULT nextval('actor_id_seq'),
    name VARCHAR(50)
);

Screenshot of pgAdmin creating actor_id_seq starting at 10000 with increment 1 and cache 10

Screenshot of pgAdmin creating the actor_master table with an id that defaults to nextval of the sequence

Screenshot of pgAdmin inserting John Doe into actor_master

Screenshot of pgAdmin selecting from actor_master, showing John Doe with id 10001

  • Retrieving Sequence Values:
SELECT nextval('actor_id_seq');

Screenshot of pgAdmin returning 10002 from nextval on the sequence

  • Altering a Sequence:
ALTER SEQUENCE actor_id_seq RESTART WITH 10000;

Screenshot of pgAdmin running ALTER SEQUENCE actor_id_seq RESTART WITH 10000

Screenshot of pgAdmin returning 10000 from nextval after the sequence restart

Conclusion:

In the realm of PostgreSQL, sequences serve as fundamental tools for generating ordered numeric values, particularly beneficial for managing primary keys within database tables. By customizing sequences with parameters like starting points, increment values, and caching configurations, database administrators can fine-tune the behavior of these sequences to suit specific application needs.

Whether it's creating, utilizing, retrieving, or altering sequences, their functionality empowers efficient data management and reliable generation of unique identifiers within the database schema.

Understanding sequences equips developers with the necessary tools to optimize database structures and streamline data handling processes.

Share:
0
+0

Related Articles