th3mouk/materialized-view
Composer 安装命令:
composer require th3mouk/materialized-view
包简介
Declarative, PostgreSQL-native management of materialized views: define views as versioned SQL, synchronise, rebuild, refresh (concurrently) and read them safely. Runs on Doctrine DBAL or a bare PDO connection.
关键字:
README 文档
README
Declarative, PostgreSQL-native management of materialized views — on Doctrine DBAL or a bare PDO connection.
Define a materialized view once, as a versioned .sql file plus a small PHP definition, and let the library create it, detect drift, rebuild it safely, refresh it (including CONCURRENTLY), and let your app read it without surprises. No ORM owns the DDL; PostgreSQL stays the source of truth for the physical object.
This is the framework-agnostic core. It talks to PostgreSQL through a tiny Connection port, so Doctrine is optional: run it on a plain PDO handle, or hand it a Doctrine DBAL connection to additionally get primary/replica routing, middlewares, profiling and read-only ORM mapping. For Symfony — autoconfiguration, console commands, the locked deploy lane and async refresh — use th3mouk/materialized-view-bundle.
Why this library exists
A materialized view is a physical PostgreSQL object with its own lifecycle: CREATE MATERIALIZED VIEW … AS …, REFRESH MATERIALIZED VIEW [CONCURRENTLY], no CREATE OR REPLACE, destructive redefinition, unique-index preconditions for concurrent refresh, dependency ordering. Doctrine ORM can read the rows as a read-only projection, but it cannot express any of that — and at the time of writing no maintained, framework-agnostic PHP library manages PostgreSQL materialized views. This library fills that gap — natively on Doctrine DBAL, or on a bare PDO connection when you don't run an ORM.
See docs/internals/design-rationale.md for the full reasoning and the competitive landscape.
Highlights
- Declarative: SQL lives in
db/matviews/*.sql; a tiny PHP class declares name, indexes, rebuild & population policy. - Drift detection via a canonical hash stored in
COMMENT ON MATERIALIZED VIEW(travels with database clones). - Safe rebuilds:
drop_createand a low-lockside_by_sidestrategy, with index and GRANT re-application. - Refresh runtime:
CONCURRENTLYwith precondition validation, primary/replica awareness (on the Doctrine backend),lock_timeout/statement_timeout, advisory locks,ANALYZE. - Catalog-derived dependency ordering (
pg_depend/pg_rewrite) — no hand-maintained graph, no drift. - Doctrine optional: the engine runs on a bare
PDOhandle with zero extra Composer dependencies; a Doctrine DBAL connection is natively supported and adds primary/replica routing, middlewares, profiling and read-only ORM mapping. Seedocs/guide/connection-backends.md. - Read-only ORM mapping (optional, Doctrine ORM) with a write guard and an unpopulated-read readiness guard.
Installation
composer require th3mouk/materialized-view
Requirements: PHP ≥ 8.4, PostgreSQL (12+; tested against 17), and one connection backend:
- Doctrine DBAL ≥ 4.4 —
composer require doctrine/dbal. Recommended: unlocks primary/replica routing, middlewares, profiling, and the optional read-only ORM mapping (doctrine/orm≥ 3.6). - or the
pdo_pgsqlextension — no extra Composer dependency; wire the manager withMaterializedViewManager::forPdo().
The core itself depends only on php and psr/log; the backend is your choice.
60-second example (framework-agnostic)
Given a base table orders (id bigint, category text, amount numeric, created_at timestamptz):
db/matviews/sales_by_category.sql
SELECT category, count(*) AS order_count, sum(amount) AS total_amount FROM orders GROUP BY category
use Th3Mouk\MaterializedView\Core\Definition\MaterializedViewDefinition; use Th3Mouk\MaterializedView\Core\Definition\MaterializedViewIndex; use Th3Mouk\MaterializedView\Core\Definition\SqlFileSource; use Th3Mouk\MaterializedView\Core\Registry\MaterializedViewRegistry; $definition = MaterializedViewDefinition::create('public.sales_by_category') ->fromSql(SqlFileSource::fromProjectPath('db/matviews/sales_by_category.sql')) ->withIndex(MaterializedViewIndex::unique( name: 'ux_sales_by_category_category', columns: ['category'], )); $registry = MaterializedViewRegistry::fromDefinitions([$definition]);
Wire the manager onto your connection backend:
use Th3Mouk\MaterializedView\Core\MaterializedViewManager; // With Doctrine DBAL (recommended — primary/replica routing, middlewares, profiling): $manager = MaterializedViewManager::forConnection($dbalConnection); // …or without Doctrine, on a bare PDO connection: $manager = MaterializedViewManager::forPdo(new PDO($dsn, $user, $password));
$manager->syncAll($registry); // create / rebuild on drift $manager->refresh($definition); // REFRESH MATERIALIZED VIEW (CONCURRENTLY when possible)
Documentation
| Tier | Audience | Start here |
|---|---|---|
| Getting started | Users — set it up fast | docs/getting-started.md |
| Guide | Users — advanced concepts | docs/guide/ |
| Internals | Maintainers — design & upstream references | docs/internals/ |
A full table of contents is in docs/README.md.
Compatibility
| This library | PHP | Doctrine DBAL (optional) | Doctrine ORM (optional) | PostgreSQL |
|---|---|---|---|---|
^1.x |
≥ 8.4 | ^4.4 | ^3.6 | 12 – 17 |
The core needs only a PostgreSQL connection — Doctrine DBAL or pdo_pgsql. Doctrine is optional and natively supported; it adds primary/replica routing, middlewares, profiling and read-only ORM mapping. We track DBAL major versions deliberately and do not pin a tight upper bound that would strand the library (see docs/internals/compatibility-and-evolution.md).
License
Apache-2.0 — Copyright © 2026 Jérémy Marodon (th3mouk). See NOTICE.
If you use or redistribute this package, keep the NOTICE attribution —
crediting Jérémy Marodon (th3mouk) and naming this library in your product's
documentation or credits. Please contribute upstream rather than
maintaining a public fork.
th3mouk/materialized-view 适用场景与选型建议
th3mouk/materialized-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dbal」 「analytics」 「doctrine」 「postgresql」 「pdo」 「postgres」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 th3mouk/materialized-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 th3mouk/materialized-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 th3mouk/materialized-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Analytics chooser extensions for site settings.
Make pimcore migration simple
A Laravel Nova Card to show Fathom Analytics stats.
Use a REST API as if it was your local database
统计信息
- 总下载量: 170
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 44
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-06-05