2013-03-04 01:23:31 +01:00
|
|
|
-- create a table to use as a basis for views and materialized views in various combinations
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_t (id int NOT NULL PRIMARY KEY, type text NOT NULL, amt numeric NOT NULL);
|
|
|
|
INSERT INTO mvtest_t VALUES
|
2013-03-04 01:23:31 +01:00
|
|
|
(1, 'x', 2),
|
|
|
|
(2, 'x', 3),
|
|
|
|
(3, 'y', 5),
|
|
|
|
(4, 'y', 7),
|
|
|
|
(5, 'z', 11);
|
|
|
|
-- we want a view based on the table, too, since views present additional challenges
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE VIEW mvtest_tv AS SELECT type, sum(amt) AS totamt FROM mvtest_t GROUP BY type;
|
|
|
|
SELECT * FROM mvtest_tv ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
2013-03-19 16:33:37 +01:00
|
|
|
x | 5
|
2013-03-04 01:23:31 +01:00
|
|
|
y | 12
|
|
|
|
z | 11
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
-- create a materialized view with no data, and confirm correct behavior
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tm AS SELECT type, sum(amt) AS totamt FROM mvtest_t GROUP BY type WITH NO DATA;
|
|
|
|
QUERY PLAN
|
|
|
|
----------------------------
|
2013-03-04 01:23:31 +01:00
|
|
|
HashAggregate
|
2013-12-12 17:24:38 +01:00
|
|
|
Group Key: type
|
2016-04-15 19:04:17 +02:00
|
|
|
-> Seq Scan on mvtest_t
|
2013-12-12 17:24:38 +01:00
|
|
|
(3 rows)
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tm AS SELECT type, sum(amt) AS totamt FROM mvtest_t GROUP BY type WITH NO DATA;
|
|
|
|
SELECT relispopulated FROM pg_class WHERE oid = 'mvtest_tm'::regclass;
|
2013-05-06 19:26:51 +02:00
|
|
|
relispopulated
|
|
|
|
----------------
|
2013-03-04 01:23:31 +01:00
|
|
|
f
|
|
|
|
(1 row)
|
|
|
|
|
2016-10-10 22:41:57 +02:00
|
|
|
SELECT * FROM mvtest_tm ORDER BY type;
|
2016-04-15 19:04:17 +02:00
|
|
|
ERROR: materialized view "mvtest_tm" has not been populated
|
2013-03-04 01:23:31 +01:00
|
|
|
HINT: Use the REFRESH MATERIALIZED VIEW command.
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW mvtest_tm;
|
|
|
|
SELECT relispopulated FROM pg_class WHERE oid = 'mvtest_tm'::regclass;
|
2013-05-06 19:26:51 +02:00
|
|
|
relispopulated
|
|
|
|
----------------
|
2013-03-04 01:23:31 +01:00
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE UNIQUE INDEX mvtest_tm_type ON mvtest_tm (type);
|
2016-10-10 22:41:57 +02:00
|
|
|
SELECT * FROM mvtest_tm ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
2016-10-10 22:41:57 +02:00
|
|
|
x | 5
|
2013-03-04 01:23:31 +01:00
|
|
|
y | 12
|
|
|
|
z | 11
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
-- create various views
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tvm AS SELECT * FROM mvtest_tv ORDER BY type;
|
|
|
|
QUERY PLAN
|
|
|
|
----------------------------------
|
2013-03-19 16:33:37 +01:00
|
|
|
Sort
|
2016-04-15 19:04:17 +02:00
|
|
|
Sort Key: mvtest_t.type
|
2013-03-19 16:33:37 +01:00
|
|
|
-> HashAggregate
|
2016-04-15 19:04:17 +02:00
|
|
|
Group Key: mvtest_t.type
|
|
|
|
-> Seq Scan on mvtest_t
|
2013-12-12 17:24:38 +01:00
|
|
|
(5 rows)
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tvm AS SELECT * FROM mvtest_tv ORDER BY type;
|
|
|
|
SELECT * FROM mvtest_tvm;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
2013-03-19 16:33:37 +01:00
|
|
|
x | 5
|
2013-03-04 01:23:31 +01:00
|
|
|
y | 12
|
|
|
|
z | 11
|
|
|
|
(3 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tmm AS SELECT sum(totamt) AS grandtot FROM mvtest_tm;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_tvmm AS SELECT sum(totamt) AS grandtot FROM mvtest_tvm;
|
|
|
|
CREATE UNIQUE INDEX mvtest_tvmm_expr ON mvtest_tvmm ((grandtot > 0));
|
|
|
|
CREATE UNIQUE INDEX mvtest_tvmm_pred ON mvtest_tvmm (grandtot) WHERE grandtot < 0;
|
|
|
|
CREATE VIEW mvtest_tvv AS SELECT sum(totamt) AS grandtot FROM mvtest_tv;
|
2013-03-04 01:23:31 +01:00
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tvvm AS SELECT * FROM mvtest_tvv;
|
|
|
|
QUERY PLAN
|
|
|
|
----------------------------------
|
2013-03-04 01:23:31 +01:00
|
|
|
Aggregate
|
|
|
|
-> HashAggregate
|
2016-04-15 19:04:17 +02:00
|
|
|
Group Key: mvtest_t.type
|
|
|
|
-> Seq Scan on mvtest_t
|
2013-12-12 17:24:38 +01:00
|
|
|
(4 rows)
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_tvvm AS SELECT * FROM mvtest_tvv;
|
|
|
|
CREATE VIEW mvtest_tvvmv AS SELECT * FROM mvtest_tvvm;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_bb AS SELECT * FROM mvtest_tvvmv;
|
|
|
|
CREATE INDEX mvtest_aa ON mvtest_bb (grandtot);
|
2013-03-04 01:23:31 +01:00
|
|
|
-- check that plans seem reasonable
|
2016-04-15 19:04:17 +02:00
|
|
|
\d+ mvtest_tvm
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mvtest_tvm"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------+----------+--------------+-------------
|
|
|
|
type | text | | | | extended | |
|
|
|
|
totamt | numeric | | | | main | |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT type,
|
|
|
|
totamt
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_tv
|
2023-01-18 19:23:57 +01:00
|
|
|
ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
\d+ mvtest_tvm
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mvtest_tvm"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------+----------+--------------+-------------
|
|
|
|
type | text | | | | extended | |
|
|
|
|
totamt | numeric | | | | main | |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT type,
|
|
|
|
totamt
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_tv
|
2023-01-18 19:23:57 +01:00
|
|
|
ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
\d+ mvtest_tvvm
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mvtest_tvvm"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
----------+---------+-----------+----------+---------+---------+--------------+-------------
|
|
|
|
grandtot | numeric | | | | main | |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT grandtot
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_tvv;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
\d+ mvtest_bb
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mvtest_bb"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
----------+---------+-----------+----------+---------+---------+--------------+-------------
|
|
|
|
grandtot | numeric | | | | main | |
|
2013-03-04 01:23:31 +01:00
|
|
|
Indexes:
|
2016-04-15 19:04:17 +02:00
|
|
|
"mvtest_aa" btree (grandtot)
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT grandtot
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_tvvmv;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
|
|
|
-- test schema behavior
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE SCHEMA mvtest_mvschema;
|
|
|
|
ALTER MATERIALIZED VIEW mvtest_tvm SET SCHEMA mvtest_mvschema;
|
|
|
|
\d+ mvtest_tvm
|
|
|
|
\d+ mvtest_tvmm
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mvtest_tvmm"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
----------+---------+-----------+----------+---------+---------+--------------+-------------
|
|
|
|
grandtot | numeric | | | | main | |
|
2013-07-16 19:55:44 +02:00
|
|
|
Indexes:
|
2016-04-15 19:04:17 +02:00
|
|
|
"mvtest_tvmm_expr" UNIQUE, btree ((grandtot > 0::numeric))
|
|
|
|
"mvtest_tvmm_pred" UNIQUE, btree (grandtot) WHERE grandtot < 0::numeric
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT sum(totamt) AS grandtot
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_mvschema.mvtest_tvm;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SET search_path = mvtest_mvschema, public;
|
|
|
|
\d+ mvtest_tvm
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "mvtest_mvschema.mvtest_tvm"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------+----------+--------------+-------------
|
|
|
|
type | text | | | | extended | |
|
|
|
|
totamt | numeric | | | | main | |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2023-01-18 19:23:57 +01:00
|
|
|
SELECT type,
|
|
|
|
totamt
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_tv
|
2023-01-18 19:23:57 +01:00
|
|
|
ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
|
|
|
-- modify the underlying table data
|
2016-04-15 19:04:17 +02:00
|
|
|
INSERT INTO mvtest_t VALUES (6, 'z', 13);
|
2013-03-04 01:23:31 +01:00
|
|
|
-- confirm pre- and post-refresh contents of fairly simple materialized views
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tm ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
|
|
|
x | 5
|
|
|
|
y | 12
|
|
|
|
z | 11
|
|
|
|
(3 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvm ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
|
|
|
x | 5
|
|
|
|
y | 12
|
|
|
|
z | 11
|
|
|
|
(3 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_tm;
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_tvm;
|
|
|
|
SELECT * FROM mvtest_tm ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
|
|
|
x | 5
|
|
|
|
y | 12
|
|
|
|
z | 24
|
|
|
|
(3 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvm ORDER BY type;
|
2013-03-04 01:23:31 +01:00
|
|
|
type | totamt
|
|
|
|
------+--------
|
|
|
|
x | 5
|
|
|
|
y | 12
|
|
|
|
z | 24
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
RESET search_path;
|
|
|
|
-- confirm pre- and post-refresh contents of nested materialized views
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tmm;
|
|
|
|
QUERY PLAN
|
|
|
|
------------------------
|
|
|
|
Seq Scan on mvtest_tmm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvmm;
|
|
|
|
QUERY PLAN
|
|
|
|
-------------------------
|
|
|
|
Seq Scan on mvtest_tvmm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvvm;
|
|
|
|
QUERY PLAN
|
|
|
|
-------------------------
|
|
|
|
Seq Scan on mvtest_tvvm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tmm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
28
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvmm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
28
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvvm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
28
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW mvtest_tmm;
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_tvmm;
|
|
|
|
ERROR: cannot refresh materialized view "public.mvtest_tvmm" concurrently
|
2014-09-05 07:20:33 +02:00
|
|
|
HINT: Create a unique index with no WHERE clause on one or more columns of the materialized view.
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW mvtest_tvmm;
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_tvvm;
|
2013-03-04 01:23:31 +01:00
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tmm;
|
|
|
|
QUERY PLAN
|
|
|
|
------------------------
|
|
|
|
Seq Scan on mvtest_tmm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvmm;
|
|
|
|
QUERY PLAN
|
|
|
|
-------------------------
|
|
|
|
Seq Scan on mvtest_tvmm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
EXPLAIN (costs off)
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvvm;
|
|
|
|
QUERY PLAN
|
|
|
|
-------------------------
|
|
|
|
Seq Scan on mvtest_tvvm
|
2013-03-04 01:23:31 +01:00
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tmm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
41
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvmm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
41
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvvm;
|
2013-03-04 01:23:31 +01:00
|
|
|
grandtot
|
|
|
|
----------
|
|
|
|
41
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- test diemv when the mv does not exist
|
2013-05-06 17:57:05 +02:00
|
|
|
DROP MATERIALIZED VIEW IF EXISTS no_such_mv;
|
|
|
|
NOTICE: materialized view "no_such_mv" does not exist, skipping
|
2017-02-06 10:33:58 +01:00
|
|
|
-- make sure invalid combination of options is prohibited
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_tvmm WITH NO DATA;
|
2013-07-16 19:55:44 +02:00
|
|
|
ERROR: CONCURRENTLY and WITH NO DATA options cannot be used together
|
2013-08-02 18:49:03 +02:00
|
|
|
-- no tuple locks on materialized views
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_tvvm FOR SHARE;
|
|
|
|
ERROR: cannot lock rows in materialized view "mvtest_tvvm"
|
2013-03-14 19:34:51 +01:00
|
|
|
-- test join of mv and view
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM mvtest_tm m LEFT JOIN mvtest_tv v USING (type) ORDER BY type;
|
2013-03-14 19:34:51 +01:00
|
|
|
type | mtot | vtot
|
|
|
|
------+------+------
|
2013-03-19 16:33:37 +01:00
|
|
|
x | 5 | 5
|
2013-03-14 19:34:51 +01:00
|
|
|
y | 12 | 12
|
|
|
|
z | 24 | 24
|
|
|
|
(3 rows)
|
|
|
|
|
2013-03-04 01:23:31 +01:00
|
|
|
-- make sure that dependencies are reported properly when they block the drop
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_t;
|
|
|
|
ERROR: cannot drop table mvtest_t because other objects depend on it
|
|
|
|
DETAIL: view mvtest_tv depends on table mvtest_t
|
Sort the dependent objects before recursing in findDependentObjects().
Historically, the notices output by DROP CASCADE tended to come out
in uncertain order, and in some cases you might get different claims
about which object depends on which other one. This is because we
just traversed the dependency tree in the order in which pg_depend
entries are seen, and nbtree has never promised anything about the
order of equal-keyed index entries. We've put up with that for years,
hacking regression tests when necessary to prevent them from emitting
unstable output. However, it's a problem for pending work that will
change nbtree's behavior for equal keys, as that causes unexpected
changes in the regression test results.
Hence, adjust findDependentObjects to sort the results of each
indexscan before processing them. The sort is on descending OID of
the dependent objects, hence more or less reverse creation order.
While this rule could still result in bogus regression test failures
if an OID wraparound occurred mid-test, that seems unlikely to happen
in any plausible development or packaging-test scenario.
This is enough to ensure output stability for ordinary DROP CASCADE
commands, but not for DROP OWNED BY, because that has a different
code path with the same problem. We might later choose to sort in
the DROP OWNED BY code as well, but this patch doesn't do so.
I've also not done anything about reverting the existing hacks to
suppress unstable DROP CASCADE output in specific regression tests.
It might be worth undoing those, but it seems like a distinct question.
The first indexscan loop in findDependentObjects is not touched,
meaning there is a hazard of unstable error reports from that too.
However, said hazard is not the fault of that code: it was designed
on the assumption that there could be at most one "owning" object
to complain about, and that assumption does not seem unreasonable.
The recent patch that added the possibility of multiple
DEPENDENCY_INTERNAL_AUTO links broke that assumption, but we should
fix that situation not band-aid around it. That's a matter for
another patch, though.
Discussion: https://postgr.es/m/12244.1547854440@sss.pgh.pa.us
2019-01-21 19:48:07 +01:00
|
|
|
materialized view mvtest_mvschema.mvtest_tvm depends on view mvtest_tv
|
|
|
|
materialized view mvtest_tvmm depends on materialized view mvtest_mvschema.mvtest_tvm
|
2016-04-15 19:04:17 +02:00
|
|
|
view mvtest_tvv depends on view mvtest_tv
|
|
|
|
materialized view mvtest_tvvm depends on view mvtest_tvv
|
|
|
|
view mvtest_tvvmv depends on materialized view mvtest_tvvm
|
|
|
|
materialized view mvtest_bb depends on view mvtest_tvvmv
|
|
|
|
materialized view mvtest_tm depends on table mvtest_t
|
|
|
|
materialized view mvtest_tmm depends on materialized view mvtest_tm
|
2013-03-04 01:23:31 +01:00
|
|
|
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
|
|
|
-- make sure dependencies are dropped and reported
|
|
|
|
-- and make sure that transactional behavior is correct on rollback
|
|
|
|
-- incidentally leaving some interesting materialized views for pg_dump testing
|
|
|
|
BEGIN;
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_t CASCADE;
|
2013-03-04 01:23:31 +01:00
|
|
|
NOTICE: drop cascades to 9 other objects
|
2016-04-15 19:04:17 +02:00
|
|
|
DETAIL: drop cascades to view mvtest_tv
|
Sort the dependent objects before recursing in findDependentObjects().
Historically, the notices output by DROP CASCADE tended to come out
in uncertain order, and in some cases you might get different claims
about which object depends on which other one. This is because we
just traversed the dependency tree in the order in which pg_depend
entries are seen, and nbtree has never promised anything about the
order of equal-keyed index entries. We've put up with that for years,
hacking regression tests when necessary to prevent them from emitting
unstable output. However, it's a problem for pending work that will
change nbtree's behavior for equal keys, as that causes unexpected
changes in the regression test results.
Hence, adjust findDependentObjects to sort the results of each
indexscan before processing them. The sort is on descending OID of
the dependent objects, hence more or less reverse creation order.
While this rule could still result in bogus regression test failures
if an OID wraparound occurred mid-test, that seems unlikely to happen
in any plausible development or packaging-test scenario.
This is enough to ensure output stability for ordinary DROP CASCADE
commands, but not for DROP OWNED BY, because that has a different
code path with the same problem. We might later choose to sort in
the DROP OWNED BY code as well, but this patch doesn't do so.
I've also not done anything about reverting the existing hacks to
suppress unstable DROP CASCADE output in specific regression tests.
It might be worth undoing those, but it seems like a distinct question.
The first indexscan loop in findDependentObjects is not touched,
meaning there is a hazard of unstable error reports from that too.
However, said hazard is not the fault of that code: it was designed
on the assumption that there could be at most one "owning" object
to complain about, and that assumption does not seem unreasonable.
The recent patch that added the possibility of multiple
DEPENDENCY_INTERNAL_AUTO links broke that assumption, but we should
fix that situation not band-aid around it. That's a matter for
another patch, though.
Discussion: https://postgr.es/m/12244.1547854440@sss.pgh.pa.us
2019-01-21 19:48:07 +01:00
|
|
|
drop cascades to materialized view mvtest_mvschema.mvtest_tvm
|
|
|
|
drop cascades to materialized view mvtest_tvmm
|
2016-04-15 19:04:17 +02:00
|
|
|
drop cascades to view mvtest_tvv
|
|
|
|
drop cascades to materialized view mvtest_tvvm
|
|
|
|
drop cascades to view mvtest_tvvmv
|
|
|
|
drop cascades to materialized view mvtest_bb
|
|
|
|
drop cascades to materialized view mvtest_tm
|
|
|
|
drop cascades to materialized view mvtest_tmm
|
2013-03-04 01:23:31 +01:00
|
|
|
ROLLBACK;
|
|
|
|
-- some additional tests not using base tables
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE VIEW mvtest_vt1 AS SELECT 1 moo;
|
|
|
|
CREATE VIEW mvtest_vt2 AS SELECT moo, 2*moo FROM mvtest_vt1 UNION ALL SELECT moo, 3*moo FROM mvtest_vt1;
|
|
|
|
\d+ mvtest_vt2
|
2016-11-03 17:00:00 +01:00
|
|
|
View "public.mvtest_vt2"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Description
|
|
|
|
----------+---------+-----------+----------+---------+---------+-------------
|
|
|
|
moo | integer | | | | plain |
|
|
|
|
?column? | integer | | | | plain |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT mvtest_vt1.moo,
|
2022-05-21 20:45:58 +02:00
|
|
|
2 * mvtest_vt1.moo AS "?column?"
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_vt1
|
2013-11-11 19:36:38 +01:00
|
|
|
UNION ALL
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT mvtest_vt1.moo,
|
|
|
|
3 * mvtest_vt1.moo
|
|
|
|
FROM mvtest_vt1;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM mvtest_vt2 UNION ALL SELECT moo, 3*moo FROM mvtest_vt2;
|
2013-03-04 01:23:31 +01:00
|
|
|
\d+ mv_test2
|
2016-11-03 17:00:00 +01:00
|
|
|
Materialized view "public.mv_test2"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
----------+---------+-----------+----------+---------+---------+--------------+-------------
|
|
|
|
moo | integer | | | | plain | |
|
|
|
|
?column? | integer | | | | plain | |
|
2013-03-04 01:23:31 +01:00
|
|
|
View definition:
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT mvtest_vt2.moo,
|
2022-05-21 20:45:58 +02:00
|
|
|
2 * mvtest_vt2.moo AS "?column?"
|
2016-04-15 19:04:17 +02:00
|
|
|
FROM mvtest_vt2
|
2013-11-11 19:36:38 +01:00
|
|
|
UNION ALL
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT mvtest_vt2.moo,
|
|
|
|
3 * mvtest_vt2.moo
|
|
|
|
FROM mvtest_vt2;
|
2013-03-04 01:23:31 +01:00
|
|
|
|
|
|
|
CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345;
|
2013-05-06 19:26:51 +02:00
|
|
|
SELECT relispopulated FROM pg_class WHERE oid = 'mv_test3'::regclass;
|
|
|
|
relispopulated
|
|
|
|
----------------
|
2013-03-04 01:23:31 +01:00
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP VIEW mvtest_vt1 CASCADE;
|
2013-03-04 01:23:31 +01:00
|
|
|
NOTICE: drop cascades to 3 other objects
|
2016-04-15 19:04:17 +02:00
|
|
|
DETAIL: drop cascades to view mvtest_vt2
|
2013-03-04 01:23:31 +01:00
|
|
|
drop cascades to materialized view mv_test2
|
|
|
|
drop cascades to materialized view mv_test3
|
2013-07-16 19:55:44 +02:00
|
|
|
-- test that duplicate values on unique index prevent refresh
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_foo(a, b) AS VALUES(1, 10);
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv AS SELECT * FROM mvtest_foo;
|
|
|
|
CREATE UNIQUE INDEX ON mvtest_mv(a);
|
|
|
|
INSERT INTO mvtest_foo SELECT * FROM mvtest_foo;
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv;
|
|
|
|
ERROR: could not create unique index "mvtest_mv_a_idx"
|
2013-07-16 19:55:44 +02:00
|
|
|
DETAIL: Key (a)=(1) is duplicated.
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv;
|
|
|
|
ERROR: new data for materialized view "mvtest_mv" contains duplicate rows without any null columns
|
2013-07-16 19:55:44 +02:00
|
|
|
DETAIL: Row: (1,10)
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_foo CASCADE;
|
|
|
|
NOTICE: drop cascades to materialized view mvtest_mv
|
2013-10-09 21:26:09 +02:00
|
|
|
-- make sure that all columns covered by unique indexes works
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_foo(a, b, c) AS VALUES(1, 2, 3);
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv AS SELECT * FROM mvtest_foo;
|
|
|
|
CREATE UNIQUE INDEX ON mvtest_mv (a);
|
|
|
|
CREATE UNIQUE INDEX ON mvtest_mv (b);
|
|
|
|
CREATE UNIQUE INDEX on mvtest_mv (c);
|
|
|
|
INSERT INTO mvtest_foo VALUES(2, 3, 4);
|
|
|
|
INSERT INTO mvtest_foo VALUES(3, 4, 5);
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv;
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv;
|
|
|
|
DROP TABLE mvtest_foo CASCADE;
|
|
|
|
NOTICE: drop cascades to materialized view mvtest_mv
|
2013-11-03 00:38:17 +01:00
|
|
|
-- allow subquery to reference unpopulated matview if WITH NO DATA is specified
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv1 AS SELECT 1 AS col1 WITH NO DATA;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv2 AS SELECT * FROM mvtest_mv1
|
|
|
|
WHERE col1 = (SELECT LEAST(col1) FROM mvtest_mv1) WITH NO DATA;
|
|
|
|
DROP MATERIALIZED VIEW mvtest_mv1 CASCADE;
|
|
|
|
NOTICE: drop cascades to materialized view mvtest_mv2
|
2013-10-09 21:26:09 +02:00
|
|
|
-- make sure that types with unusual equality tests work
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_boxes (id serial primary key, b box);
|
|
|
|
INSERT INTO mvtest_boxes (b) VALUES
|
2013-10-09 21:26:09 +02:00
|
|
|
('(32,32),(31,31)'),
|
|
|
|
('(2.0000004,2.0000004),(1,1)'),
|
|
|
|
('(1.9999996,1.9999996),(1,1)');
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_boxmv AS SELECT * FROM mvtest_boxes;
|
|
|
|
CREATE UNIQUE INDEX mvtest_boxmv_id ON mvtest_boxmv (id);
|
|
|
|
UPDATE mvtest_boxes SET b = '(2,2),(1,1)' WHERE id = 2;
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_boxmv;
|
|
|
|
SELECT * FROM mvtest_boxmv ORDER BY id;
|
2013-10-09 21:26:09 +02:00
|
|
|
id | b
|
|
|
|
----+-----------------------------
|
|
|
|
1 | (32,32),(31,31)
|
|
|
|
2 | (2,2),(1,1)
|
|
|
|
3 | (1.9999996,1.9999996),(1,1)
|
|
|
|
(3 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_boxes CASCADE;
|
|
|
|
NOTICE: drop cascades to materialized view mvtest_boxmv
|
2013-11-04 21:31:07 +01:00
|
|
|
-- make sure that column names are handled correctly
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_v (i int, j int);
|
2016-06-27 21:57:21 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj, kk) AS SELECT i, j FROM mvtest_v; -- error
|
|
|
|
ERROR: too many column names were specified
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj) AS SELECT i, j FROM mvtest_v; -- ok
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v_2 (ii) AS SELECT i, j FROM mvtest_v; -- ok
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj, kk) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- error
|
|
|
|
ERROR: too many column names were specified
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v_4 (ii) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
|
2016-04-15 19:04:17 +02:00
|
|
|
ALTER TABLE mvtest_v RENAME COLUMN i TO x;
|
|
|
|
INSERT INTO mvtest_v values (1, 2);
|
|
|
|
CREATE UNIQUE INDEX mvtest_mv_v_ii ON mvtest_mv_v (ii);
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv_v;
|
|
|
|
UPDATE mvtest_v SET j = 3 WHERE x = 1;
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv_v;
|
2016-06-27 21:57:21 +02:00
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv_v_2;
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv_v_3;
|
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv_v_4;
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_v;
|
2013-11-04 21:31:07 +01:00
|
|
|
x | j
|
|
|
|
---+---
|
|
|
|
1 | 3
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_mv_v;
|
2013-11-04 21:31:07 +01:00
|
|
|
ii | jj
|
|
|
|
----+----
|
|
|
|
1 | 3
|
|
|
|
(1 row)
|
|
|
|
|
2016-06-27 21:57:21 +02:00
|
|
|
SELECT * FROM mvtest_mv_v_2;
|
|
|
|
ii | j
|
|
|
|
----+---
|
|
|
|
1 | 3
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM mvtest_mv_v_3;
|
|
|
|
ii | jj
|
|
|
|
----+----
|
|
|
|
1 | 3
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM mvtest_mv_v_4;
|
|
|
|
ii | j
|
|
|
|
----+---
|
|
|
|
1 | 3
|
|
|
|
(1 row)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_v CASCADE;
|
2016-06-27 21:57:21 +02:00
|
|
|
NOTICE: drop cascades to 4 other objects
|
|
|
|
DETAIL: drop cascades to materialized view mvtest_mv_v
|
|
|
|
drop cascades to materialized view mvtest_mv_v_2
|
|
|
|
drop cascades to materialized view mvtest_mv_v_3
|
|
|
|
drop cascades to materialized view mvtest_mv_v_4
|
Change unknown-type literals to type text in SELECT and RETURNING lists.
Previously, we left such literals alone if the query or subquery had
no properties forcing a type decision to be made (such as an ORDER BY or
DISTINCT clause using that output column). This meant that "unknown" could
be an exposed output column type, which has never been a great idea because
it could result in strange failures later on. For example, an outer query
that tried to do any operations on an unknown-type subquery output would
generally fail with some weird error like "failed to find conversion
function from unknown to text" or "could not determine which collation to
use for string comparison". Also, if the case occurred in a CREATE VIEW's
query then the view would have an unknown-type column, causing similar
failures in queries trying to use the view.
To fix, at the tail end of parse analysis of a query, forcibly convert any
remaining "unknown" literals in its SELECT or RETURNING list to type text.
However, provide a switch to suppress that, and use it in the cases of
SELECT inside a set operation or INSERT command. In those cases we already
had type resolution rules that make use of context information from outside
the subquery proper, and we don't want to change that behavior.
Also, change creation of an unknown-type column in a relation from a
warning to a hard error. The error should be unreachable now in CREATE
VIEW or CREATE MATVIEW, but it's still possible to explicitly say "unknown"
in CREATE TABLE or CREATE (composite) TYPE. We want to forbid that because
it's nothing but a foot-gun.
This change creates a pg_upgrade failure case: a matview that contains an
unknown-type column can't be pg_upgraded, because reparsing the matview's
defining query will now decide that the column is of type text, which
doesn't match the cstring-like storage that the old materialized column
would actually have. Add a checking pass to detect that. While at it,
we can detect tables or composite types that would fail, essentially
for free. Those would fail safely anyway later on, but we might as
well fail earlier.
This patch is by me, but it owes something to previous investigations
by Rahila Syed. Also thanks to Ashutosh Bapat and Michael Paquier for
review.
Discussion: https://postgr.es/m/CAH2L28uwwbL9HUM-WR=hromW1Cvamkn7O-g8fPY2m=_7muJ0oA@mail.gmail.com
2017-01-25 15:17:18 +01:00
|
|
|
-- Check that unknown literals are converted to "text" in CREATE MATVIEW,
|
|
|
|
-- so that we don't end up with unknown-type columns.
|
|
|
|
CREATE MATERIALIZED VIEW mv_unspecified_types AS
|
|
|
|
SELECT 42 as i, 42.5 as num, 'foo' as u, 'foo'::unknown as u2, null as n;
|
|
|
|
\d+ mv_unspecified_types
|
|
|
|
Materialized view "public.mv_unspecified_types"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------+----------+--------------+-------------
|
|
|
|
i | integer | | | | plain | |
|
|
|
|
num | numeric | | | | main | |
|
|
|
|
u | text | | | | extended | |
|
|
|
|
u2 | text | | | | extended | |
|
|
|
|
n | text | | | | extended | |
|
|
|
|
View definition:
|
|
|
|
SELECT 42 AS i,
|
|
|
|
42.5 AS num,
|
|
|
|
'foo'::text AS u,
|
|
|
|
'foo'::text AS u2,
|
|
|
|
NULL::text AS n;
|
|
|
|
|
|
|
|
SELECT * FROM mv_unspecified_types;
|
|
|
|
i | num | u | u2 | n
|
|
|
|
----+------+-----+-----+---
|
|
|
|
42 | 42.5 | foo | foo |
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP MATERIALIZED VIEW mv_unspecified_types;
|
2016-06-27 21:57:21 +02:00
|
|
|
-- make sure that create WITH NO DATA does not plan the query (bug #13907)
|
|
|
|
create materialized view mvtest_error as select 1/0 as x; -- fail
|
|
|
|
ERROR: division by zero
|
|
|
|
create materialized view mvtest_error as select 1/0 as x with no data;
|
|
|
|
refresh materialized view mvtest_error; -- fail here
|
|
|
|
ERROR: division by zero
|
|
|
|
drop materialized view mvtest_error;
|
2014-03-06 17:37:02 +01:00
|
|
|
-- make sure that matview rows can be referenced as source rows (bug #9398)
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE TABLE mvtest_v AS SELECT generate_series(1,10) AS a;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_v AS SELECT a FROM mvtest_v WHERE a <= 5;
|
|
|
|
DELETE FROM mvtest_v WHERE EXISTS ( SELECT * FROM mvtest_mv_v WHERE mvtest_mv_v.a = mvtest_v.a );
|
|
|
|
SELECT * FROM mvtest_v;
|
2014-03-06 17:37:02 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
(5 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
SELECT * FROM mvtest_mv_v;
|
2014-03-06 17:37:02 +01:00
|
|
|
a
|
|
|
|
---
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(5 rows)
|
|
|
|
|
2016-04-15 19:04:17 +02:00
|
|
|
DROP TABLE mvtest_v CASCADE;
|
|
|
|
NOTICE: drop cascades to materialized view mvtest_mv_v
|
2014-08-26 16:56:26 +02:00
|
|
|
-- make sure running as superuser works when MV owned by another role (bug #11208)
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE ROLE regress_user_mvtest;
|
|
|
|
SET ROLE regress_user_mvtest;
|
2021-08-07 19:29:32 +02:00
|
|
|
-- this test case also checks for ambiguity in the queries issued by
|
|
|
|
-- refresh_by_match_merge(), by choosing column names that intentionally
|
|
|
|
-- duplicate all the aliases used in those queries
|
|
|
|
CREATE TABLE mvtest_foo_data AS SELECT i,
|
|
|
|
i+1 AS tid,
|
2023-03-13 10:15:44 +01:00
|
|
|
fipshash(random()::text) AS mv,
|
|
|
|
fipshash(random()::text) AS newdata,
|
|
|
|
fipshash(random()::text) AS newdata2,
|
|
|
|
fipshash(random()::text) AS diff
|
2014-08-26 16:56:26 +02:00
|
|
|
FROM generate_series(1, 10) i;
|
2016-04-15 19:04:17 +02:00
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_foo AS SELECT * FROM mvtest_foo_data;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest_mv_foo AS SELECT * FROM mvtest_foo_data;
|
|
|
|
ERROR: relation "mvtest_mv_foo" already exists
|
|
|
|
CREATE MATERIALIZED VIEW IF NOT EXISTS mvtest_mv_foo AS SELECT * FROM mvtest_foo_data;
|
|
|
|
NOTICE: relation "mvtest_mv_foo" already exists, skipping
|
|
|
|
CREATE UNIQUE INDEX ON mvtest_mv_foo (i);
|
2014-08-26 16:56:26 +02:00
|
|
|
RESET ROLE;
|
2016-04-15 19:04:17 +02:00
|
|
|
REFRESH MATERIALIZED VIEW mvtest_mv_foo;
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv_foo;
|
|
|
|
DROP OWNED BY regress_user_mvtest CASCADE;
|
|
|
|
DROP ROLE regress_user_mvtest;
|
2024-02-05 10:01:30 +01:00
|
|
|
-- Concurrent refresh requires a unique index on the materialized
|
|
|
|
-- view. Test what happens if it's dropped during the refresh.
|
|
|
|
CREATE OR REPLACE FUNCTION mvtest_drop_the_index()
|
|
|
|
RETURNS bool AS $$
|
|
|
|
BEGIN
|
|
|
|
EXECUTE 'DROP INDEX IF EXISTS mvtest_drop_idx';
|
|
|
|
RETURN true;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE MATERIALIZED VIEW drop_idx_matview AS
|
|
|
|
SELECT 1 as i WHERE mvtest_drop_the_index();
|
|
|
|
NOTICE: index "mvtest_drop_idx" does not exist, skipping
|
|
|
|
CREATE UNIQUE INDEX mvtest_drop_idx ON drop_idx_matview (i);
|
|
|
|
REFRESH MATERIALIZED VIEW CONCURRENTLY drop_idx_matview;
|
|
|
|
ERROR: could not find suitable unique index on materialized view
|
|
|
|
DROP MATERIALIZED VIEW drop_idx_matview; -- clean up
|
2016-08-11 17:22:25 +02:00
|
|
|
-- make sure that create WITH NO DATA works via SPI
|
|
|
|
BEGIN;
|
|
|
|
CREATE FUNCTION mvtest_func()
|
|
|
|
RETURNS void AS $$
|
|
|
|
BEGIN
|
|
|
|
CREATE MATERIALIZED VIEW mvtest1 AS SELECT 1 AS x;
|
|
|
|
CREATE MATERIALIZED VIEW mvtest2 AS SELECT 1 AS x WITH NO DATA;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
SELECT mvtest_func();
|
|
|
|
mvtest_func
|
|
|
|
-------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM mvtest1;
|
|
|
|
x
|
|
|
|
---
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM mvtest2;
|
|
|
|
ERROR: materialized view "mvtest2" has not been populated
|
|
|
|
HINT: Use the REFRESH MATERIALIZED VIEW command.
|
|
|
|
ROLLBACK;
|
2020-11-16 03:52:40 +01:00
|
|
|
-- INSERT privileges if relation owner is not allowed to insert.
|
|
|
|
CREATE SCHEMA matview_schema;
|
|
|
|
CREATE USER regress_matview_user;
|
|
|
|
ALTER DEFAULT PRIVILEGES FOR ROLE regress_matview_user
|
|
|
|
REVOKE INSERT ON TABLES FROM regress_matview_user;
|
|
|
|
GRANT ALL ON SCHEMA matview_schema TO public;
|
|
|
|
SET SESSION AUTHORIZATION regress_matview_user;
|
|
|
|
CREATE MATERIALIZED VIEW matview_schema.mv_withdata1 (a) AS
|
2020-11-21 11:45:30 +01:00
|
|
|
SELECT generate_series(1, 10) WITH DATA;
|
2020-11-16 03:52:40 +01:00
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
2020-11-21 11:45:30 +01:00
|
|
|
CREATE MATERIALIZED VIEW matview_schema.mv_withdata2 (a) AS
|
|
|
|
SELECT generate_series(1, 10) WITH DATA;
|
|
|
|
QUERY PLAN
|
|
|
|
--------------------------------------
|
|
|
|
ProjectSet (actual rows=10 loops=1)
|
|
|
|
-> Result (actual rows=1 loops=1)
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
REFRESH MATERIALIZED VIEW matview_schema.mv_withdata2;
|
2020-11-16 03:52:40 +01:00
|
|
|
CREATE MATERIALIZED VIEW matview_schema.mv_nodata1 (a) AS
|
|
|
|
SELECT generate_series(1, 10) WITH NO DATA;
|
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
|
|
|
CREATE MATERIALIZED VIEW matview_schema.mv_nodata2 (a) AS
|
|
|
|
SELECT generate_series(1, 10) WITH NO DATA;
|
|
|
|
QUERY PLAN
|
|
|
|
-------------------------------
|
|
|
|
ProjectSet (never executed)
|
|
|
|
-> Result (never executed)
|
|
|
|
(2 rows)
|
|
|
|
|
2020-11-21 11:45:30 +01:00
|
|
|
REFRESH MATERIALIZED VIEW matview_schema.mv_nodata2;
|
2020-11-16 03:52:40 +01:00
|
|
|
RESET SESSION AUTHORIZATION;
|
|
|
|
ALTER DEFAULT PRIVILEGES FOR ROLE regress_matview_user
|
|
|
|
GRANT INSERT ON TABLES TO regress_matview_user;
|
|
|
|
DROP SCHEMA matview_schema CASCADE;
|
2020-11-21 11:45:30 +01:00
|
|
|
NOTICE: drop cascades to 4 other objects
|
|
|
|
DETAIL: drop cascades to materialized view matview_schema.mv_withdata1
|
|
|
|
drop cascades to materialized view matview_schema.mv_withdata2
|
|
|
|
drop cascades to materialized view matview_schema.mv_nodata1
|
2020-11-16 03:52:40 +01:00
|
|
|
drop cascades to materialized view matview_schema.mv_nodata2
|
|
|
|
DROP USER regress_matview_user;
|
2020-12-30 13:23:24 +01:00
|
|
|
-- CREATE MATERIALIZED VIEW ... IF NOT EXISTS
|
|
|
|
CREATE MATERIALIZED VIEW matview_ine_tab AS SELECT 1;
|
|
|
|
CREATE MATERIALIZED VIEW matview_ine_tab AS SELECT 1 / 0; -- error
|
|
|
|
ERROR: relation "matview_ine_tab" already exists
|
|
|
|
CREATE MATERIALIZED VIEW IF NOT EXISTS matview_ine_tab AS
|
|
|
|
SELECT 1 / 0; -- ok
|
|
|
|
NOTICE: relation "matview_ine_tab" already exists, skipping
|
|
|
|
CREATE MATERIALIZED VIEW matview_ine_tab AS
|
|
|
|
SELECT 1 / 0 WITH NO DATA; -- error
|
|
|
|
ERROR: relation "matview_ine_tab" already exists
|
|
|
|
CREATE MATERIALIZED VIEW IF NOT EXISTS matview_ine_tab AS
|
|
|
|
SELECT 1 / 0 WITH NO DATA; -- ok
|
|
|
|
NOTICE: relation "matview_ine_tab" already exists, skipping
|
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
|
|
|
CREATE MATERIALIZED VIEW matview_ine_tab AS
|
|
|
|
SELECT 1 / 0; -- error
|
|
|
|
ERROR: relation "matview_ine_tab" already exists
|
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
|
|
|
CREATE MATERIALIZED VIEW IF NOT EXISTS matview_ine_tab AS
|
|
|
|
SELECT 1 / 0; -- ok
|
|
|
|
NOTICE: relation "matview_ine_tab" already exists, skipping
|
|
|
|
QUERY PLAN
|
|
|
|
------------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
|
|
|
CREATE MATERIALIZED VIEW matview_ine_tab AS
|
|
|
|
SELECT 1 / 0 WITH NO DATA; -- error
|
|
|
|
ERROR: relation "matview_ine_tab" already exists
|
|
|
|
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
|
|
|
|
CREATE MATERIALIZED VIEW IF NOT EXISTS matview_ine_tab AS
|
|
|
|
SELECT 1 / 0 WITH NO DATA; -- ok
|
|
|
|
NOTICE: relation "matview_ine_tab" already exists, skipping
|
|
|
|
QUERY PLAN
|
|
|
|
------------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
DROP MATERIALIZED VIEW matview_ine_tab;
|