Git Product home page Git Product logo

pg_materialized_views_refresh_topologically's Introduction

pg_materialized_views_refresh_topologically

If your materialized views take a while to refresh, you'll probably want to refresh them regularly at a time when the database load is low, say every night at 3 a.m. Running a REFRESH on every materialized view every 24 hours works fine as long as all your materialized views obtain their data only from tables. Things become slightly more complicated once they obtain their data from other materialized views: Suppose materialized view B obtains its input from materialized view A, and you run a cron job every night at 3 a.m. which refreshes all materialized views in arbitrary order. If B is refreshed before A, the data in B will still be 24 hours old immediately after the cron job has finished.

This python script solves this problem by representing the materialized views in a directed acyclic graph (DAG), where an incoming edge on B from A means that materialized view B receives input from materialized view A. This graph is then topologically sorted in order to execute all REFRESH MATERIALIZED VIEW statements in correct order.

Usage

To refresh all materialized views inside a given database, run:

./pg_materialized_views_refresh_topologically

To refresh all materialized views inside a given database schema, run:

./pg_materialized_views_refresh_topologically --schema <schema>

See ./pg_materialized_views_refresh_topologically -h for more details.

Connection parameters (host, port etc.) are set with environment variables. The password has to be set in the password file.

Concurrent Refresh

This script will first attempt to refresh the materialized view concurrently. If that fails, it does an ordinary refresh. This means you don't need to worry about error messages in the form of:

ERROR:  cannot refresh materialized view "my_schema.my_view" concurrently

pg_materialized_views_refresh_topologically's People

Contributors

nroi avatar lpyfm avatar antoniol avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.