Sub topic 10
Altering, Renaming, and Dropping Materialized Views
This chapter assumes that you know basic SQL, PostgreSQL indexes, and the difference between a table and a plain view. It also assumes that sales_daily is an existing materialized view built from an orders table.
A materialized view is a stored query result. You can manage it with many commands that also apply to tables, but its defining query has an important limitation: PostgreSQL does not provide a command to edit that query in place. To change the query, create a replacement materialized view and migrate callers to it.
Renaming a materialized view
Use ALTER MATERIALIZED VIEW ... RENAME TO to change the relation name:
ALTER MATERIALIZED VIEW sales_daily
RENAME TO sales_by_day;The stored rows and the materialized view's indexes remain in place. PostgreSQL also preserves dependencies that refer to the relation internally. For example, a dependent object can continue to refer to the renamed materialized view.
After the rename, use the new name in SQL commands:
SELECT *
FROM sales_by_day
ORDER BY day DESC
LIMIT 7;
REFRESH MATERIALIZED VIEW sales_by_day;A rename does not change the query that defines the materialized view. It also does not automatically rename every related object. For example, an index might retain its old name:
CREATE UNIQUE INDEX sales_daily_day_idx
ON sales_by_day (day);The index now belongs to sales_by_day, even though its name contains sales_daily. Rename it separately if the name matters for administration:
ALTER INDEX sales_daily_day_idx
RENAME TO sales_by_day_day_idx;Moving or changing ownership
ALTER MATERIALIZED VIEW also supports administrative changes such as changing the owner or schema:
ALTER MATERIALIZED VIEW sales_by_day
OWNER TO reporting_user;
ALTER MATERIALIZED VIEW sales_by_day
SET SCHEMA reporting;The role running these commands needs the required privileges. Moving a materialized view to another schema changes how clients address it. Code that uses public.sales_by_day must use reporting.sales_by_day afterward unless the search path resolves the new schema.
A schema move does not move the materialized view's data to another database. PostgreSQL objects exist inside one database, so a cross-database move requires an export and import or another migration process.
Changing the defining query
PostgreSQL does not support this form:
-- Not valid PostgreSQL:
ALTER MATERIALIZED VIEW sales_by_day
AS SELECT ...;To change the definition, build a new object. The safest approach depends on how much downtime you can accept. A simple migration can use a temporary name:
CREATE MATERIALIZED VIEW sales_by_day_new AS
SELECT order_date AS day,
count(*) AS orders,
sum(total) AS revenue
FROM orders
GROUP BY order_date;
CREATE UNIQUE INDEX sales_by_day_new_day_idx
ON sales_by_day_new (day);
BEGIN;
ALTER MATERIALIZED VIEW sales_by_day
RENAME TO sales_by_day_old;
ALTER MATERIALIZED VIEW sales_by_day_new
RENAME TO sales_by_day;
COMMIT;This changes the names quickly, but it can break dependencies, permissions, grants, and application queries that refer to the old relation. Copy those settings as part of the migration. Also consider that the new materialized view is populated before the name switch, which avoids exposing an empty replacement.
Dropping a materialized view
Use DROP MATERIALIZED VIEW when the stored result and its definition are no longer needed:
DROP MATERIALIZED VIEW sales_by_day;The default behavior is RESTRICT. PostgreSQL refuses the drop if other objects depend on the materialized view. This default helps prevent accidental breakage.
Use IF EXISTS when a deployment may run against a database where the object is absent:
DROP MATERIALIZED VIEW IF EXISTS sales_by_day;Use CASCADE only when you have reviewed the dependency tree:
DROP MATERIALIZED VIEW sales_by_day CASCADE;CASCADE can remove dependent views, rules, and other objects. It can also leave applications without objects they expect. Prefer RESTRICT during normal operations, and use CASCADE only in a deliberate migration or cleanup.
| Operation | Keeps stored rows | Changes query definition | Main risk |
|---|---|---|---|
| Rename | Yes | No | Callers or scripts still use the old name |
| Move schema | Yes | No | Search paths and permissions change |
| Drop | No | Removes object | Dependencies may be removed with `CASCADE` |
Exercise
Why should you use RESTRICT instead of CASCADE when dropping a materialized view during routine administration?
Show answer
RESTRICT refuses the drop when dependent objects exist, which exposes possible breakage before anything is removed. CASCADE can remove those dependent objects automatically.