pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

plan ignores partition bounds and attachment: a re-bounded or detached partition gives No changes detected

Open akuzminsky opened this issue 1 month ago • 0 comments

Version: v1.13.1 (darwin/arm64), PostgreSQL 18.6 (postgres:18), default embedded plan database

Summary

plan does not compare a partition's bounds or whether it is attached. A partition re-attached with different bounds, or detached altogether, gives No changes detected., although dump of the same database shows both differences. The partition fields of a table (PartitionOf, PartitionBound) appear to be used only when creating a table (internal/diff/table.go), not when comparing one.

This matters for drift detection: an empty plan is taken to mean the schema matches, and here it does not.

Reproduction

-- schema.sql
CREATE TABLE events (
    id int NOT NULL,
    at date NOT NULL
) PARTITION BY RANGE (at);

CREATE TABLE events_2026_01 PARTITION OF events FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE events_2026_02 PARTITION OF events FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
  1. pgschema apply --db r2 --file schema.sql --auto-approve, then pgschema plan --db r2 --file schema.sql: No changes detected.

  2. Change the bounds of one partition on the target:

    ALTER TABLE events DETACH PARTITION events_2026_01;
    ALTER TABLE events ATTACH PARTITION events_2026_01 FOR VALUES FROM ('2026-01-01') TO ('2026-01-15');
    

    pgschema plan --db r2 --file schema.sql: No changes detected.

  3. Detach the other one:

    ALTER TABLE events DETACH PARTITION events_2026_02;
    

    pgschema plan --db r2 --file schema.sql: No changes detected.

The database at that point:

    relname     | relispartition |                      bound
----------------+----------------+--------------------------------------------------
 events_2026_01 | t              | FOR VALUES FROM ('2026-01-01') TO ('2026-01-15')
 events_2026_02 | f              |

and pgschema dump --db r2 sees it correctly:

CREATE TABLE IF NOT EXISTS events_2026_01 PARTITION OF events FOR VALUES FROM ('2026-01-01') TO ('2026-01-15');
...
CREATE TABLE IF NOT EXISTS events_2026_02 (
    id integer NOT NULL,

When the detached table keeps constraints from the parent (in our case its primary key and a foreign key, on a parent with an identity column), the plan does show changes. But they drop those constraints and re-add the identity, and never re-attach the table. After applying them the plan is empty and the table is still detached.

Expected

A plan that brings the partitions back to the desired state, e.g. ALTER TABLE events DETACH PARTITION events_2026_01; followed by ALTER TABLE events ATTACH PARTITION events_2026_01 FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');, and ATTACH PARTITION for events_2026_02. At the least, a non-empty plan.

akuzminsky avatar Oct 09 '26 23:10 akuzminsky