---
title: "Sequences and other volatiles in Neki"
description: "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."
url: "https://swe.quest/articles/sequences-and-other-volatiles-in-neki-867c9857ecacbacb"
canonical: "https://planetscale.com/blog/neki-volatile-expressions"
source: "PlanetScale"
published: "2026-10-14"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 14
tags: ["query-engines","postgres","distributed-databases","sql"]
---

# Sequences and other volatiles in Neki

A post on [PlanetScale](https://swe.quest/sources/planetscale.md), published 2026-10-14, 14 min read, Deep dive. Original: [https://planetscale.com/blog/neki-volatile-expressions](https://planetscale.com/blog/neki-volatile-expressions)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Distributed databases](https://swe.quest/tags/distributed-databases.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL joins and WHERE semantics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- PostgreSQL sequences ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- database sharding basics ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- query planning concepts

## Keywords

Neki, PlanetScale, PostgreSQL, nextval, CREATE SEQUENCE, volatile functions

## Related posts

- [Primary keys in sharded databases](https://swe.quest/articles/primary-keys-in-sharded-databases-4b73737ab618f6da.md): PgDog · 2025-09-03 · Medium · 5 min read · PostgreSQL, Distributed databases, Storage engines
  PgDog generates globally unique primary keys across Postgres shards without extensions or external ID services.
- [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.
- [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.
- [Building a faster, lazier router for Postgres](https://swe.quest/articles/building-a-faster-lazier-router-for-postgres-bc9a70ce219128ed.md): PlanetScale · 2026-10-01 · Hard · 15 min read · PostgreSQL, Distributed databases, Performance engineering, Go, Query engines
  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.
- [The architecture of Neki](https://swe.quest/articles/the-architecture-of-neki-b0a20bdb70a4c3cd.md): PlanetScale · 2026-09-18 · Medium · 9 min read · PostgreSQL, Distributed databases, Replication, System design, Kubernetes
  Neki shards unmodified PostgreSQL behind a single connection string.

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)
