Skip to content

fix: skip parent-defined columns in partition diffs (#709) - #715

Open
Staberman wants to merge 1 commit into
pgplex:mainfrom
Staberman:fix/partition-add-column
Open

Staberman wants to merge 1 commit into
pgplex:mainfrom
Staberman:fix/partition-add-column

Conversation

@Staberman

Copy link
Copy Markdown

Summary

Adding, dropping or retyping a column on a partitioned table made plan emit the same ALTER TABLE for 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), so apply failed.

Fixes #709

Changes

  • internal/diff/table.go: in diffTablesWithTables, 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 in PARTITION OF are still diffed.
  • internal/diff/partition.go: small helpers (partitionFollowsParent, partitionParentHasColumn, withParentColumnType) with a comment explaining the rule.
  • New fixture testdata/diff/create_table/issue_709_partition_add_column covering ADD COLUMN, DROP COLUMN and ALTER COLUMN TYPE across two partitions.
  • Partitions that change parent or bound (DETACH/ATTACH, plan ignores partition bounds and attachment: a re-bounded or detached partition gives No changes detected #639) behave as before.

Testing

  • PGSCHEMA_TEST_FILTER="create_table/issue_709" go test ./internal/diff -run TestDiffFromFiles fails 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 TestPlanAndApply passes (applies the plan on embedded Postgres and checks the follow-up plan is empty).
  • go vet ./... and gofmt clean.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 3/5

[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.

Findings

  1. P1 Partition settings are skipped ▶
  2. P1 Constraint drops still fail ▶

Summary

The PR removes duplicate column additions, drops, and type changes from partitions that keep their parent and bound.

  • Partition column changes appear only on their parent.

Reviews (1) · Last reviewed commit: "fix: skip parent-defined columns in part..." · Reviewed by Greptile

Comment thread internal/diff/table.go
Comment on lines +218 to +220
if followsParent && partitionParentHasColumn(newTables, newTable, name) {
continue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 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.

Comment thread internal/diff/table.go
Comment on lines +238 to +240
if followsParent && partitionParentHasColumn(oldTables, oldTable, name) {
continue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Constraint drops still fail

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment thread internal/diff/table.go
Comment on lines +218 to +219
if followsParent && partitionParentHasColumn(newTables, newTable, name) {
continue
Comment thread internal/diff/table.go
Comment on lines +238 to +239
if followsParent && partitionParentHasColumn(oldTables, oldTable, name) {
continue

This branch has not been deployed

No deployments
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.

Adding a column to a partitioned table emits ADD COLUMN for its partitions and fails with SQLSTATE 42809

2 participants