pg_ivm
pg_ivm : incremental view maintenance on PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 2840 | pg_ivm | pg_ivm | 1.13 | FEAT | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--sLd-- | No | Yes | Yes | Yes | no | no |
| Relationships | |
|---|---|
| Schemas | pg_catalog |
| See Also | age hll rum pg_graphql pg_jsonschema jsquery pg_hint_plan |
deb takeover by pgdg since 2026-01
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | MIXED | 1.13 | 18 17 16 15 14 | pg_ivm | - |
| RPM | PIGSTY | 1.13 | 18 17 16 15 14 | pg_ivm_$v | - |
| DEB | PIGSTY | 1.13 | 18 17 16 15 14 | postgresql-$v-pg-ivm | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
el8.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
el9.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
el9.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
el10.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
el10.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
d12.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
d12.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
d13.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
d13.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
u22.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
u22.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
u24.x86_64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
u24.aarch64 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 | PGDG 1.13 |
Source
pig build pkg pg_ivm;# build debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_ivm;# install via package name, for the active PG version pig install pg_ivm -v 18; # install for PG 18 pig install pg_ivm -v 17; # install for PG 17 pig install pg_ivm -v 16; # install for PG 16 pig install pg_ivm -v 15; # install for PG 15 pig install pg_ivm -v 14; # install for PG 14Config this extension to shared_preload_libraries:
shared_preload_libraries = 'pg_ivm';Create this extension with:
CREATE EXTENSION pg_ivm;Usage
The pg_ivm extension provides Incremental View Maintenance (IVM), updating materialized views by applying only incremental changes rather than recomputing from scratch. Views are updated immediately in AFTER triggers when base tables are modified.
CREATE EXTENSION pg_ivm;Configuration
Add pg_ivm to preload libraries for correct maintenance:
shared_preload_libraries = 'pg_ivm'Functions
create_immv
pgivm.create_immv(immv_name text, view_definition text) RETURNS bigintCreates an Incrementally Maintainable Materialized View (IMMV). Triggers are automatically created to keep the view updated. A unique index is created automatically if possible.
SELECT pgivm.create_immv('myview', 'SELECT * FROM mytab');refresh_immv
pgivm.refresh_immv(immv_name text, with_data bool) RETURNS bigintCompletely replaces IMMV contents. With with_data = false, the IMMV becomes unpopulated and triggers are dropped. With with_data = true, triggers and indexes are recreated.
SELECT pgivm.refresh_immv('myview', true);get_immv_def
pgivm.get_immv_def(immv regclass) RETURNS textReturns the reconstructed SELECT command for an IMMV.
IMMV Metadata Catalog
The pgivm.pg_ivm_immv catalog stores IMMV information:
| Column | Type | Description |
|---|---|---|
immvrelid | regclass | OID of the IMMV |
viewdef | text | Query tree for the view definition |
ispopulated | bool | Whether IMMV is currently populated |
Examples
Create an IMMV with aggregates:
SELECT pgivm.create_immv('immv_agg', 'SELECT bid, count(*), sum(abalance), avg(abalance) FROM pgbench_accounts JOIN pgbench_branches USING(bid) GROUP BY bid');Updates to base tables are reflected automatically:
UPDATE pgbench_accounts SET abalance = abalance + 1000 WHERE aid = 4112345; SELECT * FROM immv_agg WHERE bid = 42; -- aggregates updated automaticallyList all IMMVs:
SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS def FROM pgivm.pg_ivm_immv;Drop an IMMV with DROP TABLE:
DROP TABLE myview;Disable/Enable Maintenance
Disable immediate maintenance before bulk modifications, then refresh:
SELECT pgivm.refresh_immv('myview', false); -- disable -- ... bulk modifications ... SELECT pgivm.refresh_immv('myview', true); -- refresh and re-enableSupported Query Features
- Inner and outer joins (including self-join)
DISTINCTclause- Aggregate functions:
count,sum,avg,min,max - Simple subqueries in
FROMclause EXISTSsubqueries inWHEREclause- Simple CTEs (
WITHqueries) GROUP BYclause