pg_tviews
pg_tviews · Transactional materialized views with automatic incremental refresh for PostgreSQL
Overview
PostgreSQL · 16, 17, 18
| Relationships | |
|---|---|
| Requires | jsonb_delta |
Usage
Sources:
- pg_tviews.control
- Cargo.toml
- CHANGELOG.md
- docs/reference/api.md
- docs/reference/ddl.md
- docs/reference/owner-frame.md
- docs/operations/upgrade.md
- docs/operations/performance-tuning.md
pg_tviews maintains derived TVIEW tables transactionally from analyzed queries and base-table triggers. This documentation covers the prerelease 0.1.0-beta.42. Its current Cargo build targets PostgreSQL 18, and its API lives in tviews with a dependency on jsonb_delta. The catalog’s numeric version 0.1.0 does not mean a stable release exists.
Enable and Create
Preload the matching library, preserving other entries, and restart. Installation requires a superuser; missing preload or a library/catalog revision mismatch refuses writes and extension functions with 55000.
A definition needs an entity key and JSONB document; a SELECT without an explicit UUID id receives a deterministic row ID. Maintenance runs in the TVIEW owner’s frame. The owner needs source privileges, writers need base-table privileges and readers need TVIEW SELECT. tviews.pg_tviews_create_or_replace requires ownership and treats options as a complete declaration.
Definitions and Security
Order-sensitive aggregates, LIMIT and similar expressions require ordering keys that determine each row. Volatile functions and definitions depending on session identity, privileges, statistics or unsupported relations are refused. Writer-specific custom settings are reset during refresh; pin supported settings through the TVIEW settings option. A setting deciding which rows are retained is refused.
Only pg_tviews writes the derived tables: an external write is rejected, including a superuser’s, except that the owner may TRUNCATE to rebuild. Row security on tv_* must not filter the owner. A data-only restore must omit TVIEW table data and refill derived tables from sources. Back up definitions and base data together.
Beta.42 refuses a full refresh with 42501 before locking if a writer without owner privileges cannot write every joined or embedded relation it would lock. The health component full_refresh_routes lists these relations. Definitions producing duplicate keys fail consistently with 21000; they are recomputed rather than patched.
Concurrency and Monitoring
Writers and maintenance require READ COMMITTED; stronger isolation is refused with 0A000. Retry deadlocks reported as 40P01. Queued refresh work at commit fails with 55000. Bulk lock escalation now covers the relevant column set, while full refresh still waits. Kept plans use an LRU budget of 64 MB measured at preparation; PostgreSQL generic plans add overhead. tviews.pg_tviews_show_queue() reports kept_plans and kept_plan_bytes.
Inspect tviews.registry and the health check after changes. CALL tviews.pg_tviews_time_boundary() refreshes time-dependent TVIEWs owned by the caller, dependencies first. UNLOGGED storage loses derived rows after crash recovery or promotion and is unavailable on standbys. Change notifications on tviews_changes are not a durable log; monitor feed gaps and invalidate consumer caches when necessary.
Upgrade to beta.42
Back up and rehearse, pause writers, stop PostgreSQL before replacing the preloaded library, install matching files and restart. The direct SQL update is from the preceding beta.41 only. Keep the exact prerelease spelling in executable SQL:
The update re-derives plans and triggers as each owner and leaves stored rows intact. Definitions that cannot be re-derived are named in warnings and marked for re-registration; timeout/cancellation can abort the update. Restore grants on replaced functions, repair refused definitions and complete re-registration before resuming writers. Older versions require the documented migration using saved definitions in dependency order. No downgrade scripts exist; rollback requires matching old binaries and a verified backup restored into a new database.