Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ is a smoke tour of the built binary, not a second test suite.
Every other clause of such a file reaches the scratch table as the deparser prints it, not as
written; a file with no `NOT VALID` constraint runs as written (see
[docs/copy-and-swap-design.md](docs/copy-and-swap-design.md#d1--no-durable-scratch-database)).
A fourth executable-SQL edit is permitted for the online create of a partition: `CreatedPartition`
builds, from a bare `CREATE TABLE name PARTITION OF parent FOR VALUES ...`, the
`ALTER TABLE parent ATTACH PARTITION name FOR VALUES ...` that attaches a table of that name,
reusing the `CREATE`'s own relation and bound nodes unchanged, and reads the deparsed statement
back to confirm it names the same tables and bound. It refuses every other clause of the
`CREATE` rather than carry it.
A separate comparison-only exception permits `RowSecurityChange.CanonicalSQLForNamespace` to erase
only the validated operation target's schema on a fresh parsed tree. The comparison key
must already include the canonical namespace and table.
Expand Down
10 changes: 5 additions & 5 deletions demo/tour.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ dry_run() {
else
assert_eq "dry-run refusal exit of [$sql]" 2 "$status"
fi
assert_eq "plan format_version of [$sql]" 46 "$(jq -r '.format_version' <<<"$out")"
assert_eq "plan format_version of [$sql]" 47 "$(jq -r '.format_version' <<<"$out")"
assert_eq "disposition of [$sql]" "$disposition" "$(jq -r '.disposition' <<<"$out")"
assert_eq "route of [$sql]" "$route" "$(jq -r '.statements[0].route' <<<"$out")"
assert_eq "reason of [$sql]" "$reason" "$(jq -r '.statements[0].decisions[0].reason' <<<"$out")"
Expand Down Expand Up @@ -344,7 +344,7 @@ diff_plan() {
if [ "$CHECK" = 1 ]; then
out=$("$PGS" diff --url "$PG_DSN" --desired "$desired" --schema public --json) || status=$?
assert_eq "diff exit of [$desired]" 0 "$status"
assert_eq "diff format_version of [$desired]" 46 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff format_version of [$desired]" 47 "$(jq -r '.format_version' <<<"$out")"
assert_eq "statement count of [$desired]" "$count" "$(jq -r '.statements | length' <<<"$out")"
case "$(jq -r '.statements[0].sql' <<<"$out")" in
*"$fragment"*) ;;
Expand All @@ -371,7 +371,7 @@ diff_advisory() {
if [ "$CHECK" = 1 ]; then
out=$("$PGS" diff --url "$PG_DSN" --desired "$desired" --schema public --json) || status=$?
assert_eq "diff exit of [$desired]" 0 "$status"
assert_eq "diff format_version of [$desired]" 46 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff format_version of [$desired]" 47 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff disposition of [$desired]" execute "$(jq -r '.disposition' <<<"$out")"
assert_eq "statement count of [$desired]" "$count" "$(jq -r '.statements | length' <<<"$out")"
assert_eq "advisory count of [$desired]" 1 "$(jq -r '.advisories | length' <<<"$out")"
Expand Down Expand Up @@ -399,7 +399,7 @@ diff_refused() {
if [ "$CHECK" = 1 ]; then
out=$("$PGS" diff --url "$PG_DSN" --desired "$desired" --schema public --json) || status=$?
assert_eq "diff refusal exit of [$desired]" 2 "$status"
assert_eq "diff format_version of [$desired]" 46 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff format_version of [$desired]" 47 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff disposition of [$desired]" refuse "$(jq -r '.disposition' <<<"$out")"
assert_eq "diff reason of [$desired]" unsupported-statement "$(jq -r '.statements[0].reason' <<<"$out")"
assert_eq "diff class of [$desired]" "$class" "$(jq -r '.statements[0].class' <<<"$out")"
Expand Down Expand Up @@ -439,7 +439,7 @@ run_pull() {
out=$("$PGS" diff --url "$PG_DSN" --schema public --desired "$desired" --json) || status=$?
assert_eq "diff exit of [$desired]" 0 "$status"
# Plan report contract version (plan.FormatVersion), same pin as diff_plan.
assert_eq "diff format_version of [$desired]" 46 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff format_version of [$desired]" 47 "$(jq -r '.format_version' <<<"$out")"
assert_eq "diff disposition of [$desired]" execute "$(jq -r '.disposition' <<<"$out")"
case "$(basename "$desired")" in
contacts.sql)
Expand Down
4 changes: 2 additions & 2 deletions docs/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ are the canonical example.
> `make gen-capabilities`; do not edit the generated regions below by hand.

<!-- capabilities:begin summary -->
**72 operations: 35 supported today (2 of them in preview), 15 planned behind a typed refusal,
**72 operations: 36 supported today (2 of them in preview), 14 planned behind a typed refusal,
17 out of scope by design, and 5 with no online mechanism for pg-sprite to build on.**
<!-- capabilities:end summary -->

Expand Down Expand Up @@ -280,7 +280,7 @@ or the only online form is a direct statement the operator runs).
<!-- capabilities:begin partitioned_tables -->
| Operation | Status | Engine path | Online-safety problem? | Behavior and why |
| --- | --- | --- | --- | --- |
| `CREATE TABLE ... PARTITION OF` | 🟡 | native, planned flow | Yes | Typed refusal at both doors: the imperative door does not take `CREATE TABLE`, and the declarative create path refuses the form at plan time and re-checks it at apply — attaching a partition takes a brief `ACCESS EXCLUSIVE` on the **parent**, which the greenfield absence proof does not cover. A desired file for a partition that already exists is refused before planning builds anything, since the scratch build would attach to the live parent, and a desired file that declares several partitions, or a parent with its partitions, is refused at parse as a `capability-boundary`. The partition-aware flow is planned |
| `CREATE TABLE ... PARTITION OF` | ✅ | native, planned flow | Yes | Run as written, the statement takes `ACCESS EXCLUSIVE` on the **parent**, so it queues behind every reader of the tree and every query after it queues behind it. `migrate --alter` runs a bare `CREATE TABLE name PARTITION OF schema.parent FOR VALUES ...` as the online create of a partition instead: it creates the table standalone with `LIKE` the parent (its columns, defaults, generation expressions, storage, compression, and `CHECK`s; the parent's identity is taken on at the attach), which reads the parent without blocking it, then runs the online attach of that table with the statement's own bound (see the `ATTACH PARTITION` row), and reports the change on the parent. Running the same statement again resumes: an empty standalone table of that name is attached, and a partition already attached changes nothing; a table of that name holding rows is refused (`unsupported-statement`, `environmental`). A `DEFAULT` bound, an unqualified parent, and any clause beside the bound (a column list, constraints, `IF NOT EXISTS`, `UNLOGGED`, `WITH (...)`, `TABLESPACE`, `USING`, `PARTITION BY`) are refused (`unsupported-statement`, `capability-boundary`) before anything runs, as is a parent with a `DEFAULT` partition. Acknowledgements (`--force` included) are refused as usage errors. `--dry-run` plans it on the parent with the `online-create-partition` execution and refuses what the run refuses before it reads the parent; like the online attach's, it does not read the parent's `DEFAULT` partition, which only the run refuses. In a desired file, `PARTITION OF` is refused at plan time, since the scratch build would attach to the live parent, and a file that declares several partitions, or a parent with its partitions, is refused at parse as a `capability-boundary` |
| `ATTACH PARTITION` | ✅ | native, planned flow | Yes | Run as written, the attach scans the new table under `ACCESS EXCLUSIVE` to validate its bound and builds the parent's indexes on it, so `migrate --alter` runs a lone `ATTACH PARTITION ... FOR VALUES` as the online attach instead: while writes continue, the table is given a validated `CHECK` proving the bound, the parent's `CHECK` constraints and `NOT NULL`s, the parent's indexes built concurrently (a unique one backing its constraint through `USING INDEX`), and each of the parent's foreign keys, validated; then the `ATTACH` runs in one brief transaction that scans and builds nothing, and the proof `CHECK` is dropped. Each step commits on its own, and running the same statement again resumes, or finds the table attached and changes nothing. A row outside the bound fails the run with nothing attached. A parent with a `DEFAULT` partition is refused (`unsupported-statement`, `capability-boundary`) before anything runs, since excluding the bound from it refuses writes inside the bound until the attach; an `ATTACH PARTITION ... DEFAULT` stays rewrite-required. `--force SCHEMA.TABLE` runs the blocking `ATTACH PARTITION` as submitted under bounded budgets and records the override. A desired schema cannot express an attach, so `diff` never produces one |
| `DETACH PARTITION [CONCURRENTLY]` | ✅ | native, safer sequence | Yes | The plain form is rewritten to `DETACH PARTITION ... CONCURRENTLY`, which takes `SHARE UPDATE EXCLUSIVE` on the table, so reads and writes of the other partitions keep running. It runs outside a transaction block under the brief budgets: `--lock-timeout` bounds each lock wait, the wait for transactions that still read the partition through the table included, and `--statement-timeout` the whole statement. Flagged **destructive**, since the partition's rows leave the table: `migrate` refuses it with `destructive-change` unless `--allow-destructive` names the table. A detach that stops after its first transaction committed fails with `partition-detach-interrupted` and leaves the partition detach-pending; running the same statement again finishes it with `DETACH PARTITION ... FINALIZE`. A rerun that finds the partition already a table of its own with the table's columns, because an earlier run or a detach the server finished after its client went away detached it, runs nothing and exits 0 with `already_detached: true`; a same-named table with those columns that never was a partition gets the same verdict, since PostgreSQL keeps no record of a finished detach. PostgreSQL detaches one partition of a table at a time, so while another partition is detach-pending the detach fails with `partition-detach-pending` before anything runs, naming that partition and its `FINALIZE`. PostgreSQL refuses a concurrent detach from a table with a `DEFAULT` partition, so both forms are refused there (`unsupported-statement`, `by-design`, in `--dry-run` too) before anything runs; `--force SCHEMA.TABLE` runs the plain `DETACH PARTITION` as submitted under bounded budgets, taking `ACCESS EXCLUSIVE` on the table and the partition. The table may itself be a partition, and the partition may itself be partitioned, whose whole subtree then leaves with it; among table shapes, only a `DEFAULT` partition of the table detached from blocks the concurrent form, not one inside the detached subtree. A foreign key that references the table is checked before the first transaction commits: while a row still references the partition, the run fails with `execution-failed`, the server's error (SQLSTATE `23503`) reachable with `errors.As`, and the partition stays attached, not detach-pending. An unqualified partition name resolves with the table's schema first on the search path, in the rerun that finishes a pending detach too. `DETACH PARTITION ... FINALIZE` itself is refused with `unsupported-statement`: rerun the statement that started the detach. A desired schema cannot express a detach, so `diff` never produces one |
| `SET TABLESPACE` on a **partitioned parent** | ✅ | native, as-is | Yes | Metadata-only: a partitioned parent has no storage, so PostgreSQL records the tablespace as the default for partitions created later, under a brief `ACCESS EXCLUSIVE` on the parent alone, and moves no existing partition or index. Runs as written under bounded locks. Moving an existing partition is a `SET TABLESPACE` on that partition, a rewrite refused as in the `SET TABLESPACE` row. `ALTER INDEX ... SET TABLESPACE` is not taken by the imperative front door. A desired file does not express a table's tablespace, so `diff` never produces one |
Expand Down
20 changes: 10 additions & 10 deletions docs/cli-output-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ itself.
```console
$ pg-sprite migrate --alter 'ALTER TABLE users ADD COLUMN note text' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -247,7 +247,7 @@ plans the safer online sequence instead: the decision carries it in
```console
$ pg-sprite migrate --alter 'ALTER TABLE users ADD CONSTRAINT users_email_key UNIQUE (email)' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -588,7 +588,7 @@ column first, then build the constraint as a separate, named
```console
$ pg-sprite migrate --alter 'ALTER TABLE users ADD COLUMN nickname text UNIQUE' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -640,7 +640,7 @@ would not fit the declared free disk.
```console
$ pg-sprite migrate --alter 'ALTER TABLE users ALTER COLUMN id TYPE text' --dry-run --copy-and-swap --free-disk 10GiB --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -781,7 +781,7 @@ column, a relocation — so the plan is `unavailable` and nothing executes.
```console
$ pg-sprite migrate --alter 'ALTER TABLE users ADD COLUMN joined timestamptz DEFAULT clock_timestamp()' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -820,7 +820,7 @@ when it starts.
```console
$ pg-sprite migrate --alter 'CREATE INDEX events_created_idx ON events (created)' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "events",
Expand Down Expand Up @@ -916,7 +916,7 @@ cause is the report-level `reason`.
```console
$ pg-sprite migrate --alter 'ALTER TABLE events ADD CONSTRAINT events_pkey PRIMARY KEY USING INDEX events_id_idx' --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "events",
Expand Down Expand Up @@ -982,7 +982,7 @@ table. A mismatched acknowledgement executes nothing and exits 1.
```console
$ pg-sprite migrate --alter 'ALTER TABLE users DROP COLUMN email' --dry-run --allow-destructive public.users --json
{
"format_version": 46,
"format_version": 47,
"source": "alter",
"schema": "public",
"table": "users",
Expand Down Expand Up @@ -1027,7 +1027,7 @@ object it removes, and the fingerprint covers it.
```console
$ pg-sprite migrate --desired orders.sql --dry-run --json
{
"format_version": 46,
"format_version": 47,
"source": "diff",
"schema": "public",
"table": "orders",
Expand Down Expand Up @@ -1205,7 +1205,7 @@ CREATE TABLE users (
```console
$ pg-sprite diff --desired /tmp/users.sql --json
{
"format_version": 46,
"format_version": 47,
"source": "diff",
"schema": "public",
"table": "users",
Expand Down
2 changes: 1 addition & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ and row, checksum, and catch-up counters directly; that snapshot is the
$ pg-sprite migrate --dry-run --json --alter "ALTER TABLE orders ADD COLUMN ref text UNIQUE" > plan.json; echo $?
2
$ jq -c '{format_version, table, disposition}' plan.json
{"format_version": 46,"table":"orders","disposition":"rewrite-required"}
{"format_version": 47,"table":"orders","disposition":"rewrite-required"}
```

The exit code answers the question a pipeline asks first, without parsing
Expand Down
Loading