Skip to content

migrate: let the dry run plan the online create of a partition - #691

Open
Kiran01bm wants to merge 4 commits into
mainfrom
kiran01bm/partition-of-dryrun
Open

Kiran01bm wants to merge 4 commits into
mainfrom
kiran01bm/partition-of-dryrun

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

migrate --dry-run now plans a bare CREATE TABLE … PARTITION OF … FOR VALUES … as the online create of a partition, with a new online-create-partition execution in the plan report (format version 47), instead of refusing it.

Why

A dry run is how an operator, or an orchestrator such as a CI gate, previews a change before it runs. The previous PR taught the run to add a partition without locking the partitioned table, but left the dry run refusing the same statement, so anything that gates on the dry run would block a change the run makes safely. This PR makes the preview say what the run will do.

What

  • The CLI dry run sends an admitted PARTITION OF down its own short path: the run's pre-read checks, then a plan on the partitioned parent (schema/table are the parent's) whose one statement executes with execution: online-create-partition and exec_sql the statement itself.
  • migrate.AdmitCreatePartition is the shared admission, used by both the run and the dry run: acknowledgements are a usage error, an unqualified parent is refused, and a copy-and-swap run stopped on a table of the new partition's name refuses it, each with the run's verdict. It is listed among the stopped-run checks the front-door walk test recognizes.
  • planner.ExecutionOnlineCreatePartition joins the closed execution set, and the fingerprint covers it (as it does online-attach), so a pin on the online plan cannot carry over to the statement run as written. plan.RouteOnlineCreatePartition marks the statement.
  • FormatVersion 46 → 47, with the version history, the execution table, and the fingerprint paragraph in docs/plan-report.md, every pinned example, and the demo's format_version assertions moved to 47.
  • The text dry run gets a note for the new execution. The capability row, limitations.md, and schemabot-integration.md now say the dry run plans it. Like the online attach's dry run, it does not read the parent's DEFAULT partition, which only the run refuses.

Before / after

The same statement through --dry-run and through the run:

CREATE TABLE app.orders_2027 PARTITION OF app.orders
  FOR VALUES FROM ('2027-01-01') TO ('2028-01-01');

Before
  migrate --dry-run --json  ->  refused, capability-boundary, exit 2
                                ("the dry run does not plan the online create yet")
  migrate (the run)         ->  executed: created standalone, attached online

After
  migrate --dry-run --json  ->  format_version 47, schema app, table orders,
                                disposition execute, exit 0
                                statement: execution online-create-partition,
                                           exec_sql [the CREATE as submitted]
  migrate (the run)         ->  executed: created standalone, attached online
                                (unchanged)

An unqualified parent or a DEFAULT bound is refused the same way by both.

First step of creating a partition online: the run creates the table
standalone and attaches it with the online attach, which needs the
CREATE's own bound as an ATTACH statement.
CREATE TABLE ... PARTITION OF takes ACCESS EXCLUSIVE on the parent; creating
the table with LIKE and running the online attach reaches the same partition
while the tree keeps serving reads and writes.
…a partition

The imperative door refused every CREATE TABLE and pointed at the
declarative door, which refuses PARTITION OF too, so adding a partition had
no route through pg-sprite.
The run creates a partition online while the dry run refused the statement,
so a gate on the dry run blocked a change the run would make.
@Kiran01bm
Kiran01bm marked this pull request as ready for review December 2, 2026 02:22
@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-02T02:22:54.644562Z
We couldn’t complete this review. Comment @codex review to try again.
1b66483 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 2, 2026

Copy link
Copy Markdown
Collaborator

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

Inspected the whole change at 1b66483ba1bca086224dba13661440a849fef9c7 (18 files, +364/-110) against the run it plans: runCreatePartitionDryRun against Run through onlineCreatePartition and runOnlineCreatePartition, and then into attachPartitionFlow.run. All 18 checks at this head pass. I ran no test: the new tests need a PostgreSQL container, and this review was done from the PR's files without a local build.

The PR makes migrate --dry-run of a bare CREATE TABLE ... PARTITION OF ... FOR VALUES plan the online create the run already performs, instead of refusing it. The shared admission (AdmitCreatePartition) now makes the same ack, attach-shape and stopped-run checks in both paths; the plan carries the new online-create-partition execution at format_version 47; the fingerprint covers it through hashesExecution. The side-effect profile is what a dry run needs: it makes only catalog reads (LiveFacts on the ATTACH, where ProveExpressions returns early because the statement has no probes, and findStoppedRun's one read), takes no advisory lock, creates no table, and writes nothing. The ATTACH strategy matches the run: the table is created standalone with LIKE, then attached by the online attach under the parent's advisory lock (LK-1), so the parent sees SHARE UPDATE EXCLUSIVE only for the ATTACH and the DEFAULT-partition scan is the run's own refusal. I found no blocking defect. The one place the dry run and the run disagree on an input the dry run now accepts is finding 1.

Invariants (docs/invariants.md): ST-2 (#L1238) is the entry this extends: the new admission reads a copy-and-swap run stopped on the new partition's name and is added to stoppedRunChecks (copy_swap_stopped_doors_test.go#L24-L30), so the PR upholds it; its *Enforced:* text lags (see 2/2, finding 3). LK-1 and ST-7 are upheld: the run keeps the parent's advisory lock and runs the statement the gate admitted against the parent the preflight verified (attach_partition_flow.go#L283-L288); the dry run reads and locks nothing the run does not.

Non-blocking

  1. The dry run reports an online create on a parent that is not partitioned as executable (exit 0); the run refuses it. dryrun_create_partition.go#L72-L76 routes to online-create-partition whenever the parent exists, and #L86 exits non-zero only for a missing table or a non-executing plan. The sibling attach dry run only routes RouteOnlineAttach inside if facts.Target.Partitioned() (dryrun.go#L184, the call at #L200), and RouteOnlineCreatePartition documents its input as a report "on the partitioned parent" (plan.go#L640-L647), but the create path has no such check. The planner classifies any PartitionOf as RouteNative (planner.go#L1797-L1802) without looking at the parent, so for CREATE TABLE s.child PARTITION OF s.plain_table FOR VALUES ... on an ordinary table the plan says the statement executes online. The run then passes CheckTable, the privilege checks and the index-connection check, and only the executor's admission refuses it with ErrNotPartitionedTable (attach_partition_flow.go#L289-L290), before createTable (#L318-L319), so nothing is created; the run ends as a failed verdict with an error, not a refusal. The cost is a green plan whose pinned fingerprint then fails at apply, which is the drift a dry run exists to rule out. It is also new at this head: before the PR the dry run refused every PARTITION OF statement, so a mistyped parent was never reported as executable. The fix is the sibling's guard: refuse (or fall through to the same refusal the run gives) when !facts.Target.Partitioned(), and say in the docs that a non-partitioned parent is refused. Reasoned from the code, not run.
Test case: the dry run and the apply agree on a non-partitioned parent (not run)
func TestMigrateAlterDryRunAgreesWithTheApplyOnANonPartitionedParent(t *testing.T) {
	url := testutil.StartPostgres(t)
	pool, err := dbconn.NewPool(t.Context(), dbconn.Config{URL: url})
	require.NoError(t, err)
	t.Cleanup(pool.Close)
	schema := testutil.NewSchema(t, pool)
	_, err = pool.Exec(t.Context(), fmt.Sprintf(
		`CREATE TABLE %[1]s.orders (id bigint NOT NULL, day date NOT NULL)`, schema))
	require.NoError(t, err)
	sql := fmt.Sprintf(`CREATE TABLE %[1]s.orders_2027
	    PARTITION OF %[1]s.orders
	    FOR VALUES FROM ('2027-01-01') TO ('2028-01-01')`, schema)

	cmd := newMigrateCmd(url, sql)
	cmd.DryRun = true
	_, dryErr := runMigrate(t, cmd)
	_, runErr := runMigrate(t, newMigrateCmd(url, sql))

	require.Error(t, runErr, "the apply cannot create a partition of a table that is not partitioned")
	assert.Error(t, dryErr, "the dry run must not report it executable")
}
  1. The decision line for the statement still says the parent is locked, next to a note that says it is not. For a PARTITION OF the planner's decision keeps ReasonPartitionParentLock (the planner.go#L1802 branch above, which does not know the execution is rewritten later), and the text renderer prints it as "takes a brief exclusive lock on the partitioned parent, briefly blocking access to every partition" (dryrun_text.go#L561-L562, used for the decision line at #L184). Two lines away the new note says the online create "does not lock the parent" (#L400). An operator reading the plan before a busy-table change sees both claims about the same statement; the JSON plan carries the same partition-parent-lock reason beside online-create-partition. The online path does hold the parent briefly (the ATTACH takes SHARE UPDATE EXCLUSIVE, and LIKE reads it), so the accurate statement is neither of the two. Either route the decision's reason for this execution (so the online create reports what the attach does) or word the note so it does not contradict the decision. I read the two strings and the call sites; I did not render the output.

  2. The new surface has integration tests only, and three behaviors this PR adds have no test. TestMigrateAlterCreatesAPartitionOnline and the refusal test cover the happy path, the plan's table and fingerprint, an unqualified parent and a DEFAULT bound; both need a container. Compare the online attach, which has plan-level unit tests for the same contract: TestFingerprintCoversOnlineAttachExecution (plan_test.go#L1336) and TestRouteOnlineAttachLiftsOnlyABoundedAttach (plan_test.go#L1359). I found no counterpart for the create: nothing pins that RouteOnlineCreatePartition leaves a refused or non-PARTITION OF statement alone, or that hashesExecution (plan.go#L1037-L1041) distinguishes the new value without a database. Also untested: (a) the dry run's refusal over a copy-and-swap run stopped on a table of the new partition's name, the one behavior AdmitCreatePartition adds beyond the attach shape (the doors test checks that admitCreatePartition is named, not that it refuses); (b) the dry run on a parent that does not exist, which the doc comment promises exits with the refusal code (dryrun_create_partition.go#L24-L25); (c) the run's stopped-run check now happening before the parent's preflight and privilege checks (online_create_partition.go#L105 against #L151). (c) is a harmless reorder, but it is a change in which verdict a role without privileges sees when a stopped run exists, and nothing pins it. A unit test per the attach pair, plus an integration case for (a), closes it.

Verified

  • Read the diff in full and the surrounding code at the head: the CLI dry run, AdmitCreatePartition/admitCreatePartition, the run's branch at migrate.go#L354-L357, refuseOverStoppedRun (copy_swap_stopped.go#L187), createTable and refuseTableWithRows in the executor, the planner's PartitionOf branch, the plan router and fingerprint, and the text renderer. Every cited line was re-read at the head.
  • The dry run is read-only: no DDL, no catalog write, no advisory lock. The admission it shares with the run runs the same ack, shape and stopped-run checks in the same order, and a missing parent is reported as a missing table with the refusal exit.
  • The run's lock levels are unchanged by this PR; the DEFAULT-partition refusal stays run-only, and the docs say so (docs/limitations.md and docs/plan-report.md both state it).
  • Tests: not run here (all of them need PostgreSQL). Mutation table below is by reading, not by running:
Mutant Caught by
RouteOnlineCreatePartition not called in the dry run TestMigrateAlterCreatesAPartitionOnline (asserts the execution), needs a container
hashesExecution ignores the new execution the same test's NotEqual on the fingerprint, needs a container; no unit test (finding 3)
refuseOverStoppedRun removed from admitCreatePartition TestEveryDoorChecksForAStoppedRunBeforeItExecutes flags a door with no check; no behavior test (finding 3)
the create dry run routes a non-partitioned parent to online-create-partition (as at the head) survives: no test plans a non-partitioned parent (finding 1)
ReasonPartitionParentLock text unchanged for the online create not caught: no test reads the decision line (finding 2)
AdmitCreatePartition skips the unqualified-parent refusal TestMigrateAlterDryRunRefusesAPartitionTheOnlineCreateDoesNotTake, "unqualified parent" case

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

@aparajon

aparajon commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

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

Read the change from two seats: an operator or CI author who pins a migrate --dry-run fingerprint and applies it later, and a host or downstream importer that links pkg/migrate or parses the plan report and dispatches on execution. The exported surface that moves is migrate.AdmitCreatePartition (new) and migrate.CreatePartitionDryRunVerdict (removed), plus one new value in the closed execution set at format_version 47.

Non-blocking

  1. The dry run's documented limits name one run-only refusal; the dry run leaves out four. docs/limitations.md#L24, the online-create-partition row in docs/plan-report.md#L245 and the capabilities.yaml note all say the plan refuses "the same shapes" and that only the parent's DEFAULT partition is left to the run. The run also makes checks the dry run does not: the parent must be partitioned (review 1/2, finding 1; attach_partition_flow.go#L289-L290), the connected role needs the index-build tier on the parent and create rights in the new table's schema (online_create_partition.go#L155-L168), the index-connection check (#L170), and a same-named table that holds rows (create_partition_online.go#L126-L140); the plan-report row says the last is refused but not that the plan does not show it. A reader who treats a green plan as "this will apply" is told one exception and meets four more. List them in one sentence in the plan-report row (the reference for plan consumers) and shorten the others to point at it. If finding 1 of 1/2 is fixed, the first of the four drops out.

  2. The reference page still tells readers to do by hand what migrate --alter now does. The CREATE TABLE ... PARTITION OF row of the lock table (docs/postgres-online-ddl-reference.md#L264) is right about PostgreSQL run as written ("ACCESS EXCLUSIVE on parent", not online), and ends "create the table on its own and attach it instead". It has no pointer to the online-create-partition execution, which does exactly that, so an operator who lands on this page first rebuilds the sequence by hand and never learns the tool runs it. One clause ("pg-sprite migrate --alter runs a bare PARTITION OF as this sequence; see the limitations row") closes it. The page is the one adopters search for the lock levels, which is why it matters more than its size suggests.

  3. ST-2's *Enforced:* text does not list the new door. The entry enumerates every door that reads a stopped copy-and-swap run before acting on the table (docs/invariants.md#L1270-L1285): the front door, refuseOverStoppedRun's callers for an index rename and a reviewed plan's drop, the desired-plan doors, the row security doors, and the dry-run admissions. admitCreatePartition is now a door (it calls refuseOverStoppedRun for the new partition's name, online_create_partition.go#L105) and is in stoppedRunChecks, so the walking test holds it, but the registry does not say so, and the registry is what a reviewer of a later create path reads. The PR upholds ST-2 and needs no change to the rule; add admitCreatePartition (and AdmitCreatePartition in the dry run) to the sentence, as the enforcement moved.

  4. Library and plan consumers get no upgrade note for the removed export and the new execution value. migrate.CreatePartitionDryRunVerdict is removed and AdmitCreatePartition takes its place with a different shape (it returns the ATTACH, a verdict, and a refused flag, and may return a usage error for an acknowledgement). A host that imported the old function to forecast the refusal stops compiling, and nothing in docs/ or a doc comment says what to call instead; the new function's comment is good for a reader of the code but does not mention the removal. On the plan side, online-create-partition joins the closed set a consumer dispatches on (docs/plan-report.md#L381); the version note (#L56) and the contract row (#L245) are complete, but the page has no example payload for any online execution, so a consumer must infer from prose what the statement's exec_sql carries (the statement itself, which the PR's own test asserts but the row does not say in those words). One sentence in the changelog or the PR body naming the replacement, and an example statements[0] in the plan-report row, would cover both.

What reads well for adopters

  • The plan reports the change on the partitioned parent, not on the not-yet-existing table, so an existing check that keys on table does not meet a name that is not in the catalog; the docs state it in the plan-report row, the capabilities.yaml note and the AdmitCreatePartition comment.
  • Resuming is documented in the same breath as the contract ("running the same statement again resumes from it, and a table of that name that holds rows is refused"), which is what an operator needs after an interrupted run.
  • The fingerprint behavior is stated and tested: a plan pinned for the online create cannot carry over to the same statement run as written (TestMigrateAlterCreatesAPartitionOnline), so an old pin fails closed rather than applying the blocking form.
  • The dry run's refusal for shapes the online create does not take (unqualified parent, DEFAULT bound) carries the same reason and class as the apply's, and the test asserts the two agree, which is the property a CI author scripting --dry-run needs.
  • docs/cli.md and docs/cli-output-examples.md move to version 47 with the change.

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, 7 non-blocking (see the review comments above).

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

@JashLal

JashLal commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

🤖 I reviewed all 18 changed files at 1b66483ba1bca086224dba13661440a849fef9c7, including the shared admission, the dry-run route and fingerprint, and the report version 47 fixtures and docs.

Verify the stopped-child refusal still names the parent. The old apply path called the refusal helper with the parent-targeted ATTACH and then restored the submitted CREATE as the verdict statement. The shared path now gives the helper the child-targeted CREATE directly. This risks reporting the child in a verdict whose documented target is the parent. Preserve the parent-targeted input and restore the original SQL on refusal, or demonstrate that both dry-run and apply still report the parent. A parity test with a stopped checkpoint on the child would exercise the branch the new tests leave uncovered. Admission change

GitHub reported all 18 checks successful. I did not run local tests. Direct Builderbot lookup failed to resolve its host. GitHub showed no current-head Builderbot check or review, and I did not independently enumerate unresolved inline threads.

@JashLal JashLal 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.

🤖 Approved by Codex on Jash’s behalf after reviewing the current head. See my substantive review comment for findings and validation limits.

Base automatically changed from kiran01bm/partition-of-route to main December 3, 2026 04:30
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.

3 participants