CREATE MATERIALIZED VIEW my_materialized_view 
AS
SELECT emp_id, emp_name, dept_name, salary 
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;

select * 
from my_materialized_view

--Since materialized views do not update automatically, you must refresh them manually using:

REFRESH MATERIALIZED VIEW my_materialized_view;

--To avoid blocking queries while refreshing, use the CONCURRENTLY option:

REFRESH MATERIALIZED VIEW CONCURRENTLY my_materialized_view;


/*
Updating a Materialized View Automatically
To automate refreshes, you can use a cron job or PostgreSQL’s pg_cron extension:
*/

--SELECT cron.schedule('refresh_materialized_view', '0 * * * *', 'REFRESH MATERIALIZED VIEW my_materialized_view');