Skip to main content

Postgres Materialized View Dependency Manager

Project description

https://badge.fury.io/py/pg-materialize.svg https://travis-ci.org/aanari/pg-materialize.svg?branch=master https://coveralls.io/repos/github/aanari/pg-materialize/badge.svg?branch=master

pg_materialize is a utility for generating PostgreSQL creation and refresh scripts from a directory containing Materialized View SQL definitions. It uses psqlparse to transform the SQL into parse trees, identifies which Materialized Views have a dependency on other views by generating a DAG, and produces the correct order for constructing and refreshing these views. The source directory is traversed recursively, cross-schema views are handled correctly, and extraneous transaction syntax blocks are filtered out from the final output. The files from each subsequent run are timestamped with the suffix YYYYMMDD-HHMMSS.sql.

Supported Python Versions

  • Python 2.7

  • Python 3.3+

Installation

If you have pip on your system, you can simply install or upgrade the Python library:

pip install -U pg_materialize

Alternately, you can download the source distribution from PyPI, unarchive it, and run:

python setup.py install

Note: both of the methods described above install pg_materialize as a system-wide package. You may consider using virtualenv to create isolated Python environments instead.

Usage

Example Command:

pg_materialize -v \
    -i ~/Projects/my_project/src \
    -o ~/Projects/my_project/output \
    -p _mv \
    -I invites

Example Output:

Found 97 Scripts in /Users/ali/Projects/my_project/src
Identified 169 Materialized Views, Containing 90 View Dependencies

Materialized View Dependencies:
'public.users_mv': ['public.user_addresses_mv', 'public.user_invites_mv'],
'public.orders_mv': ['public.payment_methods_mv']

Selecting 97 Materialized Views for Refresh
Successfully Saved Creation Script to ~/Projects/my_project/output/create-20170824-120626.sql
Successfully Saved Refresh Script to ~/Projects/my_project/output/refresh-20170824-120626.sql
Process Complete!

Example Creation Script:

BEGIN;

    -- ~/Projects/my_project/src/public/user_addresses.sql

    CREATE MATERIALIZED VIEW IF NOT EXISTS public.user_addresses_mv AS (
        SELECT *
        FROM public.user_addresses
        WHERE created_at >= CURRENT_DATE - INTERVAL '6 MONTHS'
    ) WITH DATA;

    -- ~/Projects/my_project/src/public/user_invites.sql

    CREATE MATERIALIZED VIEW IF NOT EXISTS public.user_invites_mv AS (
        SELECT *
        FROM public.user_invites
        WHERE created_at >= CURRENT_DATE - INTERVAL '6 MONTHS'
    ) WITH DATA;

    -- ~/Projects/my_project/src/public/payment_methods.sql

    CREATE MATERIALIZED VIEW IF NOT EXISTS public.payment_methods_mv AS (
        SELECT *
        FROM public.payment_methods
        WHERE created_at >= CURRENT_DATE - INTERVAL '6 MONTHS'
    ) WITH DATA;

    -- ~/Projects/my_project/src/public/users.sql

    CREATE MATERIALIZED VIEW IF NOT EXISTS public.users_mv AS (
        SELECT *
        FROM public.users
        WHERE created_at >= CURRENT_DATE - INTERVAL '6 MONTHS'
    ) WITH DATA;

    -- ~/Projects/my_project/src/public/orders.sql

    CREATE MATERIALIZED VIEW IF NOT EXISTS public.orders_mv AS (
        SELECT *
        FROM public.orders
        WHERE created_at >= CURRENT_DATE - INTERVAL '6 MONTHS'
    ) WITH DATA;

COMMIT;

Example Refresh Script:

BEGIN;

    REFRESH MATERIALIZED VIEW CONCURRENTLY public.user_addresses_mv;

    REFRESH MATERIALIZED VIEW CONCURRENTLY public.payment_methods_mv;

    REFRESH MATERIALIZED VIEW CONCURRENTLY public.users_mv;

    REFRESH MATERIALIZED VIEW CONCURRENTLY public.orders_mv;

COMMIT;

Options

pg_materialize accepts the following command line arguments.

-d
--dry-run

Analyzes dependencies without actually generating the output files.

-i
--input-dir

The directory for the PostgreSQL scripts to analyze.

-I
--ignore-refresh

Regex pattern to match when ignoring refresh on Materialized Views (i.e. hist for users_mv_hist).

-o
--output_dir

The directory for the output creation and refresh scripts.

-p
--pattern

Materialized View regex pattern to match (i.e. _mv for users_mv).

-v
--verbose

Enables verbose logging.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg-materialize-0.2.10.dev15.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

pg_materialize-0.2.10.dev15-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pg-materialize-0.2.10.dev15.tar.gz.

File metadata

File hashes

Hashes for pg-materialize-0.2.10.dev15.tar.gz
Algorithm Hash digest
SHA256 994628a7b3cd12db833cc8c59e12e83af4eaeb1c7acef6f35584fb2734da29c4
MD5 113c9ce6fdbb7a6db878bcbd60918e5b
BLAKE2b-256 e7490e23d02cc79f2519d0563264f3c0b5f3529559239ad77d3a21036292d14d

See more details on using hashes here.

File details

Details for the file pg_materialize-0.2.10.dev15-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pg_materialize-0.2.10.dev15-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 65dcd61973cacf9b4b6be2e56238592d4347490835ff6713899c34a42eebc0de
MD5 09fbcafdbc833de5038a65bf2685ae57
BLAKE2b-256 37dc71972abb39ce833637e4f73d0ca1b5ec26a55cc264b2180a49c37c8220bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page