Skip to content

executor: add columns of a create set's types to live tables - #735

Merged
aparajon merged 2 commits into
mainfrom
armand/create-set-alters
Dec 4, 2026
Merged

aparajon merged 2 commits into
mainfrom
armand/create-set-alters

Conversation

@aparajon

@aparajon aparajon commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Why

A change that creates an enum type and adds a column of it to a table the target already holds has no way to commit both together. A create set holds only new types and new tables. Run as separate steps, the type commits first, so a failed ALTER leaves the type behind with no column using it. A caller that wants all or nothing has to refuse the change and ask for two.

What

  • CreateSet gains Alters: changes to live tables, each a preflight.CheckTable proof plus an ALTER TABLE statement. They run after the set's types and tables, in the same transaction, so the change makes all of them or none.
  • An alter is admitted only when it targets its proof's table in the set's schema (ST-7). It must only add columns, at least one of a type the set creates. Columns may carry no constraint, no generated value, no default that is not a constant, and no NOT NULL without a default. Anything else is ErrCreateSetAlter, before anything runs.
  • Alters run as the session's own role, as a native step does, since the creation role owns what the set creates, not the live table.
  • Adding a column takes ACCESS EXCLUSIVE, so each attempt first waits out the altered tables' long-lived readers and writers.
  • PlanCreateSetWithAlters lists the alters last and admits them as execution does. PlanCreateSet is unchanged.
  • The integration guide and ST-7's Enforced line describe the alters.

Before and after

A change creates order_kind and adds kind order_kind to the live orders table, and the ALTER fails, for example because the column already exists.

Before                                  After

┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ step 1: CREATE TYPE order_kind   │    │ one transaction:                 │
│ (commits)                        │    │ CREATE TYPE order_kind           │
└────────────────┬─────────────────┘    │ ALTER TABLE orders ADD kind      │
                 │                      └────────────────┬─────────────────┘
                 ▼                                       │ ALTER fails
┌──────────────────────────────────┐                     ▼
│ step 2: ALTER TABLE orders       │    ┌──────────────────────────────────┐
│ fails                            │    │ rolled back: no type, no column  │
└────────────────┬─────────────────┘    └──────────────────────────────────┘
                 ▼
┌──────────────────────────────────┐
│ order_kind left behind, unused   │ ✗
└──────────────────────────────────┘

How

An alter is checked twice: against its proof, and by its parsed operations through statement.ParseOps, so the shape rule comes from the real grammar. The live-table lock wait reuses awaitLockQueue. An altered table's entry replaces the writers-only entry a foreign key would give the same table. The statements run under the schema then public, the search path a native attempt uses.

Risk

The new path runs only when a caller passes Alters. Existing sets run exactly as before. Upholds ST-7: each alter runs only against the table its proof verified. Upholds LK-2: alters run under the set's lock_timeout and bounded retry, after the holder wait.

Upgrade notes

None. PlanCreateSet and CreateSet without Alters are unchanged.

🤖 Generated with Claude Code

Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon
aparajon marked this pull request as ready for review December 4, 2026 11:23
@JashLal

JashLal commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

🤖 Reviewed 41b04ad. Two findings affect the atomicity and lock guarantees of adding a column through a create set.

  1. [P1] Verify the live-column outcome before committing the enum. The new admission checks have no guard for conditional adds, and readback verifies the new objects alone. If ADD COLUMN IF NOT EXISTS kind order_kind reaches execution with orders.kind already present as text, PostgreSQL skips the add successfully and the enum can commit without the required live column. Reject conditional alters at admission or verify the resulting column type before committing. Extend the duplicate-column rollback test to pin the conditional case. Alter execution and readback
  2. [P1] A second column can force a heap and index rewrite under the set's lock. The shape gate requires one set-type column but admits other column types based on syntax alone. An alter that adds kind order_kind alongside quantity app.positive_quantity DEFAULT '1', where positive_quantity is a constrained domain over integer, matches the same qualified-type and quoted-literal shape as the accepted test case. PostgreSQL rewrites the table and its indexes for a constrained-domain add even with a literal default. Restrict live-table adds to types known to be fast or check the resolved type and reject constrained domains before the alter. Admission · PostgreSQL ALTER TABLE

I reviewed the full diff statically.

The automated review result was unavailable. No local tests were run; review of surrounding source at this exact head was limited to the full diff.

A create set refuses ADD COLUMN IF NOT EXISTS. When the column already
exists, PostgreSQL skips the add, and the set would commit its enum type
with a column of another type left in place.

Before the set runs, the executor now asks the server what each added
column costs, with the add-column probe the planner uses. It refuses
the set unless every column is proven to add on the catalog alone. A
column whose type rewrites the table, such as a domain with a CHECK,
would otherwise rewrite it under the set's lock.

The probe now names a type the same change creates by its bare name
when the statement qualifies it with the altered table's schema, so a
column of app.order_kind resolves to the probe's copy of the type.

Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon

aparajon commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 Both findings are fixed in e05e83c.

  • Conditional alters: a create set now refuses ADD COLUMN IF NOT EXISTS at admission, before anything runs. The duplicate-column test gained the conditional case: with orders.kind already text, ADD COLUMN IF NOT EXISTS kind order_kind refuses with ErrCreateSetAlter, and the type is not created.
  • A second column that rewrites the table: before the set runs, the executor now asks the server what each added column costs, with the planner's add-column probe (preflight.AddColumnRewrites). The set is refused unless every column is proven to add on the catalog alone. quantity app.positive_quantity DEFAULT '1', a domain with a CHECK, now refuses with "has a type that rewrites", and nothing runs. A column that runs user code, needs a NOT NULL scan, or could not be proven because a lock was held is refused the same way.
  • While wiring the proof in, I found that the probe could not resolve a column of the set's own type when it was written with its schema (app.order_kind), because that type does not exist until the set runs. The probe now uses the bare name for a type the change creates, which also fixes the planner's proof for that spelling. A new integration test covers it.

— Claude Code (Claude Opus 5.5)

@aparajon
aparajon merged commit b9196fb into main Dec 4, 2026
18 checks passed
@aparajon
aparajon deleted the armand/create-set-alters branch December 4, 2026 16:45
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