Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make update_view for materialized views create less downtime #386

Open
Roguelazer opened this issue Apr 4, 2023 · 1 comment · May be fixed by #387
Open

make update_view for materialized views create less downtime #386

Roguelazer opened this issue Apr 4, 2023 · 1 comment · May be fixed by #387

Comments

@Roguelazer
Copy link

Right now, update_materialized_view results in a DROP MATERIALIZED VIEW followed by a CREATE MATERIALIZED VIEW with the new version; if the view is large, this can result in substantial downtime, since the drop acquires an ACCESS EXCLUSIVE lock on the view and prevents any queries from running.

Another option would be to do CREATE MATERIALIZED VIEW {name}_tmp ; ALTER MATERIALIZED VIEW {name} RENAME TO {name}_old ; ALTER MATERIALIZED VIEW {name}_new RENAME TO {name} ; DROP MATERIALIZED VIEW {name}_old. This results in negligible downtime, but costs twice the disk space. A further improvement could be to optionally run the (potentially expensive, disk-blocking) DROP in a different transaction if you pass an argument like 'i_will_clean_up_myself: true`, but in my experimentation, just doing it this way takes some expensive matview migrations from minutes of downtime to fractions of a second.

I've attached a PR for this behavior.

@Roguelazer Roguelazer linked a pull request Apr 4, 2023 that will close this issue
@derekprior
Copy link
Contributor

I really like this idea and have manually done the same many years ago when dealing with a project that used materialized views heavily.

We'll need to be cognizant of things like view and index name length limits. I see there's a PR already associated to this. I likely won't be able t have a look until I'm back from vacation but just wanted to say I'm generally in support of the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants