Skip to content

Instantly share code, notes, and snippets.

@Elektordi
Last active August 29, 2023 17:05
Show Gist options
  • Save Elektordi/a5e0c6634d228f03f873b7dbe313cf6a to your computer and use it in GitHub Desktop.
Save Elektordi/a5e0c6634d228f03f873b7dbe313cf6a to your computer and use it in GitHub Desktop.
Refresh all materialized views in a schema (Postgres)
CREATE OR REPLACE FUNCTION remote.refresh()
RETURNS bool
LANGUAGE plpgsql
AS $$
DECLARE
r record;
BEGIN
for r in SELECT matviewname FROM pg_catalog.pg_matviews where schemaname = 'remote'
loop
raise notice 'Refreshing %...', r.matviewname;
execute 'REFRESH MATERIALIZED VIEW CONCURRENTLY remote.' || quote_ident(r.matviewname) || ';';
end loop;
return true;
END;
$$
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment