Package: pg-materialize
pg_materializeis a utility for generating PostgreSQL creation and refresh scripts from a directory containingMaterialized ViewSQL definitions. It usespsqlparseto 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 suffixYYYYMMDD-HHMMSS.sql.Supported Python VersionsPython 2.7Python 3.3+InstallationIf you havepipon your system, you can simply install or upgrade the Python library:pipinstall-Upg_materializeAlternately, you can download the source distribution fromPyPI, unarchive it, and run:pythonsetup.pyinstallNote: both of the methods described above installpg_materializeas a system-wide package. You may consider usingvirtualenvto create isolated Python environments instead.UsageExample Command:pg_materialize-v\-i~/Projects/my_project/src\-o~/Projects/my_project/output\-p_mv\-IinvitesExample 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.sqlCREATEMATERIALIZEDVIEWIFNOTEXISTSpublic.user_addresses_mvAS(SELECT*FROMpublic.user_addressesWHEREcreated_at>=CURRENT_DATE-INTERVAL'6 MONTHS')WITHDATA;-- ~/Projects/my_project/src/public/user_invites.sqlCREATEMATERIALIZEDVIEWIFNOTEXISTSpublic.user_invites_mvAS(SELECT*FROMpublic.user_invitesWHEREcreated_at>=CURRENT_DATE-INTERVAL'6 MONTHS')WITHDATA;-- ~/Projects/my_project/src/public/payment_methods.sqlCREATEMATERIALIZEDVIEWIFNOTEXISTSpublic.payment_methods_mvAS(SELECT*FROMpublic.payment_methodsWHEREcreated_at>=CURRENT_DATE-INTERVAL'6 MONTHS')WITHDATA;-- ~/Projects/my_project/src/public/users.sqlCREATEMATERIALIZEDVIEWIFNOTEXISTSpublic.users_mvAS(SELECT*FROMpublic.usersWHEREcreated_at>=CURRENT_DATE-INTERVAL'6 MONTHS')WITHDATA;-- ~/Projects/my_project/src/public/orders.sqlCREATEMATERIALIZEDVIEWIFNOTEXISTSpublic.orders_mvAS(SELECT*FROMpublic.ordersWHEREcreated_at>=CURRENT_DATE-INTERVAL'6 MONTHS')WITHDATA;COMMIT;Example Refresh Script:BEGIN;REFRESHMATERIALIZEDVIEWCONCURRENTLYpublic.user_addresses_mv;REFRESHMATERIALIZEDVIEWCONCURRENTLYpublic.payment_methods_mv;REFRESHMATERIALIZEDVIEWCONCURRENTLYpublic.users_mv;REFRESHMATERIALIZEDVIEWCONCURRENTLYpublic.orders_mv;COMMIT;Optionspg_materializeaccepts the following command line arguments.-d--dry-runAnalyzes dependencies without actually generating the output files.-i--input-dirThe directory for the PostgreSQL scripts to analyze.-I--ignore-refreshRegex pattern to match when ignoring refresh on Materialized Views (i.e.histforusers_mv_hist).-o--output_dirThe directory for the output creation and refresh scripts.-p--patternMaterialized View regex pattern to match (i.e._mvforusers_mv).-v--verboseEnables verbose logging.