Last active
April 28, 2016 14:35
-
-
Save fnordfish/4b6effa1171311cefdecdd211ff8c7e6 to your computer and use it in GitHub Desktop.
Refresh a materialized view concurrently won't work when it's not populated yet. This function tries "concurrently" first and falls back to normal refresh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION safe_refresh_materialized_view_concurrently(_name regclass) | |
| RETURNS void AS $$ | |
| BEGIN | |
| EXECUTE 'REFRESH MATERIALIZED VIEW CONCURRENTLY ' || _name; | |
| EXCEPTION | |
| -- when a materialized view is not populated, concurrently will not work and throw feature_not_supported | |
| WHEN feature_not_supported THEN | |
| EXECUTE 'REFRESH MATERIALIZED VIEW ' || _name; | |
| END | |
| $$ LANGUAGE plpgsql; | |
| select safe_refresh_materialized_view_concurrently('some_schema.some_materialized_view'::regclass); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment