I've got a materialized view called price_changes
used for some reporting. I've also got a cron job refreshing the materialized view with refresh materialized view price_changes
. Everything is working great.
I'd like to give users looking at the report a message "Data is fresh as of X". I could store it somewhere when cron runs, but does postgres already store this metadata somewhere?
The simplest form to refresh a materialized view is a Complete Refresh. It loads the contents of a materialized view from scratch. This means, if the SQL query of the materialized view has an execution time of two hours, the Complete Refresh takes at least two hours as well – or ofter even longer.
The “Complete” refresh (7.75 mins) is more faster than the “Fast” refresh (48.9 mins), The parameter “atomic_refresh=FALSE” works only with “complete” refresh, so “truncate” is only possible with “complete“.
A materialized view log is located in the master database in the same schema as the master table. A master table can have only one materialized view log defined on it. Oracle Database can use this materialized view log to perform fast refreshes for all fast-refreshable materialized views based on the master table.
I don't think there is anything built in the system that provides this as of 9.3.4. When I need to provide the date of last refresh I add a column called 'last_refresh' to the select query in the materialized view since data in the materialized view won't change until it is refreshed.
I also prefer this for security reasons as you may not want to give the sql user access to the system tables, if the information is being stored there.
Depending if you need the time, you can use either:
CURRENT_DATE
now()
Just date:
CREATE MATERIALIZED VIEW mv_address AS SELECT *, CURRENT_DATE AS last_refresh FROM address;
With date and time:
CREATE MATERIALIZED VIEW mv_address AS SELECT *, now() AS last_refresh FROM address;
Update 2017-02-17:
PostgreSQL version 9.4+ now includes CONCURRENTLY
option. If you use REFRESH MATERIALIZED VIEW CONCURRENTLY
option be aware of what @Smudge indicated in the comments. This would really only be an issue for large and frequently updated data sets. If your data set is small or infrequently updated then you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With