How to refresh all materialized views in Postgresql 9.3 at once?

后端 未结 5 1646
青春惊慌失措
青春惊慌失措 2021-02-05 09:38

I am loading a bunch of data into a PostgresQL 9.3 database and then I want to refresh all materialized views that depend on the updated tables. Is there a way to do it automat

5条回答
  •  时光取名叫无心
    2021-02-05 10:06

    Looks like current version of PostgreSQL (9.3.1) does not have such functionality, have had to write my own function instead:

    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;
    

    (on github: https://github.com/sorokine/RefreshAllMaterializedViews)

提交回复
热议问题