- Newest
- Most votes
- Most comments
Hi,
Thank you for posting your question here.
Based on my experience, some of the recommendations would be as below:
When managing materialized views in Redshift, it's important to consider the dependencies between the materialized view and other objects in your database. Dropping a materialized view that is depended upon by other views or functions can lead to issues, as you've experienced.
Here are some best practices for updating materialized views in Redshift while preserving dependencies:
- Refreshing the Materialized View: Instead of dropping and recreating the materialized view, you can use the
REFRESH MATERIALIZED VIEW
command to update the contents of the materialized view. This will update the materialized view without affecting the dependent objects.
REFRESH MATERIALIZED VIEW my_materialized_view;
- Incremental Refreshes: If your materialized view is being updated frequently, consider using incremental refreshes. This involves updating the materialized view with only the changes since the last refresh, rather than a full refresh. This can be more efficient and minimize the impact on dependent objects.
CREATE MATERIALIZED VIEW my_materialized_view REFRESH INCREMENTAL;
- Scheduling Refreshes: Automate the refresh process by scheduling regular updates to the materialized view. This can be done using a scheduler tool or by creating a scheduled query in Redshift.
CREATE EVENT SCHEDULE my_schedule WHEN SCHEDULE EVERY 1 HOUR; REFRESH MATERIALIZED VIEW my_materialized_view ON SCHEDULE my_schedule;
- Monitoring Dependencies: Keep track of the objects that depend on your materialized views. You can use the
PG_DEPEND
system catalog view to identify these dependencies.
SELECT dependent.relname AS dependent_object, dependent.relkind AS dependent_type, referenced.relname AS referenced_object, referenced.relkind AS referenced_type FROM pg_depend JOIN pg_class AS dependent ON pg_depend.dependentid = dependent.oid JOIN pg_class AS referenced ON pg_depend.referencedid = referenced.oid WHERE referenced.relname = 'my_materialized_view' AND dependent.relkind IN ('v', 'f');
- Gradual Rollouts: If you need to make significant changes to a materialized view, consider a gradual rollout approach. This involves creating a new materialized view with the desired changes, and then gradually migrating dependent objects to use the new view, rather than dropping the old one.
By following these best practices, you can effectively manage and update materialized views in Redshift while preserving the dependencies on those views.
Comment here if you have additional questions, happy to help.
Abhishek
Thank you so much for your detailed response, Abhishek!
In our case, we're looking to make slight alterations to the DDL of our materialized view. We're hoping to achieve this without resorting to dropping (CASCADE) or completely recreating the materialized view, to avoid disrupting dependent objects.
Is it possible to alter the DDL of a materialized view directly in Redshift without affecting its dependencies?
Thanks again for your help! Maddie
Splitting my answer in two parts due to character limit in comments:
Pat1/2: It's important to note that the specific changes you can make to the materialized view's DDL are limited to those that don't affect the structure or output of the materialized view. For example, you can rename the materialized view or modify the query, but you can't change the column structure or the underlying data sources.
it is possible to alter the DDL (Data Definition Language) of a materialized view in Redshift. Redshift provides the
ALTER MATERIALIZED VIEW
command, which allows you to modify the definition of a materialized view without dropping and recreating it.Here are the steps to alter a materialized view's DDL in Redshift while preserving its dependencies:
- Identify the Materialized View and Its Dependencies:
- Use the
PG_DEPEND
system catalog view to identify the objects that depend on the materialized view.
SELECT dependent.relname AS dependent_object, dependent.relkind AS dependent_type, referenced.relname AS referenced_object, referenced.relkind AS referenced_type FROM pg_depend JOIN pg_class AS dependent ON pg_depend.dependentid = dependent.oid JOIN pg_class AS referenced ON pg_depend.referencedid = referenced.oid WHERE referenced.relname = 'my_materialized_view' AND dependent.relkind IN ('v', 'f');
- Use the
- Identify the Materialized View and Its Dependencies:
Part2/2:
-
Alter the Materialized View's DDL:
- Use the
ALTER MATERIALIZED VIEW
command to modify the definition of the materialized view.
ALTER MATERIALIZED VIEW my_materialized_view RENAME TO new_materialized_view_name;
- Or, you can modify the materialized view's query:
ALTER MATERIALIZED VIEW my_materialized_view REFRESH MATERIALIZED VIEW my_materialized_view WITH ( QUERY = 'SELECT ... FROM ...' );
- Use the
-
Verify the Materialized View's Dependencies:
- Recheck the dependencies using the
PG_DEPEND
system catalog view to ensure that the dependent objects are still working as expected.
- Recheck the dependencies using the
Or the least, you can consider gradual roll outs as I mentioned in my answer to minimize the impact.
-
At this time there is no way to circumvent the dependency and you will need to drop-create. However, you can minimize impact of these drop-creates to your users by controlling when you deploy newer version of your application. It is expected that system rollouts are communicated to users and they are not accessing the system during these times. And they should anticipate disruptions if they are still going to access the systems during releases, in fact they can cause disruptions to the release also! If your systems are expected to be 24x7 then this needs to be factored in when architecting it.
Relevant content
- asked 9 months ago
- asked 5 months ago
- asked 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 2 years ago
You can’t even rename a materialized view, what makes you think you can change the internal definition of it? Honestly if you are dropping and recreating them every night you should just switch to using physical tables instead of materialized views.