Skip to content
blockPublic

About

Online schema change engine for PostgreSQL

Resources

Code of conduct

Contributing

Security policy

Stars

13 stars

Watchers

2 watching

Forks

Latest commit

 

History

569 Commits

Folders and files

pg-sprite

Change the shape of live PostgreSQL tables while your application keeps reading and writing them

CI Slot Release Proof Vulnerability Scan Release PostgreSQL tested: 14 | 15 | 16 | 17 | 18 Maturity: preview

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.

How it works

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 TYPE today.
  • A typed refusal when neither is safe. Nothing runs, and the verdict carries a reason and class that 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.

What it looks like

pg-sprite replacing a blocking ADD CONSTRAINT with the safer online sequence: dry-run, real run, then the catalog proof

  • 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 TABLE file 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.

Install

brew install block/tap/pg-sprite

Release 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@latest

Quick start

pg-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.orders

Connect directly to the primary, not through a pooler (why). The CLI guide walks through each task with captured output.

Commands

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.

What pg-sprite does not do yet

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, then VALIDATE); dropping one runs only through migrate --alter.
  • Renames are refused unless --force names 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 from migrate --alter only as an accepted blocking change (passthrough); a desired file drops an index concurrently under --allow-destructive and --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 codes

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.

Docs

Development

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 PostgreSQL

Integration tests run against real PostgreSQL through testcontainers. testing.md covers the version matrix and the local database targets; demo/README.md covers the tour.

Contributing

Not yet; see CONTRIBUTING. Safety-relevant issue reports are welcome even at this stage.

License

Apache 2.0

About

Online schema change engine for PostgreSQL

Resources

Code of conduct

Contributing

Security policy

Stars

13 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages