Skip to content

Commit ddc1382

Browse files
authored
Some enhancements to READEME.md (#122)
- Added an example demonstrating an IMMV with aggregate functions (Issue #105) - Added examples for listing and deleting an IMMV. (Issue #109) - Noted that IMMVs must be manually dropped and recreated after restoring data from pg_dump or performing pg_upgrade. (Issue #118) - Added a reinder to set session_preload_libraries or shared_preload_libraries during instlation. (Issue #119)
1 parent 966a865 commit ddc1382

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

README.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,25 @@ If you installed PostgreSQL from rpm or deb, you will need the devel package (fo
6868

6969
> **Important:** Don't forget to set the `PG_CONFIG` variable (`make PG_CONFIG=...`) or the `PATH` to the `pg_config` command in case you want to use `pg_ivm` on a non-default or custom build of PostgreSQL. Read more [here](https://wiki.postgresql.org/wiki/Building_and_Installing_PostgreSQL_Extension_Modules).
7070
71-
And, execute CREATE EXTENSION comand.
71+
Once installed, execute `CREATE EXTENSION` comand.
7272

7373
```sql
7474
CREATE EXTENSION pg_ivm;
7575
```
7676

77+
### Configuration
78+
79+
To ensure `pg_ivm` maintains IMMVs correctly, add it to either shared_preload_libraries or session_preload_libraries in postgresql.conf:
80+
81+
```
82+
# Add pg_ivm to preload libraries
83+
shared_preload_libraries = 'pg_ivm'
84+
# OR
85+
session_preload_libraries = 'pg_ivm'
86+
```
87+
88+
After making this change, restart PostgreSQL for the configuration to take effect.
89+
7790
### RPM packages and yum repository
7891

7992
RPM packages of pg_ivm are available from the [PostgreSQL yum repository](https://yum.postgresql.org/). See the [instruction](https://yum.postgresql.org/howto/) for details. Note that we are not the maintainer of this yum repository and RPMs for pg_ivm in it may be not always latest.
@@ -133,6 +146,8 @@ The catalog `pgivm.pg_ivm_immv` stores IMMV information.
133146

134147
## Example
135148

149+
### `CREATE MATERIALIZED VIEW` and `REFRESH MATERIALIZED VIEW`
150+
136151
In general, IMMVs allow faster updates than `REFRESH MATERIALIZED VIEW` at the price of slower updates to their base tables. Update of base tables is slower because triggers will be invoked and the IMMV is updated in triggers per modification statement.
137152

138153
For example, suppose a normal materialized view defined as below:
@@ -156,6 +171,8 @@ REFRESH MATERIALIZED VIEW
156171
Time: 20575.721 ms (00:20.576)
157172
```
158173

174+
### Creating an IMMV
175+
159176
On the other hand, after creating IMMV with the same view definition as below:
160177

161178
```
@@ -169,7 +186,7 @@ NOTICE: created index "immv_index" on immv "immv"
169186
(1 row)
170187
```
171188

172-
updating a tuple in a base table takes more than the normal view, but its content is updated automatically and this is faster than the `REFRESH MATERIALIZED VIEW` command.
189+
Updating a tuple in a base table takes more than the normal view, but its content is updated automatically and this is faster than the `REFRESH MATERIALIZED VIEW` command.
173190

174191
```sql
175192
test=# UPDATE pgbench_accounts SET abalance = 1234 WHERE aid = 1;
@@ -198,6 +215,110 @@ UPDATE 1
198215
Time: 3224.741 ms (00:03.225)
199216
```
200217

218+
### IMMV with Aggregate Functions
219+
220+
You can create an IMMV that includes aggregate functions.
221+
222+
```sql
223+
test=# SELECT pgivm.create_immv('immv_agg',
224+
'SELECT bid, count(*), sum(abalance), avg(abalance)
225+
FROM pgbench_accounts JOIN pgbench_branches USING(bid) GROUP BY bid');
226+
NOTICE: created index "immv_agg_index" on immv "immv_agg"
227+
create_immv
228+
-------------
229+
100
230+
(1 row)
231+
232+
Time: 5772.625 ms (00:05.773)
233+
```
234+
235+
Creating this view takes about five seconds, and the normal refresh operation requires a similar amount of time. The following example demonstrates refreshing the IMMV using `refresh_immv`. The execution time would be approximately the same if you used `REFRESH MATERIALIZED VIEW` on a regular materialized view with the same definition.
236+
237+
```sql
238+
test=# SELECT pgivm.refresh_immv('immv_agg',true);
239+
refresh_immv
240+
--------------
241+
100
242+
(1 row)
243+
244+
Time: 5766.292 ms (00:05.766)
245+
```
246+
247+
When a base table is updated, the IMMV is also automatically updated incrementally, as expected.
248+
249+
```sql
250+
test=# SELECT bid, count, sum, avg FROM immv_agg WHERE bid = 42;
251+
bid | count | sum | avg
252+
-----+--------+-------+------------------------
253+
42 | 100000 | 38774 | 0.38774000000000000000
254+
(1 row)
255+
256+
Time: 3.123 ms
257+
258+
test=# UPDATE pgbench_accounts SET abalance = abalance + 1000 WHERE aid = 4112345 AND bid = 42;
259+
UPDATE 1
260+
Time: 16.616 ms
261+
262+
test=# SELECT bid, count, sum, avg FROM immv_agg WHERE bid = 42;
263+
bid | count | sum | avg
264+
-----+--------+-------+------------------------
265+
42 | 100000 | 39774 | 0.39774000000000000000
266+
(1 row)
267+
268+
Time: 1.987 ms
269+
```
270+
271+
After updating a row in the `pgbench_accounts` table, you can see that the aggregated values in `immv_agg` are updated automatically.
272+
273+
### Listing IMMVs and Viewing Their Definitions
274+
275+
You can find all IMMVs in the `pg_ivm_immv` catalog and view their definition queries by executing the `get_immv_def` function."
276+
277+
```sql
278+
test=# SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS immv_def FROM pgivm.pg_ivm_immv;
279+
immv | immv_def
280+
----------+--------------------------------------------
281+
immv_agg | SELECT pgbench_accounts.bid, +
282+
| count(*) AS count, +
283+
| sum(pgbench_accounts.abalance) AS sum,+
284+
| avg(pgbench_accounts.abalance) AS avg +
285+
| FROM (pgbench_accounts +
286+
| JOIN pgbench_branches USING (bid)) +
287+
| GROUP BY pgbench_accounts.bid
288+
immv | SELECT a.aid, +
289+
| b.bid, +
290+
| a.abalance, +
291+
| b.bbalance +
292+
| FROM (pgbench_accounts a +
293+
| JOIN pgbench_branches b USING (bid))
294+
(2 rows)
295+
```
296+
297+
### Dropping an IMMV
298+
299+
An IMMV can be dropped using the `DROP TABLE` command. Once an IMMV is dropped, its entry is automatically removed from the `pg_ivm_immv` catalog.
300+
301+
```sql
302+
test=# DROP TABLE immv;
303+
DROP TABLE
304+
305+
test=# SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS immv_def FROM pgivm.pg_ivm_immv;
306+
immv | immv_def
307+
----------+--------------------------------------------
308+
immv_agg | SELECT pgbench_accounts.bid, +
309+
| count(*) AS count, +
310+
| sum(pgbench_accounts.abalance) AS sum,+
311+
| avg(pgbench_accounts.abalance) AS avg +
312+
| FROM (pgbench_accounts +
313+
| JOIN pgbench_branches USING (bid)) +
314+
| GROUP BY pgbench_accounts.bid
315+
(1 row)
316+
```
317+
318+
## `pg_dump` and `pg_upgrade`
319+
320+
After restoring data from a `pg_dump` backup or upgrading `PostgreSQL` using `pg_upgrade`, all IMMVs must be manually dropped and recreated.
321+
201322
## Supported View Definitions and Restriction
202323

203324
Currently, IMMV's view definition can contain inner joins, DISTINCT clause, some built-in aggregate functions, simple sub-queries in `FROM` clause, EXISTS sub-queries, and simple CTE (`WITH` query). Inner joins including self-join are supported, but outer joins are not supported. Supported aggregate functions are count, sum, avg, min and max. Other aggregates, sub-queries which contain an aggregate or `DISTINCT` clause, sub-queries in other than `FROM` clause, window functions, `HAVING`, `ORDER BY`, `LIMIT`/`OFFSET`, `UNION`/`INTERSECT`/`EXCEPT`, `DISTINCT ON`, `TABLESAMPLE`, `VALUES`, and `FOR UPDATE`/`SHARE` can not be used in view definition.

0 commit comments

Comments
 (0)