diff --git a/.project b/.project new file mode 100644 index 0000000..cc07ee0 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + RefreshAllMaterializedViews + + + + + + + + diff --git a/RefreshAllMaterializedViews.sql b/RefreshAllMaterializedViews.sql new file mode 100644 index 0000000..f548d1d --- /dev/null +++ b/RefreshAllMaterializedViews.sql @@ -0,0 +1,19 @@ +-- +-- function to refresh all materialized views in a schema +-- + +CREATE OR REPLACE FUNCTION RefreshAllMaterializedViews(schema_arg TEXT DEFAULT 'public') +RETURNS INT AS $$ + DECLARE + r RECORD; + BEGIN + RAISE NOTICE 'Refreshing materialized view in schema %', schema_arg; + FOR r IN SELECT matviewname FROM pg_matviews WHERE schemaname = schema_arg + LOOP + RAISE NOTICE 'Refreshing %.%', schema_arg, r.matviewname; + EXECUTE 'REFRESH MATERIALIZED VIEW ' || schema_arg || '.' || r.matviewname; + END LOOP; + + RETURN 1; + END +$$ LANGUAGE plpgsql;