实例
postgres=# create table test(id int, info text);
CREATE TABLE
postgres=# insert into test select n, n||'info' from generate_series(1,4) n;
INSERT 0 4
postgres=#
postgres=# create materialized view view_test
postgres-# as
postgres-# select id, info
postgres-# from test ;
SELECT 4
postgres=#
postgres=# create unique index idx_test_id on test using btree(id);
CREATE INDEX
postgres=# select * from view_test;
id | info
----+-------
1 | 1info
2 | 2info
3 | 3info
4 | 4info
(4 rows)
postgres=# select * from test;
id | info
----+-------
1 | 1info
2 | 2info
3 | 3info
4 | 4info
(4 rows)
postgres=# insert into test values(5,'5info');
INSERT 0 1
postgres=# select * from test;
id | info
----+-------
1 | 1info
2 | 2info
3 | 3info
4 | 4info
5 | 5info
(5 rows)
postgres=# select * from view_test;
id | info
----+-------
1 | 1info
2 | 2info
3 | 3info
4 | 4info
(4 rows)
postgres=# refresh materialized view view_test;
REFRESH MATERIALIZED VIEW
postgres=# select * from view_test;
id | info
----+-------
1 | 1info
2 | 2info
3 | 3info
4 | 4info
5 | 5info
(5 rows)