Sub topic 15
When not to use sequential IDs
Assume you know basic SQL, primary keys, foreign keys, and transactions. This chapter focuses on choosing an identifier strategy rather than creating an identity column.
A sequential ID is a compact number generated from a PostgreSQL sequence. It works well for many internal tables, but it is not automatically the best identifier for every system.
Limits of sequential IDs
Sequential IDs reveal information. If a client can see /orders/1042 and later /orders/1047, it can guess that other orders exist between those values. The ID does not grant access by itself, but predictable IDs make insecure authorization easier to exploit. Every request must still check whether the current user may access the selected row.
Sequential IDs also make distributed systems harder to design. Two independent databases can both create ID 1. Combining their data requires remapping IDs or assigning non-overlapping sequence ranges.
A sequential ID is usually a poor public identifier when you want these properties:
- IDs that are difficult to guess.
- IDs that can be created independently in different services.
- IDs that do not reveal approximate creation order or record count.
- IDs that can be generated before a central database is available.
UUIDs for public or distributed identifiers
A UUID is a 128-bit identifier normally written as a string such as 550e8400-e29b-41d4-a716-446655440000. PostgreSQL can generate UUID values with the pgcrypto extension.
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE documents (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
owner_id bigint NOT NULL,
title text NOT NULL
);
INSERT INTO documents (owner_id, title)
VALUES (42, 'Design notes')
RETURNING id;UUIDs are much larger than bigint values. They use more space in the table, indexes, and foreign keys. Random UUIDs can also make index insertion less locality-friendly than increasing numbers. These costs matter for very large, write-heavy tables.
UUIDs are useful when clients need opaque identifiers or when multiple services create records without coordinating a shared sequence. Keep an internal numeric key when database efficiency matters, and expose a separate UUID when public URLs need opaque values.
Natural keys
A natural key uses a meaningful business value, such as an immutable country code. It can avoid an extra generated column, but it is safe only when the value is unique, present, stable, and suitable for every referencing table.
Email addresses, usernames, and product names often fail these tests. People change email addresses, names can be corrected, and business rules may allow duplicates later. A natural key also makes foreign keys wider and updates more expensive.
| Strategy | Strength | Main cost |
|---|---|---|
| Identity `bigint` | Small and index-friendly | Predictable and centrally generated |
| UUID | Opaque and suitable for distributed creation | Larger indexes and less locality |
| Natural key | Represents a business fact | Can change and spread business rules |
Decision rule
Use a sequential identity key for an internal, centrally written table when compact joins and index performance matter. Use UUIDs when identifiers cross trust boundaries or databases. Use a natural key only when the business value is genuinely immutable and universally unique. You can combine these choices: keep a generated internal primary key and add a separate unique public identifier.
Exercise
Should an email address usually be the primary key for a users table?
Show answer
No. Email addresses can change and may have privacy implications. Use a generated key, then add a suitable unique constraint for the current email address.