v.24.12Improvement
Create Materialized View Now Supports UNION ALL with Insert Behavior Consistent with JOIN
Now it's allowed toCREATE MATERIALIZED VIEWwithUNION [ALL]in query. Behavior is the same as for matview withJOIN: only the first table inSELECTexpression will work as a trigger for insert, all other tables will be ignored. However, if there are many references to the first table (e.g., UNION with itself), all of them will be processed as the inserted block of data. #72347 (alesapin).
Why it matters
This feature allows users to define materialized views that aggregate data from multiple tables or repeated references to the same table usingUNION. It solves the limitation where only simple queries or JOINs were supported, expanding flexibility for data transformations and incremental processing triggered by inserts into the first table in the SELECT clause.How to use it
Create aMATERIALIZED VIEW with a query containing UNION [ALL]. Inserts will trigger processing based on the first table referenced in the SELECT statement, including all its references if repeated (e.g., UNION with itself). Other tables involved in the union are ignored as triggers. Example syntax:CREATE MATERIALIZED VIEW view_name TO target_table AS
SELECT FROM table1
UNION ALL
SELECT FROM table2Only inserts into
table1 will trigger the materialized view refresh.