pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

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

Open neglect-yp opened this issue 2 weeks ago • 0 comments

Summary

When adding a column to an existing partitioned table, pgschema plan emits ALTER TABLE ... ADD COLUMN for both the parent and each existing partition. PostgreSQL propagates the parent's column addition to its partitions, and rejects the subsequent direct column addition on a partition. Consequently, pgschema apply fails.

Versions tested

  • pgschema v1.14.1
  • PostgreSQL 17.10

Reproduction

Existing schema (old.sql):

CREATE TABLE events (
    id bigint NOT NULL,
    created_at date NOT NULL
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_01 PARTITION OF events
    FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');

new.sql adds only a nullable column to the parent's declaration:

CREATE TABLE events (
    id bigint NOT NULL,
    payload text,
    created_at date NOT NULL
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_01 PARTITION OF events
    FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');

Actual result

Both versions generate the following SQL:

ALTER TABLE events ADD COLUMN payload text;

ALTER TABLE events_2026_01 ADD COLUMN payload text;

apply exits with status 1:

Error: failed to execute concatenated statements in group 1: ERROR: cannot add column to a partition (SQLSTATE 42809)

In this reproduction, the execution group is rolled back: neither table has payload after the failure.

Expected result

Generate only the parent column addition:

ALTER TABLE events ADD COLUMN payload text;

Applying it should add the column to the parent and its existing partitions, and a subsequent plan against new.sql should report no changes.

neglect-yp avatar Nov 18 '26 06:11 neglect-yp