Skip to content

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

PlanetScale 15 min read Deep dive hardMaster

Building a faster, lazier router for Postgres

Neki, 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.

Related posts

  • The architecture of Neki

    PlanetScale mediumAdept

    Neki 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.

  • How Figma’s databases team lived to tell the scale

    Figma mediumAdept

    Figma horizontally sharded its RDS Postgres stack after vertical partitioning ran out of room. Multi-terabyte tables were hurting vacuum reliability and approaching RDS IOPS limits. Instead of moving to CockroachDB, TiDB, Spanner, Vitess or NoSQL, the team built sharding on its existing Postgres fleet using several pieces: colocated table groups ("colos") that share a shard key such as UserID, FileID or OrgID; hash-based routing to avoid hotspots from auto-incrementing and Snowflake IDs; logical sharding via views, kept separate from physical shard splits; and a DBProxy query engine that parses and routes queries and also does load-shedding and request hedging. Deliberate tradeoffs included dropping atomic cross-shard transactions, accepting slower range scans, keeping rollback possible, and using full logical replication. Sharding the first table took nine months.

  • Making large Postgres migrations practical: 1TB in 2 hours with PeerDB

    ClickHouse mediumAdept

    PeerDB migrated a 1TB, 3.6-billion-row Postgres table in about 1 hour 50 minutes with 8 threads, compared with 8 hours 40 minutes for native logical replication and 17 hours for pg_dump/pg_restore. The speedup comes from parallel snapshotting. A consistent snapshot is exported via pg_export_snapshot, the table is split into CTID ranges, and each range is streamed with binary COPY, with per-partition retries and progress visibility. Past a certain level of parallelism the source RDS network bandwidth becomes the limit, so 16 threads were no faster than 8 at 1TB. For CDC, PeerDB keeps the replication slot consumed independently of destination writes. It also reconstructs unchanged TOAST columns from batch caches and MERGE against the target, so REPLICA IDENTITY FULL is not needed.

  • We're building Postgres in Rust. Using the LLVM of databases

    Turso easyNovice

    Turso, a Rust rewrite of SQLite, is adding a Postgres frontend by parsing Postgres SQL into a common AST and compiling it to Turso's VDBE bytecode, turning the engine into an "LLVM of databases" with pluggable frontends. The argument is that SQL databases differ mostly in on-disk layout, not in core operations, so Postgres semantics can be expressed in or added to the VDBE. A C-to-VDBE compiler running Doom illustrates that the bytecode works as a real VM. The work starts from the earlier pgmicro experiment and is early-stage but runnable from source. Planned pieces include the Postgres wire protocol, embedded and in-browser use, live materialized views, and WASM-sandboxed extensions, aiming for broad rather than 100% compatibility.

  • How we scale an open source, multi-tenant storage engine for Postgres written in Rust

    Neon mediumAdept

    Neon shards its Rust-based Pageserver storage layer by dividing each Postgres relation's 8KiB block numbers into stripes (default 256MiB) and hashing each stripe to a Pageserver. Large tenants then get I/O that scales with dataset size, and they stop competing with small tenants for resources. All shards subscribe to the Paxos-based Safekeepers and filter WAL down to their own pages, while Postgres routes GetPage@LSN reads directly to the right shard by hashing, with no proxy in between. Tables need to exceed stripe size × shard count (e.g. 2GiB with 8 shards) to use full throughput. Planned work includes shard-aware Safekeepers to cut WAL fan-out cost and shard splitting, since shard count is currently fixed at tenant creation.

The quest for software engineering knowledge.

Look:
Layout: