apply creates partition-child PK/UNIQUE as LOCAL not INHERITED → plan never converges (v1.11.1)
Summary
When pgschema apply builds a partitioned table whose UNIQUE/PRIMARY KEY is declared on the parent, it creates each partition child's constraint as a local constraint (pg_constraint.conislocal = true, coninhcount = 0, no conparentid) instead of an inherited one. PostgreSQL's own behavior — and the state produced by pg_dump, or a plain CREATE TABLE — is that the child constraint is inherited from the parent (conislocal = false, coninhcount = 1, conparentid → parent constraint).
pgschema's planner expects the inherited form. On every subsequent plan it sees the local form in the catalog, decides the constraint differs, and emits a DROP + re-ADD of the identically-named, identically-defined constraint — forever. The plan never reaches No changes detected, even immediately after a successful apply of the same files.
Reproduction
Desired-state file (demo.sql) — parent declares the UNIQUE; children are bare PARTITION OF:
CREATE TABLE demo.p (
band_id int NOT NULL,
geom_id int NOT NULL,
UNIQUE (band_id, geom_id)
) PARTITION BY LIST (band_id);
CREATE TABLE demo.p_12 PARTITION OF demo.p FOR VALUES IN (12);
CREATE TABLE demo.p_13 PARTITION OF demo.p FOR VALUES IN (13);
pgschema apply --schema demo --file demo.sql ...against an empty schema → succeeds.pgschema plan --schema demo --file demo.sql ...against the just-applied database.- Expected:
No changes detected. - Actual:
Plan: 2 to modify.— for each partition:
(same name, same definition — a no-op drop + re-add that never converges)~ p_12 - p_12_band_id_geom_id_key (constraint) + p_12_band_id_geom_id_key (constraint)
- Expected:
Root cause evidence
For one partition child the constraint definition is byte-identical (pg_get_constraintdef matches), but the inheritance metadata differs by how it was built:
| Built by | conislocal |
coninhcount |
conparentid <> 0 |
subsequent plan |
|---|---|---|---|---|
| pgschema apply | true |
0 |
false |
perpetual drop + re-add |
| pg_dump / Sqitch / manual | false |
1 |
true |
clean (No changes) |
pgschema should either create the child constraint as inherited (add the constraint to the parent and let Postgres propagate it, rather than ALTER TABLE <child> ADD CONSTRAINT directly), or its planner should treat a local child constraint whose definition matches the parent's inherited constraint as equal.
Impact
Any environment pgschema builds from scratch that contains partitioned tables can never be verified clean. In our real migration this was ~95 constraints across 3 schemas (25 partitioned parents, 94 partition children).
.pgschemaignore does not help. Adding:
[constraints]
patterns = ["p_.*_band_id_geom_id_key"]
has zero effect on the churn (identical diff count with and without the pattern) — the exclusion appears not to match partition-propagated/derived constraints, so there is no user-side workaround.
Possibly related (same fresh partitioned-schema apply)
A standalone CREATE SEQUENCE (referenced by a column DEFAULT nextval(...)) is reported by plan as sequences: 1 to add, but after apply the sequence does not exist in the target (pg_sequences count = 0), and the next plan again reports 1 to add. It is never materialized. Happy to split this into a separate issue if preferred.
Environment
- pgschema 1.11.1@7efe9b83 (darwin/arm64)
- PostgreSQL 17.7 (AWS Aurora)
- External plan database (
--plan-host) with PostGIS; per-schema apply in dependency order.