Updating Materialized Views (Without Worrying Too Much)
Blog post from Mergify
Materialized views offer performance benefits by physically storing data but present challenges in schema modifications due to their structure, which necessitates dropping and recreating them to implement changes like adding new columns. This process can disrupt production queries and dependencies, and lengthy recomputations can cause migration pipelines to time out. The article outlines a strategy to manage these issues by creating a new version of the view in parallel with the existing one, using techniques such as creating views with "NO DATA" to enable faster migrations and then performing offline or scheduled refreshes to populate the view without disrupting operations. The method involves a multi-step process: creating a versioned view, updating application pointers to the new view, and eventually dropping the old view once stability is confirmed. This approach, which includes using AI to flag risky migrations, promotes a calm and predictable handling of materialized view updates, ensuring minimal disruption during deployment.