---
title: "Choosing a Postgres Primary Key"
description: "Postgres primary key options progress from integer and serial sequences to standards-compliant IDENTITY columns, then to random IDs and UUIDs, each fixing a weakness of the last."
url: "https://swe.quest/articles/choosing-a-postgres-primary-key-0faa005e666f2a79"
canonical: "https://supabase.com/blog/choosing-a-postgres-primary-key"
source: "Supabase"
published: "2022-09-08"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 14
tags: ["postgres","databases","data-structures"]
---

# Choosing a Postgres Primary Key

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2022-09-08, 14 min read, Deep dive. Original: [https://supabase.com/blog/choosing-a-postgres-primary-key](https://supabase.com/blog/choosing-a-postgres-primary-key)

## Summary

Postgres primary key options progress from integer and serial sequences to standards-compliant IDENTITY columns, then to random IDs and UUIDs, each fixing a weakness of the last. Sequences can leave gaps and expose guessable, enumerable IDs. Python's random module is unsafe for IDs, so the secrets module is needed. UUIDv1 and v4 are hard to guess but are twice the size of bigint, and they sort and index poorly. Newer k-sortable formats (UUIDv6/v7/v8, ULID, KSUID, Snowflake and others) fix ordering, and the uuid-ossp and Rust-based pg\_idkit extensions are benchmarked on generation speed and table/index size.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)
- [Data structures](https://swe.quest/tags/data-structures.md) (Architecture & design)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres indexes ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

UUIDv7, UUIDv4, pg\_idkit, uuid-ossp, ULID, KSUID, Snowflake, IDENTITY columns

## Related posts

- [Remember to use primary keys vs unique constraints in Postgres](https://swe.quest/articles/remember-to-use-primary-keys-vs-unique-constraints-in-postgres-429326ad5b407be8.md): Neon · 2024-05-15 · Medium · 5 min read · PostgreSQL, Schema migrations, Replication
  Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication.
- [One, Two, Skip a Few...](https://swe.quest/articles/one-two-skip-a-few-b1af2f9386afbc09.md): incident.io · 2021-07-12 · Medium · 8 min read · PostgreSQL, Replication, Incidents & postmortems, Databases
  Postgres sequences pre-log 32 values to the WAL (SEQ\_LOG\_VALS), so replicas see sequence state as it would be after a primary crash.
- [Dynamic Table Partitioning in Postgres](https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e.md): Supabase · 2023-10-03 · Medium · 20 min read · PostgreSQL, Schema migrations, SQL, Scaling
  Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock.
- [Sequences and other volatiles in Neki](https://swe.quest/articles/sequences-and-other-volatiles-in-neki-867c9857ecacbacb.md): PlanetScale · 2026-10-14 · Hard · 14 min read · Query engines, PostgreSQL, Distributed databases, SQL
  Neki, PlanetScale's sharded Postgres, implements Postgres sequences by classifying sequence operations like nextval() as router-only, with an authoritative shard hosting all sequences so values are never duplicated across shards.
- [Postgres to ClickHouse: Data Modeling Tips](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4.md): ClickHouse · 2024-08-28 · Medium · 9 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, SQL, Replication
  Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
