Repository navigation
Conversation
[Medium impact] The change affects migration plans for partitioned tables in one bounded case. Fix the lost partition settings and duplicate constraint drops before merging. FindingsSummaryThe PR removes duplicate column additions, drops, and type changes from partitions that keep their parent and bound.
Reviews (1) · Last reviewed commit: "fix: skip parent-defined columns in part..." · Reviewed by Greptile |
| if followsParent && partitionParentHasColumn(newTables, newTable, name) { | ||
| continue | ||
| } |
There was a problem hiding this comment.
Partition settings are skipped
Skipping the added column also skips its partition-specific settings. If events gains payload text and an existing partition declares payload DEFAULT 'pending', payload NOT NULL, or a column comment, the plan only adds the parent's column. The partition's settings are silently lost.
Keep skipping ADD COLUMN, but record the partition's overrides and comment for emission after the parent's addition.
| if followsParent && partitionParentHasColumn(oldTables, oldTable, name) { | ||
| continue | ||
| } |
There was a problem hiding this comment.
Skipping the partition's DroppedColumns leaves constraint drops unaware that the parent already removes them. For example, drop events.legacy when events_2026_01 has a local CHECK (legacy IS NOT NULL). The parent runs first by name and removes that check with the column. The partition then emits DROP CONSTRAINT for the missing check because its droppedColumnSet is empty, so apply still fails.
Track parent-driven column drops when deciding which partition constraints need explicit drops.
ADD COLUMN, DROP COLUMN and ALTER COLUMN TYPE on a partitioned table recurse to its partitions, and PostgreSQL rejects them on a partition (SQLSTATE 42809). The diff compared each partition's columns against the desired state on its own, so a plan for a changed parent also emitted the statements for every partition, and apply failed. While a partition keeps its parent and bound, a column the parent defines is now the parent's to add, drop and retype; the partition's diff only keeps what a PARTITION OF declaration can override (DEFAULT, NOT NULL). A partition that changes parent or bound is unchanged. Fixes pgplex#709
eb47cf1 to
f51bdcc
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Parent-driven column changes discard partition-specific defaults, nullability, and comments and fail to track dependent constraint removal, causing incorrect partition schemas and failing DROP CONSTRAINT statements.
2 open issues
What changed in this PR
Addresses #709 by preventing pgschema from emitting redundant column changes for partitions when PostgreSQL propagates those changes from the parent.
Changes:
- Suppresses partition-level column additions, drops, and type changes when the parent and bound remain unchanged.
- Adds helpers for attachment checks, parent-column lookup, and type normalization.
- Adds a regression fixture covering all three operations across two partitions.
| File | Description |
|---|---|
testdata/diff/create_table/issue_709_partition_add_column/plan.txt |
Records the parent-only plan summary. |
testdata/diff/create_table/issue_709_partition_add_column/plan.sql |
Records parent-only migration SQL. |
testdata/diff/create_table/issue_709_partition_add_column/plan.json |
Records structured migration steps. |
testdata/diff/create_table/issue_709_partition_add_column/old.sql |
Defines the initial parent and two partitions. |
testdata/diff/create_table/issue_709_partition_add_column/new.sql |
Defines the added, dropped, and retyped columns. |
testdata/diff/create_table/issue_709_partition_add_column/diff.sql |
Specifies the expected parent-only diff. |
internal/diff/table.go |
Suppresses redundant structural column changes on partitions. |
internal/diff/partition.go |
Adds parent-following and column-type helpers. |
Review effort: Balanced
| if followsParent && partitionParentHasColumn(newTables, newTable, name) { | ||
| continue |
| if followsParent && partitionParentHasColumn(oldTables, oldTable, name) { | ||
| continue |

Summary
Adding, dropping or retyping a column on a partitioned table made
planemit the sameALTER TABLEfor every partition as well. PostgreSQL applies those changes to the partitions itself and rejects them on a partition directly (cannot add column to a partition, SQLSTATE 42809), soapplyfailed.Fixes #709
Changes
internal/diff/table.go: indiffTablesWithTables, a partition that keeps the same parent and bound no longer gets ADD COLUMN, DROP COLUMN or an ALTER COLUMN TYPE for a column its parent defines. Defaults, NOT NULL and other overrides declared inPARTITION OFare still diffed.internal/diff/partition.go: small helpers (partitionFollowsParent,partitionParentHasColumn,withParentColumnType) with a comment explaining the rule.testdata/diff/create_table/issue_709_partition_add_columncovering ADD COLUMN, DROP COLUMN and ALTER COLUMN TYPE across two partitions.Testing
PGSCHEMA_TEST_FILTER="create_table/issue_709" go test ./internal/diff -run TestDiffFromFilesfails on main (plan has ADD COLUMN for both partitions) and passes now.go test ./internal/diff ./internal/plan ./ir/...pass.PGSCHEMA_TEST_FILTER="create_table/" go test ./cmd -run TestPlanAndApplypasses (applies the plan on embedded Postgres and checks the follow-up plan is empty).go vet ./...andgofmtclean.