Change the shape of live PostgreSQL tables while your application keeps reading and writing them
Vision · How it works · Install · Quick start · CLI guide · Docs
pg-sprite is an online schema-change engine for PostgreSQL: community, RDS, and
Aurora, tested on 14 through 18. Give it a DDL statement or a desired
CREATE TABLE file. It works out what the change would lock and rewrite, runs
the safest sequence that reaches the same end state, and refuses with a typed
verdict when it cannot prove one.
Important
Preview: ready to try. Everyday changes such as adding a column, an index,
or a constraint run today with short locks. Rewriting a whole table, such as
changing a column's type, is in preview and runs only with --copy-and-swap.
We're not at 1.0 yet, so commands and output may change between releases.
pg-sprite capabilities --json says which changes are ga and which are preview.
A planner classifies each change, a router picks a strategy, and an executor carries it out:
- Native idioms when PostgreSQL has one:
CREATE INDEX CONCURRENTLY,NOT VALID+VALIDATE, fast defaults,ADD CONSTRAINT … USING INDEX. - Copy-and-swap for genuine rewrites: a shadow table filled in chunks while
writes stream through logical decoding, checksum-verified, then swapped in.
It is resumable and runs a general
ALTER COLUMN TYPEtoday. - A typed refusal when neither is safe. Nothing runs, and the verdict
carries a
reasonandclassthat automation can route on.
The planner is PostgreSQL's missing MySQL-style ALGORITHM= / LOCK=
declaration. It proves the cost and the lock impact before anything executes
(reference). Anything that is not
changing a table's shape, such as data backfills or access control, is another
tool's job; capabilities.md names which.
- Improve: a blocking form is replaced with the safer online sequence (above; why it is safer is in safer-sequences.md).
- Diff: a desired
CREATE TABLEfile in, a classified plan out (demo). - Refuse: no safe path exists, so nothing runs and the command exits 2 (demo).
- Lint: blocking idioms flagged offline, no database needed (demo).
Every output shape, as text and JSON, is in cli-output-examples.md.
brew install block/tap/pg-spriteRelease archives for linux and darwin on amd64 and arm64, with checksums.txt,
are on the releases page. The
binary is pure Go, so go install works anywhere with no C toolchain:
go install github.com/block/pg-sprite/cmd/pg-sprite@latestpg-sprite lint changes.sql # offline: flag blocking idioms
export PGSPRITE_URL='postgres://user:pass@primary:5432/app'
pg-sprite migrate --dry-run --alter 'ALTER TABLE app.orders ADD CONSTRAINT qty_positive CHECK (qty > 0)'
pg-sprite migrate --alter 'ALTER TABLE app.orders ADD CONSTRAINT qty_positive CHECK (qty > 0)'
pg-sprite pull --schema app --out schema # one desired file per table
pg-sprite diff --schema app --desired schema/orders.sql # what would converge app.ordersConnect directly to the primary, not through a pooler (why). The CLI guide walks through each task with captured output.
| Command | Database | What it does |
|---|---|---|
migrate |
required | Plan, route, and execute one change under bounded lock and statement budgets (copy-and-swap flags) |
migrate --dry-run |
required | The same plan against the real table; executes nothing |
diff |
required | Plan the changes that converge a live table onto a desired file; changes nothing |
pull |
required | Write one desired file per supported table in a schema |
status |
required | Show live pg-sprite sessions |
reap |
required | Drop orphan replication slots that stopped copy-and-swap runs left behind |
capabilities |
none | Print the support matrix (--json for automation) |
fmt |
none | Canonicalize a schema file |
lint |
none | Flag patterns the engine would refuse, rewrite, or gate |
suggest |
none | Show the safer form the engine would run for risky DDL |
Only migrate and reap ever write. If a multi-step change fails partway,
execution-model.md says what state the table is in.
Every boundary fails closed with a typed refusal, never a silently wrong or incomplete result. The full list is in limitations.md, and the support matrix is capabilities.md.
- Copy-and-swap is in preview and carries only
ALTER COLUMN TYPE; other rewrites refuse (running it). - No connection poolers on pg-sprite's own connection, except Supabase's session endpoint (poolers).
- Foreign keys a desired file adds run online (
NOT VALID, thenVALIDATE); dropping one runs only throughmigrate --alter. - Renames are refused unless
--forcenames the table, since they break running application instances. - Destructive changes need
--allow-destructive SCHEMA.TABLE, and desired plans also need--expect-fingerprint. - Index maintenance (
DROP INDEX,REINDEX) runs frommigrate --alteronly as an accepted blocking change (passthrough); a desired file drops an index concurrently under--allow-destructiveand--expect-fingerprint, and a partitioned table's index only as an accepted blocking drop. - Non-table objects such as views, functions, and triggers are outside the declarative model, apart from adding enum labels.
- Tables are never dropped; that stays with the owner of the whole schema.
| Exit | Meaning |
|---|---|
| 0 | Executed through an online-safe path, or a dry run, diff, or pull found nothing to refuse |
| 1 | Failed: a PostgreSQL, operational, or usage error. A safer sequence that stopped partway keeps its committed steps, named in executed_sql |
| 2 | Refused: no online-safe path. The verdict names the typed reason and class. A migrate --desired plan refused partway keeps the statements before it |
| 3 | Committed through the accepted-blocking passthrough, without an online-safety guarantee |
Gate on non-zero. The full rules, including a lost COMMIT and library
callers, are in cli-output-examples.md.
- Vision: what pg-sprite is and is not
- Capabilities and limitations: what runs, what refuses, and why
- CLI guide and output examples
- Copy-and-swap: running a table rewrite
- Execution model: partial states and how failures are reported
- Go library: embedding pg-sprite in an orchestrator through
pkg/migrate(Run,RunDesired); SchemaBot is the worked example - Safety and invariants: the safety-critical core and its rules
- Design docs: everything else
make setup # one-time: configure git hooks
make build # build ./... and bin/pg-sprite
make test # full suite; integration tests need Docker
make test-unit # unit tests only
make demo # runnable tour of the CLI against a local PostgreSQLIntegration tests run against real PostgreSQL through testcontainers. testing.md covers the version matrix and the local database targets; demo/README.md covers the tour.
Not yet; see CONTRIBUTING. Safety-relevant issue reports are welcome even at this stage.