I am trying to mimic snapshot materialized view based on this article on valena.com and have created the materialized views that I need.
My next task is to execute the refresh materialized view scripts on a nightly basis in PostgreSQL. I am using pgAdmin and found out that I need to install pgagent on my database server (Linux) and create jobs in pgAdmin by writing pgscript.
Is that what I need, or is there a better way to run this script on a nightly basis?
for all i in tables that begin with name 'mview_%'
SELECT refresh_matview(i);
end loop;
I just placed an entry in my crontab:
This calls the script every three minutes, you can tune that.
And inside
matviewsRefresh.sh:Of course,
matview_refresh_allis a pl/pgsql function that loops over all my materialized views and refresh the old ones (I added an auxiliary table that records the time of last refresh for each mview, and each one has a different refresh frequency)