Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 14 min read Deep dive mediumAdept

Choosing a Postgres Primary Key

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.

Related posts

  • Remember to use primary keys vs unique constraints in Postgres

    Neon mediumAdept

    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. Primary keys combine uniqueness with NOT NULL, create an index, act as the usual foreign-key target, and help the query planner. If you rely on UNIQUE, you need to add NOT NULL and run ALTER TABLE ... REPLICA IDENTITY USING INDEX yourself. The simpler habit is id SERIAL PRIMARY KEY, with UNIQUE kept for naturally unique columns like emails or SKUs.

  • One, Two, Skip a Few...

    incident.io mediumAdept

    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. When incident.io promoted a follower during a database upgrade, every per-organisation incident ID sequence jumped forward by up to 32, producing gaps like #INC-7 to #INC-39. Sequences guarantee monotonic values but not gap-free ones, and rollbacks can also skip numbers. The fix replaced them with a BEFORE INSERT trigger computing MAX(external_id)+1 per organisation, backed by an index, and the post recommends keeping external IDs separate from internal primary keys.

  • Dynamic Table Partitioning in Postgres

    Supabase mediumAdept

    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. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.

  • Sequences and other volatiles in Neki

    PlanetScale hardMaster

    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. The planner relies on Postgres's immutable/stable/volatile expression classification: immutable expressions can be pushed to shards, stable ones like now() or to_char() need consistent clocks or GUCs such as lc_time, and volatile ones must run exactly once per row of the table expression. Determining that cardinality correctly under cross-shard joins, filter reordering and shard-local join pushdown is the hard part, because optimizations that deduplicate join-multiplied rows are invalid for volatile expressions.

  • Postgres to ClickHouse: Data Modeling Tips

    ClickHouse mediumAdept

    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. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.

The quest for software engineering knowledge.

Look:
Layout: