pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

`DROP` of materialized view for refactor - does not recognize that view is materialized

Open saosebastiao opened this issue 7 months ago • 0 comments

Severity: Critical (apply fails when modifying any MV that has indexes to add or any MV referenced by another object pgschema wants to recreate).

Repro: add an index to an existing materialized view in your desired state:

-- desired state file
CREATE MATERIALIZED VIEW IF NOT EXISTS my_mv AS SELECT ...;
CREATE INDEX my_mv_x_idx ON my_mv (x);  -- index NOT in current state

Actual: pgschema generates:

DROP VIEW IF EXISTS my_mv RESTRICT;  -- BUG: should be DROP MATERIALIZED VIEW
-- ... index creation skipped because the DROP fails

Postgres errors:

ERROR: "my_mv" is not a view (SQLSTATE 42809)

IF EXISTS only suppresses error when the object doesn't exist at all; it does NOT suppress error when the object exists with a different relkind.

Suggested fix: check pg_class.relkind before issuing DROP; emit DROP MATERIALIZED VIEW IF EXISTS for relkind 'm', DROP VIEW IF EXISTS for 'v'.

saosebastiao avatar Apr 27 '26 19:04 saosebastiao