Postgres Materialized View Dependency Manager
Project description
Description
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.6, 2.7
Python 3.3+
Installing
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.
Analyzes dependencies without actually generating the output files.
The directory for the PostgreSQL scripts to analyze.
Regex pattern to match when ignoring refresh on Materialized Views (i.e. “hist” for “users_mv_hist”).
The directory for the output creation and refresh scripts.
Materialized View regex pattern to match (i.e. “_mv” for “users_mv”),
Enables verbose logging.
License (MIT)
Copyright (c) 2017 Ali Anari
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pg-materialize-0.2.6.tar.gz
.
File metadata
- Download URL: pg-materialize-0.2.6.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ec7e2a2ebab485683684ed1067828f69433ef55b7a6af8b30bad0e41a1c6121 |
|
MD5 | 3fd92cae28ccf06b1ab24a20b8aab865 |
|
BLAKE2b-256 | 289c507c1470e110d9c6a598fa4a998755efb10077ec9c5aa04160ee1eb32800 |
File details
Details for the file pg_materialize-0.2.6-py2-none-any.whl
.
File metadata
- Download URL: pg_materialize-0.2.6-py2-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac4e705251905aaa8aff2f44cfe4c206288a0d207ced9fb8fc816cdde42cd64e |
|
MD5 | f458c20f333e8ae197dd81f027f8cc9c |
|
BLAKE2b-256 | b681f397b93f3ff5e504a1f1c5371d89592cc0b218d2deda9f746e4b249aa119 |