There are several ways that we have created to create and maintain materialized views within Postgres. You can have it updated nightly via IS's cron job, via a trigger when certain tables are changed, or execute a function when you want it updated (such as logging into a program).
To create a materialized view, this is what you need to do:
SELECT * FROM tools.cs_refresh_mviews();
This just calls tools.cs_refresh_mviews(mv_id) with all the materialized views that have a sort order > 0 and in the order of the sort order.
SELECT * FROM tools.cs_refresh_mviews(mv_id);
This refreshes the specific view for which you have supplied the mv_id value. It first makes a copy of the existing view into the (mv_schema).t(mv_name). If the materialized view failed, it copies that data from the temp table back to the materialized view.
CREATE TRIGGER "schemaname_tablename_tr" AFTER INSERT OR UPDATE OR DELETE ON "schemaname"."tablename" FOR EACH STATEMENT EXECUTE PROCEDURE SELECT * FROM tools.cs_refresh_mviews_t(mv_id);
This is a trigger that can be attached attached to any table, so that when data is changed in the table the materialized view gets re-created. Internally this called the tools.cs_refresh_mviews(mv_id).
| mv_id | mv_schema | mv_name | mv_fields | mv_query | sort_key | replication_time |
|---|---|---|---|---|---|---|
| 1 | matviews | mv_crf_for_protocol_id | SELECT * FROM views.crf_for_protocol_id_mv(); | 1 | ||
| 2 | matviews | mv_crf_for_tla | SELECT * FROM views.crf_for_tla_mv(); | 2 | ||
| 4 | matviews | mv_employee_phone_list | SELECT * FROM views.employee_phone_list(); | 5 | ||
| 5 | matviews | mv_protocol_milestone_dates | SELECT * FROM views.protocol_milestone_dates_mv; | 3 | ||
| 6 | matviews | mv_protocol_milestones_dfperms | SELECT * FROM views.protocol_milestones_dfperms_mv; | 6 | ||
| 8 | matviews | mv_mpmybt_employee_data | SELECT * FROM views.mpmybt_employee_data; | 8 | ||
| 9 | protimp | protocols | select * from protimp.protocols_mv(); | 4 |
| mv_id | mv_schema | mv_name | mv_fields | mv_query | sort_key | replication_time |
|---|---|---|---|---|---|---|
| 1 | matviews | mv_crf_for_protocol_id | SELECT * FROM views.crf_for_protocol_id_mv(); | 1 | ||
| 2 | matviews | mv_crf_for_tla | SELECT * FROM views.crf_for_tla_mv(); | 2 | ||
| 4 | matviews | mv_employee_phone_list | SELECT * FROM views.employee_phone_list(); | 5 | ||
| 5 | matviews | mv_protocol_milestone_dates | SELECT * FROM views.protocol_milestone_dates_mv; | 3 | ||
| 6 | matviews | mv_protocol_milestones_dfperms | SELECT * FROM views.protocol_milestones_dfperms_mv; | 6 | ||
| 8 | matviews | mv_mpmybt_employee_data | SELECT * FROM views.mpmybt_employee_data; | 8 | ||
| 9 | protimp | protocols | select * from protimp.protocols_mv(); | 4 |
-- LloydAlbin - 25 Mar 2009