0
0
PostgresqlComparisonBeginner · 3 min read

Serial vs Bigserial in PostgreSQL: Key Differences and Usage

In PostgreSQL, serial is an auto-incrementing integer type using 4 bytes, while bigserial uses 8 bytes for larger numbers. Use serial for smaller ranges and bigserial when you expect very large sequences beyond 2 billion.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of serial and bigserial in PostgreSQL.

Featureserialbigserial
Storage Size4 bytes (integer)8 bytes (bigint)
Value Range-2,147,483,648 to 2,147,483,647-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Use CaseStandard auto-increment IDsVery large auto-increment IDs
Underlying Typeintegerbigint
Sequence Typeinteger sequencebigint sequence
⚖️

Key Differences

serial and bigserial are not true data types but convenient shortcuts in PostgreSQL. Both create an integer column and an associated sequence that auto-increments when you insert new rows.

The main difference is the size of the integer used: serial uses a 4-byte integer, which supports values up to about 2 billion. bigserial uses an 8-byte bigint, supporting much larger values up to about 9 quintillion.

This means serial is suitable for most applications with moderate row counts, while bigserial is designed for very large tables or when you expect the ID to exceed the 2 billion limit. Both automatically create a sequence object and set the default value of the column to the next sequence number.

⚖️

Code Comparison

Here is how you define a table with a serial column for auto-incrementing IDs.

sql
CREATE TABLE users_serial (
  id serial PRIMARY KEY,
  name text NOT NULL
);

INSERT INTO users_serial (name) VALUES ('Alice'), ('Bob');

SELECT * FROM users_serial;
Output
id | name ----+------- 1 | Alice 2 | Bob (2 rows)
↔️

Bigserial Equivalent

Here is the equivalent table using bigserial for larger auto-incrementing IDs.

sql
CREATE TABLE users_bigserial (
  id bigserial PRIMARY KEY,
  name text NOT NULL
);

INSERT INTO users_bigserial (name) VALUES ('Alice'), ('Bob');

SELECT * FROM users_bigserial;
Output
id | name ----+------- 1 | Alice 2 | Bob (2 rows)
🎯

When to Use Which

Choose serial when your table will have fewer than about 2 billion rows, which covers most typical applications. It uses less storage and is slightly faster due to smaller integer size.

Choose bigserial when you expect extremely large tables or need to avoid running out of IDs, such as in high-scale systems or logs that grow indefinitely.

In summary, serial is the default choice for most cases, and bigserial is for special cases requiring very large numeric ranges.

Key Takeaways

serial uses 4-byte integers; bigserial uses 8-byte integers for auto-incrementing.
Use serial for most tables with moderate row counts.
Use bigserial when you expect IDs to exceed 2 billion.
Both create sequences and set default values automatically.
Choosing the right type helps optimize storage and future-proof your database.