Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fnordfish/4b6effa1171311cefdecdd211ff8c7e6 to your computer and use it in GitHub Desktop.

Select an option

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.
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