Reference
Add a Foreign Key Constraint to a Large Sharded Table Without Blocking Writes on Neki
This guide adds a foreign key from a 2,000,000-row line_items table to customers on a sharded Neki database while a writer keeps inserting. It adds the constraint as NOT VALID, finds and fixes the rows that would fail validation, and then validates it. Both tables share a shard group and shard key, so every child row and its parent live on the same shard and each shard can check the constraint on its own. →
Allocate Ticketing Seats Without Double-Booking on Neki
This guide allocates seats on Neki with a conditional UPDATE that moves a seat from available to held in one statement, so two buyers can never hold the same seat. It also assigns any free seat with FOR UPDATE SKIP LOCKED, confirms a hold into a booking, and lets an expired hold be claimed again. →
Backfill a New Column in Batches While Serving Traffic on Neki
This guide adds a total_cents column to a 2,000,000-row line_items table on a sharded Neki database and fills it in batches of 100 customers while an application keeps inserting new rows and updating existing ones. Batching by the shard key keeps each UPDATE short, so an application update that touches a row the backfill is working on waits for one batch, not for the whole table. →
Cancel a Data Migration and Clean Up Copied Rows on Neki with the PlanetScale CLI
This guide starts a MoveTables workflow on a Neki database, lets it copy 200,000 rows into a second logical database, and then cancels it before any traffic moves. __neki.workflow_cancel removes the copied table from every shard by default; with keep_data it leaves the rows for you to inspect and drop. The guide also covers the two things cancel does not do on its own: it refuses once reads have switched, and it leaves the target database's entry in the data topology. →
Change a Column Type on a Large Sharded Table Without Downtime on Neki
This guide widens a column from integer to bigint on a 2,000,000-row line_items table sharded across two Neki shards, using an online DDL workflow while a writer keeps inserting. Each data shard copies its rows into a shadow table with the new type and swaps it in at cutover. In this guide no insert failed, and one insert waited 6.3 seconds at cutover. →
Change Postgres Parameters for One Group of Shards on Neki with the PlanetScale CLI
This guide changes a Postgres parameter for one group of shards on Neki without touching the others. A configuration profile holds the settings for the shards that belong to it, so the guide puts one shard in a second profile, raises work_mem there, and reads the value back from every shard to show that only the new profile's shard changed. →
Choose Between Hash, Range, and Modulo Shard Keys for an Integer Id on Neki
Neki offers three shard index types for an integer column: xxhash, range and modulo. This guide shards the same 10,000-row table three ways and compares where rows land, where new sequential ids go, and how the router plans equality, IN and range queries. In this build all three route an equality lookup to one shard and scatter range predicates; they differ in placement. →
Complete a Cross-Shard Transfer Reliably with an Outbox Table on Neki
This guide moves money between two accounts that live on different shards of Neki without one transaction across shards. The debit and an outbox row commit together on the source shard, and a relay later applies the credit on the destination shard exactly once, using a table of applied transfer ids. →
Connect a Java Spring Boot Service to Neki with HikariCP
This guide connects a Spring Boot 4.1 service to a sharded Neki table through its default HikariCP pool. The JDBC URL passes two router settings as startup options, so the router rejects any query without the shard key and any @Transactional method that reaches a second shard. →
Create Short-Lived Least-Privilege Roles for a Batch Job on Neki with the PlanetScale CLI
This guide gives a nightly batch job its own database role on Neki. The role has no inherited roles, so it starts with no access. The script grants it only what the job needs, gives it a time to live, and deletes it when the job ends, whether the job succeeds or fails. The guide tests what the role can and cannot do, and what happens to a session when the role is deleted. →
Delete a Shard Safely After Moving Its Data on Neki with the PlanetScale CLI
This guide consolidates a Neki shard group from two shards onto one new shard with a Reshard workflow, then deletes the two old shards with pscale branch shard delete. The CLI refuses to delete a shard that the data topology still references, but it does not check whether the shard still holds rows, so the guide checks every table on each shard before deleting it. →
Design an Ad Impression Table So Campaign Queries Stay Single-Shard on Neki
This guide shards an ad impression table by campaign_id on Neki, so every per-campaign report, join and spend total runs on one shard. It compares the same data sharded by impression_id, where each of those queries goes to every shard, and shows the one cost of the design: a very busy campaign makes its shard heavier than the others. →
Dry-Run a Table Move Before Creating the Workflow on Neki with the PlanetScale CLI
This guide validates a MoveTables plan on a Neki database by calling __neki.move_tables_create with {"dry_run": true}. The dry run returns a JSON preflight report with an overall ok flag and one finding per check per source shard, and it creates no workflow. The guide also shows what the report does not check, so you can add those checks yourself before creating the real workflow. →
Erase One User's Data Across Several Shard Groups on Neki
This guide erases one user's personal data for a privacy request on Neki, where the user's rows are in several shard groups. The user's profile and sessions are sharded by user_id, the user's purchases are sharded by order_id, and a consent log is unsharded. The erasure script deletes or anonymizes the rows in each place, records a receipt, and can be run again safely. The guide then checks every shard directly to show that no row for the user is left. It covers the rows in the database. It does not cover backups, exports or logs. →
Export a Sharded Neki Table with COPY TO STDOUT for a Data Team
This guide exports a sharded table from Neki to CSV and binary files with COPY ... TO STDOUT, so a data team can load it into a warehouse. The router gathers the rows from every shard into one stream. The guide also covers the two things COPY does not do on Neki: it cannot export a query, and it cannot run on a session pinned to one shard. →
Force a Schema Change onto the Direct Path on Neki with the PlanetScale CLI
An online DDL workflow on Neki normally applies a single-table ALTER TABLE by copying the table into a shadow table on each data shard. For a change that does not need a copy, such as adding a nullable column, the apply_direct option runs the statement on each shard instead. This guide runs the same kind of change both ways on a 200,000-row table and compares the time, the per-shard path, and what each leaves behind. →
Handle Router Not-Implemented Errors with Neki Error Codes and Hints
This guide handles the errors that the Neki router returns for statement shapes it does not implement yet. These errors carry SQLSTATE NK013 and a numeric code in square brackets, and the published Neki error codes page says what each code means and what to do instead. The guide writes a small wrapper that recognizes the code in an error, tells a not-implemented error apart from other failures, and shows the HINT when there is one. →
Import an Existing Postgres Database into Neki with the PlanetScale CLI
This guide moves a Postgres database into a new, unsharded PlanetScale Neki database with pg_dump and pg_restore, then checks the result with row counts and content checksums. The import is offline: source writes stay stopped from the start of the dump until cutover. →
Isolate a Large Tenant on Its Own Neki Shard with the PlanetScale CLI
This guide uses a range shard index on tenant_id to place one large tenant on a dedicated Neki shard while every other tenant shares a second shard, then moves the dedicated shard to a larger configuration profile while reads continue. The range key bounds are hexadecimal, which determines where the boundaries go. →
Keep a Direct-Message Conversation on One Shard in a Neki Social App
This guide shards conversations and their messages by conversation_id on Neki, so reading a thread and sending a message each run on one shard. A second table sharded by user_id holds each user's inbox, so listing a user's conversations is also a single-shard query. →
Keep a Multi-Tenant Audit Log Sharded by Tenant with a Retention Job on Neki
This guide shards an audit log by tenant_id on Neki and runs a retention job that deletes each tenant's expired rows with one single-shard DELETE at a time. Every tenant can have its own retention window, and the job can be re-run safely. →
Keep a Player's Wallet and Inventory in One Transaction on a Neki Game Backend
This guide keeps a player's wallet, inventory and purchase history on one shard of Neki, so a purchase is one ordinary single-shard Postgres transaction. Setting __neki.tx_mode=single makes the router reject any transaction that would touch a second shard. →
Keep Foreign-Key Related Tables on the Same Shard Group on Neki
When a parent table and its child tables share a Neki shard group and the same shard key, every child row lives on the same shard as its parent. This guide places customers, orders and order_items together, carries customer_id into each child's primary key, adds foreign keys that each shard enforces locally, and compares the join plans with a child table placed by a different key. →
Keep Ledger Transactions on One Neki Shard with the PlanetScale CLI
This guide shards a payments ledger by account_id on Neki and uses the __neki.tx_mode and __neki.fanout session settings so that a transaction touching a second shard, or a query that scatters across every shard, fails instead of running. A cross-account transfer is then written as two single-shard transactions linked by a transfer_id. →
Move Cold Archive Data to a Separate Shard Group on Cheaper Hardware on Neki
A Neki database can run shards from more than one configuration profile, and each profile sets its own cluster size and replica count. This guide adds an archive profile with a single-node PS-10 shard, binds an orders_archive table to a shard group on that shard, moves orders older than a cutoff out of the hot table in verified batches, and reads both tiers with single-shard queries. →