pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

apply creates partition children as detached standalone tables (drops PARTITION OF ... FOR VALUES) — parent gets 0 attached partitions (v1.11.1)

Open saosebastiao opened this issue 5 months ago • 0 comments

Summary

When pgschema apply builds a partitioned table from scratch, it creates the parent correctly (PARTITION BY ...) but creates every child as a detached, standalone plain table. It drops the PARTITION OF <parent> FOR VALUES ... / DEFAULT attachment clause and copies the parent's column list into the child. The resulting parent has zero attached partitions (pg_inherits empty, child relispartition = false) and therefore cannot accept any inserts.

This looks related to #495 but is more fundamental. #495 concerns constraint metadata flags on partitions it assumes are already attached ("the partitions themselves remain correctly attached"). This report shows that on the create path, pgschema never attaches the children at all.

Environment

  • pgschema 1.11.1@7efe9b83 darwin/arm64 (2026-06-23)
  • PostgreSQL 17.7 (AWS Aurora)
  • External plan database (--plan-host), per-schema apply

Minimal reproduction (generic — no PostGIS, no parent constraint required)

Desired-state file schema.sql:

CREATE TABLE events (
  id bigint NOT NULL,
  region text NOT NULL,
  payload text
) PARTITION BY LIST (region);

CREATE TABLE events_us    PARTITION OF events FOR VALUES IN ('us');
CREATE TABLE events_eu    PARTITION OF events FOR VALUES IN ('eu');
CREATE TABLE events_other PARTITION OF events DEFAULT;

Run:

pgschema plan --schema demo --file schema.sql --host ... --plan-host ...

Expected DDL

Children emitted as ... PARTITION OF events FOR VALUES IN (...) / DEFAULT, attaching them to the parent.

Actual DDL

CREATE TABLE IF NOT EXISTS events (
    id bigint NOT NULL,
    region text NOT NULL,
    payload text
) PARTITION BY LIST (region);

CREATE TABLE IF NOT EXISTS events_eu    ( id bigint NOT NULL, region text NOT NULL, payload text );
CREATE TABLE IF NOT EXISTS events_other ( id bigint NOT NULL, region text NOT NULL, payload text );
CREATE TABLE IF NOT EXISTS events_us    ( id bigint NOT NULL, region text NOT NULL, payload text );

The PARTITION OF ... FOR VALUES / DEFAULT clauses are gone and the children carry inlined columns.

Resulting catalog state after apply

relation relkind relispartition attached_children
events p (partitioned) f 0
events_us / events_eu / events_other r (plain) f 0

The parent cannot route any row:

INSERT INTO events (id, region, payload) VALUES (1, 'us', 'x');
ERROR:  no partition of relation "events" found for row
DETAIL:  Partition key of the failing row contains (region) = (us).

The identical SQL run directly through psql produces 3 attached partitions and accepts inserts (verified: attached_children = 3, inserts succeed).

Variants tested — all fail identically

  1. LIST partitioning (above) — detached.
  2. RANGE partitioning (FOR VALUES FROM ... TO ...) — detached.
  3. Explicit ALTER TABLE <parent> ATTACH PARTITION <child> FOR VALUES ... in the desired file — pgschema omits the ATTACH statements entirely; children created detached.
  4. Parent with a PRIMARY KEY (the #495 shape) — still detached.
  5. Single inline --file schema.sql (no \i includes) — identical, so not an include artifact.

Impact

Any partitioned table pgschema builds from scratch is left non-functional (the parent cannot route inserts). This is a hard blocker for a from-scratch pgschema-managed rebuild of any environment containing partitioned tables, and it makes apply unsafe against existing partitioned production tables (pgschema's model has no representation of partition membership on the create path).

Suggested fix

On the create path, emit children as CREATE TABLE <child> PARTITION OF <parent> FOR VALUES ... / DEFAULT (or preserve an explicit ALTER TABLE ... ATTACH PARTITION present in the source), rather than as standalone CREATE TABLE with inlined columns.

saosebastiao avatar Jul 02 '26 19:07 saosebastiao