WikiGalaxy

Personalize

PostgreSQL Serial

What is PostgreSQL Serial?

The SERIAL type in PostgreSQL is a convenient way to define an auto-incrementing column. It is essentially a shorthand for creating a sequence object and setting a default value for the column.

How SERIAL Works

When you define a column with SERIAL, PostgreSQL automatically creates a sequence and sets the column's default value to the next value of the sequence.

Types of SERIAL

PostgreSQL provides three types of SERIAL: SERIAL, BIGSERIAL, and SMALLSERIAL. These correspond to different integer sizes.

Use Cases

The SERIAL type is commonly used for primary keys, where a unique identifier is needed for each row in a table.


CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  username VARCHAR(50) NOT NULL
);
    

Benefits of Using SERIAL

Using SERIAL simplifies the process of creating unique identifiers and reduces the likelihood of errors associated with manual sequence management.

Difference Between SERIAL and Identity Columns

While SERIAL uses sequences, identity columns introduced in PostgreSQL 10 offer a more standardized SQL way of handling auto-incrementing fields.

Console Output:

Table "users" created successfully.

Creating a Table with BIGSERIAL

Why Use BIGSERIAL?

The BIGSERIAL type is used when you anticipate needing more than 2 billion unique identifiers, which is the limit for the standard SERIAL type.

Defining a BIGSERIAL Column

To define a column as BIGSERIAL, simply replace SERIAL with BIGSERIAL in your table definition.


CREATE TABLE orders (
  order_id BIGSERIAL PRIMARY KEY,
  order_date DATE NOT NULL
);
    

Advantages of BIGSERIAL

BIGSERIAL provides a larger range for identifiers, making it suitable for applications with high insertion rates.

Console Output:

Table "orders" created successfully.

Using SMALLSERIAL

What is SMALLSERIAL?

The SMALLSERIAL type is used for smaller ranges of numbers, up to 32,767, making it ideal for small datasets.

Defining a SMALLSERIAL Column

To use SMALLSERIAL, declare the column with this type in your table definition.


CREATE TABLE small_data (
  id SMALLSERIAL PRIMARY KEY,
  description TEXT
);
    

Benefits of SMALLSERIAL

SMALLSERIAL uses less storage space, which can be beneficial for optimizing database performance in certain scenarios.

Console Output:

Table "small_data" created successfully.

Managing Sequences Manually

Creating a Sequence

Although SERIAL handles sequences automatically, you can manually create sequences using the CREATE SEQUENCE command.


CREATE SEQUENCE custom_seq
  START 1
  INCREMENT 1;
    

Using a Custom Sequence

Once a sequence is created, it can be used to set default values for columns, similar to how SERIAL works.

Console Output:

Sequence "custom_seq" created successfully.

Altering SERIAL Columns

Changing a SERIAL Column to BIGSERIAL

If you need to change a SERIAL column to BIGSERIAL, you must manually alter the column and update its sequence.


ALTER TABLE users
  ALTER COLUMN id TYPE BIGINT,
  ALTER COLUMN id SET DEFAULT nextval('users_id_seq'::regclass);
    

Considerations When Altering Columns

Altering a column type can have implications on existing data and indexes, so it should be done with caution.

Console Output:

Table "users" altered successfully.

Dropping a SERIAL Column

Steps to Drop a SERIAL Column

Dropping a SERIAL column involves removing the column and optionally the associated sequence if it's no longer needed.


ALTER TABLE users
  DROP COLUMN id;
DROP SEQUENCE IF EXISTS users_id_seq;
    

Impact of Dropping a Column

Removing a column will result in data loss for that column, so ensure that any necessary data is backed up or migrated before performing this operation.

Console Output:

Column "id" and sequence "users_id_seq" dropped successfully.

SERIAL vs. Identity Columns

Understanding Identity Columns

Identity columns, introduced in PostgreSQL 10, provide a SQL-standard way of defining auto-incrementing columns, offering an alternative to SERIAL.


CREATE TABLE products (
  product_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  product_name VARCHAR(100)
);
    

Advantages of Identity Columns

Identity columns are more flexible in terms of sequence management and are part of the SQL standard, making them preferable for new applications.

Console Output:

Table "products" created successfully.

Resetting a SERIAL Sequence

Why Reset a Sequence?

Resetting a sequence might be necessary if you want to restart the numbering, for instance, after deleting all rows from a table.


ALTER SEQUENCE users_id_seq RESTART WITH 1;
    

Considerations for Resetting

Resetting a sequence should be done with caution as it can lead to potential conflicts with existing data if not handled properly.

Console Output:

Sequence "users_id_seq" reset successfully.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025