Skip to content

migrate: name the index rebuild when refusing a partitioned parent retype - #709

Open
Kiran01bm wants to merge 2 commits into
mainfrom
kiran01bm/pd218-parent-retype-index-refusal
Open

Kiran01bm wants to merge 2 commits into
mainfrom
kiran01bm/pd218-parent-retype-index-refusal

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

Retyping a column that a partitioned table's index reads is now refused with its own cause, partitioned-index-rebuild, and the refusal says how to run the change, instead of pointing at --copy-and-swap.

Why

Some type changes, such as widening varchar(10) to varchar(20), are instant on an ordinary table. On a partitioned table, PostgreSQL rebuilds every partition's index that reads the column while it locks the whole table, so pg-sprite refuses to run it directly. The refusal said to turn on copy-and-swap (pg-sprite's rebuild-a-copy route), but copy-and-swap does not take partitioned tables either, so an operator who followed the advice hit a second refusal and still had no way forward.

What

  • New cause verdict.CausePartitionedIndexRebuild (partitioned-index-rebuild) under copy-and-swap-refused, class capability-boundary, listed in siteRefusals() and in TestReasonAndCauseTokensAreFlat.

  • The planner marks such a retype with RebuildsPartitionedIndexes (in-process only, not in the plan JSON or the fingerprint, so no format bump).

  • New pkg/migrate/partitioned_index_rebuild.go. The run's gate, the imperative dry run, the dry run's preflight and the desired-state door use it when every copy-and-swap decision is such a retype and the statement does not resume a stopped run.

  • The detail names the rebuild and the two-run path:

    1. Drop the index in its own desired-state run, under --accept-blocking, --allow-destructive and --expect-fingerprint.
    2. Retype the column, which now changes in place, and recreate the index; pg-sprite builds it partition by partition.

    Or run the statement with --force, which holds the lock while the indexes rebuild.

  • An ordinary table's retype keeps copy-and-swap-not-enabled. With --copy-and-swap passed, the refusal is still copy-and-swap-partitioned.

  • Docs: refusal-classes.md, schemabot-integration.md, cli-output-examples.md, limitations.md, binary-coercible-type-changes.md, capabilities.md and capabilities.yaml.

  • Tests: migrate.Run with default options asserts the new cause. Further tests check that the opened route still gives copy-and-swap-partitioned, that an ordinary table and a rewriting retype keep the old cause, and that the CLI dry run matches the run. The new-cause tests fail without the change.

Before / after

events is partitioned and has an index on name varchar(10); the change widens the column.

input: pg-sprite migrate --alter "ALTER TABLE events ALTER COLUMN name TYPE varchar(20)"

Before
  planner  ──▶ would rebuild every partition's index ──▶ copy-and-swap route
  refusal  ──▶ cause copy-and-swap-not-enabled:
               "copy-and-swap is in preview ... (migrate --copy-and-swap ...)"
  operator ──▶ adds --copy-and-swap ──▶ refused again: copy-and-swap-partitioned

After
  planner  ──▶ would rebuild every partition's index ──▶ copy-and-swap route
  refusal  ──▶ cause partitioned-index-rebuild:
               "... run it in two runs instead: first drop the partitioned
                index in a run of its own ..., then retype the column ...
                and recreate the index ..."
  operator ──▶ follows the two runs; no dead end

…type

The closed copy-and-swap gate told the operator to pass --copy-and-swap,
which only reaches the partitioned-parent refusal.
@Kiran01bm
Kiran01bm marked this pull request as ready for review December 3, 2026 02:15
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-12-03T02:15:42.104586Z
We couldn’t complete this review. Comment @codex review to try again.
ff4ffd3 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@aparajon

aparajon commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

🤖 Review 1/2 (correctness) of ff4ffd3: 1 blocking, 3 non-blocking.

Inspected the whole change at ff4ffd37e5dbb2deb667905acf86db6e4e844eb6 (+307/-28, 20 files): the planner marker (planner.go), the new partitioned_index_rebuild.go, the four call sites that swap copySwapNotEnabledVerdict for closedCopySwapVerdict (run, dry-run admission, dry-run preflight, plan admission), the verdict token and registry entry, the tests, and the docs, against routesToClosedCopySwap and the CLI's --force grammar. I read the failing CI logs of run 47003786724 for every red leg.

CI: 7 checks red, 6 of them from one new test in this PR, which is blocking. Supabase compatibility, test (PostgreSQL 14), 15, 16, 17 and test (PostgreSQL 18, other packages) each end in one failing test, TestRunRefusesAParentRetypeThatWouldRebuildEveryPartitionIndexWithCopySwapOpened (the test this PR adds), and the seventh, all-green, is the gate over them. No other test fails in any of those legs. test (PostgreSQL 18, schemachange), build, lint, smoke, the docs-guard unit tests and the rest are green. I did not rerun anything.

The change itself reads correct: the route is unchanged, the new cause is minted only when every decision routed to the closed route is a retype the planner marked, and any other mix, a resumed stopped run, or an opened route keeps the old behavior. The fail-closed direction is right: a decoded plan, a retype the planner could not verify, or a retype that rewrites the column anyway all fall back to copy-and-swap-not-enabled.

Invariants (docs/invariants.md): RF-7 (every refusal has one class, registry complete) is extended: the new site is in siteRefusals and in TestReasonAndCauseTokensAreFlat, so TestRefusalRegistryIsComplete covers it. ST-6 (preflight before the first write) is upheld: the refusal is minted before anything runs, and the tests assert the index filenode and column type are unchanged. AB-4 (acceptance reaches only an eligible refusal) is upheld: the new site is not in the eligible set, so --accept-blocking cannot lift it. RF-10 (destructive acknowledgement) agrees with the detail's naming of --allow-destructive for the index drop. Nothing is weakened.

Blocking

  1. TestRunRefusesAParentRetypeThatWouldRebuildEveryPartitionIndexWithCopySwapOpened fails on every PostgreSQL version: it opens copy-and-swap on a pool that is too small for the route. The test builds its table with retypeOrders, whose pool is dbconn.NewPool(ctx, dbconn.Config{URL: url}) with the default size of four, and then runs migrate.Run with copySwapRunOptions. With the route opened, Run checks the pool against CopySwapMinConns before the route's preflight (copy_swap.go#L359-L360), so it returns an error instead of the partitioned-parent refusal the test asserts, and the test stops at its first require.NoError (#L118):

    --- FAIL: TestRunRefusesAParentRetypeThatWouldRebuildEveryPartitionIndexWithCopySwapOpened (0.22s)
        partitioned_parent_retype_integration_test.go:118:
            Error:      Received unexpected error:
                        migrate: copy-and-swap needs 6 connections (4 copy workers, one for the catch-up's flushes, one for checkpoint saves and progress reads), pool holds 4: the pool cannot hold every session the operation needs at once
            Messages:   a refusal returns no error
    

    It is the same line in all six legs (for example test (PostgreSQL 14) and Supabase compatibility). The other copy-and-swap integration tests in the package build their pool with the floor (desired_copy_swap_integration_test.go#L51, copy_swap_connection_integration_test.go#L39): MaxConnsFloor: migrate.CopySwapMinConns(copier.Options{}). The fix is in the test only: give retypeOrders that floor, which the other tests in the file tolerate, or open a separate pool with it for this test. This is the one test that proves the PR's central claim, that an operator who opens the route lands on copy-and-swap-partitioned, so it has to pass before merge. The production change is not at fault. Reasoned from the log and the code, not rerun: after the fix the run should reach the preflight and end in the cause the test asserts.

Fix sketch (not run)
func retypeOrders(t *testing.T) (*pgxpool.Pool, string) {
	t.Helper()
	url := testutil.NewDatabase(t, testutil.StartPostgres(t))
	pool, err := dbconn.NewPool(t.Context(), dbconn.Config{
		URL:           url,
		MaxConnsFloor: migrate.CopySwapMinConns(copier.Options{}),
	})
	// ...

with "github.com/block/pg-sprite/pkg/copier" added to the imports.

Non-blocking

  1. The refusal's detail is written for one front door but shown by both, so each door gets a step that does not apply. partitionedIndexRebuildDetail says "remove it from the desired file" and, as the alternative, "run the statement under --force SCHEMA.TABLE". The desired-state refusal (closedCopySwapPlanRefusal) carries the same text, and migrate --desired rejects the flag outright (--force cannot be combined with --desired, migrate_desired.go#L33-L34); a migrate --alter operator has no desired file to edit and is told to move to one. The two-run path is right for both, since --alter refuses a partitioned index's drop (RF-6). Nothing asserts the detail text, so the mismatch has no test either. A detail per door, or wording that says "in a desired-state run" for the drop and "with migrate --alter" for --force, closes it.

  2. Several branches of the new predicate are untested. rebuildsPartitionedIndexesOnly and planRebuildsPartitionedIndexesOnly decide between two causes, but the tests cover only the all-rebuild and all-rewrite cases. Not covered: a multi-operation statement with one marked retype beside one that rewrites (must keep copy-and-swap-not-enabled), a plan mixing a marked statement with an unmarked routed one (the return false at #L76), the !resumes guard of closedCopySwapVerdict (a run stopped on the table keeps the old cause), and the desired-state dry run (only the imperative CLI dry run has a test, dryrun_copy_swap_route_integration_test.go). By reading, deleting the !resumes guard, or flipping the early return false to continue, would survive the PR's tests. The rebuildsPartitionedIndexesOnly loop is a pure function over []planner.Decision, so a table-driven unit test needs no database.

  3. A plan that crosses a process boundary loses the marker, and then gives the old advice. RebuildsPartitionedIndexes is json:"-", so a plan.Report decoded from JSON reads false and AdmitPlanCopySwapRoute falls back to copy-and-swap-not-enabled, which points at --copy-and-swap again. That fails safe, and RunDesired plans in process, so nothing fires today. It becomes a live gap if a host ever hands AdmitPlanCopySwapRoute or the dry-run helpers a decoded report; the comment on the field says so, and a line in schemabot-integration.md next to the cause would tell an importer it can see the old cause for the same change.

Verified

  • Read every changed code file in full and the callers of closedCopySwapVerdict against routesToClosedCopySwap (an opened route or a --force run never reaches the new cause) and refusesClosedCopySwap (a resumed run keeps the old cause).
  • Read the six failing logs: one failing test each, the same assertion line and the same pool-size message; no other failure, and no infrastructure error, in any red leg.
  • Compiled by CI: build, lint, and the docs-guard unit tests pass. I ran no test locally; the new and changed scenarios need PostgreSQL, so the mutation table is by reading.
Mutant Caught by
RebuildsPartitionedIndexes never set in classifyOp TestClassifyRelabelOnAPartitionedParent, TestRunRefusesAParentRetypeThatWouldRebuildEveryPartitionIndex
rebuildsPartitionedIndexes drops the route == RouteNative check the "reads a column the change rewrites anyway" case in TestClassifyRelabelOnAPartitionedParent
closedCopySwapVerdict always returns the old cause the run, desired and CLI dry-run tests for the new cause
closedCopySwapPlanRefusal always returns the old cause TestRunDesiredRetypesAParentColumnByItsPartitionedIndexes (the indexed-column case)
!resumes guard removed survives: no test resumes a stopped run on a retype (non-blocking 2)
return false in the loops changed to continue survives: no mixed statement or plan (non-blocking 2)
Registry entry for the new site removed TestRefusalRegistryIsComplete

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon

aparajon commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

🤖 Review 2/2 (OSS adoption and integration) of ff4ffd3: 1 blocking, 2 non-blocking.

The blocking count is the PR-wide total from comment 1/2 (the red test); this comment adds no blocking finding of its own.

Read the change from two seats: an operator who hits the refusal on the CLI and has only its detail to go on, and a host that imports pkg/migrate and pkg/verdict and branches on cause. The surface that changes is a verdict token, which is a public contract: partitioned-index-rebuild now appears where copy-and-swap-not-enabled appeared for the same statement.

Non-blocking

  1. A host that branches on copy-and-swap-not-enabled sees the same change come back under a new cause, and nothing in the PR says so for importers. The old cause means "open the route", which a host can act on by setting CopySwapOptions.Enabled; the new one means the route would not run it either. docs/schemabot-integration.md is updated in the right place, and the new row in refusal-classes.md is clear that opening the route ends in copy-and-swap-partitioned. What is missing is a release-note line: none of the 20 files is a changelog or release note, and an importer that mapped the old cause to "enable copy-and-swap" now falls through to its default for an unknown cause. The reason and the class are unchanged (copy-and-swap-refused, capability-boundary), so a host that routes on those two keeps working. One sentence for the release notes, "a partitioned parent's retype that a partitioned index reads is now refused with cause partitioned-index-rebuild, not copy-and-swap-not-enabled", lets an importer find it.

  2. The detail is the whole remedy in one sentence of roughly 900 characters, and the docs show no rendering of it. partitionedIndexRebuildDetail packs the rebuild, the flags for the drop, the retype, the index recreation and the --force alternative into one line with nested parentheses. An operator reading it in a terminal has to find the two steps inside it. The PR body already has the clean version, a short cause line and the numbered path: using that in the verdict (a first line that says what stopped the run, then the two steps on lines of their own, which verdict.IndentDetail already lays out for the multi-line slot lag detail) would read better in the CLI and in a host's own rendering. cli-output-examples.md gains a table row but no example output, so a reader cannot see what the operator will see. The rendered refusal belongs in that file beside the row, the way the other copy-and-swap refusals are shown there.

What reads well for adopters

  • The PR answers a real dead end: the old refusal sent an operator to --copy-and-swap, which refused again as copy-and-swap-partitioned, and the new cause names a path that works. The before and after in the PR body traces one statement through both versions.
  • The cause lives under the same reason and class as the one it narrows, so an importer's coarse routing does not change, and the marker is json:"-" so the plan report and its fingerprint keep their format with no version bump.
  • Every doc that lists the closed route's causes is updated together, including capabilities.yaml and the matching capabilities.md row.
  • The new tests keep the old cause pinned for a rewriting retype on both a plain table and a partitioned parent, so the two causes cannot drift into each other.

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon aparajon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Stamping with comments: 1 blocking, 5 non-blocking (see the review comments above).

This stamp was left by Claude Code (claude-sonnet-5-5).

The test that opens the copy-and-swap route reached its pool-size check on
CI runners whose default pool holds 4 connections, not the 6 it needs.
@aparajon

aparajon commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

🤖 Delta review of 44f9374: 0 blocking, 5 non-blocking.

Delta since the reviewed ff4ffd37e5dbb2deb667905acf86db6e4e844eb6. The new head is 44f93742dcbe949d63d11ac22cb7a4d72fc070d1, one commit ahead (test(migrate): size the retype fixture's pool for copy-and-swap), touching one file: partitioned_parent_retype_integration_test.go (+5/-1). No production file changed, so the invariant dispositions in my earlier review (RF-7 extended, ST-6, AB-4, RF-10 upheld) stand as written. This is one comment for both of the earlier ones (1/2 correctness, 2/2 adoption).

Blocking

None. The one blocking finding is fixed.

Fixed: the red TestRunRefusesAParentRetypeThatWouldRebuildEveryPartitionIndexWithCopySwapOpened. retypeOrders now builds its pool with MaxConnsFloor: migrate.CopySwapMinConns(copier.Options{}), which is the fix I sketched and the floor the other copy-and-swap integration tests use, with a comment saying why. By reading the route, a run with the route opened now passes the CheckPool gate in copyAndSwap and reaches checkCopySwap, where the partitioned parent is refused with preflight.CopySwapCausePartitioned, which is the cause the test asserts. The helper is shared, so the other tests in the file get the larger pool too; each of them runs with the route closed (runOptions()), where the pool size is never consulted, so nothing else in the file changes meaning.

Whether it passes is CI's to say, not mine: I ran nothing here (see the CI line below).

Prior non-blocking findings

None of the five was addressed in this commit. All five stay open and none blocks the merge.

  1. The detail names --force and "remove it from the desired file" for both front doors (1/2 non-blocking 1). Not addressed. migrate --desired rejects --force, and a migrate --alter operator has no desired file.
  2. Untested branches of the new predicate (1/2 non-blocking 2): the mixed statement, the mixed plan, the !resumes guard, the desired-state dry run. Not addressed. The commit adds no test, so the two surviving mutants in my earlier table still survive.
  3. A decoded plan loses the json:"-" marker and gets the old advice (1/2 non-blocking 3). Not addressed. Still fails safe and still latent.
  4. No release-note line for the changed cause token (2/2 non-blocking 1). Not addressed.
  5. The roughly 900-character one-line detail, and no rendered example in cli-output-examples.md (2/2 non-blocking 2). Not addressed.

New defects in the new commit

None found. The commit is test-only and adds one import (pkg/copier) that build and lint already compile. One nit with no action needed: the new comment says the default pool is smaller "on a runner with few CPUs", and the failing log showed it at 4, so on a larger runner the old fixture could have passed; the floor makes the test independent of the runner either way.

Verified at the new head

  • Compare ff4ffd3...44f9374: ahead_by 1, one commit, one file. Read the full patch and the whole test file at the new head.
  • Read copyAndSwap at the new head for the gate order: not-enabled, connection required, checkCopySwapConnection, CheckPool, then checkCopySwap. The floor satisfies the second-to-last gate and leaves the last one to produce the asserted refusal.
  • CI at post time, for run 47019294827 on this head: build, lint, smoke test, aws-boundary, zizmor, Semgrep, DCO and Vulnerability Scan are green. The seven PostgreSQL legs (test (PostgreSQL 14), 15, 16, 17, 18, schemachange, 18, other packages, and Supabase compatibility) and the unit-test job were still running when I posted, so the fix is unproven by CI. The six legs that failed on ff4ffd3 all failed on this one test, so their result is the check on this commit; if any of them is red, that is a blocker and not covered by this approval's count.

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon aparajon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Stamping with comments: 0 blocking, 5 non-blocking (see the delta review comment above). The earlier blocker, the pool-size failure in the new test, is fixed by reading; CI on this head was still running when I posted.

This stamp was left by Claude Code (claude-sonnet-5-5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants