Sequences and other volatiles in Neki
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.
Related posts
- Primary keys in sharded databases
PgDog mediumAdeptPgDog 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.
- Shard design doesn't start from your tables
PlanetScale mediumAdeptNeki'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. Cross-shard transactions get no atomic commit or shared snapshot. The design toolkit includes reference tables replicated to every shard for lookup data, separate shard groups for differently keyed data like a SKU catalog, SQL expressions in shard indexes such as lower(sku), range and modulo index types for legacy schemes, and global secondary indexes that turn non-shard-key lookups into two single-shard reads. The core advice is to design shard groups from your most important queries and transactions rather than from your tables, and to change them later with the Reshard workflow as needed.
- One, Two, Skip a Few...
incident.io mediumAdeptPostgres 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.
- Building a faster, lazier router for Postgres
PlanetScale hardMasterNeki, PlanetScale's Postgres sharding router, cuts per-query overhead by keeping PostgreSQL wire-protocol bytes intact and decoding only as much as each query requires. Results pass through as a raw byte slice and are only opened further when needed. They become row views for LIMIT, pointer slices for reordering and merge-sorting, and decoded, cached sort keys only for the columns being compared. Bind parameters and join keys are copied as length-prefixed frames, text and binary formats are preserved, and dropping columns copies contiguous runs of column frames instead of re-serializing values. Full decoding into typed values happens only when the router must compute new results, such as cross-shard aggregates. This contrasts with Vitess, which eagerly converts every row at RPC boundaries.
- The architecture of Neki
PlanetScale mediumAdeptNeki shards unmodified PostgreSQL behind a single connection string. A Router speaks the Postgres wire protocol, parses and plans queries against the sharding layout, and either pushes joins down to shards or runs cross-shard nested-loop, hash or merge joins itself. Each instance runs a Sidecar that pools connections by lifetime (statement, transaction, session advisory lock) and a PostgresManager. Admin handles failover, pg_rewind and per-shard sync/async/cross-zone durability, while an authoritative shard keeps custom type OIDs consistent and etcd holds the Data Topology. A Replicator built on logical replication drives MoveTables, Reshard and shadow-table OnlineDDL, and the Router buffers retryable queries during cutovers and failovers.