Is there a postgres command to list/drop all materialized views?

Pure SQL Show all: SELECT oid::regclass::text FROM pg_class WHERE relkind = ‘m’; Names are automatically double-quoted and schema-qualified where needed according to your current search_path in the cast from regclass to text. In the system catalog pg_class materialized views are tagged with relkind = ‘m’. The manual: m = materialized view To drop all, you … Read more

Materialized View vs. Tables: What are the advantages?

Dynamic query rewriting. Materialized views define not only relationships, but also allow you to precompute expensive joins and aggregations. The optimizer is smart enough to use the MV to fetch relevant data even if the MV isn’t explicitly used in the query (given DB settings, etc). Your question was tagged as Oracle, but MSSQL also … Read more

Refresh a materialized view automatically using a rule or notify

You should refresh the view in triggers after insert/update/delete/truncate for each statement on table1 and table2. create or replace function refresh_mat_view() returns trigger language plpgsql as $$ begin refresh materialized view mat_view; return null; end $$; create trigger refresh_mat_view after insert or update or delete or truncate on table1 for each statement execute procedure refresh_mat_view(); … Read more

How can I ensure that a materialized view is always up to date?

I’ll need to invoke REFRESH MATERIALIZED VIEW on each change to the tables involved, right? Yes, PostgreSQL by itself will never call it automatically, you need to do it some way. How should I go about doing this? Many ways to achieve this. Before giving some examples, keep in mind that REFRESH MATERIALIZED VIEW command … Read more