---
title: "Primary keys in sharded databases"
description: "PgDog generates globally unique primary keys across Postgres shards without extensions or external ID services."
url: "https://swe.quest/articles/primary-keys-in-sharded-databases-4b73737ab618f6da"
canonical: "https://pgdog.dev/blog/sharded-primary-keys"
source: "PgDog"
published: "2025-09-03"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 5
tags: ["postgres","distributed-databases","storage-engines"]
---

# Primary keys in sharded databases

A post on [PgDog](https://swe.quest/sources/pgdog.md), published 2025-09-03, 5 min read, Deep dive. Original: [https://pgdog.dev/blog/sharded-primary-keys](https://pgdog.dev/blog/sharded-primary-keys)

## Summary

PgDog generates globally unique primary keys across Postgres shards without extensions or external ID services. It reuses Postgres's hash-partition algorithm, so each shard can call satisfies\_hash\_partition and loop nextval until a value belongs to that shard. Setting pgdog.next\_id\_seq as the column DEFAULT is enough, and pgdog.next\_uuid\_auto does the same for UUIDs. Sequence caching (ALTER SEQUENCE ... CACHE 1000) makes the brute-force loop cheap and cuts WAL writes, at the cost of gaps after a crash but never duplicates.

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)
- [Distributed databases](https://swe.quest/tags/distributed-databases.md) (Data & databases)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)

## Prerequisites

- Postgres sequences ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- hash partitioning
- database sharding basics ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))

## Keywords

PgDog, PostgreSQL, satisfies\_hash\_partition, BIGSERIAL, UUID, pl/PgSQL

## Related posts

- [Choosing a Postgres Primary Key](https://swe.quest/articles/choosing-a-postgres-primary-key-0faa005e666f2a79.md): Supabase · 2022-09-08 · Medium · 14 min read · PostgreSQL, Databases, Data structures
  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 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.
- [You should shard your database](https://swe.quest/articles/you-should-shard-your-database-12cdc70ed1f6d2d7.md): PgDog · 2025-11-13 · Medium · 7 min read · PostgreSQL, Distributed databases, Scaling, Query engines
  Sharding Postgres early avoids the operational pain of large single instances, illustrated by an outage where stale planner statistics caused a 300 GB table to be sequentially scanned.
- [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.
- [Shard design doesn't start from your tables](https://swe.quest/articles/shard-design-doesn-t-start-from-your-tables-283c900085b220b2.md): PlanetScale · 2026-10-23 · Medium · 8 min read · Distributed databases, PostgreSQL, System design
  Neki's shard groups co-locate related Postgres tables by hashing a shared shard key (e.g. customer\_id via xxhash) into a 64-bit keyspace split across shards, so joins and transactions for one customer stay on a single shard with full Postgres semantics.

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)
