pgschema
pgschema copied to clipboard
Composite type creation runs before its required table
Environment
- pgschema 1.13.1
- PostgreSQL 18.6
Minimal reproduction
Use two empty databases: a target database and a planning database.
Create schema.sql. The table appears before the composite type:
CREATE TABLE example (
id integer PRIMARY KEY
);
CREATE TYPE example_result AS (
rows example[]
);
Apply it:
pgschema apply \
--host "$PGHOST" --user "$PGUSER" --password "$PGPASSWORD" \
--db "$TARGET_DB" \
--plan-host "$PGHOST" --plan-user "$PGUSER" \
--plan-password "$PGPASSWORD" --plan-db "$PLAN_DB" \
--schema public --file schema.sql --auto-approve
Expected result
The apply succeeds. It creates example before example_result.
PostgreSQL creates the table’s row type automatically. The composite type can then reference example[].
Actual result
The generated migration creates example_result before example. The apply fails:
ERROR: type "example[]" does not exist (SQLSTATE 42704)
The generated ordering ignores the dependency, despite the correct order in the input file.
Suspected cause
generateCreateSQL() classifies all non-domain types as typesWithoutDeps. Composite types can depend on table-row types, but this classification does not account for them.