A simple method for periodically refresh materialized view PostgreSQL

PostgreSQL 9.3 began to support materialized views, added 9.4 CONCURRENTLY non-blocking options, but does not support similar START WITH ... NEXT ... the timing of the refresh option REFRESH.

How to achieve regular refresh materialized view? Baidu is mainly the result of the following three:

  • With operating systems such as timed task Linux / Unix or Windows in the crontab;
  • With plug-pgAgent;
  • Using a trigger, usually statement level (... FOR EACH STATEMENT ...).

Recently reach \ watch command, we found a new way to refresh materialized views.

Much explanation, directly on the sample code:

- Create a materialized view
the CREATE MATERIALIZED VIEW MAX_ID_MVIEW
AS
  the SELECT PART_ID, MAX (ID) max_id
  the FROM PART_DETAIL the GROUP BY PART_ID;
 
- Without CONCURRENTLY refresh when you do not need to create a unique index
CREATE UNIQUE INDEX IDX_MAX_ID ON MAX_ID_MVIEW (PART_ID );

- using the watch command refreshed every 120s materialized view
REFRESH MATERIALIZED VIEW CONCURRENTLY MAX_ID_MVIEW; \ watch 120

Tested effective, but the drawback is the need to keep in psql window it has been run.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159605.htm