Site icon JD Bots

Identity vs Serial in PostgreSQL: Which to Use for IDs

Featured card for Identity vs Serial in PostgreSQL, comparing identity (SQL standard, GENERATED ALWAYS, refuses typed IDs) with serial (older shorthand, a sequence default, allows duplicates).

Identity vs serial in PostgreSQL.

Both give a column numbers that count up by themselves: 1, 2, 3. An identity column is the SQL-standard way, and serial is PostgreSQL’s older shorthand. Here’s identity vs serial in PostgreSQL, and why an identity column is the better choice for IDs.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

Syntax

column_name integer GENERATED ALWAYS AS IDENTITY
column_name integer GENERATED BY DEFAULT AS IDENTITY
column_name serial

See Identity Columns and Serial Types in the PostgreSQL documentation.

The Example

A help desk numbers its tickets. It tries both ways, in two small tables side by side.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#

CREATE DATABASE helpdesk;

Output

CREATE DATABASE

postgres=#

\c helpdesk

Output

You are now connected to database "helpdesk" as user "postgres".

Step 2: Create Both Tables

helpdesk=#

CREATE TABLE tickets_serial (id serial, subject text);

Output

CREATE TABLE

helpdesk=#

CREATE TABLE tickets_identity (id integer GENERATED ALWAYS AS IDENTITY, subject text);

Output

CREATE TABLE

Step 3: Add a Ticket to Each

Leave out the id, and PostgreSQL fills it in:

helpdesk=#

INSERT INTO tickets_serial (subject) VALUES ('Printer is jammed');

Output

INSERT 0 1

helpdesk=#

INSERT INTO tickets_identity (subject) VALUES ('Printer is jammed');

Output

INSERT 0 1

Step 4: Type an ID Yourself

Now give each table an id of your own, 1, which is already taken. The serial table accepts it:

helpdesk=#

INSERT INTO tickets_serial (id, subject) VALUES (1, 'Screen is blank');

Output

INSERT 0 1

The identity table refuses it:

helpdesk=#

INSERT INTO tickets_identity (id, subject) VALUES (1, 'Screen is blank');

Output

ERROR:  cannot insert a non-DEFAULT value into column "id"
DETAIL:  Column "id" is an identity column defined as GENERATED ALWAYS.
HINT:  Use OVERRIDING SYSTEM VALUE to override.

Look at both tables:

helpdesk=#

SELECT * FROM tickets_serial;

Output

 id |      subject
----+-------------------
  1 | Printer is jammed
  1 | Screen is blank
(2 rows)

helpdesk=#

SELECT * FROM tickets_identity;

Output

 id |      subject
----+-------------------
  1 | Printer is jammed
(1 row)

The serial table now has two tickets numbered 1. GENERATED ALWAYS stopped that mistake before it happened.

Step 5: See How Each Column Is Defined

psql’s \d command describes a table:

helpdesk=#

\d tickets_serial

Output

                             Table "public.tickets_serial"
 Column  |  Type   | Collation | Nullable |                  Default
---------+---------+-----------+----------+--------------------------------------------
 id      | integer |           | not null | nextval('tickets_serial_id_seq'::regclass)
 subject | text    |           |          |

helpdesk=#

\d tickets_identity

Output

                     Table "public.tickets_identity"
 Column  |  Type   | Collation | Nullable |           Default
---------+---------+-----------+----------+------------------------------
 id      | integer |           | not null | generated always as identity
 subject | text    |           |          |

The serial column is an ordinary integer whose default calls nextval() on a sequence, so anything can overwrite it. The identity column is marked generated always as identity.

Which One to Use

Clean Up

To remove the example, connect to another database and drop the test database:

helpdesk=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE helpdesk;

Output

DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Typing your own IDs into a serial columnNothing stops it, so two rows can share a number, as the two tickets numbered 1 showed.Use GENERATED ALWAYS AS IDENTITY, and leave the column out of your inserts.
Expecting an identity column to stop every duplicateIt only picks the numbers; it doesn’t check them.Add a PRIMARY KEY to the column.

Troubleshooting

ProblemLikely causeFix
ERROR: cannot insert a non-DEFAULT value into column "id"The column is GENERATED ALWAYS AS IDENTITY, and the insert gave it a value.Leave the column out of the insert. If you really must set a number, for example when copying old data, add OVERRIDING SYSTEM VALUE after the column list, as the error’s hint says.

FAQ

Can the numbers have gaps?

Yes, with both. The numbers come from a sequence, and a number taken by an insert that fails is never handed out again.

What’s the difference between ALWAYS and BY DEFAULT?

With ALWAYS, PostgreSQL refuses a number you type unless the insert says OVERRIDING SYSTEM VALUE. With BY DEFAULT, a number you type wins.

Conclusion

serial and identity columns both count up by themselves, but only GENERATED ALWAYS AS IDENTITY refuses a number typed by mistake. Use it for new tables, with a primary key to keep the numbers unique.

Additional Resources

Related

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

Exit mobile version