Postgresql materialized view efficient with many refreshes? - postgresql

Does the postgresql materialize view rebuild the entire table on a REFRESH command? Is it efficient to TRIGGER a refresh to a materialized view that is dependent on a table with heavy writes?

Yes, REFRESH MATERIALIZED VIEW will rebuild the whole materialized view.
It would be very inefficient to do that after every data modufication.
What you could do is to use a normal table instead of a materialized view and write a trigger that keeps the table updated.

Related

Clear materialized view in PostgreSQL

I had a table with size of 10gb and materialized view on it (without join and etc.) of size 10gb too. I used pg_repack on table and its size decreased to 660mb. But after refresh materialized view concurrently and size of mat view still 10 gb. When I create the same new mat view on that table it size set 610 mb.
Are there any another ways to clear materialized view except delete and recreate? Mat view should be available in every time
You can clear a materialized view with
REFRESH MATERIALIZED VIEW mv WITH NO DATA;
But that does not make much sense. Your materialized view is bloated. This happens because REFRESH MATERIALIZED VIEW CONCURRENTLY does not actually replace, but update the existing data. There are two ways to get rid of the bloat:
REFRESH MATERIALIZED VIEW mv;
Refreshing the materialized view without CONCURRENTLY will discard the old, bloated table.
VACUUM (FULL) mv;
Both methods render the materialized view inaccessible while the statement is running, but there is no way to avoid that.

materialized view mechanism

i learn materialized view for reporting by database. the materialized view as shown on this site https://www.postgresqltutorial.com/postgresql-materialized-views/#:~:text=PosgreSQL%20extends%20the%20view%20concept%20to%20a%20next,then%20allow%20you%20to%20refresh%20this%20result%20periodically. The data in materialized view is depends on the last time you deploy from the table, if the table is being updated, you have to refresh materialized view to have full data. I have search to many sites but, i need to know this materialized view mechanism.
If the data insert into main table and i use refresh materialized view for being updated, does the materialized view truncate or insert mechanism?
or if the data drop in main table and missing some, then i refresh materialized view, is dropping data like truncate or just dropping?
Thank you for your explanation

Confirming if this trigger will do as I intend

I have been using Postgres for a while now, but I have not implemented any triggers yet. I wanted to check if this will do what I intend it to do.
On a daily basis, I am adding new rows to a table (COPY) and also updating existing rows if there is a primary key conflict (ON CONFLICT DO UPDATE SET). I then have a materialized view using that table and a few other joins, and this view is used for a lot of reporting.
I want the materialized view to update when the original table has been updated, without me needing to schedule it or run it manually. (Right now I have it scheduled with a Python psycopg2 execute command).
CREATE OR REPLACE FUNCTION refresh_mat_view()
RETURNS TRIGGER LANGUAGE plpgsql
AS $$
BEGIN
REFRESH MATERIALIZED VIEW schema_name.materialized_view_name;
RETURN NULL;
END $$;
CREATE TRIGGER refresh_view
AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE
ON sutherland.dimension_peoplesoft FOR EACH STATEMENT
EXECUTE PROCEDURE refresh_mat_view();
Would that refresh the view for every single row which is updated too? I am just imagining it trigger a refresh for each individual row which might be 100k+. It would be better to happen AFTER all inserts have been done (I have Python looping through each row in a pandas DataFrame to UPSERT into the database).
When you use a pure Materialized View, every time you refresh it, it will rebuild the whole thing. So if your data changes a lot and you need the data available fast it's not an optimized choice.
You should use Eager Materialized Views or Lazy Materialized Views that basically are "A well use of triggers". Obviously it's harder to do than a pure materialized view, but the results are better (depending on the case of use).
You should check this article Materialized View Strategies Using PostgreSQL

Materialized view on a foreign table with postgresql_fdw

Is it possible to use a materialized view on top of a foreign table with postgresql_fdw? (Postgres 9.3)
Currently thinking of some kind of "caching" for postgresql_fdw.
View can be updated by REFRESH MATERIALIZED VIEW. No real need for external triggering.

TOAD-Modify Materialized View Select Query without dropping

Is there any way I can edit the MVIEW query without dropping it in TOAD.I am not sure if we can do it?If not how Can I do it in a way that does not affect the table contents?
Thanks in advance
You can't alter a materialized view query, you have to drop and recreate it.
See: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_2001.htm
Use the ALTER MATERIALIZED VIEW statement to modify an existing
materialized view in one or more of the following ways:
To change its storage characteristics
To change its refresh method, mode, or time
To alter its structure so that it is a different type of materialized
view
To enable or disable query rewrite
As pointed out by Jeffre Kemp, you cannot change the query of an existing Materialized View.
However, if you expect that changes will be necessary in the future, you can choose one of these approaches:
use an intermediate VIEW that does all the heavy lifting; your MATERIALIZED VIEW then does a simple SELECT * FROM myView. This allows you to change the query - however, it does not allow you to add / remove columns to/from the MATERIALIZED VIEW
use a pre-built table for your materialized view; this logically separates the TABLE that holds the content from the query that is used to update the content. Now, you can drop the MATERIALIZED VIEW, add/remove columns from your TABLE as needed, and re-create the MATERIALIZED VIEW afterwards
use a synonym for all code that queries the MATERIALIZED VIEW; if necessary, create a backup table using CTAS, point the synonym to this backup table, drop and re-create your MATERIALIZED VIEW, point the synonym back to the MATERIALIZED VIEW, and drop your backup table (that's how we do it)