Compare commits

..

54 commits

Author SHA1 Message Date
Yugo Nagata
132816cffe Prepare 1.12 2025-09-04 22:59:18 +09:00
Yugo Nagata
b926225089 Fix role names in regression test expected file with "regress_" for old versions
Commit 3dcf7db2cb added the prefix "regress_" to role names in the regression
tests, but the expected file for old versions was overlooked, since that
file was added after the pull request for this fix.
2025-09-04 22:23:59 +09:00
Michael Paquier
3dcf7db2cb
Prefix role names in regression tests with "regress_" (#146)
Building PostgreSQL with -DENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
causes the regression tests of pg_ivm to fail, because the role names
used in the tests are not compliant with what upstream expects.

Enforcing restrictions in the regression tests is a good practice to
have, as roles are shared objects and running the tests could manipulate
existing clusters in unwanted ways.
2025-09-04 18:26:54 +09:00
Michael Paquier
e1c26275ff
Rework .gitignore (#147)
This commit reworks .gitignore to be more portable depending on the
options used in the PostgreSQL build this module links to, in various
aspects:
- Ignore code generated for LLVM bitcode.
- Dependencies.
- Libraries.
- Coverage files.
- WIN32 and MacOS files.
- Ignore isolation test subdirectory.
- Do *not* ignore regression.diffs and regression.out.  This is more
useful to detect if something is broken in the tree, in line with
upstream core practices.

The contents of this file are updated to something closer to what
upstream uses.
2025-09-04 15:57:51 +09:00
Li
e92cbe543d
add missing ` for REFRESH MATERIALIZED VIEW (#148) 2025-08-06 16:09:58 +09:00
Yugo Nagata
96fdf6f789
Add support for PostgreSQL 18 (#145)
- Fixed compiler errors and warnings.
- Added expected/pg_ivm_0.out to accommodate message changes introduced in PostgreSQL 18.
2025-07-09 19:26:28 +09:00
Yugo Nagata
8232f7fc9b
Update README.md
In addition to recreation, refresh may be used to apply changes to row-level security policies.
2025-06-11 16:59:09 +09:00
Yugo Nagata
f4b40e93a6 Prepare 1.11 2025-05-26 17:07:09 +09:00
Yugo Nagata
f03a6a71da Merge branch 'pr123' 2025-05-26 15:58:01 +09:00
Yugo Nagata
deb2652335 Some fix
- Revert a typo fix on IMMV deletion

For consistency of the documents, dropped IMMV should be immv
rather than immv_agg

- Remove the description about the entry modification in the catalog

Actually, the catalog entry doesn't not change when an IMMV is renamed
since it just references IMMV's Oids.
2025-05-26 15:52:31 +09:00
Yugo Nagata
406381cc37
Add meson.build (#142)
It is experimental. The codes were partially copied from orafce.
2025-05-26 15:11:27 +09:00
Yugo Nagata
1d78c158a2
Fix potential segmentation fault in create_immv (#141)
Ensure that newly added column information is passed to heap_form_tuple().
Previously, omission of this data led to a segmentation fault when building
with MSVC, though the issue has not not observed on Linux.
2025-05-26 11:44:11 +09:00
Yuta MASANO
49b52bcd5e
Fix Windows linkage errors by adding PGDLLEXPORT to function declarations (#139)
The following functions in pg_ivm.h are now explicitly marked with
PGDLLEXPORT:

- IVM_immediate_before
- IVM_immediate_maintenance
- ivm_visible_in_prestate

This change resolves linkage mismatches between function declarations in
the header and their definitions using PG_FUNCTION_INFO_V1 in the
implementation file, which caused build failures on Windows
environments.
2025-05-26 09:41:06 +09:00
Yugo Nagata
3f33229efe
Use quoted column names in targetlist of subquery substituting modified table (#135)
Fix a bug introduced by 5b8b2f0, which built targetlist of subquery substituting
modified table. When a table has a column whose name includes a capital letter,
incremental view maintenance failed because crafted targetlist contains incorrect
column name which was parsed as lower case.

Isseu #124
2025-05-08 17:09:37 +09:00
Ivan Kozik
7966a92b8e
Fix typo in README (#134) 2025-05-07 09:38:38 +09:00
Kyungmin Kim
eab5195be5
fix some typos (#129) 2025-04-03 17:19:29 +09:00
qmitchell-aa
c31d5ec9ab
README - detail on renaming IMMV + fix typo in drop IMMV 2025-03-12 09:15:29 -04:00
Yugo Nagata
437b2d22d7 Add pg_ivm_immv.lastivmupdate to README.md 2025-03-11 23:04:07 +09:00
Yugo Nagata
ddc1382c63
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)
2025-03-11 17:58:47 +09:00
Yugo Nagata
966a865d60 Support for PostgreSQL 15 or earlier
- Not use List APIs for xid that are introduced from PostgreSQL 16

- Fix an error during isolation test
   ERROR:  subquery in FROM must have an alias

- Not use isolation test for PostgreSQL 13
2025-03-11 16:24:34 +09:00
Yugo Nagata
52b72ab5c5
Update README.md 2025-03-10 18:35:11 +09:00
Yugo Nagata
f1166c0421
Fix potential view inconsistency issues (#121)
Previously, the view contents could become inconsistent with the base tables
in the following scenarios:

1) A concurrent transaction modifies a base table and commits before the
   incremental view maintenance starts in the current transaction.

2) A concurrent transaction modifies a base table and commits before the
   create_immv or refresh_immv command generates data.

3) Concurrent transactions incrementally update a view with a self-join
   or modify multiple base tables simultaneously.

Incremental updates of a view are generally performed sequentially using an
exclusive lock. However, even if we are able to acquire the lock, a concurrent
transaction may have already incrementally updated the view and been committed
before we can acquire it. In REPEATABLE READ or SERIALIZABLE isolation levels,
this could lead to an inconsistent view state, which is the cause of the first
issue.

To fix this, a new field, lastivmupdate, has been added to the pg_ivm_immv
catalog to record the transaction ID of the most recent update to the view.
Before performing view maintenance, the transaction ID is checked. If the
transaction was still in progress at the start of the current transaction,
an error is raised to prevent anomalies.

To fix the second issue, the timing of CreateTrigger() has been moved to
before data generation. This ensures that locks conflicting with table
modifications have been acquired on all base tables. In addition, the latest
snapshot is used in READ COMMITTED level during the data generation to reflect
committed changes from concurrent transactions. Additionally, inconsistencies
that cannot be avoided through locking are prevented by checking the transaction
ID of the last view update, as done for the first issue.

However, concurrent table modifications and create_immv execution still cannot
be detected at the time of view creation. Therefore, create_immv raises a warning
in REPEATABLE READ or SERIALIZABLE isolation levels, suggesting that the command
be used in READ COMMITTED mode or that refresh_immv be executed afterward to
ensure the view remains consistent.

The third issue was caused by the snapshot used for checking tuple visibility in
the table's pre-update state not being the latest one. To fix this, the latest
snapshot is now used in READ COMMITTED mode.

Isolation tests are also added.

Issue #104
2025-03-10 18:26:54 +09:00
Yugo Nagata
5b8b2f0a82
Fix targetlist of subquery substituting modified table (#117)
A RTE of modified table in the view definition query is substituted
by a subquery representing a delta table or a pre-update state table
during view maintenance. After this rewrite, Var that used to reference
the table column should become to references the corresponding column
in the subquery targetlist. Previously, the targetlist contained only
existing columns of the table. This was not a problem as long as the
table didn't have any dropped column because varattnos in the query
tree  was identical to resno of the targetlist. However, if the table
has a dropped column, we cannot assume this correspondence, so an error
like the following occurred in that situation.

ERROR: could not find attribute 43 in subquery targetlist

To fix it,  put "null" as a dummy value at the position in the targetlist
of a dropped column so that varattnos in the query tree is identical to
resno of the targetlist. We would also able to fix this by walking the
query tree to rewrite varattnos, but crafting targetlist is more simple
and reasonable.

Issue #85
2025-02-20 12:58:03 +09:00
Yugo Nagata
417c291454
Change the schema from pg_catalog to pgivm (#116)
Previously, pg_upgrade failed due to the permission denied
because the pg_ivm_immv catalog was in the pg_catalog catalog
(Issue #79). To fix this, all objects created by pg_ivm are
moved to theschema pgivm, which is also created by pg_ivm.

pg_ivm is still not relocatable and this must be installed
to the pgivm schema because the catalog and some internal
functions are referred to unqualified by the schema name
from the pg_ivm module. In future, this might be able to
relocatable during installation, though.

This commit affects compatibility with previous releases.
To allow to access objects like create_immv function as
previous, you need to qualify them with the schema name
or setup search_path properly.
2025-02-17 12:07:21 +09:00
Yugo Nagata
be13c952c2
Update README.md 2025-02-12 16:39:42 +09:00
Yongtao Huang
094add99f5
Clean duplicated code and some typos (#112)
Also, argument order mismatch of apply_new_delta_with_count()  between
declaration and definition is fixed.
2025-02-12 15:30:46 +09:00
Yongtao Huang
b8de2801c7
Fix a typo (#111) 2024-12-23 11:44:22 +09:00
Yugo Nagata
ae3761558f Replace the wrong file in the previous commit 2024-12-10 03:41:33 +09:00
Yugo Nagata
49068fb141 Add a missing file in the previous commit
I bumped the version number tentatively in 80a7648ad but the
update script is forgotten and this caused an install error.
2024-12-10 03:30:16 +09:00
Yugo Nagata
80a7648add Bump up to 1.10 2024-12-09 20:01:46 +09:00
Yugo Nagata
d292c70946
Prevent automatic index creation with set-returning function (#106)
Previously, a unique index is automatically created even when
a set-returning function is contained in FROM clause, but this
results in an error due to key duplication. Now, an index is
not created automatically when a relation other than table,
sub-query, or join is contained in FROM clause.

Issue (#99)
2024-12-09 19:53:32 +09:00
Yugo Nagata
48ddc99f44 Make some code cleaning in pg_ivm.c
Remove some while characters, and replace RangeVarGetRelidExtende
with RangeVarGetRelid, which are introduced by edde972624.
2024-10-21 13:12:50 +09:00
Ishant Bhaskar
edde972624
Fix a failure in DROP EXTENSION (#96)
When pg_ivm is dropped, the error "could not open relation with OID ..." occurred
at the hook function that enables DROP TABLE on an IMMV to remove the entry in
pg_ivm_immv. It was because that the primary key was already dropped at the time
pg_ivm_immv's toast is been dropped. Also, DROP TABLE command issued concurrently
with DROP EXTENSION pg_Ivm also could cause the same error because pg_ivm_immv
could be already dropped.

This race condition is fixed by using always RangeVarGetRelidExtended to get OID of
pg_ivm_immv instead of using a cache of get_relname_relid results. This makes sure
that pg_ivm_immv exists when this is scanned.
2024-10-21 12:45:44 +09:00
reshke
36d4a4770e
Create .gitignore file for excluding any binary/executable files (#103)
The .gitignore file is copied from pg_bigm. 

This doesn't contain files generated on Windows development environments for now,
but they will be added when anyone using Windows claim it.
2024-10-21 10:39:02 +09:00
reshke
b7be2aa7ff
Drop unused params from ExecCreateImmv function (#102) 2024-10-14 21:45:52 +09:00
Yugo Nagata
85b11c359a
Add a note for create_immv in README.md
With PostgreSQL 17 or later, while `refresh_immv` is running, 
the `search_path` is temporarily changed to `pg_catalog, pg_temp`.
2024-08-06 04:11:25 +09:00
Yugo Nagata
bb3c999c05 Add a missing file
Issue #98
2024-08-06 02:24:43 +09:00
Yugo Nagata
12a0120d45 Prepare 1.9 2024-07-31 13:00:31 +09:00
Yugo Nagata
65a2d36b22
Add support for PostgreSQL 17 (#92)
Compilation errors and warning are fixed.

The design of create_immv is also chaned as similar to PG17, that is,
firstly a relation is created without data then it is populated
by using the refresh logic.

This commit contains the following changes:

 - Change functions to use a safe search_path during maintenance operations
   when used with PostgreSQL 17

  This prevents maintenance operations (automatic maintenance of IMMVs and
  refresh_immv) from performing unsafe access.  Functions used by IMMVs that
  need to reference non-default schemas must specify a search path during
  function creation.

 - refresh_immv can be executed by users with the MAINTAIN privilege
   when used with PostgreSQL 17

Issue #90
2024-07-31 12:37:43 +09:00
thoshiai
7dff2f5402
Add support for PostgreSQL 17 (#84)
This commit allows to use pg_ivm with the latest master branch code of
PostgreSQL. We may need additional fixes because PG17  is not released
yet, though.
2024-03-27 15:28:39 +09:00
reshke
76888cd975
Fix compile with PG17 (#76)
Support for PostgreSQL17
Rename OverrideSearchPath to SearchPathMatcher from PG17.
2024-03-25 09:25:27 +09:00
Yugo Nagata
9736c8c832 Prepare 1.8 2024-03-01 23:28:28 +09:00
thoshiai
fc2339e16c
Fix checking for expressions containing an EXISTS subquery (#71)
EXISTS subquery is currently allowed only directly under WHERE clause
or in AND expression that is directly under WHERE. However, the check
was insufficient previously so that views using expressions other than
AND containing an EXISTS subquery could be created without an error
and it caused incorrect maintenance results.

To fix this check, add a new boolean member allow_context into 
check_ivm_restriction_context. This member means whether EXISTS
subquery is allowed in the current node being examined in 
check_ivm_restriction_walker. This should be set to true just before
calling check_ivm_restriction_walker for nodes directly under WHERE
or operands of AND expression direct under WHERE, and is reset to
false on every call of the function.

In passing, move the check for OR and NOT expression from 
rewrite_exists_subquery_walker to check_ivm_restriction_context, 
with some code cleaning.

---------

Co-authored-by: Yugo Nagata <nagata@sraoss.co.jp>
2024-03-01 20:33:56 +09:00
Yugo Nagata
980c4be338 Fix typo in the previous commit 2024-03-01 19:56:05 +09:00
Yugo Nagata
d67995c0ab Fix an error raised when dropping pg_ivm extension
Previously, DROP EXTENSION pg_ivm failed due to the failure of
opening the index on pg_ivm_immv in PgIvmObjectAccessHook that is
called on dropping pg_ivm_immv, because when pg_ivm_immv is being
dropped, the index on it is already dropped.

This is fixed to return immediately from the hook function if the
dropped table is pg_ivm_immv.
2024-03-01 14:48:55 +09:00
Colin Zhao
8f5bb5300a Check if PgIvmImmvRelationId is invalid before open it (#78)
When pg_ivm is installed shared_preload_libraries without executing
CREATE EXTENSION command, the hook function is set while the catalog
table pg_ivm_immv is not created. In this case, the hook function
failed to open the catalog table and an error was raised.

Although this way of installing pg_ivm was not considered, it would
be nice to reduce any possible troubles on users. Therefore, to
prevent this error, check if PgIvmImmvRelationId is invalid before
open it. When this is invalid, pg_ivm_immv relation is not created
yet, so there are not any IMMVs, so we don't have to do anything
in this hook function.

Review and commit message by Yugo Nagata
2024-03-01 14:16:57 +09:00
Yugo Nagata
01f0ea0eb1 Fix for view using both DISTINCT and EXISTS
In previous commit, maintenance of views using EXISTS and containing
duplicated tuples was fixed, but it was insufficient because it
raised an error during maintenance of views using both DISTINCT
and EXISTS. The cause was that tuples were tried to be duplicated
even when DISTINCT was specified. In this commit, it is fixed not
to duplicate tuples when DISTINCT is used.
2024-02-29 17:55:55 +09:00
Yugo Nagata
8c3b4ba9d7 Consider tuple duplicity in maintenance of EXISTS views
When a tuple is inserted into a table in an EXISTS subquery,
the duplicity of row is computed by count(*), but it was not
considered and only one tuple was inserted with ignoring
the duplicity.

This is fixed by duplicating rows as much as the duplicity
by using generate_series at inserting.

(Issue #82)
2024-02-29 17:55:55 +09:00
Éric Redon
aab9db3605
Fix typo in README heading (#81) 2024-02-20 16:37:14 +09:00
Yugo Nagata
8f87d0914a Prepare 1.7 2023-09-13 11:13:40 +09:00
Yugo Nagata
71f9d268b0
Add support for PostgreSQL 16 (#69) (#70)
Build errors/warnings against PostgreSQL 16 are fixed.
Also, adapted to the change of codes, including:

- Get rid of the "new" and "old" entries in a view's rangetable.
(Although, removed codes were dead codes because pg_ivm doesn't
 have any rules in pg_rewrite.)

- Rework query relation permission checking

- Require empty Bitmapsets to be represented as NULL

- Fix some compiler warnings
2023-09-11 15:23:51 +09:00
Yugo Nagata
6f99049848 Prepare 1.6 2023-08-31 21:11:36 +09:00
Yugo Nagata
1b4fb57774
Prohibit types without default btree opclass in the target list (#67)
Currently, types that does not have an equality operator cannot
be used in the target list of the view definition, because all
of target list entries are used for comparison to identify rows
to be updated or deleted in the view. Previously, an error is
raised at the time such view is incrementally maintained, this
is fixed to check that at the view creation time.

This restriction may be relaxed in future after we can use an
index to identify rows in the view.

Issue #61
2023-08-31 21:06:23 +09:00
thoshiai
c355f4003b
Support exists_subquery (#53)
Add EXISTS clause support in IVM
   
Correlated subqueries using EXISTS in WHERE clause are supported.
   
An EXISTS subquery in WHERE clause is rewritten to LATERAL subquery
in FROM clause, and IVM' process can handle this like as a normal join. 
Also, hidden columns "ivm_exists_count_X__"  are added to check if 
EXISTS condition is satisfied.  This column stores the count of how many 
rows in the subquery are correlated to (joined to) each row of the main 
query. When a base table contained in EXISTS clause is modified, this 
count value in IMMV is updated, and a row whose count becomes zero 
is deleted.

restrictions :
- EXISTS subqueries are allowed only in WHERE clause.
- aggregate functions are not supported together with EXISTS.
- EXISTS subqueries in a subquery are not supported.
- EXISTS condition can use only with AND Expr
2023-08-31 11:51:36 +09:00
44 changed files with 7461 additions and 694 deletions

30
.gitignore vendored Normal file
View file

@ -0,0 +1,30 @@
# Object files
*.o
# Coverage files
*.gcno
*.gcda
# Libraries
*.lib
*.a
# LLVM bitcode
*.bc
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.app
# Dependencies
.deps
# Generated subdirectories
/results/
/output_iso/

View file

@ -1,5 +1,7 @@
# contrib/pg_ivm/Makefile # contrib/pg_ivm/Makefile
PG_CONFIG ?= pg_config
MODULE_big = pg_ivm MODULE_big = pg_ivm
OBJS = \ OBJS = \
$(WIN32RES) \ $(WIN32RES) \
@ -13,10 +15,25 @@ PGFILEDESC = "pg_ivm - incremental view maintenance on PostgreSQL"
EXTENSION = pg_ivm EXTENSION = pg_ivm
DATA = pg_ivm--1.0.sql \ DATA = pg_ivm--1.0.sql \
pg_ivm--1.0--1.1.sql pg_ivm--1.1--1.2.sql pg_ivm--1.2--1.3.sql \ pg_ivm--1.0--1.1.sql pg_ivm--1.1--1.2.sql pg_ivm--1.2--1.3.sql \
pg_ivm--1.3--1.4.sql pg_ivm--1.4--1.5.sql pg_ivm--1.3--1.4.sql pg_ivm--1.4--1.5.sql pg_ivm--1.5--1.6.sql \
pg_ivm--1.6--1.7.sql pg_ivm--1.7--1.8.sql pg_ivm--1.8--1.9.sql \
pg_ivm--1.9--1.10.sql \
pg_ivm--1.10.sql \
pg_ivm--1.10--1.11.sql pg_ivm--1.11--1.12.sql
REGRESS = pg_ivm create_immv refresh_immv REGRESS = pg_ivm create_immv refresh_immv
PG_CONFIG ?= pg_config PGVER = $(shell $(PG_CONFIG) --version | sed "s/^[^ ]* \([0-9]*\).*$$/\1/" 2>/dev/null)
# We assume PG13 is the only version that is supported by pg_ivm but
# missing pg_isolation_regress.
ifneq ($(PGVER),13)
ISOLATION = create_insert refresh_insert insert_insert \
create_insert2 refresh_insert2 insert_insert2 \
create_insert3 refresh_insert3 insert_insert3
ISOLATION_OPTS = --load-extension=pg_ivm
endif
PGXS := $(shell $(PG_CONFIG) --pgxs) PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS) include $(PGXS)

220
README.md
View file

@ -2,7 +2,7 @@
The `pg_ivm` module provides Incremental View Maintenance (IVM) feature for PostgreSQL. The `pg_ivm` module provides Incremental View Maintenance (IVM) feature for PostgreSQL.
The extension is compatible with PostgreSQL 13, 14, and 15. The extension is compatible with PostgreSQL 13, 14, 15, 16, 17, and 18.
## Description ## Description
@ -13,7 +13,7 @@ There are two approaches with regard to timing of view maintenance: immediate an
We call a materialized view supporting IVM an **Incrementally Maintainable Materialized View (IMMV)**. To create IMMV, you have to call `create_immv` function with a relation name and a view definition query. For example: We call a materialized view supporting IVM an **Incrementally Maintainable Materialized View (IMMV)**. To create IMMV, you have to call `create_immv` function with a relation name and a view definition query. For example:
```sql ```sql
SELECT create_immv('myview', 'SELECT * FROM mytab'); SELECT pgivm.create_immv('myview', 'SELECT * FROM mytab');
``` ```
creates an IMMV with name 'myview' defined as `SELECT * FROM mytab`. This is corresponding to the following command to create a normal materialized view; creates an IMMV with name 'myview' defined as `SELECT * FROM mytab`. This is corresponding to the following command to create a normal materialized view;
@ -25,7 +25,7 @@ CREATE MATERIALIZED VIEW myview AS SELECT * FROM mytab;
When an IMMV is created, some triggers are automatically created so that the view's contents are immediately updated when its base tables are modified. When an IMMV is created, some triggers are automatically created so that the view's contents are immediately updated when its base tables are modified.
```sql ```sql
postgres=# SELECT create_immv('m', 'SELECT * FROM t0'); postgres=# SELECT pgivm.create_immv('m', 'SELECT * FROM t0');
NOTICE: could not create an index on immv "m" automatically NOTICE: could not create an index on immv "m" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -54,7 +54,10 @@ postgres=# SELECT * FROM m; -- automatically updated
(4 rows) (4 rows)
``` ```
Note that if you use PostgreSQL 17 or later, during automatic maintenance of an IMMV, the `search_path` is temporarily changed to `pg_catalog, pg_temp`.
## Installation ## Installation
To install `pg_ivm`, execute this in the module's directory: To install `pg_ivm`, execute this in the module's directory:
```shell ```shell
@ -65,62 +68,87 @@ If you installed PostgreSQL from rpm or deb, you will need the devel package (fo
> **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). > **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).
And, execute CREATE EXTENSION comand. Once installed, execute `CREATE EXTENSION` comand.
``` ```sql
CREATE EXTENSION pg_ivm; CREATE EXTENSION pg_ivm;
``` ```
### Configuration
To ensure `pg_ivm` maintains IMMVs correctly, add it to either shared_preload_libraries or session_preload_libraries in postgresql.conf:
```
# Add pg_ivm to preload libraries
shared_preload_libraries = 'pg_ivm'
# OR
session_preload_libraries = 'pg_ivm'
```
After making this change, restart PostgreSQL for the configuration to take effect.
### RPM packages and yum repository ### RPM packages and yum repository
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. 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.
## Objects ## Objects
When `pg_ivm` is installed, the following objects are created. When `pg_ivm` is installed, the 'pgivm' schema is created, along with the following objects within this schema.
### Functions ### Functions
#### create_imm #### create_immv
Use `create_immv` function to create IMMV. Use `create_immv` function to create IMMV.
``` ```
create_immv(immv_name text, view_definition text) RETURNS bigint pgivm.create_immv(immv_name text, view_definition text) RETURNS bigint
``` ```
`create_immv` defines a new IMMV of a query. A table of the name `immv_name` is created and a query specified by `view_definition` is executed and used to populate the IMMV. The query is stored in `pg_ivm_immv`, so that it can be refreshed later upon incremental view maintenance. `create_immv` returns the number of rows in the created IMMV. `create_immv` defines a new IMMV of a query. A table of the name `immv_name` is created and a query specified by `view_definition` is executed and used to populate the IMMV. The query is stored in `pg_ivm_immv`, so that it can be refreshed later upon incremental view maintenance. `create_immv` returns the number of rows in the created IMMV.
When an IMMV is created, some triggers are automatically created so that the view's contents are immediately updated when its base tables are modified. In addition, a unique index is created on the IMMV automatically if possible. If the view definition query has a GROUP BY clause, a unique index is created on the columns of GROUP BY expressions. Also, if the view has DISTINCT clause, a unique index is created on all columns in the target list. Otherwise, if the IMMV contains all primary key attributes of its base tables in the target list, a unique index is created on these attributes. In other cases, no index is created. When an IMMV is created, some triggers are automatically created so that the view's contents are immediately updated when its base tables are modified. In addition, a unique index is created on the IMMV automatically if possible. If the view definition query has a GROUP BY clause, a unique index is created on the columns of GROUP BY expressions. Also, if the view has DISTINCT clause, a unique index is created on all columns in the target list. Otherwise, if the IMMV contains all primary key attributes of its base tables in the target list, a unique index is created on these attributes. In other cases, no index is created.
#### refresh_imm `create_immv` acquires an `AccessExclusiveLock` on the view. However, even if we are able to acquire the lock, a concurrent transaction may have already incrementally updated and committed the view before we can acquire it. In the `REPEATABLE READ` or `SERIALIZABLE` isolation levels, this could lead to an inconsistent state of the view. Unfortunately, this situation cannot be detected during the creation of the view. As a result, `create_immv` raises a warning in these isolation levels, suggesting that the command be used in `READ COMMITTED` or that `refresh_immv` be executed afterward to make the view's contents remain consistent.
Note that if you use PostgreSQL 17 or later, while `create_immv` is running, the `search_path` is temporarily changed to `pg_catalog, pg_temp`.
#### refresh_immv
Use `refresh_immv` function to refresh IMMV. Use `refresh_immv` function to refresh IMMV.
``` ```
refresh_immv(immv_name text, with_data bool) RETURNS bigint pgivm.refresh_immv(immv_name text, with_data bool) RETURNS bigint
``` ```
`refresh_immv` completely replaces the contents of an IMMV as `REFRESH MATERIALIZED VIEW` command does for a materialized view. To execute this function you must be the owner of the IMMV. The old contents are discarded. `refresh_immv` completely replaces the contents of an IMMV as `REFRESH MATERIALIZED VIEW` command does for a materialized view. To execute this function you must be the owner of the IMMV (with PostgreSQL 16 or earlier) or have the `MAINTAIN` privilege on the IMMV (with PostgreSQL 17 or later). The old contents are discarded.
The with_data flag is corresponding to `WITH [NO] DATA` option of REFRESH MATERIALIZED VIEW` command. If with_data is true, the backing query is executed to provide the new data, and if the IMMV is unpopulated, triggers for maintaining the view are created. Also, a unique index is created for IMMV if it is possible and the view doesn't have that yet. If with_data is false, no new data is generated and the IMMV become unpopulated, and the triggers are dropped from the IMMV. Note that unpopulated IMMV is still scannable although the result is empty. This behaviour may be changed in future to raise an error when an unpopulated IMMV is scanned. The with_data flag is corresponding to `WITH [NO] DATA` option of `REFRESH MATERIALIZED VIEW` command. If with_data is true, the backing query is executed to provide the new data, and if the IMMV is unpopulated, triggers for maintaining the view are created. Also, a unique index is created for IMMV if it is possible and the view doesn't have that yet. If with_data is false, no new data is generated and the IMMV become unpopulated, and the triggers are dropped from the IMMV. Note that unpopulated IMMV is still scannable although the result is empty. This behaviour may be changed in future to raise an error when an unpopulated IMMV is scanned.
`refresh_immv` acquires `AccessExclusiveLock` on the view. However, even if we are able to acquire the lock, a concurrent transaction may have already incrementally updated and committed the view before we can acquire the lock. In `REPEATABLE READ` or `SERIALIZABLE` isolation level, this could lead to an inconsistent state of the view. Therefore, an error is raised to prevent anomalies when this situation is detected.
Note that if you use PostgreSQL 17 or later, while `refresh_immv` is running, the `search_path` is temporarily changed to `pg_catalog, pg_temp`.
#### get_immv_def #### get_immv_def
`get_immv_def` reconstructs the underlying SELECT command for an IMMV. (This is a decompiled reconstruction, not the original text of the command.) `get_immv_def` reconstructs the underlying SELECT command for an IMMV. (This is a decompiled reconstruction, not the original text of the command.)
``` ```
get_immv_def(immv regclass) RETURNS text pgivm.get_immv_def(immv regclass) RETURNS text
``` ```
### IMMV metadata catalog ### IMMV metadata catalog
The catalog `pg_ivm_immv` stores IMMV information. The catalog `pgivm.pg_ivm_immv` stores IMMV information.
|Name|Type|Description| |Name|Type|Description|
|:---|:---|:---| |:---|:---|:---|
|immvrelid|regclass|The OID of the IMMV| |immvrelid|regclass|The OID of the IMMV|
|viewdef|text|Query tree (in the form of a nodeToString() representation) for the view definition| |viewdef|text|Query tree (in the form of a nodeToString() representation) for the view definition|
|ispopulated|bool|True if IMMV is currently populated| |ispopulated|bool|True if IMMV is currently populated|
|lastivmupdate|xid8|The transaction ID of the most recent incremental update to the view|
## Example ## Example
### `CREATE MATERIALIZED VIEW` and `REFRESH MATERIALIZED VIEW`
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. 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.
For example, suppose a normal materialized view defined as below: For example, suppose a normal materialized view defined as below:
@ -144,10 +172,12 @@ REFRESH MATERIALIZED VIEW
Time: 20575.721 ms (00:20.576) Time: 20575.721 ms (00:20.576)
``` ```
### Creating an IMMV
On the other hand, after creating IMMV with the same view definition as below: On the other hand, after creating IMMV with the same view definition as below:
``` ```
test=# SELECT create_immv('immv', test=# SELECT pgivm.create_immv('immv',
'SELECT a.aid, b.bid, a.abalance, b.bbalance 'SELECT a.aid, b.bid, a.abalance, b.bbalance
FROM pgbench_accounts a JOIN pgbench_branches b USING(bid)'); FROM pgbench_accounts a JOIN pgbench_branches b USING(bid)');
NOTICE: created index "immv_index" on immv "immv" NOTICE: created index "immv_index" on immv "immv"
@ -157,7 +187,7 @@ NOTICE: created index "immv_index" on immv "immv"
(1 row) (1 row)
``` ```
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. 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.
```sql ```sql
test=# UPDATE pgbench_accounts SET abalance = 1234 WHERE aid = 1; test=# UPDATE pgbench_accounts SET abalance = 1234 WHERE aid = 1;
@ -171,7 +201,7 @@ test=# SELECT * FROM immv WHERE aid = 1;
(1 row) (1 row)
``` ```
An appropriate index on IMMV is necessary for efficient IVM because we need to looks for tuples to be updated in IMMV. If there are no indexes, it will take a long time. An appropriate index on IMMV is necessary for efficient IVM because we need to look for tuples to be updated in IMMV. If there are no indexes, it will take a long time.
Therefore, when an IMMV is created by the `create_immv` function, a unique index is created on it automatically if possible. If the view definition query has a GROUP BY clause, a unique index is created on the columns of GROUP BY expressions. Also, if the view has DISTINCT clause, a unique index is created on all columns in the target list. Otherwise, if the IMMV contains all primary key attributes of its base tables in the target list, a unique index is created on these attributes. In other cases, no index is created. Therefore, when an IMMV is created by the `create_immv` function, a unique index is created on it automatically if possible. If the view definition query has a GROUP BY clause, a unique index is created on the columns of GROUP BY expressions. Also, if the view has DISTINCT clause, a unique index is created on all columns in the target list. Otherwise, if the IMMV contains all primary key attributes of its base tables in the target list, a unique index is created on these attributes. In other cases, no index is created.
@ -186,13 +216,141 @@ UPDATE 1
Time: 3224.741 ms (00:03.225) Time: 3224.741 ms (00:03.225)
``` ```
### IMMV with Aggregate Functions
You can create an IMMV that includes aggregate functions.
```sql
test=# SELECT pgivm.create_immv('immv_agg',
'SELECT bid, count(*), sum(abalance), avg(abalance)
FROM pgbench_accounts JOIN pgbench_branches USING(bid) GROUP BY bid');
NOTICE: created index "immv_agg_index" on immv "immv_agg"
create_immv
-------------
100
(1 row)
Time: 5772.625 ms (00:05.773)
```
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.
```sql
test=# SELECT pgivm.refresh_immv('immv_agg',true);
refresh_immv
--------------
100
(1 row)
Time: 5766.292 ms (00:05.766)
```
When a base table is updated, the IMMV is also automatically updated incrementally, as expected.
```sql
test=# SELECT bid, count, sum, avg FROM immv_agg WHERE bid = 42;
bid | count | sum | avg
-----+--------+-------+------------------------
42 | 100000 | 38774 | 0.38774000000000000000
(1 row)
Time: 3.123 ms
test=# UPDATE pgbench_accounts SET abalance = abalance + 1000 WHERE aid = 4112345 AND bid = 42;
UPDATE 1
Time: 16.616 ms
test=# SELECT bid, count, sum, avg FROM immv_agg WHERE bid = 42;
bid | count | sum | avg
-----+--------+-------+------------------------
42 | 100000 | 39774 | 0.39774000000000000000
(1 row)
Time: 1.987 ms
```
After updating a row in the `pgbench_accounts` table, you can see that the aggregated values in `immv_agg` are updated automatically.
### Listing IMMVs and Viewing Their Definitions
You can find all IMMVs in the `pg_ivm_immv` catalog and view their definition queries by executing the `get_immv_def` function."
```sql
test=# SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS immv_def FROM pgivm.pg_ivm_immv;
immv | immv_def
----------+--------------------------------------------
immv_agg | SELECT pgbench_accounts.bid, +
| count(*) AS count, +
| sum(pgbench_accounts.abalance) AS sum,+
| avg(pgbench_accounts.abalance) AS avg +
| FROM (pgbench_accounts +
| JOIN pgbench_branches USING (bid)) +
| GROUP BY pgbench_accounts.bid
immv | SELECT a.aid, +
| b.bid, +
| a.abalance, +
| b.bbalance +
| FROM (pgbench_accounts a +
| JOIN pgbench_branches b USING (bid))
(2 rows)
```
### Dropping an IMMV
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.
```sql
test=# DROP TABLE immv
DROP TABLE
test=# SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS immv_def FROM pgivm.pg_ivm_immv;
immv | immv_def
----------+--------------------------------------------
immv_agg | SELECT pgbench_accounts.bid, +
| count(*) AS count, +
| sum(pgbench_accounts.abalance) AS sum,+
| avg(pgbench_accounts.abalance) AS avg +
| FROM (pgbench_accounts +
| JOIN pgbench_branches USING (bid)) +
| GROUP BY pgbench_accounts.bid
(1 row)
```
### Renaming an IMMV
An IMMV can be renamed using the `ALTER TABLE` command.
```sql
test=# ALTER TABLE immv_agg RENAME TO immv_agg2;
ALTER TABLE
test=# SELECT immvrelid AS immv, pgivm.get_immv_def(immvrelid) AS immv_def FROM pgivm.pg_ivm_immv;
immv | immv_def
-----------+--------------------------------------------
immv_agg2 | SELECT pgbench_accounts.bid, +
| count(*) AS count, +
| sum(pgbench_accounts.abalance) AS sum,+
| avg(pgbench_accounts.abalance) AS avg +
| FROM (pgbench_accounts +
| JOIN pgbench_branches USING (bid)) +
| GROUP BY pgbench_accounts.bid
(1 row)
```
## `pg_dump` and `pg_upgrade`
After restoring data from a `pg_dump` backup or upgrading `PostgreSQL` using `pg_upgrade`, all IMMVs must be manually dropped and recreated.
## Supported View Definitions and Restriction ## Supported View Definitions and Restriction
Currently, IMMV's view definition can contain inner joins, DISTINCT clause, some built-in aggregate functions, simple sub-queries in `FROM` clause, 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. 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.
The base tables must be simple tables. Views, materialized views, inheritance parent tables, partitioned tables, partitions, and foreign tables can not be used. The base tables must be simple tables. Views, materialized views, inheritance parent tables, partitioned tables, partitions, and foreign tables can not be used.
The targetlist cannot contain system columns, columns whose name starts with `__ivm_`. Any system column cannot be included in the view definition query.
The target list cannot columns whose name starts with `__ivm_`.
Data type used in the target list in the view must have default operator class for access method btree. For example, `json`, `xml`, or `point` type cannot be in the target list.
Logical replication is not supported, that is, even when a base table at a publisher node is modified, IMMVs at subscriber nodes defined on these base tables are not updated. Logical replication is not supported, that is, even when a base table at a publisher node is modified, IMMVs at subscriber nodes defined on these base tables are not updated.
@ -216,12 +374,13 @@ Targetlist cannot contain expressions which contain an aggregate in it.
### Subqueries ### Subqueries
Simple subqueries in `FROM` clause are supported. Simple subqueries in `FROM` clause and EXISTS subqueries in 'WHERE' clause are supported.
#### Restrictions on Subqueries #### Restrictions on Subqueries
Subqueries can be used only in `FROM` clause. Subqueries in target list or `WHERE` clause are not supported. Subqueries using EXISTS and simple subqueries in FROM clause are supported. EXISTS subqueries with condition other than 'AND' and Subqueries in targetlist are not supported. EXISTS subquery is supported only in WHERE but not in the targetlist.
If EXISTS contains columns that refer to columns in tables in the outer query, such columns must be included in the targetlist.
Subqueries containing an aggregate function or `DISTINCT` are not supported. Subqueries containing an aggregate function or `DISTINCT` are not supported.
### CTE ### CTE
@ -238,7 +397,7 @@ Recursive queries (`WITH RECURSIVE`) are not allowed. Unreferenced CTEs are not
`DISTINCT` is allowed in IMMV's definition queries. Suppose an IMMV defined with DISTINCT on a base table containing duplicate tuples. When tuples are deleted from the base table, a tuple in the view is deleted if and only if the multiplicity of the tuple becomes zero. Moreover, when tuples are inserted into the base table, a tuple is inserted into the view only if the same tuple doesn't already exist in it. `DISTINCT` is allowed in IMMV's definition queries. Suppose an IMMV defined with DISTINCT on a base table containing duplicate tuples. When tuples are deleted from the base table, a tuple in the view is deleted if and only if the multiplicity of the tuple becomes zero. Moreover, when tuples are inserted into the base table, a tuple is inserted into the view only if the same tuple doesn't already exist in it.
Physically, an IMMV defined with `DISTINCT` contains tuples after eliminating duplicates, and the multiplicity of each tuple is stored in a extra column named `__ivm_count__` that is added when such IMMV is created. Physically, an IMMV defined with `DISTINCT` contains tuples after eliminating duplicates, and the multiplicity of each tuple is stored in an extra column named `__ivm_count__` that is added when such IMMV is created.
### TRUNCATE ### TRUNCATE
@ -247,20 +406,29 @@ When a base table is truncated, the IMMV is also truncated and the contents beco
### Concurrent Transactions ### Concurrent Transactions
Suppose an IMMV is defined on two base tables and each table was modified in different a concurrent transaction simultaneously. In the transaction which was committed first, the IMMV can be updated considering only the change which happened in this transaction. On the other hand, in order to update the IMMV correctly in the transaction which was committed later, we need to know the changes occurred in both transactions. For this reason, `ExclusiveLock` is held on an IMMV immediately after a base table is modified in `READ COMMITTED` mode to make sure that the IMMV is updated in the latter transaction after the former transaction is committed. In `REPEATABLE READ` or `SERIALIZABLE` mode, an error is raised immediately if lock acquisition fails because any changes which occurred in other transactions are not be visible in these modes and IMMV cannot be updated correctly in such situations. However, as an exception if the IMMV has only one base table and doesn't use DISTINCT or GROUP BY, and the table is modified by `INSERT`, then the lock held on the IMMV is `RowExclusiveLock`. Incremental updates of a view are basically performed in sequentially even with concurrent transactions running.
Suppose an IMMV is defined on two base tables and each table is modified in different concurrent transactions simultaneously. In the transaction which was committed first, the IMMV can be updated considering only the change made in that transaction. However, to update the IMMV correctly in the transaction that commits later, we need to account for the changes made in both transactions.
For this reason, `ExclusiveLock` is held on an IMMV immediately after a base table is modified in `READ COMMITTED` isolation level. This ensures that the IMMV is updated in the latter transaction only after the former transaction has committed. In `REPEATABLE READ` or `SERIALIZABLE` isolation level, an error is raised immediately if lock acquisition fails, as changes made by other transactions are not visible in these levels, and the IMMV cannot be updated correctly in such situations.
However, as an exception if the IMMV has only one base table, does not use DISTINCT or GROUP BY, and is modified by `INSERT`, then the lock held on the IMMV is `RowExclusiveLock`.
Even if we are able to acquire a lock, a concurrent transaction may have already incrementally updated and committed the view before we can acquire the lock. In `REPEATABLE READ` or `SERIALIZABLE` isolation level, this could lead to an inconsistent state of the view. Therefore, an error is raised to prevent anomalies when this situation is detected.
### Row Level Security ### Row Level Security
If some base tables have row level security policy, rows that are not visible to the materialized view's owner are excluded from the result. In addition, such rows are excluded as well when views are incrementally maintained. However, if a new policy is defined or policies are changed after the materialized view was created, the new policy will not be applied to the view contents. To apply the new policy, you need to recreate IMMV. If some base tables have row level security policy, rows that are not visible to the materialized view's owner are excluded from the result. In addition, such rows are excluded as well when views are incrementally maintained. However, if a new policy is defined or policies are changed after the materialized view was created, the new policy will not be applied to the view contents. To apply the new policy, you need to refresh or recreate the IMMV.
### How to Disable or Enable Immediate Maintenance ### How to Disable or Enable Immediate Maintenance
IVM is effective when we want to keep an IMMV up-to-date and small fraction of a base table is modified infrequently. Due to the overhead of immediate maintenance, IVM is not effective when a base table is modified frequently. Also, when a large part of a base table is modified or large data is inserted into a base table, IVM is not effective and the cost of maintenance can be larger than refresh from scratch. IVM is effective when we want to keep an IMMV up-to-date and small fraction of a base table is modified infrequently. Due to the overhead of immediate maintenance, IVM is not effective when a base table is modified frequently. Also, when a large part of a base table is modified or large data is inserted into a base table, IVM is not effective and the cost of maintenance can be larger than refresh from scratch.
In such situation, we can use `refesh_immv` function with `with_data = false` to disable immediate maintenance before modifying a base table. After a base table modification, call `refresh_immv`with `with_data = true` to refresh the view data and enable immediate maintenance. In such situation, we can use `refresh_immv` function with `with_data = false` to disable immediate maintenance before modifying a base table. After a base table modification, call `refresh_immv`with `with_data = true` to refresh the view data and enable immediate maintenance.
## Authors ## Developer
IVM Development Group IVM Development Group
- Original Authors
- https://github.com/yugo-n - https://github.com/yugo-n
- https://github.com/thoshiai - https://github.com/thoshiai

File diff suppressed because it is too large Load diff

View file

@ -1,22 +1,22 @@
CREATE TABLE t (i int PRIMARY KEY); CREATE TABLE t (i int PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 100); INSERT INTO t SELECT generate_series(1, 100);
SELECT create_immv('mv', 'SELECT * FROM t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM t');
NOTICE: created index "mv_index" on immv "mv" NOTICE: created index "mv_index" on immv "mv"
create_immv create_immv
------------- -------------
100 100
(1 row) (1 row)
SELECT create_immv(' mv2 ( x ) ', 'SELECT * FROM t WHERE i%2 = 0'); SELECT pgivm.create_immv(' mv2 ( x ) ', 'SELECT * FROM t WHERE i%2 = 0');
NOTICE: created index "mv2_index" on immv "mv2" NOTICE: created index "mv2_index" on immv "mv2"
create_immv create_immv
------------- -------------
50 50
(1 row) (1 row)
SELECT create_immv('mv3', 'WITH d AS (DELETE FROM t RETURNING NULL) SELECT * FROM t'); SELECT pgivm.create_immv('mv3', 'WITH d AS (DELETE FROM t RETURNING NULL) SELECT * FROM t');
ERROR: materialized views must not use data-modifying statements in WITH ERROR: materialized views must not use data-modifying statements in WITH
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | get_immv_def immvrelid | get_immv_def
-----------+----------------------- -----------+-----------------------
mv | SELECT i + mv | SELECT i +
@ -27,20 +27,20 @@ SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1;
(2 rows) (2 rows)
-- contain immv -- contain immv
SELECT create_immv('mv_in_immv01', 'SELECT i FROM mv'); SELECT pgivm.create_immv('mv_in_immv01', 'SELECT i FROM mv');
ERROR: including IMMV in definition is not supported on incrementally maintainable materialized view ERROR: including IMMV in definition is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_in_immv02', 'SELECT t.i FROM t INNER JOIN mv2 ON t.i = mv2.x'); SELECT pgivm.create_immv('mv_in_immv02', 'SELECT t.i FROM t INNER JOIN mv2 ON t.i = mv2.x');
ERROR: including IMMV in definition is not supported on incrementally maintainable materialized view ERROR: including IMMV in definition is not supported on incrementally maintainable materialized view
-- SQL other than SELECT -- SQL other than SELECT
SELECT create_immv('mv_in_create', 'CREATE TABLE in_create(i int)'); SELECT pgivm.create_immv('mv_in_create', 'CREATE TABLE in_create(i int)');
ERROR: view definition must specify SELECT statement ERROR: view definition must specify SELECT statement
SELECT create_immv('mv_in_insert', 'INSERT INTO t VALUES(10)'); SELECT pgivm.create_immv('mv_in_insert', 'INSERT INTO t VALUES(10)');
ERROR: view definition must specify SELECT statement ERROR: view definition must specify SELECT statement
SELECT create_immv('mv_in_update', 'UPDATE t SET i = 10'); SELECT pgivm.create_immv('mv_in_update', 'UPDATE t SET i = 10');
ERROR: view definition must specify SELECT statement ERROR: view definition must specify SELECT statement
SELECT create_immv('mv_in_delete', 'DELETE FROM t'); SELECT pgivm.create_immv('mv_in_delete', 'DELETE FROM t');
ERROR: view definition must specify SELECT statement ERROR: view definition must specify SELECT statement
SELECT create_immv('mv_in_drop', 'DROP TABLE t'); SELECT pgivm.create_immv('mv_in_drop', 'DROP TABLE t');
ERROR: view definition must specify SELECT statement ERROR: view definition must specify SELECT statement
DROP TABLE t; DROP TABLE t;
ERROR: cannot drop table t because other objects depend on it ERROR: cannot drop table t because other objects depend on it
@ -48,7 +48,7 @@ DETAIL: table mv depends on table t
table mv2 depends on table t table mv2 depends on table t
HINT: Use DROP ... CASCADE to drop the dependent objects too. HINT: Use DROP ... CASCADE to drop the dependent objects too.
DROP TABLE mv; DROP TABLE mv;
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | get_immv_def immvrelid | get_immv_def
-----------+----------------------- -----------+-----------------------
mv2 | SELECT i AS x + mv2 | SELECT i AS x +
@ -57,7 +57,7 @@ SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1;
(1 row) (1 row)
DROP TABLE mv2; DROP TABLE mv2;
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | get_immv_def immvrelid | get_immv_def
-----------+-------------- -----------+--------------
(0 rows) (0 rows)

325
expected/create_insert.out Normal file
View file

@ -0,0 +1,325 @@
Parsed test spec with 2 sessions
starting permutation: s1 create s2 insert c1 check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 create s2 c1 insert check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 create insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert create c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create: <... completed>
create_immv
-----------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 create c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 create c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create: <... completed>
create_immv
-----------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 c2 create check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert c2 create check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)

345
expected/create_insert2.out Normal file
View file

@ -0,0 +1,345 @@
Parsed test spec with 2 sessions
starting permutation: s1 create s2 insert c1 check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 create s2 c1 insert check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 create insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert create c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create: <... completed>
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s1 s2 create c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 create c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create: <... completed>
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s2 insert s1 c2 create check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s2 s1 insert c2 create check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)

329
expected/create_insert3.out Normal file
View file

@ -0,0 +1,329 @@
Parsed test spec with 2 sessions
starting permutation: s1 create s2 insert c1 check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
ERROR: could not serialize access due to read/write dependencies among transactions
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 create s2 c1 insert check2 c2 mv
step s1: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
ERROR: could not serialize access due to read/write dependencies among transactions
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 create insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
ERROR: could not serialize access due to read/write dependencies among transactions
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert create c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create: <... completed>
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s1 s2 create c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
ERROR: could not serialize access due to read/write dependencies among transactions
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 create c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
<waiting ...>
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create: <... completed>
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s2 insert s1 c2 create check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)
starting permutation: s2 s1 insert c2 create check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
tx1: NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
tx1: WARNING: inconsistent view can be created in isolation level SERIALIZABLE or REPEATABLE READ
DETAIL: The view may not include effects of a concurrent transaction.
HINT: create_immv should be used in isolation level READ COMMITTED, or execute refresh_immv to make sure the view is consistent.
step create:
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
create_immv
-----------
1
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ng
(1 row)

497
expected/insert_insert.out Normal file
View file

@ -0,0 +1,497 @@
Parsed test spec with 2 sessions
starting permutation: s1 update1 s2 update2 c1 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1; <waiting ...>
step c1: COMMIT;
step update2: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 update1 s2 c1 update2 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 update2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step update2: UPDATE b SET j = 111 WHERE i = 1; <waiting ...>
step c1: COMMIT;
step update2: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update2 update1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step update1: UPDATE a SET j = 11 WHERE i = 1; <waiting ...>
step c2: COMMIT;
step update1: <... completed>
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 c1 update2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 update1 c2 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1; <waiting ...>
step c2: COMMIT;
step update1: <... completed>
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 c2 update1 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 update2 c2 update1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 insert2 c1 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201); <waiting ...>
step c1: COMMIT;
step insert2: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 c1 insert2 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 insert2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201); <waiting ...>
step c1: COMMIT;
step insert2: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert2 insert1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200); <waiting ...>
step c2: COMMIT;
step insert1: <... completed>
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 c1 insert2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 insert1 c2 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200); <waiting ...>
step c2: COMMIT;
step insert1: <... completed>
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 c2 insert1 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert2 c2 insert1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
20|20|200
20|20|201
20|21|200
20|21|201
21|20|200
21|20|201
21|21|200
21|21|201
(12 rows)
check_mv
--------
ok
(1 row)

373
expected/insert_insert2.out Normal file
View file

@ -0,0 +1,373 @@
Parsed test spec with 2 sessions
starting permutation: s1 update1 s2 update2 c1 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 update1 s2 c1 update2 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 update2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update2 update1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 c1 update2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 update1 c2 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 c2 update1 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 update2 c2 update1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 insert2 c1 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 c1 insert2 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 insert2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert2 insert1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 c1 insert2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 insert1 c2 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 c2 insert1 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert2 c2 insert1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)

373
expected/insert_insert3.out Normal file
View file

@ -0,0 +1,373 @@
Parsed test spec with 2 sessions
starting permutation: s1 update1 s2 update2 c1 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 update1 s2 c1 update2 check2 c2 mv
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step s2: SELECT;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 update2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update2 update1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 update1 c1 update2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
step c1: COMMIT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
11|11|100
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 update1 c2 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 update2 s1 c2 update1 check1 c1 mv
step s2: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step s1: SELECT;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 update2 c2 update1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step update2: UPDATE b SET j = 111 WHERE i = 1;
step c2: COMMIT;
step update1: UPDATE a SET j = 11 WHERE i = 1;
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|111
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 insert2 c1 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 insert1 s2 c1 insert2 check2 c2 mv
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step s2: SELECT;
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 insert2 c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert2 insert1 c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert1 c1 insert2 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
step c1: COMMIT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
20|20|200
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 insert1 c2 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c2: COMMIT;
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert2 s1 c2 insert1 check1 c1 mv
step s2: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step s1: SELECT;
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert2 c2 insert1 check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert2: INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201);
step c2: COMMIT;
step insert1: INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv();
x| y| z
--+--+---
10|10|100
10|11|100
11|10|100
11|11|100
21|21|201
(5 rows)
check_mv
--------
ok
(1 row)

View file

@ -1,20 +1,25 @@
CREATE EXTENSION pg_ivm; CREATE EXTENSION pg_ivm;
GRANT ALL ON SCHEMA public TO public; GRANT ALL ON SCHEMA public TO public;
-- create a table to use as a basis for views and materialized views in various combinations -- create a table to use as a basis for views and materialized views in various combinations
CREATE TABLE mv_base_a (i int, j int); CREATE TABLE mv_base_a (x int, i int, y int, j int);
CREATE TABLE mv_base_b (x int, i int, y int, k int);
-- test for base tables with dropped columns
ALTER TABLE mv_base_a DROP COLUMN x;
ALTER TABLE mv_base_a DROP COLUMN y;
ALTER TABLE mv_base_b DROP COLUMN x;
ALTER TABLE mv_base_b DROP COLUMN y;
INSERT INTO mv_base_a VALUES INSERT INTO mv_base_a VALUES
(1,10), (1,10),
(2,20), (2,20),
(3,30), (3,30),
(4,40), (4,40),
(5,50); (5,50);
CREATE TABLE mv_base_b (i int, k int);
INSERT INTO mv_base_b VALUES INSERT INTO mv_base_b VALUES
(1,101), (1,101),
(2,102), (2,102),
(3,103), (3,103),
(4,104); (4,104);
SELECT create_immv('mv_ivm_1', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i)'); SELECT pgivm.create_immv('mv_ivm_1', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i)');
NOTICE: could not create an index on immv "mv_ivm_1" automatically NOTICE: could not create an index on immv "mv_ivm_1" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -76,6 +81,21 @@ SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
4 | 40 | 104 4 | 40 | 104
(4 rows) (4 rows)
-- test for renaming column name to camel style
BEGIN;
ALTER TABLE mv_base_a RENAME i TO "I";
ALTER TABLE mv_base_a RENAME j TO "J";
UPDATE mv_base_a SET "J" = 0 WHERE "I" = 1;
SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
i | j | k
---+----+-----
1 | 0 | 101
2 | 20 | 102
3 | 30 | 103
4 | 40 | 104
(4 rows)
ROLLBACK;
-- TRUNCATE a base table in join views -- TRUNCATE a base table in join views
BEGIN; BEGIN;
TRUNCATE mv_base_a; TRUNCATE mv_base_a;
@ -97,7 +117,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE FUNCTION ivm_func() RETURNS int LANGUAGE 'sql' CREATE FUNCTION ivm_func() RETURNS int LANGUAGE 'sql'
AS 'SELECT 1' IMMUTABLE; AS 'SELECT 1' IMMUTABLE;
SELECT create_immv('mv_ivm_func', 'SELECT * FROM ivm_func()'); SELECT pgivm.create_immv('mv_ivm_func', 'SELECT * FROM ivm_func()');
NOTICE: could not create an index on immv "mv_ivm_func" automatically NOTICE: could not create an index on immv "mv_ivm_func" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -106,7 +126,7 @@ HINT: Create an index on the immv for efficient incremental maintenance.
1 1
(1 row) (1 row)
SELECT create_immv('mv_ivm_no_tbl', 'SELECT 1'); SELECT pgivm.create_immv('mv_ivm_no_tbl', 'SELECT 1');
NOTICE: could not create an index on immv "mv_ivm_no_tbl" automatically NOTICE: could not create an index on immv "mv_ivm_no_tbl" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -118,7 +138,7 @@ HINT: Create an index on the immv for efficient incremental maintenance.
ROLLBACK; ROLLBACK;
-- result of materialized view have DISTINCT clause or the duplicate result. -- result of materialized view have DISTINCT clause or the duplicate result.
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_duplicate', 'SELECT j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_duplicate', 'SELECT j FROM mv_base_a');
NOTICE: could not create an index on immv "mv_ivm_duplicate" automatically NOTICE: could not create an index on immv "mv_ivm_duplicate" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -127,7 +147,7 @@ HINT: Create an index on the immv for efficient incremental maintenance.
5 5
(1 row) (1 row)
SELECT create_immv('mv_ivm_distinct', 'SELECT DISTINCT j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_distinct', 'SELECT DISTINCT j FROM mv_base_a');
NOTICE: created index "mv_ivm_distinct_index" on immv "mv_ivm_distinct" NOTICE: created index "mv_ivm_distinct_index" on immv "mv_ivm_distinct"
create_immv create_immv
------------- -------------
@ -180,7 +200,7 @@ SELECT * FROM mv_ivm_distinct ORDER BY 1;
ROLLBACK; ROLLBACK;
-- support SUM(), COUNT() and AVG() aggregate functions -- support SUM(), COUNT() and AVG() aggregate functions
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(i), AVG(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(i), AVG(j) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg" NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg"
create_immv create_immv
------------- -------------
@ -233,7 +253,7 @@ SELECT * FROM mv_ivm_agg ORDER BY 1,2,3,4;
ROLLBACK; ROLLBACK;
-- support COUNT(*) aggregate function -- support COUNT(*) aggregate function
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg" NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg"
create_immv create_immv
------------- -------------
@ -264,7 +284,7 @@ SELECT * FROM mv_ivm_agg ORDER BY 1,2,3;
ROLLBACK; ROLLBACK;
-- TRUNCATE a base table in aggregate views -- TRUNCATE a base table in aggregate views
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg" NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg"
create_immv create_immv
------------- -------------
@ -285,7 +305,7 @@ SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i;
ROLLBACK; ROLLBACK;
-- support aggregate functions without GROUP clause -- support aggregate functions without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a');
create_immv create_immv
------------- -------------
1 1
@ -314,7 +334,7 @@ SELECT * FROM mv_ivm_group ORDER BY 1;
ROLLBACK; ROLLBACK;
-- TRUNCATE a base table in aggregate views without GROUP clause -- TRUNCATE a base table in aggregate views without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a');
create_immv create_immv
------------- -------------
1 1
@ -336,7 +356,7 @@ SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a;
ROLLBACK; ROLLBACK;
-- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect. -- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect.
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_avg_bug', 'SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_avg_bug', 'SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i');
NOTICE: created index "mv_ivm_avg_bug_index" on immv "mv_ivm_avg_bug" NOTICE: created index "mv_ivm_avg_bug_index" on immv "mv_ivm_avg_bug"
create_immv create_immv
------------- -------------
@ -383,7 +403,7 @@ SELECT * FROM mv_ivm_avg_bug ORDER BY 1,2,3;
ROLLBACK; ROLLBACK;
-- support MIN(), MAX() aggregate functions -- support MIN(), MAX() aggregate functions
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min_max(i, min_j, max_j)', 'SELECT i, MIN(j), MAX(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_min_max(i, min_j, max_j)', 'SELECT i, MIN(j), MAX(j) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_min_max_index" on immv "mv_ivm_min_max" NOTICE: created index "mv_ivm_min_max_index" on immv "mv_ivm_min_max"
create_immv create_immv
------------- -------------
@ -430,7 +450,7 @@ SELECT * FROM mv_ivm_min_max ORDER BY 1,2,3;
ROLLBACK; ROLLBACK;
-- support MIN(), MAX() aggregate functions without GROUP clause -- support MIN(), MAX() aggregate functions without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min_max(min_j, max_j)', 'SELECT MIN(j), MAX(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_min_max(min_j, max_j)', 'SELECT MIN(j), MAX(j) FROM mv_base_a');
create_immv create_immv
------------- -------------
1 1
@ -467,7 +487,7 @@ SELECT * FROM mv_ivm_min_max;
ROLLBACK; ROLLBACK;
-- Test MIN/MAX after search_path change -- Test MIN/MAX after search_path change
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min', 'SELECT MIN(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_min', 'SELECT MIN(j) FROM mv_base_a');
create_immv create_immv
------------- -------------
1 1
@ -501,7 +521,7 @@ SELECT * FROM mv_ivm_min ORDER BY 1,2,3;
ROLLBACK; ROLLBACK;
-- aggregate views with column names specified -- aggregate views with column names specified
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg" NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg"
create_immv create_immv
------------- -------------
@ -523,7 +543,7 @@ SELECT * FROM mv_ivm_agg ORDER BY 1,2;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a,b)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a,b)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg" NOTICE: created index "mv_ivm_agg_index" on immv "mv_ivm_agg"
create_immv create_immv
------------- -------------
@ -545,14 +565,14 @@ SELECT * FROM mv_ivm_agg ORDER BY 1,2;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a,b,c)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a,b,c)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
ERROR: too many column names were specified ERROR: too many column names were specified
ROLLBACK; ROLLBACK;
-- support self join view and multiple change on the same table -- support self join view and multiple change on the same table
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30);
SELECT create_immv('mv_self(v1, v2)', SELECT pgivm.create_immv('mv_self(v1, v2)',
'SELECT t1.v, t2.v FROM base_t AS t1 JOIN base_t AS t2 ON t1.i = t2.i'); 'SELECT t1.v, t2.v FROM base_t AS t1 JOIN base_t AS t2 ON t1.i = t2.i');
NOTICE: could not create an index on immv "mv_self" automatically NOTICE: could not create an index on immv "mv_self" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -626,7 +646,7 @@ CREATE TABLE base_r (i int, v int);
CREATE TABLE base_s (i int, v int); CREATE TABLE base_s (i int, v int);
INSERT INTO base_r VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_r VALUES (1, 10), (2, 20), (3, 30);
INSERT INTO base_s VALUES (1, 100), (2, 200), (3, 300); INSERT INTO base_s VALUES (1, 100), (2, 200), (3, 300);
SELECT create_immv('mv(v1, v2)', 'SELECT r.v, s.v FROM base_r AS r JOIN base_s AS s USING(i)');; SELECT pgivm.create_immv('mv(v1, v2)', 'SELECT r.v, s.v FROM base_r AS r JOIN base_s AS s USING(i)');;
NOTICE: could not create an index on immv "mv" automatically NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -671,7 +691,7 @@ CREATE TABLE ri1 (i int PRIMARY KEY);
CREATE TABLE ri2 (i int PRIMARY KEY REFERENCES ri1(i) ON UPDATE CASCADE ON DELETE CASCADE, v int); CREATE TABLE ri2 (i int PRIMARY KEY REFERENCES ri1(i) ON UPDATE CASCADE ON DELETE CASCADE, v int);
INSERT INTO ri1 VALUES (1),(2),(3); INSERT INTO ri1 VALUES (1),(2),(3);
INSERT INTO ri2 VALUES (1),(2),(3); INSERT INTO ri2 VALUES (1),(2),(3);
SELECT create_immv('mv_ri(i1, i2)', 'SELECT ri1.i, ri2.i FROM ri1 JOIN ri2 USING(i)'); SELECT pgivm.create_immv('mv_ri(i1, i2)', 'SELECT ri1.i, ri2.i FROM ri1 JOIN ri2 USING(i)');
NOTICE: created index "mv_ri_index" on immv "mv_ri" NOTICE: created index "mv_ri_index" on immv "mv_ri"
create_immv create_immv
------------- -------------
@ -695,10 +715,173 @@ SELECT * FROM mv_ri ORDER BY i2;
10 | 10 10 | 10
(2 rows) (2 rows)
ROLLBACK;
-- support subquery for using EXISTS()
BEGIN;
SELECT pgivm.create_immv('mv_ivm_exists_subquery', 'SELECT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
NOTICE: could not create an index on immv "mv_ivm_exists_subquery" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
create_immv
-------------
4
(1 row)
SELECT pgivm.create_immv('mv_ivm_exists_subquery2', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) AND a.i > 2');
NOTICE: could not create an index on immv "mv_ivm_exists_subquery2" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
create_immv
-------------
2
(1 row)
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+----+------------------------
1 | 10 | 1
2 | 20 | 1
3 | 30 | 1
4 | 40 | 1
(4 rows)
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+----+------------------------
3 | 30 | 1
4 | 40 | 1
(2 rows)
INSERT INTO mv_base_a VALUES(1,10),(6,60),(3,30),(3,300);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
1 | 10 | 1
1 | 10 | 1
2 | 20 | 1
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(7 rows)
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(4 rows)
INSERT INTO mv_base_b VALUES(1,101);
INSERT INTO mv_base_b VALUES(1,111);
INSERT INTO mv_base_b VALUES(2,102);
INSERT INTO mv_base_b VALUES(6,106);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
1 | 10 | 3
1 | 10 | 3
2 | 20 | 2
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
6 | 60 | 1
(8 rows)
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
6 | 60 | 1
(5 rows)
UPDATE mv_base_a SET i = 1 WHERE j =60;
UPDATE mv_base_b SET i = 10 WHERE k = 101;
UPDATE mv_base_b SET k = 1002 WHERE k = 102;
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
1 | 10 | 1
1 | 10 | 1
1 | 60 | 1
2 | 20 | 2
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(8 rows)
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(4 rows)
DELETE FROM mv_base_a WHERE (i,j) = (1,60);
DELETE FROM mv_base_b WHERE i = 2;
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
1 | 10 | 1
1 | 10 | 1
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(6 rows)
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(4 rows)
--- EXISTS subquery with tuple duplication and DISTINCT
SELECT pgivm.create_immv('mv_ivm_exists_subquery_distinct', 'SELECT DISTINCT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
NOTICE: created index "mv_ivm_exists_subquery_distinct_index" on immv "mv_ivm_exists_subquery_distinct"
create_immv
-------------
4
(1 row)
DELETE FROM mv_base_b WHERE i = 1 or i = 3;
INSERT INTO mv_base_b VALUES (1,100), (3,300);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
i | j | __ivm_exists_count_0__
---+-----+------------------------
1 | 10 | 1
1 | 10 | 1
3 | 30 | 1
3 | 30 | 1
3 | 300 | 1
4 | 40 | 1
(6 rows)
SELECT * FROM mv_ivm_exists_subquery_distinct ORDER BY i, j;
i | j | __ivm_exists_count_0__ | __ivm_count__
---+-----+------------------------+---------------
1 | 10 | 1 | 2
3 | 30 | 1 | 2
3 | 300 | 1 | 1
4 | 40 | 1 | 1
(4 rows)
ROLLBACK; ROLLBACK;
-- support simple subquery in FROM clause -- support simple subquery in FROM clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a,( SELECT * FROM mv_base_b) b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a,( SELECT * FROM mv_base_b) b WHERE a.i = b.i');
NOTICE: could not create an index on immv "mv_ivm_subquery" automatically NOTICE: could not create an index on immv "mv_ivm_subquery" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -722,28 +905,48 @@ SELECT * FROM mv_ivm_subquery ORDER BY i,j;
ROLLBACK; ROLLBACK;
-- disallow non-simple subqueries -- disallow non-simple subqueries
SELECT create_immv('mv_ivm_exists_subquery', 'SELECT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) b WHERE a.i = b.i');
ERROR: unsupported subquery on incrementally maintainable materialized view
HINT: Only simple subquery in FROM clause is supported.
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) b WHERE a.i = b.i');
ERROR: aggregate functions in nested query are not supported on incrementally maintainable materialized view ERROR: aggregate functions in nested query are not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT DISTINCT i FROM mv_base_b) b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT DISTINCT i FROM mv_base_b) b WHERE a.i = b.i');
ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )');
ERROR: unsupported subquery on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: Only simple subquery in FROM clause is supported. HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a');
ERROR: unsupported subquery on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: Only simple subquery in FROM clause is supported. HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a');
ERROR: unsupported subquery on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: Only simple subquery in FROM clause is supported. HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v');
ERROR: unsupported subquery on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: Only simple subquery in FROM clause is supported. HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: targetlist must contain vars that are referred to in EXISTS subquery
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: OR or NOT conditions and EXISTS condition can not be used together
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_a a2 WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a2.i = b.i))');
ERROR: nested sublink is not supported on incrementally maintainable materialized view
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT EXISTS(SELECT 1 from mv_base_b) FROM mv_base_a a');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT false OR EXISTS(SELECT 1 FROM mv_base_a) FROM mv_base_b');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: OR or NOT conditions and EXISTS condition can not be used together
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, CASE EXISTS(SELECT 1 FROM mv_base_a) WHEN true THEN 100 ELSE 10 END), mv_base_b');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM mv_base_a a WHERE CASE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) WHEN true THEN false ELSE true END');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM mv_base_a a WHERE true and CASE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) WHEN true THEN false ELSE true END');
ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: sublink only supports subquery with EXISTS clause in WHERE clause
-- support join subquery in FROM clause -- support join subquery in FROM clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp'); SELECT pgivm.create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp');
NOTICE: could not create an index on immv "mv_ivm_join_subquery" automatically NOTICE: could not create an index on immv "mv_ivm_join_subquery" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -776,7 +979,7 @@ SELECT * FROM mv_ivm_join_subquery ORDER BY i,j,k;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
-- nested subquery -- nested subquery
SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN (SELECT * FROM mv_base_a) a USING(i)) tmp'); SELECT pgivm.create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN (SELECT * FROM mv_base_a) a USING(i)) tmp');
NOTICE: could not create an index on immv "mv_ivm_join_subquery" automatically NOTICE: could not create an index on immv "mv_ivm_join_subquery" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -809,7 +1012,7 @@ SELECT * FROM mv_ivm_join_subquery ORDER BY i,j,k;
ROLLBACK; ROLLBACK;
-- support simple CTE -- support simple CTE
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); 'WITH b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -834,7 +1037,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i'); 'WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -859,7 +1062,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM mv_base_b) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'WITH b AS ( SELECT * FROM mv_base_b) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -884,7 +1087,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'SELECT * FROM (WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'SELECT * FROM (WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -909,7 +1112,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM (SELECT * FROM mv_base_b) b2) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'WITH b AS ( SELECT * FROM (SELECT * FROM mv_base_b) b2) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -934,7 +1137,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH x AS ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) SELECT * FROM x'); 'WITH x AS ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) SELECT * FROM x');
NOTICE: could not create an index on immv "mv_cte" automatically NOTICE: could not create an index on immv "mv_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -968,7 +1171,7 @@ SELECT * FROM mv_cte ORDER BY i,j,k;
ROLLBACK; ROLLBACK;
-- nested CTE -- nested CTE
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_nested_cte', 'WITH v AS ( WITH a AS (SELECT * FROM mv_base_a) SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN a USING(i)) SELECT * FROM v'); SELECT pgivm.create_immv('mv_ivm_nested_cte', 'WITH v AS ( WITH a AS (SELECT * FROM mv_base_a) SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN a USING(i)) SELECT * FROM v');
NOTICE: could not create an index on immv "mv_ivm_nested_cte" automatically NOTICE: could not create an index on immv "mv_ivm_nested_cte" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1003,7 +1206,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30);
SELECT create_immv('mv_cte_multi(v1, v2)', SELECT pgivm.create_immv('mv_cte_multi(v1, v2)',
'WITH t AS (SELECT * FROM base_t) SELECT t1.v, t2.v FROM t AS t1 JOIN t AS t2 ON t1.i = t2.i'); 'WITH t AS (SELECT * FROM base_t) SELECT t1.v, t2.v FROM t AS t1 JOIN t AS t2 ON t1.i = t2.i');
NOTICE: could not create an index on immv "mv_cte_multi" automatically NOTICE: could not create an index on immv "mv_cte_multi" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -1054,18 +1257,20 @@ SELECT * FROM mv_cte_multi ORDER BY v1;
ROLLBACK; ROLLBACK;
--- disallow not-simple CTE --- disallow not-simple CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
ERROR: aggregate functions in nested query are not supported on incrementally maintainable materialized view ERROR: aggregate functions in nested query are not supported on incrementally maintainable materialized view
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view
SELECT pgivm.create_immv('mv_cte_fail', 'WITH a AS (SELECT i, j FROM mv_base_a) SELECT a.i,a.j FROM a WHERE EXISTS(WITH b AS (SELECT i FROM mv_base_b) SELECT 1 FROM b WHERE a.i = b.i)');
ERROR: CTE in EXIST clause is not supported on incrementally maintainable materialized view
-- unreferenced CTE -- unreferenced CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a');
ERROR: Ureferenced WITH query is not supported on incrementally maintainable materialized view ERROR: Ureferenced WITH query is not supported on incrementally maintainable materialized view
-- views including NULL -- views including NULL
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1,10),(2, NULL); INSERT INTO base_t VALUES (1,10),(2, NULL);
SELECT create_immv('mv', 'SELECT * FROM base_t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM base_t');
NOTICE: could not create an index on immv "mv" automatically NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1092,7 +1297,7 @@ SELECT * FROM mv ORDER BY i;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int); CREATE TABLE base_t (i int);
SELECT create_immv('mv', 'SELECT * FROM base_t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM base_t');
NOTICE: could not create an index on immv "mv" automatically NOTICE: could not create an index on immv "mv" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1118,7 +1323,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (1, 10), (1, 20); INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (1, 10), (1, 20);
SELECT create_immv('mv', 'SELECT i, sum(v) FROM base_t GROUP BY i'); SELECT pgivm.create_immv('mv', 'SELECT i, sum(v) FROM base_t GROUP BY i');
NOTICE: created index "mv_index" on immv "mv" NOTICE: created index "mv_index" on immv "mv"
create_immv create_immv
------------- -------------
@ -1144,7 +1349,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4), (NULL, 5); INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4), (NULL, 5);
SELECT create_immv('mv', 'SELECT i, min(v), max(v) FROM base_t GROUP BY i'); SELECT pgivm.create_immv('mv', 'SELECT i, min(v), max(v) FROM base_t GROUP BY i');
NOTICE: created index "mv_index" on immv "mv" NOTICE: created index "mv_index" on immv "mv"
create_immv create_immv
------------- -------------
@ -1190,6 +1395,8 @@ CREATE FUNCTION mytype_out(mytype)
RETURNS cstring AS 'int4out' RETURNS cstring AS 'int4out'
LANGUAGE INTERNAL STRICT IMMUTABLE; LANGUAGE INTERNAL STRICT IMMUTABLE;
NOTICE: argument type mytype is only a shell NOTICE: argument type mytype is only a shell
LINE 1: CREATE FUNCTION mytype_out(mytype)
^
CREATE TYPE mytype ( CREATE TYPE mytype (
LIKE = int4, LIKE = int4,
INPUT = mytype_in, INPUT = mytype_in,
@ -1216,7 +1423,7 @@ CREATE OPERATOR CLASS mytype_ops
OPERATOR 3 = , OPERATOR 3 = ,
FUNCTION 1 mytype_cmp(mytype,mytype); FUNCTION 1 mytype_cmp(mytype,mytype);
CREATE TABLE t_mytype (x mytype); CREATE TABLE t_mytype (x mytype);
SELECT create_immv('mv_mytype', SELECT pgivm.create_immv('mv_mytype',
'SELECT * FROM t_mytype'); 'SELECT * FROM t_mytype');
NOTICE: could not create an index on immv "mv_mytype" automatically NOTICE: could not create an index on immv "mv_mytype" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
@ -1235,108 +1442,108 @@ SELECT * FROM mv_mytype;
ROLLBACK; ROLLBACK;
-- outer join is not supported -- outer join is not supported
SELECT create_immv('mv(a,b)', SELECT pgivm.create_immv('mv(a,b)',
'SELECT a.i, b.i FROM mv_base_a a LEFT JOIN mv_base_b b ON a.i=b.i'); 'SELECT a.i, b.i FROM mv_base_a a LEFT JOIN mv_base_b b ON a.i=b.i');
ERROR: OUTER JOIN is not supported on incrementally maintainable materialized view ERROR: OUTER JOIN is not supported on incrementally maintainable materialized view
-- contain system column -- contain system column
SELECT create_immv('mv_ivm01', 'SELECT i,j,xmin FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm01', 'SELECT i,j,xmin FROM mv_base_a');
ERROR: system column is not supported on incrementally maintainable materialized view ERROR: system column is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm02', 'SELECT i,j FROM mv_base_a WHERE xmin = ''610'''); SELECT pgivm.create_immv('mv_ivm02', 'SELECT i,j FROM mv_base_a WHERE xmin = ''610''');
ERROR: system column is not supported on incrementally maintainable materialized view ERROR: system column is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm03', 'SELECT i,j,xmin::text AS x_min FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm03', 'SELECT i,j,xmin::text AS x_min FROM mv_base_a');
ERROR: system column is not supported on incrementally maintainable materialized view ERROR: system column is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm04', 'SELECT i,j,xidsend(xmin) AS x_min FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm04', 'SELECT i,j,xidsend(xmin) AS x_min FROM mv_base_a');
ERROR: system column is not supported on incrementally maintainable materialized view ERROR: system column is not supported on incrementally maintainable materialized view
-- contain ORDER BY -- contain ORDER BY
SELECT create_immv('mv_ivm07', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) ORDER BY i,j,k'); SELECT pgivm.create_immv('mv_ivm07', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) ORDER BY i,j,k');
ERROR: ORDER BY clause is not supported on incrementally maintainable materialized view ERROR: ORDER BY clause is not supported on incrementally maintainable materialized view
-- contain HAVING -- contain HAVING
SELECT create_immv('mv_ivm08', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) GROUP BY i,j,k HAVING SUM(i) > 5'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) GROUP BY i,j,k HAVING SUM(i) > 5');
ERROR: HAVING clause is not supported on incrementally maintainable materialized view ERROR: HAVING clause is not supported on incrementally maintainable materialized view
-- contain GROUP BY without aggregate -- contain GROUP BY without aggregate
SELECT create_immv('mv_ivm08', 'SELECT i,j FROM mv_base_a GROUP BY i,j'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT i,j FROM mv_base_a GROUP BY i,j');
ERROR: GROUP BY clause without aggregate is not supported on incrementally maintainable materialized view ERROR: GROUP BY clause without aggregate is not supported on incrementally maintainable materialized view
-- contain view or materialized view -- contain view or materialized view
CREATE VIEW b_view AS SELECT i,k FROM mv_base_b; CREATE VIEW b_view AS SELECT i,k FROM mv_base_b;
CREATE MATERIALIZED VIEW b_mview AS SELECT i,k FROM mv_base_b; CREATE MATERIALIZED VIEW b_mview AS SELECT i,k FROM mv_base_b;
SELECT create_immv('mv_ivm07', 'SELECT a.i,a.j FROM mv_base_a a,b_view b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm07', 'SELECT a.i,a.j FROM mv_base_a a,b_view b WHERE a.i = b.i');
ERROR: VIEW or MATERIALIZED VIEW is not supported on incrementally maintainable materialized view ERROR: VIEW or MATERIALIZED VIEW is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm08', 'SELECT a.i,a.j FROM mv_base_a a,b_mview b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT a.i,a.j FROM mv_base_a a,b_mview b WHERE a.i = b.i');
ERROR: VIEW or MATERIALIZED VIEW is not supported on incrementally maintainable materialized view ERROR: VIEW or MATERIALIZED VIEW is not supported on incrementally maintainable materialized view
-- contain mutable functions -- contain mutable functions
SELECT create_immv('mv_ivm12', 'SELECT i,j FROM mv_base_a WHERE i = random()::int'); SELECT pgivm.create_immv('mv_ivm12', 'SELECT i,j FROM mv_base_a WHERE i = random()::int');
ERROR: mutable function is not supported on incrementally maintainable materialized view ERROR: mutable function is not supported on incrementally maintainable materialized view
HINT: functions must be marked IMMUTABLE HINT: functions must be marked IMMUTABLE
-- LIMIT/OFFSET is not supported -- LIMIT/OFFSET is not supported
SELECT create_immv('mv_ivm13', 'SELECT i,j FROM mv_base_a LIMIT 10 OFFSET 5'); SELECT pgivm.create_immv('mv_ivm13', 'SELECT i,j FROM mv_base_a LIMIT 10 OFFSET 5');
ERROR: LIMIT/OFFSET clause is not supported on incrementally maintainable materialized view ERROR: LIMIT/OFFSET clause is not supported on incrementally maintainable materialized view
-- DISTINCT ON is not supported -- DISTINCT ON is not supported
SELECT create_immv('mv_ivm14', 'SELECT DISTINCT ON(i) i, j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm14', 'SELECT DISTINCT ON(i) i, j FROM mv_base_a');
ERROR: DISTINCT ON is not supported on incrementally maintainable materialized view ERROR: DISTINCT ON is not supported on incrementally maintainable materialized view
-- TABLESAMPLE clause is not supported -- TABLESAMPLE clause is not supported
SELECT create_immv('mv_ivm15', 'SELECT i, j FROM mv_base_a TABLESAMPLE SYSTEM(50)'); SELECT pgivm.create_immv('mv_ivm15', 'SELECT i, j FROM mv_base_a TABLESAMPLE SYSTEM(50)');
ERROR: TABLESAMPLE clause is not supported on incrementally maintainable materialized view ERROR: TABLESAMPLE clause is not supported on incrementally maintainable materialized view
-- window functions are not supported -- window functions are not supported
SELECT create_immv('mv_ivm16', 'SELECT *, cume_dist() OVER (ORDER BY i) AS rank FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm16', 'SELECT *, cume_dist() OVER (ORDER BY i) AS rank FROM mv_base_a');
ERROR: window functions are not supported on incrementally maintainable materialized view ERROR: window functions are not supported on incrementally maintainable materialized view
-- aggregate function with some options is not supported -- aggregate function with some options is not supported
SELECT create_immv('mv_ivm17', 'SELECT COUNT(*) FILTER(WHERE i < 3) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm17', 'SELECT COUNT(*) FILTER(WHERE i < 3) FROM mv_base_a');
ERROR: aggregate function with FILTER clause is not supported on incrementally maintainable materialized view ERROR: aggregate function with FILTER clause is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm18', 'SELECT COUNT(DISTINCT i) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm18', 'SELECT COUNT(DISTINCT i) FROM mv_base_a');
ERROR: aggregate function with DISTINCT arguments is not supported on incrementally maintainable materialized view ERROR: aggregate function with DISTINCT arguments is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm19', 'SELECT array_agg(j ORDER BY i DESC) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm19', 'SELECT array_agg(j ORDER BY i DESC) FROM mv_base_a');
ERROR: aggregate function with ORDER clause is not supported on incrementally maintainable materialized view ERROR: aggregate function with ORDER clause is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm20', 'SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),())'); SELECT pgivm.create_immv('mv_ivm20', 'SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),())');
ERROR: GROUPING SETS, ROLLUP, or CUBE clauses is not supported on incrementally maintainable materialized view ERROR: GROUPING SETS, ROLLUP, or CUBE clauses is not supported on incrementally maintainable materialized view
-- inheritance parent is not supported -- inheritance parent is not supported
BEGIN; BEGIN;
CREATE TABLE parent (i int, v int); CREATE TABLE parent (i int, v int);
CREATE TABLE child_a(options text) INHERITS(parent); CREATE TABLE child_a(options text) INHERITS(parent);
SELECT create_immv('mv_ivm21', 'SELECT * FROM parent'); SELECT pgivm.create_immv('mv_ivm21', 'SELECT * FROM parent');
ERROR: inheritance parent is not supported on incrementally maintainable materialized view ERROR: inheritance parent is not supported on incrementally maintainable materialized view
ROLLBACK; ROLLBACK;
-- UNION statement is not supported -- UNION statement is not supported
SELECT create_immv('mv_ivm22', 'SELECT i,j FROM mv_base_a UNION ALL SELECT i,k FROM mv_base_b'); SELECT pgivm.create_immv('mv_ivm22', 'SELECT i,j FROM mv_base_a UNION ALL SELECT i,k FROM mv_base_b');
ERROR: UNION/INTERSECT/EXCEPT statements are not supported on incrementally maintainable materialized view ERROR: UNION/INTERSECT/EXCEPT statements are not supported on incrementally maintainable materialized view
-- DISTINCT clause in nested query are not supported -- DISTINCT clause in nested query are not supported
SELECT create_immv('mv_ivm23', 'SELECT * FROM (SELECT DISTINCT i,j FROM mv_base_a) AS tmp');; SELECT pgivm.create_immv('mv_ivm23', 'SELECT * FROM (SELECT DISTINCT i,j FROM mv_base_a) AS tmp');;
ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view
-- empty target list is not allowed with IVM -- empty target list is not allowed with IVM
SELECT create_immv('mv_ivm25', 'SELECT FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm25', 'SELECT FROM mv_base_a');
ERROR: empty target list is not supported on incrementally maintainable materialized view ERROR: empty target list is not supported on incrementally maintainable materialized view
-- FOR UPDATE/SHARE is not supported -- FOR UPDATE/SHARE is not supported
SELECT create_immv('mv_ivm26', 'SELECT i,j FROM mv_base_a FOR UPDATE'); SELECT pgivm.create_immv('mv_ivm26', 'SELECT i,j FROM mv_base_a FOR UPDATE');
ERROR: FOR UPDATE/SHARE clause is not supported on incrementally maintainable materialized view ERROR: FOR UPDATE/SHARE clause is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm27', 'SELECT * FROM (SELECT i,j FROM mv_base_a FOR UPDATE) AS tmp;'); SELECT pgivm.create_immv('mv_ivm27', 'SELECT * FROM (SELECT i,j FROM mv_base_a FOR UPDATE) AS tmp;');
ERROR: FOR UPDATE/SHARE clause is not supported on incrementally maintainable materialized view ERROR: FOR UPDATE/SHARE clause is not supported on incrementally maintainable materialized view
-- tartget list cannot contain ivm column that start with '__ivm' -- tartget list cannot contain ivm column that start with '__ivm'
SELECT create_immv('mv_ivm28', 'SELECT i AS "__ivm_count__" FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm28', 'SELECT i AS "__ivm_count__" FROM mv_base_a');
ERROR: column name __ivm_count__ is not supported on incrementally maintainable materialized view ERROR: column name __ivm_count__ is not supported on incrementally maintainable materialized view
-- expressions specified in GROUP BY must appear in the target list. -- expressions specified in GROUP BY must appear in the target list.
SELECT create_immv('mv_ivm29', 'SELECT COUNT(i) FROM mv_base_a GROUP BY i;'); SELECT pgivm.create_immv('mv_ivm29', 'SELECT COUNT(i) FROM mv_base_a GROUP BY i;');
ERROR: GROUP BY expression not appearing in select list is not supported on incrementally maintainable materialized view ERROR: GROUP BY expression not appearing in select list is not supported on incrementally maintainable materialized view
-- experssions containing an aggregate is not supported -- experssions containing an aggregate is not supported
SELECT create_immv('mv_ivm30', 'SELECT sum(i)*0.5 FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm30', 'SELECT sum(i)*0.5 FROM mv_base_a');
ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm31', 'SELECT sum(i)/sum(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm31', 'SELECT sum(i)/sum(j) FROM mv_base_a');
ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view
-- VALUES is not supported -- VALUES is not supported
SELECT create_immv('mv_ivm_only_values1', 'values(1)'); SELECT pgivm.create_immv('mv_ivm_only_values1', 'values(1)');
ERROR: VALUES is not supported on incrementally maintainable materialized view ERROR: VALUES is not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm_only_values2', 'SELECT * FROM (values(1)) AS tmp'); SELECT pgivm.create_immv('mv_ivm_only_values2', 'SELECT * FROM (values(1)) AS tmp');
ERROR: VALUES is not supported on incrementally maintainable materialized view ERROR: VALUES is not supported on incrementally maintainable materialized view
-- views containing base tables with Row Level Security -- views containing base tables with Row Level Security
DROP USER IF EXISTS ivm_admin; DROP USER IF EXISTS regress_ivm_admin;
NOTICE: role "ivm_admin" does not exist, skipping NOTICE: role "regress_ivm_admin" does not exist, skipping
DROP USER IF EXISTS ivm_user; DROP USER IF EXISTS regress_ivm_user;
NOTICE: role "ivm_user" does not exist, skipping NOTICE: role "regress_ivm_user" does not exist, skipping
CREATE USER ivm_admin; CREATE USER regress_ivm_admin;
CREATE USER ivm_user; CREATE USER regress_ivm_user;
--- create a table with RLS --- create a table with RLS
SET SESSION AUTHORIZATION ivm_admin; SET SESSION AUTHORIZATION regress_ivm_admin;
CREATE TABLE rls_tbl(id int, data text, owner name); CREATE TABLE rls_tbl(id int, data text, owner name);
INSERT INTO rls_tbl VALUES INSERT INTO rls_tbl VALUES
(1,'foo','ivm_user'), (1,'foo','regress_ivm_user'),
(2,'bar','postgres'); (2,'bar','postgres');
CREATE TABLE num_tbl(id int, num text); CREATE TABLE num_tbl(id int, num text);
INSERT INTO num_tbl VALUES INSERT INTO num_tbl VALUES
@ -1351,9 +1558,9 @@ CREATE POLICY rls_tbl_policy ON rls_tbl FOR SELECT TO PUBLIC USING(owner = curre
ALTER TABLE rls_tbl ENABLE ROW LEVEL SECURITY; ALTER TABLE rls_tbl ENABLE ROW LEVEL SECURITY;
GRANT ALL on rls_tbl TO PUBLIC; GRANT ALL on rls_tbl TO PUBLIC;
GRANT ALL on num_tbl TO PUBLIC; GRANT ALL on num_tbl TO PUBLIC;
--- create a view owned by ivm_user --- create a view owned by regress_ivm_user
SET SESSION AUTHORIZATION ivm_user; SET SESSION AUTHORIZATION regress_ivm_user;
SELECT create_immv('ivm_rls', 'SELECT * FROM rls_tbl'); SELECT pgivm.create_immv('ivm_rls', 'SELECT * FROM rls_tbl');
NOTICE: could not create an index on immv "ivm_rls" automatically NOTICE: could not create an index on immv "ivm_rls" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1364,42 +1571,42 @@ HINT: Create an index on the immv for efficient incremental maintenance.
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
id | data | owner id | data | owner
----+------+---------- ----+------+------------------
1 | foo | ivm_user 1 | foo | regress_ivm_user
(1 row) (1 row)
RESET SESSION AUTHORIZATION; RESET SESSION AUTHORIZATION;
--- inserts rows owned by different users --- inserts rows owned by different users
INSERT INTO rls_tbl VALUES INSERT INTO rls_tbl VALUES
(3,'baz','ivm_user'), (3,'baz','regress_ivm_user'),
(4,'qux','postgres'); (4,'qux','postgres');
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
id | data | owner id | data | owner
----+------+---------- ----+------+------------------
1 | foo | ivm_user 1 | foo | regress_ivm_user
3 | baz | ivm_user 3 | baz | regress_ivm_user
(2 rows) (2 rows)
--- combination of diffent kinds of commands --- combination of diffent kinds of commands
WITH WITH
i AS (INSERT INTO rls_tbl VALUES(5,'quux','postgres'), (6,'corge','ivm_user')), i AS (INSERT INTO rls_tbl VALUES(5,'quux','postgres'), (6,'corge','regress_ivm_user')),
u AS (UPDATE rls_tbl SET owner = 'postgres' WHERE id = 1), u AS (UPDATE rls_tbl SET owner = 'postgres' WHERE id = 1),
u2 AS (UPDATE rls_tbl SET owner = 'ivm_user' WHERE id = 2) u2 AS (UPDATE rls_tbl SET owner = 'regress_ivm_user' WHERE id = 2)
SELECT; SELECT;
-- --
(1 row) (1 row)
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
id | data | owner id | data | owner
----+-------+---------- ----+-------+------------------
2 | bar | ivm_user 2 | bar | regress_ivm_user
3 | baz | ivm_user 3 | baz | regress_ivm_user
6 | corge | ivm_user 6 | corge | regress_ivm_user
(3 rows) (3 rows)
--- ---
SET SESSION AUTHORIZATION ivm_user; SET SESSION AUTHORIZATION regress_ivm_user;
SELECT create_immv('ivm_rls2', 'SELECT * FROM rls_tbl JOIN num_tbl USING(id)'); SELECT pgivm.create_immv('ivm_rls2', 'SELECT * FROM rls_tbl JOIN num_tbl USING(id)');
NOTICE: could not create an index on immv "ivm_rls2" automatically NOTICE: could not create an index on immv "ivm_rls2" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1418,10 +1625,10 @@ SELECT;
SELECT * FROM ivm_rls2 ORDER BY 1,2,3; SELECT * FROM ivm_rls2 ORDER BY 1,2,3;
id | data | owner | num id | data | owner | num
----+-------+----------+--------- ----+-------+------------------+---------
2 | bar | ivm_user | two 2 | bar | regress_ivm_user | two
3 | baz_2 | ivm_user | three_2 3 | baz_2 | regress_ivm_user | three_2
6 | corge | ivm_user | six 6 | corge | regress_ivm_user | six
(3 rows) (3 rows)
DROP TABLE rls_tbl CASCADE; DROP TABLE rls_tbl CASCADE;
@ -1429,14 +1636,14 @@ NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table ivm_rls DETAIL: drop cascades to table ivm_rls
drop cascades to table ivm_rls2 drop cascades to table ivm_rls2
DROP TABLE num_tbl CASCADE; DROP TABLE num_tbl CASCADE;
DROP USER ivm_user; DROP USER regress_ivm_user;
DROP USER ivm_admin; DROP USER regress_ivm_admin;
-- automatic index creation -- automatic index creation
BEGIN; BEGIN;
CREATE TABLE base_a (i int primary key, j int); CREATE TABLE base_a (i int primary key, j int);
CREATE TABLE base_b (i int primary key, j int); CREATE TABLE base_b (i int primary key, j int);
--- group by: create an index --- group by: create an index
SELECT create_immv('mv_idx1', 'SELECT i, sum(j) FROM base_a GROUP BY i'); SELECT pgivm.create_immv('mv_idx1', 'SELECT i, sum(j) FROM base_a GROUP BY i');
NOTICE: created index "mv_idx1_index" on immv "mv_idx1" NOTICE: created index "mv_idx1_index" on immv "mv_idx1"
create_immv create_immv
------------- -------------
@ -1444,7 +1651,7 @@ NOTICE: created index "mv_idx1_index" on immv "mv_idx1"
(1 row) (1 row)
--- distinct: create an index --- distinct: create an index
SELECT create_immv('mv_idx2', 'SELECT DISTINCT j FROM base_a'); SELECT pgivm.create_immv('mv_idx2', 'SELECT DISTINCT j FROM base_a');
NOTICE: created index "mv_idx2_index" on immv "mv_idx2" NOTICE: created index "mv_idx2_index" on immv "mv_idx2"
create_immv create_immv
------------- -------------
@ -1452,7 +1659,7 @@ NOTICE: created index "mv_idx2_index" on immv "mv_idx2"
(1 row) (1 row)
--- with all pkey columns: create an index --- with all pkey columns: create an index
SELECT create_immv('mv_idx3(i_a, i_b)', 'SELECT a.i, b.i FROM base_a a, base_b b'); SELECT pgivm.create_immv('mv_idx3(i_a, i_b)', 'SELECT a.i, b.i FROM base_a a, base_b b');
NOTICE: created index "mv_idx3_index" on immv "mv_idx3" NOTICE: created index "mv_idx3_index" on immv "mv_idx3"
create_immv create_immv
------------- -------------
@ -1460,7 +1667,7 @@ NOTICE: created index "mv_idx3_index" on immv "mv_idx3"
(1 row) (1 row)
--- missing some pkey columns: no index --- missing some pkey columns: no index
SELECT create_immv('mv_idx4', 'SELECT j FROM base_a'); SELECT pgivm.create_immv('mv_idx4', 'SELECT j FROM base_a');
NOTICE: could not create an index on immv "mv_idx4" automatically NOTICE: could not create an index on immv "mv_idx4" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1469,7 +1676,7 @@ HINT: Create an index on the immv for efficient incremental maintenance.
0 0
(1 row) (1 row)
SELECT create_immv('mv_idx5', 'SELECT a.i, b.j FROM base_a a, base_b b'); SELECT pgivm.create_immv('mv_idx5', 'SELECT a.i, b.j FROM base_a a, base_b b');
NOTICE: could not create an index on immv "mv_idx5" automatically NOTICE: could not create an index on immv "mv_idx5" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause. DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance. HINT: Create an index on the immv for efficient incremental maintenance.
@ -1478,14 +1685,30 @@ HINT: Create an index on the immv for efficient incremental maintenance.
0 0
(1 row) (1 row)
--- subqueries --- subqueries: create an index
SELECT create_immv('mv_idx6(i_a, i_b)', 'SELECT a.i, b.i FROM (SELECT * FROM base_a) a, (SELECT * FROM base_b) b'); SELECT pgivm.create_immv('mv_idx6(i_a, i_b)', 'SELECT a.i, b.i FROM (SELECT * FROM base_a) a, (SELECT * FROM base_b) b');
NOTICE: created index "mv_idx6_index" on immv "mv_idx6" NOTICE: created index "mv_idx6_index" on immv "mv_idx6"
create_immv create_immv
------------- -------------
0 0
(1 row) (1 row)
--- with set-returning function: no index
SELECT pgivm.create_immv('mv_idx7', 'SELECT i FROM base_a, generate_series(1,10)');
NOTICE: could not create an index on immv "mv_idx7" automatically
DETAIL: This target list does not have all the primary key columns, or this view does not contain GROUP BY or DISTINCT clause.
HINT: Create an index on the immv for efficient incremental maintenance.
create_immv
-------------
0
(1 row)
ROLLBACK;
-- type that doesn't have default operator class for access method btree
BEGIN;
CREATE TABLE table_json (j json);
SELECT pgivm.create_immv('mv_json', 'SELECT * from table_json');
ERROR: data type json has no default operator class for access method "btree"
ROLLBACK; ROLLBACK;
-- prevent IMMV chanages -- prevent IMMV chanages
INSERT INTO mv_ivm_1 VALUES(1,1,1); INSERT INTO mv_ivm_1 VALUES(1,1,1);
@ -1497,7 +1720,7 @@ ERROR: cannot change materialized view "mv_ivm_1"
TRUNCATE mv_ivm_1; TRUNCATE mv_ivm_1;
ERROR: cannot change materialized view "mv_ivm_1" ERROR: cannot change materialized view "mv_ivm_1"
-- get_immv_def function -- get_immv_def function
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | get_immv_def immvrelid | get_immv_def
-----------+---------------------------------- -----------+----------------------------------
mv_ivm_1 | SELECT a.i, + mv_ivm_1 | SELECT a.i, +
@ -1508,7 +1731,7 @@ SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1;
(1 row) (1 row)
-- mv_base_b is not immv -- mv_base_b is not immv
SELECT 'mv_base_b'::regclass, get_immv_def('mv_base_b'); SELECT 'mv_base_b'::regclass, pgivm.get_immv_def('mv_base_b');
regclass | get_immv_def regclass | get_immv_def
-----------+-------------- -----------+--------------
mv_base_b | mv_base_b |

1743
expected/pg_ivm_0.out Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,26 +1,26 @@
CREATE TABLE t (i int PRIMARY KEY); CREATE TABLE t (i int PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 5); INSERT INTO t SELECT generate_series(1, 5);
SELECT create_immv('mv', 'SELECT * FROM t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM t');
NOTICE: created index "mv_index" on immv "mv" NOTICE: created index "mv_index" on immv "mv"
create_immv create_immv
------------- -------------
5 5
(1 row) (1 row)
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | ispopulated immvrelid | ispopulated
-----------+------------- -----------+-------------
mv | t mv | t
(1 row) (1 row)
-- Refresh IMMV with data -- Refresh IMMV with data
SELECT refresh_immv('mv', true); SELECT pgivm.refresh_immv('mv', true);
refresh_immv refresh_immv
-------------- --------------
5 5
(1 row) (1 row)
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | ispopulated immvrelid | ispopulated
-----------+------------- -----------+-------------
mv | t mv | t
@ -39,13 +39,13 @@ SELECT i FROM mv ORDER BY 1;
(6 rows) (6 rows)
-- Make IMMV unpopulated -- Make IMMV unpopulated
SELECT refresh_immv('mv', false); SELECT pgivm.refresh_immv('mv', false);
refresh_immv refresh_immv
-------------- --------------
0 0
(1 row) (1 row)
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | ispopulated immvrelid | ispopulated
-----------+------------- -----------+-------------
mv | f mv | f
@ -64,13 +64,13 @@ SELECT i FROM mv ORDER BY 1;
(0 rows) (0 rows)
-- Refresh the IMMV and make it populated. -- Refresh the IMMV and make it populated.
SELECT refresh_immv('mv', true); SELECT pgivm.refresh_immv('mv', true);
refresh_immv refresh_immv
-------------- --------------
7 7
(1 row) (1 row)
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
immvrelid | ispopulated immvrelid | ispopulated
-----------+------------- -----------+-------------
mv | t mv | t
@ -104,15 +104,15 @@ SELECT i FROM mv ORDER BY 1;
(8 rows) (8 rows)
-- Use qualified name -- Use qualified name
SELECT refresh_immv('public.mv', true); SELECT pgivm.refresh_immv('public.mv', true);
refresh_immv refresh_immv
-------------- --------------
8 8
(1 row) (1 row)
-- Use not existing IMMV -- Use not existing IMMV
SELECT refresh_immv('mv_not_existing', true); SELECT pgivm.refresh_immv('mv_not_existing', true);
ERROR: relation "mv_not_existing" does not exist ERROR: relation "mv_not_existing" does not exist
-- Try to refresh a normal table -- error -- Try to refresh a normal table -- error
SELECT refresh_immv('t', true); SELECT pgivm.refresh_immv('t', true);
ERROR: "t" is not an IMMV ERROR: "t" is not an IMMV

253
expected/refresh_insert.out Normal file
View file

@ -0,0 +1,253 @@
Parsed test spec with 2 sessions
starting permutation: s1 refresh s2 insert c1 check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 refresh s2 c1 insert check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step insert: INSERT INTO a VALUES (2); <waiting ...>
step c1: COMMIT;
step insert: <... completed>
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert refresh c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
refresh_immv
------------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 refresh c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
refresh_immv
------------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 c2 refresh check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert c2 refresh check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
2
(1 row)
step check1: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)

View file

@ -0,0 +1,211 @@
Parsed test spec with 2 sessions
starting permutation: s1 refresh s2 insert c1 check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 refresh s2 c1 insert check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step insert: INSERT INTO a VALUES (2);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert refresh c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 refresh c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 c2 refresh check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert c2 refresh check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)

View file

@ -0,0 +1,211 @@
Parsed test spec with 2 sessions
starting permutation: s1 refresh s2 insert c1 check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 refresh s2 c1 insert check2 c2 mv
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step s2: SELECT;
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh insert c1 check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step insert: INSERT INTO a VALUES (2);
ERROR: could not obtain lock on materialized view "mv" during incremental maintenance
step c1: COMMIT;
step check2: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
(1 row)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 insert refresh c2 check1 c1 mv
step s1: SELECT;
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s1 s2 refresh c1 insert check2 c2 mv
step s1: SELECT;
step s2: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
refresh_immv
------------
1
(1 row)
step c1: COMMIT;
step insert: INSERT INTO a VALUES (2);
step check2: SELECT check_mv();
check_mv
--------
ok
(1 row)
step c2: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 refresh c2 check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step refresh: SELECT pgivm.refresh_immv('mv', true); <waiting ...>
step c2: COMMIT;
step refresh: <... completed>
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 insert s1 c2 refresh check1 c1 mv
step s2: SELECT;
step insert: INSERT INTO a VALUES (2);
step s1: SELECT;
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)
starting permutation: s2 s1 insert c2 refresh check1 c1 mv
step s2: SELECT;
step s1: SELECT;
step insert: INSERT INTO a VALUES (2);
step c2: COMMIT;
step refresh: SELECT pgivm.refresh_immv('mv', true);
ERROR: the materialized view is incrementally updated in concurrent transaction
step check1: SELECT check_mv();
ERROR: current transaction is aborted, commands ignored until end of transaction block
step c1: COMMIT;
step mv: SELECT * FROM mv ORDER BY 1,2; SELECT check_mv();
x|y
-+-
1|1
2|2
(2 rows)
check_mv
--------
ok
(1 row)

814
matview.c

File diff suppressed because it is too large Load diff

101
meson.build Normal file
View file

@ -0,0 +1,101 @@
project('pg_ivm', ['c'])
pg_config = find_program('pg_config')
bindir = run_command(pg_config, '--bindir', check: true).stdout().strip()
includedir_server = run_command(pg_config, '--includedir-server', check: true).stdout().strip()
includedir = run_command(pg_config, '--includedir', check: true).stdout().strip()
pkglibdir = run_command(pg_config, '--pkglibdir', check: true).stdout().strip()
sharedir = run_command(pg_config, '--sharedir', check: true).stdout().strip()
libdir = run_command(pg_config, '--libdir', check: true).stdout().strip()
module_name = meson.project_name()
# ruleutils.c includes ruleutils_13.c or ruleutils_14.c based on PostgreSQL version
# Note: We don't need to explicitly add these files since they're included by ruleutils.c
pg_ivm_sources = files(
'createas.c',
'matview.c',
'pg_ivm.c',
'ruleutils.c',
'subselect.c',
)
if meson.get_compiler('c').get_id() == 'msvc'
incdir = [includedir_server / 'port/win32_msvc',
includedir_server / 'port/win32',
includedir_server,
includedir]
postgres_lib = meson.get_compiler('c').find_library(
'postgres',
dirs: libdir,
static: true,
required: true
)
else
incdir = [ includedir_server ]
postgres_lib = []
endif
shared_module(module_name,
pg_ivm_sources,
include_directories: incdir,
install: true,
install_dir: pkglibdir,
name_prefix: '',
dependencies: postgres_lib,
)
install_data(
'pg_ivm--1.0.sql',
'pg_ivm--1.0--1.1.sql',
'pg_ivm--1.1--1.2.sql',
'pg_ivm--1.2--1.3.sql',
'pg_ivm--1.3--1.4.sql',
'pg_ivm--1.4--1.5.sql',
'pg_ivm--1.5--1.6.sql',
'pg_ivm--1.6--1.7.sql',
'pg_ivm--1.7--1.8.sql',
'pg_ivm--1.8--1.9.sql',
'pg_ivm--1.9--1.10.sql',
'pg_ivm--1.10.sql',
'pg_ivm--1.10--1.11.sql',
'pg_ivm--1.11--1.12.sql',
'pg_ivm.control',
install_dir: sharedir / 'extension',
)
pg_regress = find_program('pg_regress',
dirs: [pkglibdir / 'pgxs/src/test/regress']
)
regress_tests = ['pg_ivm', 'create_immv', 'refresh_immv']
test('regress',
pg_regress,
args: ['--bindir', bindir,
'--inputdir', meson.current_source_dir(),
] + regress_tests,
)
pg_isolation_regress = find_program('pg_isolation_regress',
dirs: [pkglibdir / 'pgxs/src/test/isolation']
)
isolation_tests = [
'create_insert', 'refresh_insert', 'insert_insert',
'create_insert2', 'refresh_insert2', 'insert_insert2',
'create_insert3', 'refresh_insert3', 'insert_insert3'
]
isolation_opts = [
'--load-extension','pg_ivm',
]
test('isolation',
pg_isolation_regress,
args: ['--bindir', bindir,
'--inputdir', meson.current_source_dir(),
'--outputdir', 'output_iso',
] + isolation_opts + isolation_tests,
)

View file

@ -11,8 +11,6 @@ CREATE TABLE __pg_ivm__.pg_ivm_immv(
ALTER TABLE __pg_ivm__.pg_ivm_immv SET SCHEMA pg_catalog; ALTER TABLE __pg_ivm__.pg_ivm_immv SET SCHEMA pg_catalog;
--CREATE UNIQUE INDEX ON pg_catalog.pg_ivm_immv USING btree (immvrelid);
SELECT pg_catalog.pg_extension_config_dump('pg_catalog.pg_ivm_immv', ''); SELECT pg_catalog.pg_extension_config_dump('pg_catalog.pg_ivm_immv', '');
-- functions -- functions
@ -44,7 +42,7 @@ LANGUAGE C;
/* /*
* DDL trigger that removes entry from pg_ivm_immv * DDL trigger that removes entry from pg_ivm_immv
*/ */
CREATE FUNCTION pg_ivm_sql_drop_trigger_func() CREATE FUNCTION pg_catalog.pg_ivm_sql_drop_trigger_func()
RETURNS event_trigger AS $$ RETURNS event_trigger AS $$
DECLARE DECLARE
pg_class_oid OID; pg_class_oid OID;

0
pg_ivm--1.10--1.11.sql Normal file
View file

60
pg_ivm--1.10.sql Normal file
View file

@ -0,0 +1,60 @@
CREATE SCHEMA pgivm;
-- catalog
CREATE TABLE pgivm.pg_ivm_immv(
immvrelid regclass NOT NULL,
viewdef text NOT NULL,
ispopulated bool NOT NULL,
lastivmupdate xid8,
CONSTRAINT pg_ivm_immv_pkey PRIMARY KEY (immvrelid)
);
SELECT pg_catalog.pg_extension_config_dump('pgivm.pg_ivm_immv', '');
-- functions
CREATE FUNCTION pgivm.create_immv(text, text)
RETURNS bigint
STRICT
AS 'MODULE_PATHNAME', 'create_immv'
LANGUAGE C;
CREATE FUNCTION pgivm.refresh_immv(text, bool)
RETURNS bigint
STRICT
AS 'MODULE_PATHNAME', 'refresh_immv'
LANGUAGE C;
CREATE FUNCTION pgivm.get_immv_def(IN immvrelid regclass)
RETURNS text
STRICT
AS 'MODULE_PATHNAME', 'get_immv_def'
LANGUAGE C;
CREATE FUNCTION pgivm.ivm_visible_in_prestate(oid, tid, oid)
RETURNS bool
STABLE
AS 'MODULE_PATHNAME', 'ivm_visible_in_prestate'
LANGUAGE C;
-- trigger functions
CREATE FUNCTION pgivm."IVM_immediate_before"()
RETURNS trigger
AS 'MODULE_PATHNAME', 'IVM_immediate_before'
LANGUAGE C;
CREATE FUNCTION pgivm."IVM_immediate_maintenance"()
RETURNS trigger
AS 'MODULE_PATHNAME', 'IVM_immediate_maintenance'
LANGUAGE C;
CREATE FUNCTION pgivm."IVM_prevent_immv_change"()
RETURNS trigger
AS 'MODULE_PATHNAME', 'IVM_prevent_immv_change'
LANGUAGE C;
GRANT SELECT ON TABLE pgivm.pg_ivm_immv TO PUBLIC;
GRANT USAGE ON SCHEMA pgivm TO PUBLIC;

0
pg_ivm--1.11--1.12.sql Normal file
View file

0
pg_ivm--1.5--1.6.sql Normal file
View file

0
pg_ivm--1.6--1.7.sql Normal file
View file

0
pg_ivm--1.7--1.8.sql Normal file
View file

0
pg_ivm--1.8--1.9.sql Normal file
View file

18
pg_ivm--1.9--1.10.sql Normal file
View file

@ -0,0 +1,18 @@
-- create a new schema pgivm and change the objects' schema to it
CREATE SCHEMA pgivm;
ALTER TABLE pg_ivm_immv SET SCHEMA pgivm;
ALTER FUNCTION create_immv(text, text) SET SCHEMA pgivm;
ALTER FUNCTION refresh_immv(text, bool) SET SCHEMA pgivm;
ALTER FUNCTION get_immv_def(regclass) SET SCHEMA pgivm;
ALTER FUNCTION ivm_visible_in_prestate(oid, tid, oid) SET SCHEMA pgivm;
ALTER FUNCTION "IVM_immediate_before"() SET SCHEMA pgivm;
ALTER FUNCTION "IVM_immediate_maintenance"() SET SCHEMA pgivm;
ALTER FUNCTION "IVM_prevent_immv_change"() SET SCHEMA pgivm;
GRANT USAGE ON SCHEMA pgivm TO PUBLIC;
ALTER TABLE pgivm.pg_ivm_immv ADD COLUMN lastivmupdate xid8;
-- drop a garbage
DROP SCHEMA __pg_ivm__;

View file

@ -25,6 +25,8 @@
#include "parser/parser.h" #include "parser/parser.h"
#include "parser/scansup.h" #include "parser/scansup.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
#include "nodes/makefuncs.h"
#include "utils/syscache.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
@ -36,8 +38,6 @@
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
static Oid pg_ivm_immv_id = InvalidOid;
static Oid pg_ivm_immv_pkey_id = InvalidOid;
static object_access_hook_type PrevObjectAccessHook = NULL; static object_access_hook_type PrevObjectAccessHook = NULL;
@ -63,8 +63,10 @@ PG_FUNCTION_INFO_V1(get_immv_def);
static void static void
IvmXactCallback(XactEvent event, void *arg) IvmXactCallback(XactEvent event, void *arg)
{ {
if (event == XACT_EVENT_ABORT) if (event == XACT_EVENT_PRE_COMMIT)
AtAbort_IVM(); AtPreCommit_IVM();
else if (event == XACT_EVENT_ABORT)
AtAbort_IVM(InvalidSubTransactionId);
} }
static void static void
@ -72,7 +74,7 @@ IvmSubXactCallback(SubXactEvent event, SubTransactionId mySubid,
SubTransactionId parentSubid, void *arg) SubTransactionId parentSubid, void *arg)
{ {
if (event == SUBXACT_EVENT_ABORT_SUB) if (event == SUBXACT_EVENT_ABORT_SUB)
AtAbort_IVM(); AtAbort_IVM(mySubid);
} }
@ -91,7 +93,7 @@ _PG_init(void)
/* /*
* Given a C string, parse it into a qualified relation name * Given a C string, parse it into a qualified relation name
* followed by a optional parenthesized list of column names. * followed by an optional parenthesized list of column names.
*/ */
static void static void
parseNameAndColumns(const char *string, List **names, List **colNames) parseNameAndColumns(const char *string, List **names, List **colNames)
@ -122,7 +124,11 @@ parseNameAndColumns(const char *string, List **names, List **colNames)
/* Separate the name and parse it into a list */ /* Separate the name and parse it into a list */
*ptr++ = '\0'; *ptr++ = '\0';
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 160000)
*names = stringToQualifiedNameList(rawname, NULL);
#else
*names = stringToQualifiedNameList(rawname); *names = stringToQualifiedNameList(rawname);
#endif
if (!has_colnames) if (!has_colnames)
goto end; goto end;
@ -197,7 +203,7 @@ create_immv(PG_FUNCTION_ARGS)
parsetree = linitial_node(RawStmt, parsetree_list); parsetree = linitial_node(RawStmt, parsetree_list);
/* view definition should spcify SELECT query */ /* view definition should specify SELECT query */
if (!IsA(parsetree->stmt, SelectStmt)) if (!IsA(parsetree->stmt, SelectStmt))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@ -218,13 +224,17 @@ create_immv(PG_FUNCTION_ARGS)
ctas->into->options = NIL; ctas->into->options = NIL;
ctas->into->onCommit = ONCOMMIT_NOOP; ctas->into->onCommit = ONCOMMIT_NOOP;
ctas->into->tableSpaceName = NULL; ctas->into->tableSpaceName = NULL;
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 180000)
ctas->into->viewQuery = (Query *) parsetree->stmt;
#else
ctas->into->viewQuery = parsetree->stmt; ctas->into->viewQuery = parsetree->stmt;
#endif
ctas->into->skipData = false; ctas->into->skipData = false;
query = transformStmt(pstate, (Node *) ctas); query = transformStmt(pstate, (Node *) ctas);
Assert(query->commandType == CMD_UTILITY && IsA(query->utilityStmt, CreateTableAsStmt)); Assert(query->commandType == CMD_UTILITY && IsA(query->utilityStmt, CreateTableAsStmt));
ExecCreateImmv(pstate, (CreateTableAsStmt *) query->utilityStmt, NULL, NULL, &qc); ExecCreateImmv(pstate, (CreateTableAsStmt *) query->utilityStmt, &qc);
PG_RETURN_INT64(qc.nprocessed); PG_RETURN_INT64(qc.nprocessed);
} }
@ -293,7 +303,7 @@ CreateChangePreventTrigger(Oid matviewOid)
ivm_trigger->timing = TRIGGER_TYPE_BEFORE; ivm_trigger->timing = TRIGGER_TYPE_BEFORE;
ivm_trigger->trigname = "IVM_prevent_immv_change"; ivm_trigger->trigname = "IVM_prevent_immv_change";
ivm_trigger->funcname = SystemFuncName("IVM_prevent_immv_change"); ivm_trigger->funcname = PgIvmFuncName("IVM_prevent_immv_change");
ivm_trigger->columns = NIL; ivm_trigger->columns = NIL;
ivm_trigger->transitionRels = NIL; ivm_trigger->transitionRels = NIL;
ivm_trigger->whenClause = NULL; ivm_trigger->whenClause = NULL;
@ -322,10 +332,9 @@ CreateChangePreventTrigger(Oid matviewOid)
Oid Oid
PgIvmImmvRelationId(void) PgIvmImmvRelationId(void)
{ {
if (!OidIsValid(pg_ivm_immv_id)) return RangeVarGetRelid(
pg_ivm_immv_id = get_relname_relid("pg_ivm_immv", PG_CATALOG_NAMESPACE); makeRangeVar("pgivm", "pg_ivm_immv", -1),
AccessShareLock, true);
return pg_ivm_immv_id;
} }
/* /*
@ -334,14 +343,13 @@ PgIvmImmvRelationId(void)
Oid Oid
PgIvmImmvPrimaryKeyIndexId(void) PgIvmImmvPrimaryKeyIndexId(void)
{ {
if (!OidIsValid(pg_ivm_immv_pkey_id)) return RangeVarGetRelid(
pg_ivm_immv_pkey_id = get_relname_relid("pg_ivm_immv_pkey", PG_CATALOG_NAMESPACE); makeRangeVar("pgivm", "pg_ivm_immv_pkey", -1),
AccessShareLock, true);
return pg_ivm_immv_pkey_id;
} }
/* /*
* Return the SELECT part of a IMMV * Return the SELECT part of an IMMV
*/ */
Datum Datum
get_immv_def(PG_FUNCTION_ARGS) get_immv_def(PG_FUNCTION_ARGS)
@ -381,16 +389,29 @@ PgIvmObjectAccessHook(ObjectAccessType access, Oid classId,
if (access == OAT_DROP && classId == RelationRelationId && !OidIsValid(subId)) if (access == OAT_DROP && classId == RelationRelationId && !OidIsValid(subId))
{ {
Relation pgIvmImmv = table_open(PgIvmImmvRelationId(), AccessShareLock); Relation pgIvmImmv;
SysScanDesc scan; SysScanDesc scan;
ScanKeyData key; ScanKeyData key;
HeapTuple tup; HeapTuple tup;
Oid pgIvmImmvOid = PgIvmImmvRelationId();
Oid pgIvmImmvPkOid = PgIvmImmvPrimaryKeyIndexId();
/*
* Index or table not yet created (so no IMMVs yet), already dropped
* (expect IMMVs also gone soon), or renamed. It's not great that a
* rename of either object will silently break IMMVs, but that's
* better than ERROR below.
*/
if (pgIvmImmvPkOid == InvalidOid || pgIvmImmvOid == InvalidOid)
return;
pgIvmImmv = table_open(pgIvmImmvOid, AccessShareLock);
ScanKeyInit(&key, ScanKeyInit(&key,
Anum_pg_ivm_immv_immvrelid, Anum_pg_ivm_immv_immvrelid,
BTEqualStrategyNumber, F_OIDEQ, BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId)); ObjectIdGetDatum(objectId));
scan = systable_beginscan(pgIvmImmv, PgIvmImmvPrimaryKeyIndexId(), scan = systable_beginscan(pgIvmImmv, pgIvmImmvPkOid,
true, NULL, 1, &key); true, NULL, 1, &key);
tup = systable_getnext(scan); tup = systable_getnext(scan);
@ -406,7 +427,7 @@ PgIvmObjectAccessHook(ObjectAccessType access, Oid classId,
/* /*
* isImmv * isImmv
* *
* Check if this is a IMMV from oid. * Check if this is an IMMV from oid.
*/ */
bool bool
isImmv(Oid immv_oid) isImmv(Oid immv_oid)
@ -432,3 +453,12 @@ isImmv(Oid immv_oid)
else else
return true; return true;
} }
/* PgIvmFuncName()
* Build a properly-qualified reference to a pg_ivm internal function.
*/
List *
PgIvmFuncName(char *name)
{
return list_make2(makeString("pgivm"), makeString(name));
}

View file

@ -1,6 +1,6 @@
# incremental view maintenance extension_ # incremental view maintenance extension
comment = 'incremental view maintenance on PostgreSQL' comment = 'incremental view maintenance on PostgreSQL'
default_version = '1.5' default_version = '1.12'
module_pathname = '$libdir/pg_ivm' module_pathname = '$libdir/pg_ivm'
relocatable = false relocatable = false
schema = pg_catalog schema = pg_catalog

View file

@ -20,11 +20,12 @@
#include "tcop/dest.h" #include "tcop/dest.h"
#include "utils/queryenvironment.h" #include "utils/queryenvironment.h"
#define Natts_pg_ivm_immv 3 #define Natts_pg_ivm_immv 4
#define Anum_pg_ivm_immv_immvrelid 1 #define Anum_pg_ivm_immv_immvrelid 1
#define Anum_pg_ivm_immv_viewdef 2 #define Anum_pg_ivm_immv_viewdef 2
#define Anum_pg_ivm_immv_ispopulated 3 #define Anum_pg_ivm_immv_ispopulated 3
#define Anum_pg_ivm_immv_lastivmupdate 4
/* pg_ivm.c */ /* pg_ivm.c */
@ -32,14 +33,14 @@ extern void CreateChangePreventTrigger(Oid matviewOid);
extern Oid PgIvmImmvRelationId(void); extern Oid PgIvmImmvRelationId(void);
extern Oid PgIvmImmvPrimaryKeyIndexId(void); extern Oid PgIvmImmvPrimaryKeyIndexId(void);
extern bool isImmv(Oid immv_oid); extern bool isImmv(Oid immv_oid);
extern List *PgIvmFuncName(char *name);
/* createas.c */ /* createas.c */
extern ObjectAddress ExecCreateImmv(ParseState *pstate, CreateTableAsStmt *stmt, extern ObjectAddress ExecCreateImmv(ParseState *pstate, CreateTableAsStmt *stmt,
ParamListInfo params, QueryEnvironment *queryEnv,
QueryCompletion *qc); QueryCompletion *qc);
extern void CreateIvmTriggersOnBaseTables(Query *qry, Oid matviewOid, bool is_create); extern void CreateIvmTriggersOnBaseTables(Query *qry, Oid matviewOid);
extern void CreateIndexOnIMMV(Query *query, Relation matviewRel, bool is_create); extern void CreateIndexOnIMMV(Query *query, Relation matviewRel);
extern Query *rewriteQueryForIMMV(Query *query, List *colNames); extern Query *rewriteQueryForIMMV(Query *query, List *colNames);
extern void makeIvmAggColumn(ParseState *pstate, Aggref *aggref, char *resname, AttrNumber *next_resno, List **aggs); extern void makeIvmAggColumn(ParseState *pstate, Aggref *aggref, char *resname, AttrNumber *next_resno, List **aggs);
@ -48,12 +49,16 @@ extern void makeIvmAggColumn(ParseState *pstate, Aggref *aggref, char *resname,
extern Query *get_immv_query(Relation matviewRel); extern Query *get_immv_query(Relation matviewRel);
extern ObjectAddress ExecRefreshImmv(const RangeVar *relation, bool skipData, extern ObjectAddress ExecRefreshImmv(const RangeVar *relation, bool skipData,
const char *queryString, QueryCompletion *qc); const char *queryString, QueryCompletion *qc);
extern ObjectAddress RefreshImmvByOid(Oid matviewOid, bool is_create, bool skipData,
const char *queryString, QueryCompletion *qc);
extern bool ImmvIncrementalMaintenanceIsEnabled(void); extern bool ImmvIncrementalMaintenanceIsEnabled(void);
extern Query *get_immv_query(Relation matviewRel); extern PGDLLEXPORT Datum IVM_immediate_before(PG_FUNCTION_ARGS);
extern Datum IVM_immediate_before(PG_FUNCTION_ARGS); extern PGDLLEXPORT Datum IVM_immediate_maintenance(PG_FUNCTION_ARGS);
extern Datum IVM_immediate_maintenance(PG_FUNCTION_ARGS); extern Query* rewrite_query_for_exists_subquery(Query *query);
extern Datum ivm_visible_in_prestate(PG_FUNCTION_ARGS); extern PGDLLEXPORT Datum ivm_visible_in_prestate(PG_FUNCTION_ARGS);
extern void AtAbort_IVM(void); extern void AtAbort_IVM(SubTransactionId subtxid);
extern void AtPreCommit_IVM(void);
extern char *getColumnNameStartWith(RangeTblEntry *rte, char *str, int *attnum);
extern bool isIvmName(const char *s); extern bool isIvmName(const char *s);
/* ruleutils.c */ /* ruleutils.c */

View file

@ -1,6 +1,6 @@
# How to build RPM: # How to build RPM:
# #
# rpmbuild -bb pg_ivm.spec --define "pgmajorversion 14" --define "pginstdir /usr/pgsql-14" # rpmbuild -bb pg_ivm.spec --define "pgmajorversion 18" --define "pginstdir /usr/pgsql-18"
%global sname pg_ivm %global sname pg_ivm
@ -8,11 +8,11 @@
%global llvm 1 %global llvm 1
%endif %endif
Summary: PostgreSQL-based distributed RDBMS Summary: Incremental View Maintenance (IVM) feature for PostgreSQL.
Name: %{sname}_%{pgmajorversion} Name: %{sname}_%{pgmajorversion}
Version: 1.5.1 Version: 1.12
Release: 1%{dist} Release: 1%{dist}
License: BSD License: PostgreSQL
Vendor: IVM Development Group Vendor: IVM Development Group
URL: https://github.com/sraoss/%{sname} URL: https://github.com/sraoss/%{sname}
Source0: https://github.com/sraoss/%{sname}/archive/v%{version}.tar.gz Source0: https://github.com/sraoss/%{sname}/archive/v%{version}.tar.gz
@ -55,6 +55,20 @@ PATH=%{pginstdir}/bin:$PATH %{__make} %{?_smp_mflags} INSTALL_PREFIX=%{buildroot
%endif %endif
%changelog %changelog
* Mon Sep 4 2025 - Yugo Nagata <nagata@sraoss.co.jp> 1.12-1
- Update to 1.12
* Mon May 25 2025 - Yugo Nagata <nagata@sraoss.co.jp> 1.11-1
- Update to 1.11
* Tue Mar 11 2025 - Yugo Nagata <nagata@sraoss.co.jp> 1.10-1
- Update to 1.10
* Fri Jul 31 2024 - Yugo Nagata <nagata@sraoss.co.jp> 1.9-1
- Update to 1.9
* Fri Mar 1 2024 - Yugo Nagata <nagata@sraoss.co.jp> 1.8-1
- Update to 1.8
* Thu Sep 13 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.7-1
- Update to 1.7
* Thu Aug 31 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.6-1
- Update to 1.6
* Thu Mar 2 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.5.1-1 * Thu Mar 2 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.5.1-1
- Update to 1.5.1 - Update to 1.5.1
* Mon Jun 30 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.5-1 * Mon Jun 30 2023 - Yugo Nagata <nagata@sraoss.co.jp> 1.5-1

47
specs/create_insert.spec Normal file
View file

@ -0,0 +1,47 @@
# Test interaction between concurrent transactions performing
# create_immv and insert in READ COMMITTED isolation level
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s1 { SELECT; }
step create {
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 { SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 create s2 insert c1 check2 c2 mv
permutation s1 create s2 c1 insert check2 c2 mv
permutation s1 s2 create insert c1 check2 c2 mv
permutation s1 s2 insert create c2 check1 c1 mv
permutation s1 s2 create c1 insert check2 c2 mv
permutation s2 insert s1 create c2 check1 c1 mv
permutation s2 insert s1 c2 create check1 c1 mv
permutation s2 s1 insert c2 create check1 c1 mv

54
specs/create_insert2.spec Normal file
View file

@ -0,0 +1,54 @@
# Test interaction between concurrent transactions performing
# create_immv and insert in REPEATABLE READ isolation level
#
# Note:
# In this isolation level, it is possible that create_immv could
# create an inconsistent view not including effects of a concurrent
# transaction. So, an warning message is raised to suggest using it
# in READ COMMITTED or executing refresh_immv to make sure to
# make the view contents consistent.
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s1 { SELECT; }
step create {
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 {SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 create s2 insert c1 check2 c2 mv
permutation s1 create s2 c1 insert check2 c2 mv
permutation s1 s2 create insert c1 check2 c2 mv
permutation s1 s2 insert create c2 check1 c1 mv
permutation s1 s2 create c1 insert check2 c2 mv
permutation s2 insert s1 create c2 check1 c1 mv
permutation s2 insert s1 c2 create check1 c1 mv
permutation s2 s1 insert c2 create check1 c1 mv

54
specs/create_insert3.spec Normal file
View file

@ -0,0 +1,54 @@
# Test interaction between concurrent transactions performing
# create_immv and insert in SERIALIZABLE isolation level
#
# Note:
# In this isolation level, it is possible that create_immv could
# create an inconsistent view not including effects of a concurrent
# transaction. So, an warning message is raised to suggest using it
# in READ COMMITTED or executing refresh_immv to make sure to
# make the view contents consistent.
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s1 { SELECT; }
step create {
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 {SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 create s2 insert c1 check2 c2 mv
permutation s1 create s2 c1 insert check2 c2 mv
permutation s1 s2 create insert c1 check2 c2 mv
permutation s1 s2 insert create c2 check1 c1 mv
permutation s1 s2 create c1 insert check2 c2 mv
permutation s2 insert s1 create c2 check1 c1 mv
permutation s2 insert s1 c2 create check1 c1 mv
permutation s2 s1 insert c2 create check1 c1 mv

62
specs/insert_insert.spec Normal file
View file

@ -0,0 +1,62 @@
# Test interaction between concurrent transactions performing
# table modifications in READ COMMITTED isolation level
setup
{
CREATE TABLE a (i int, j int);
CREATE TABLE b (i int, j int);
INSERT INTO a VALUES (1,10);
INSERT INTO b VALUES (1,100);
SELECT pgivm.create_immv('mv(x,y,z)',
'SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i');
CREATE VIEW v(x,y,z) AS
SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
DROP TABLE b;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s1 { SELECT; }
step insert1 { INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200); }
step update1 { UPDATE a SET j = 11 WHERE i = 1; }
step check1 { SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s2 { SELECT; }
step insert2 { INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201); }
step update2 { UPDATE b SET j = 111 WHERE i = 1; }
step check2 { SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 update1 s2 update2 c1 check2 c2 mv
permutation s1 update1 s2 c1 update2 check2 c2 mv
permutation s1 s2 update1 update2 c1 check2 c2 mv
permutation s1 s2 update2 update1 c2 check1 c1 mv
permutation s1 s2 update1 c1 update2 check2 c2 mv
permutation s2 update2 s1 update1 c2 check1 c1 mv
permutation s2 update2 s1 c2 update1 check1 c1 mv
permutation s2 s1 update2 c2 update1 check1 c1 mv
permutation s1 insert1 s2 insert2 c1 check2 c2 mv
permutation s1 insert1 s2 c1 insert2 check2 c2 mv
permutation s1 s2 insert1 insert2 c1 check2 c2 mv
permutation s1 s2 insert2 insert1 c2 check1 c1 mv
permutation s1 s2 insert1 c1 insert2 check2 c2 mv
permutation s2 insert2 s1 insert1 c2 check1 c1 mv
permutation s2 insert2 s1 c2 insert1 check1 c1 mv
permutation s2 s1 insert2 c2 insert1 check1 c1 mv

62
specs/insert_insert2.spec Normal file
View file

@ -0,0 +1,62 @@
# Test interaction between concurrent transactions performing
# table modifications in REPEATABLE READ isolation level
setup
{
CREATE TABLE a (i int, j int);
CREATE TABLE b (i int, j int);
INSERT INTO a VALUES (1,10);
INSERT INTO b VALUES (1,100);
SELECT pgivm.create_immv('mv(x,y,z)',
'SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i');
CREATE VIEW v(x,y,z) AS
SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
DROP TABLE b;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s1 { SELECT; }
step insert1 { INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200); }
step update1 { UPDATE a SET j = 11 WHERE i = 1; }
step check1 { SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s2 { SELECT; }
step insert2 { INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201); }
step update2 { UPDATE b SET j = 111 WHERE i = 1; }
step check2 { SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 update1 s2 update2 c1 check2 c2 mv
permutation s1 update1 s2 c1 update2 check2 c2 mv
permutation s1 s2 update1 update2 c1 check2 c2 mv
permutation s1 s2 update2 update1 c2 check1 c1 mv
permutation s1 s2 update1 c1 update2 check2 c2 mv
permutation s2 update2 s1 update1 c2 check1 c1 mv
permutation s2 update2 s1 c2 update1 check1 c1 mv
permutation s2 s1 update2 c2 update1 check1 c1 mv
permutation s1 insert1 s2 insert2 c1 check2 c2 mv
permutation s1 insert1 s2 c1 insert2 check2 c2 mv
permutation s1 s2 insert1 insert2 c1 check2 c2 mv
permutation s1 s2 insert2 insert1 c2 check1 c1 mv
permutation s1 s2 insert1 c1 insert2 check2 c2 mv
permutation s2 insert2 s1 insert1 c2 check1 c1 mv
permutation s2 insert2 s1 c2 insert1 check1 c1 mv
permutation s2 s1 insert2 c2 insert1 check1 c1 mv

62
specs/insert_insert3.spec Normal file
View file

@ -0,0 +1,62 @@
# Test interaction between concurrent transactions performing
# table modifications in SERIALIZABLE isolation level
setup
{
CREATE TABLE a (i int, j int);
CREATE TABLE b (i int, j int);
INSERT INTO a VALUES (1,10);
INSERT INTO b VALUES (1,100);
SELECT pgivm.create_immv('mv(x,y,z)',
'SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i');
CREATE VIEW v(x,y,z) AS
SELECT a1.j, a2.j,b.j FROM a AS a1, a AS a2,b WHERE a1.i = a2.i AND a1.i = b.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
DROP TABLE b;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s1 { SELECT; }
step insert1 { INSERT INTO a VALUES (2,20); INSERT INTO b VALUES (2,200); }
step update1 { UPDATE a SET j = 11 WHERE i = 1; }
step check1 { SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2,3; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s2 { SELECT; }
step insert2 { INSERT INTO a VALUES (1,11), (2,21); INSERT INTO b VALUES (2,201); }
step update2 { UPDATE b SET j = 111 WHERE i = 1; }
step check2 { SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 update1 s2 update2 c1 check2 c2 mv
permutation s1 update1 s2 c1 update2 check2 c2 mv
permutation s1 s2 update1 update2 c1 check2 c2 mv
permutation s1 s2 update2 update1 c2 check1 c1 mv
permutation s1 s2 update1 c1 update2 check2 c2 mv
permutation s2 update2 s1 update1 c2 check1 c1 mv
permutation s2 update2 s1 c2 update1 check1 c1 mv
permutation s2 s1 update2 c2 update1 check1 c1 mv
permutation s1 insert1 s2 insert2 c1 check2 c2 mv
permutation s1 insert1 s2 c1 insert2 check2 c2 mv
permutation s1 s2 insert1 insert2 c1 check2 c2 mv
permutation s1 s2 insert2 insert1 c2 check1 c1 mv
permutation s1 s2 insert1 c1 insert2 check2 c2 mv
permutation s2 insert2 s1 insert1 c2 check1 c1 mv
permutation s2 insert2 s1 c2 insert1 check1 c1 mv
permutation s2 s1 insert2 c2 insert1 check1 c1 mv

46
specs/refresh_insert.spec Normal file
View file

@ -0,0 +1,46 @@
# Test interaction between concurrent transactions performing
# refresh_immv and insert in READ COMMITTED isolation level
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s1 { SELECT; }
step refresh { SELECT pgivm.refresh_immv('mv', true); }
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 {SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 refresh s2 insert c1 check2 c2 mv
permutation s1 refresh s2 c1 insert check2 c2 mv
permutation s1 s2 refresh insert c1 check2 c2 mv
permutation s1 s2 insert refresh c2 check1 c1 mv
permutation s1 s2 refresh c1 insert check2 c2 mv
permutation s2 insert s1 refresh c2 check1 c1 mv
permutation s2 insert s1 c2 refresh check1 c1 mv
permutation s2 s1 insert c2 refresh check1 c1 mv

View file

@ -0,0 +1,46 @@
# Test interaction between concurrent transactions performing
# refresh_immv and insert in REPEATABLE READ isolation level
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s1 { SELECT; }
step refresh { SELECT pgivm.refresh_immv('mv', true); }
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 {SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 refresh s2 insert c1 check2 c2 mv
permutation s1 refresh s2 c1 insert check2 c2 mv
permutation s1 s2 refresh insert c1 check2 c2 mv
permutation s1 s2 insert refresh c2 check1 c1 mv
permutation s1 s2 refresh c1 insert check2 c2 mv
permutation s2 insert s1 refresh c2 check1 c1 mv
permutation s2 insert s1 c2 refresh check1 c1 mv
permutation s2 s1 insert c2 refresh check1 c1 mv

View file

@ -0,0 +1,46 @@
# Test interaction between concurrent transactions performing
# refresh_immv and insert in SERIALIZABLE isolation level
setup
{
CREATE TABLE a (i int);
INSERT INTO a VALUES (1);
SELECT pgivm.create_immv('mv(x,y)', 'SELECT * FROM a a1, a a2 WHERE a1.i = a2.i');
CREATE VIEW v(x,y) AS SELECT * FROM a AS a1, a AS a2 WHERE a1.i = a2.i;
CREATE FUNCTION check_mv() RETURNS text AS
$$ SELECT CASE WHEN count(*) = 0 THEN 'ok' ELSE 'ng' END
FROM ((SELECT * FROM mv EXCEPT ALL SELECT * FROM v) UNION ALL
(SELECT * FROM v EXCEPT ALL SELECT * FROM mv)) v $$ LANGUAGE sql;
}
teardown
{
DROP FUNCTION check_mv();
DROP TABLE mv;
DROP VIEW v;
DROP TABLE a;
}
session tx1
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s1 { SELECT; }
step refresh { SELECT pgivm.refresh_immv('mv', true); }
step check1 {SELECT check_mv();}
step c1 { COMMIT; }
step mv { SELECT * FROM mv ORDER BY 1,2; SELECT check_mv(); }
session tx2
setup { BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; }
step s2 { SELECT; }
step insert { INSERT INTO a VALUES (2); }
step check2 {SELECT check_mv(); }
step c2 { COMMIT; }
permutation s1 refresh s2 insert c1 check2 c2 mv
permutation s1 refresh s2 c1 insert check2 c2 mv
permutation s1 s2 refresh insert c1 check2 c2 mv
permutation s1 s2 insert refresh c2 check1 c1 mv
permutation s1 s2 refresh c1 insert check2 c2 mv
permutation s2 insert s1 refresh c2 check1 c1 mv
permutation s2 insert s1 c2 refresh check1 c1 mv
permutation s2 s1 insert c2 refresh check1 c1 mv

View file

@ -1,30 +1,30 @@
CREATE TABLE t (i int PRIMARY KEY); CREATE TABLE t (i int PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 100); INSERT INTO t SELECT generate_series(1, 100);
SELECT create_immv('mv', 'SELECT * FROM t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM t');
SELECT create_immv(' mv2 ( x ) ', 'SELECT * FROM t WHERE i%2 = 0'); SELECT pgivm.create_immv(' mv2 ( x ) ', 'SELECT * FROM t WHERE i%2 = 0');
SELECT create_immv('mv3', 'WITH d AS (DELETE FROM t RETURNING NULL) SELECT * FROM t'); SELECT pgivm.create_immv('mv3', 'WITH d AS (DELETE FROM t RETURNING NULL) SELECT * FROM t');
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
-- contain immv -- contain immv
SELECT create_immv('mv_in_immv01', 'SELECT i FROM mv'); SELECT pgivm.create_immv('mv_in_immv01', 'SELECT i FROM mv');
SELECT create_immv('mv_in_immv02', 'SELECT t.i FROM t INNER JOIN mv2 ON t.i = mv2.x'); SELECT pgivm.create_immv('mv_in_immv02', 'SELECT t.i FROM t INNER JOIN mv2 ON t.i = mv2.x');
-- SQL other than SELECT -- SQL other than SELECT
SELECT create_immv('mv_in_create', 'CREATE TABLE in_create(i int)'); SELECT pgivm.create_immv('mv_in_create', 'CREATE TABLE in_create(i int)');
SELECT create_immv('mv_in_insert', 'INSERT INTO t VALUES(10)'); SELECT pgivm.create_immv('mv_in_insert', 'INSERT INTO t VALUES(10)');
SELECT create_immv('mv_in_update', 'UPDATE t SET i = 10'); SELECT pgivm.create_immv('mv_in_update', 'UPDATE t SET i = 10');
SELECT create_immv('mv_in_delete', 'DELETE FROM t'); SELECT pgivm.create_immv('mv_in_delete', 'DELETE FROM t');
SELECT create_immv('mv_in_drop', 'DROP TABLE t'); SELECT pgivm.create_immv('mv_in_drop', 'DROP TABLE t');
DROP TABLE t; DROP TABLE t;
DROP TABLE mv; DROP TABLE mv;
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
DROP TABLE mv2; DROP TABLE mv2;
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
DROP TABLE t; DROP TABLE t;

View file

@ -2,21 +2,28 @@ CREATE EXTENSION pg_ivm;
GRANT ALL ON SCHEMA public TO public; GRANT ALL ON SCHEMA public TO public;
-- create a table to use as a basis for views and materialized views in various combinations -- create a table to use as a basis for views and materialized views in various combinations
CREATE TABLE mv_base_a (i int, j int); CREATE TABLE mv_base_a (x int, i int, y int, j int);
CREATE TABLE mv_base_b (x int, i int, y int, k int);
-- test for base tables with dropped columns
ALTER TABLE mv_base_a DROP COLUMN x;
ALTER TABLE mv_base_a DROP COLUMN y;
ALTER TABLE mv_base_b DROP COLUMN x;
ALTER TABLE mv_base_b DROP COLUMN y;
INSERT INTO mv_base_a VALUES INSERT INTO mv_base_a VALUES
(1,10), (1,10),
(2,20), (2,20),
(3,30), (3,30),
(4,40), (4,40),
(5,50); (5,50);
CREATE TABLE mv_base_b (i int, k int);
INSERT INTO mv_base_b VALUES INSERT INTO mv_base_b VALUES
(1,101), (1,101),
(2,102), (2,102),
(3,103), (3,103),
(4,104); (4,104);
SELECT create_immv('mv_ivm_1', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i)'); SELECT pgivm.create_immv('mv_ivm_1', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i)');
SELECT * FROM mv_ivm_1 ORDER BY 1,2,3; SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
-- immediate maintenance -- immediate maintenance
@ -30,6 +37,14 @@ SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
ROLLBACK; ROLLBACK;
SELECT * FROM mv_ivm_1 ORDER BY 1,2,3; SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
-- test for renaming column name to camel style
BEGIN;
ALTER TABLE mv_base_a RENAME i TO "I";
ALTER TABLE mv_base_a RENAME j TO "J";
UPDATE mv_base_a SET "J" = 0 WHERE "I" = 1;
SELECT * FROM mv_ivm_1 ORDER BY 1,2,3;
ROLLBACK;
-- TRUNCATE a base table in join views -- TRUNCATE a base table in join views
BEGIN; BEGIN;
TRUNCATE mv_base_a; TRUNCATE mv_base_a;
@ -45,14 +60,14 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE FUNCTION ivm_func() RETURNS int LANGUAGE 'sql' CREATE FUNCTION ivm_func() RETURNS int LANGUAGE 'sql'
AS 'SELECT 1' IMMUTABLE; AS 'SELECT 1' IMMUTABLE;
SELECT create_immv('mv_ivm_func', 'SELECT * FROM ivm_func()'); SELECT pgivm.create_immv('mv_ivm_func', 'SELECT * FROM ivm_func()');
SELECT create_immv('mv_ivm_no_tbl', 'SELECT 1'); SELECT pgivm.create_immv('mv_ivm_no_tbl', 'SELECT 1');
ROLLBACK; ROLLBACK;
-- result of materialized view have DISTINCT clause or the duplicate result. -- result of materialized view have DISTINCT clause or the duplicate result.
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_duplicate', 'SELECT j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_duplicate', 'SELECT j FROM mv_base_a');
SELECT create_immv('mv_ivm_distinct', 'SELECT DISTINCT j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_distinct', 'SELECT DISTINCT j FROM mv_base_a');
INSERT INTO mv_base_a VALUES(6,20); INSERT INTO mv_base_a VALUES(6,20);
SELECT * FROM mv_ivm_duplicate ORDER BY 1; SELECT * FROM mv_ivm_duplicate ORDER BY 1;
SELECT * FROM mv_ivm_distinct ORDER BY 1; SELECT * FROM mv_ivm_distinct ORDER BY 1;
@ -63,7 +78,7 @@ ROLLBACK;
-- support SUM(), COUNT() and AVG() aggregate functions -- support SUM(), COUNT() and AVG() aggregate functions
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(i), AVG(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(i), AVG(j) FROM mv_base_a GROUP BY i');
SELECT * FROM mv_ivm_agg ORDER BY 1,2,3,4; SELECT * FROM mv_ivm_agg ORDER BY 1,2,3,4;
INSERT INTO mv_base_a VALUES(2,100); INSERT INTO mv_base_a VALUES(2,100);
SELECT * FROM mv_ivm_agg ORDER BY 1,2,3,4; SELECT * FROM mv_ivm_agg ORDER BY 1,2,3,4;
@ -75,7 +90,7 @@ ROLLBACK;
-- support COUNT(*) aggregate function -- support COUNT(*) aggregate function
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i');
SELECT * FROM mv_ivm_agg ORDER BY 1,2,3; SELECT * FROM mv_ivm_agg ORDER BY 1,2,3;
INSERT INTO mv_base_a VALUES(2,100); INSERT INTO mv_base_a VALUES(2,100);
SELECT * FROM mv_ivm_agg ORDER BY 1,2,3; SELECT * FROM mv_ivm_agg ORDER BY 1,2,3;
@ -83,7 +98,7 @@ ROLLBACK;
-- TRUNCATE a base table in aggregate views -- TRUNCATE a base table in aggregate views
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg', 'SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i');
TRUNCATE mv_base_a; TRUNCATE mv_base_a;
SELECT sum, count FROM mv_ivm_agg; SELECT sum, count FROM mv_ivm_agg;
SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i; SELECT i, SUM(j), COUNT(*) FROM mv_base_a GROUP BY i;
@ -91,7 +106,7 @@ ROLLBACK;
-- support aggregate functions without GROUP clause -- support aggregate functions without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a');
SELECT * FROM mv_ivm_group ORDER BY 1; SELECT * FROM mv_ivm_group ORDER BY 1;
INSERT INTO mv_base_a VALUES(6,60); INSERT INTO mv_base_a VALUES(6,60);
SELECT * FROM mv_ivm_group ORDER BY 1; SELECT * FROM mv_ivm_group ORDER BY 1;
@ -101,7 +116,7 @@ ROLLBACK;
-- TRUNCATE a base table in aggregate views without GROUP clause -- TRUNCATE a base table in aggregate views without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_group', 'SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a');
TRUNCATE mv_base_a; TRUNCATE mv_base_a;
SELECT sum, count, avg FROM mv_ivm_group; SELECT sum, count, avg FROM mv_ivm_group;
SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a; SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a;
@ -109,7 +124,7 @@ ROLLBACK;
-- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect. -- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect.
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_avg_bug', 'SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_avg_bug', 'SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i');
SELECT * FROM mv_ivm_avg_bug ORDER BY 1,2,3; SELECT * FROM mv_ivm_avg_bug ORDER BY 1,2,3;
INSERT INTO mv_base_a VALUES INSERT INTO mv_base_a VALUES
(1,0), (1,0),
@ -124,7 +139,7 @@ ROLLBACK;
-- support MIN(), MAX() aggregate functions -- support MIN(), MAX() aggregate functions
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min_max(i, min_j, max_j)', 'SELECT i, MIN(j), MAX(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_min_max(i, min_j, max_j)', 'SELECT i, MIN(j), MAX(j) FROM mv_base_a GROUP BY i');
SELECT * FROM mv_ivm_min_max ORDER BY 1,2,3; SELECT * FROM mv_ivm_min_max ORDER BY 1,2,3;
INSERT INTO mv_base_a VALUES INSERT INTO mv_base_a VALUES
(1,11), (1,12), (1,11), (1,12),
@ -139,7 +154,7 @@ ROLLBACK;
-- support MIN(), MAX() aggregate functions without GROUP clause -- support MIN(), MAX() aggregate functions without GROUP clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min_max(min_j, max_j)', 'SELECT MIN(j), MAX(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_min_max(min_j, max_j)', 'SELECT MIN(j), MAX(j) FROM mv_base_a');
SELECT * FROM mv_ivm_min_max; SELECT * FROM mv_ivm_min_max;
INSERT INTO mv_base_a VALUES INSERT INTO mv_base_a VALUES
(0,0), (6,60), (7,70); (0,0), (6,60), (7,70);
@ -152,7 +167,7 @@ ROLLBACK;
-- Test MIN/MAX after search_path change -- Test MIN/MAX after search_path change
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_min', 'SELECT MIN(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm_min', 'SELECT MIN(j) FROM mv_base_a');
SELECT * FROM mv_ivm_min ORDER BY 1,2,3; SELECT * FROM mv_ivm_min ORDER BY 1,2,3;
CREATE SCHEMA myschema; CREATE SCHEMA myschema;
@ -170,28 +185,28 @@ ROLLBACK;
-- aggregate views with column names specified -- aggregate views with column names specified
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
INSERT INTO mv_base_a VALUES (1,100), (2,200), (3,300); INSERT INTO mv_base_a VALUES (1,100), (2,200), (3,300);
UPDATE mv_base_a SET j = 2000 WHERE (i,j) = (2,20); UPDATE mv_base_a SET j = 2000 WHERE (i,j) = (2,20);
DELETE FROM mv_base_a WHERE (i,j) = (3,30); DELETE FROM mv_base_a WHERE (i,j) = (3,30);
SELECT * FROM mv_ivm_agg ORDER BY 1,2; SELECT * FROM mv_ivm_agg ORDER BY 1,2;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a,b)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a,b)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
INSERT INTO mv_base_a VALUES (1,100), (2,200), (3,300); INSERT INTO mv_base_a VALUES (1,100), (2,200), (3,300);
UPDATE mv_base_a SET j = 2000 WHERE (i,j) = (2,20); UPDATE mv_base_a SET j = 2000 WHERE (i,j) = (2,20);
DELETE FROM mv_base_a WHERE (i,j) = (3,30); DELETE FROM mv_base_a WHERE (i,j) = (3,30);
SELECT * FROM mv_ivm_agg ORDER BY 1,2; SELECT * FROM mv_ivm_agg ORDER BY 1,2;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_agg(a,b,c)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i'); SELECT pgivm.create_immv('mv_ivm_agg(a,b,c)', 'SELECT i, SUM(j) FROM mv_base_a GROUP BY i');
ROLLBACK; ROLLBACK;
-- support self join view and multiple change on the same table -- support self join view and multiple change on the same table
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30);
SELECT create_immv('mv_self(v1, v2)', SELECT pgivm.create_immv('mv_self(v1, v2)',
'SELECT t1.v, t2.v FROM base_t AS t1 JOIN base_t AS t2 ON t1.i = t2.i'); 'SELECT t1.v, t2.v FROM base_t AS t1 JOIN base_t AS t2 ON t1.i = t2.i');
SELECT * FROM mv_self ORDER BY v1; SELECT * FROM mv_self ORDER BY v1;
INSERT INTO base_t VALUES (4,40); INSERT INTO base_t VALUES (4,40);
@ -221,7 +236,7 @@ CREATE TABLE base_r (i int, v int);
CREATE TABLE base_s (i int, v int); CREATE TABLE base_s (i int, v int);
INSERT INTO base_r VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_r VALUES (1, 10), (2, 20), (3, 30);
INSERT INTO base_s VALUES (1, 100), (2, 200), (3, 300); INSERT INTO base_s VALUES (1, 100), (2, 200), (3, 300);
SELECT create_immv('mv(v1, v2)', 'SELECT r.v, s.v FROM base_r AS r JOIN base_s AS s USING(i)');; SELECT pgivm.create_immv('mv(v1, v2)', 'SELECT r.v, s.v FROM base_r AS r JOIN base_s AS s USING(i)');;
SELECT * FROM mv ORDER BY v1; SELECT * FROM mv ORDER BY v1;
WITH WITH
ins_r AS (INSERT INTO base_r VALUES (1,11) RETURNING 1), ins_r AS (INSERT INTO base_r VALUES (1,11) RETURNING 1),
@ -238,33 +253,72 @@ CREATE TABLE ri1 (i int PRIMARY KEY);
CREATE TABLE ri2 (i int PRIMARY KEY REFERENCES ri1(i) ON UPDATE CASCADE ON DELETE CASCADE, v int); CREATE TABLE ri2 (i int PRIMARY KEY REFERENCES ri1(i) ON UPDATE CASCADE ON DELETE CASCADE, v int);
INSERT INTO ri1 VALUES (1),(2),(3); INSERT INTO ri1 VALUES (1),(2),(3);
INSERT INTO ri2 VALUES (1),(2),(3); INSERT INTO ri2 VALUES (1),(2),(3);
SELECT create_immv('mv_ri(i1, i2)', 'SELECT ri1.i, ri2.i FROM ri1 JOIN ri2 USING(i)'); SELECT pgivm.create_immv('mv_ri(i1, i2)', 'SELECT ri1.i, ri2.i FROM ri1 JOIN ri2 USING(i)');
SELECT * FROM mv_ri ORDER BY i1; SELECT * FROM mv_ri ORDER BY i1;
UPDATE ri1 SET i=10 where i=1; UPDATE ri1 SET i=10 where i=1;
DELETE FROM ri1 WHERE i=2; DELETE FROM ri1 WHERE i=2;
SELECT * FROM mv_ri ORDER BY i2; SELECT * FROM mv_ri ORDER BY i2;
ROLLBACK; ROLLBACK;
-- support subquery for using EXISTS()
BEGIN;
SELECT pgivm.create_immv('mv_ivm_exists_subquery', 'SELECT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
SELECT pgivm.create_immv('mv_ivm_exists_subquery2', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) AND a.i > 2');
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
INSERT INTO mv_base_a VALUES(1,10),(6,60),(3,30),(3,300);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
INSERT INTO mv_base_b VALUES(1,101);
INSERT INTO mv_base_b VALUES(1,111);
INSERT INTO mv_base_b VALUES(2,102);
INSERT INTO mv_base_b VALUES(6,106);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
UPDATE mv_base_a SET i = 1 WHERE j =60;
UPDATE mv_base_b SET i = 10 WHERE k = 101;
UPDATE mv_base_b SET k = 1002 WHERE k = 102;
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
DELETE FROM mv_base_a WHERE (i,j) = (1,60);
DELETE FROM mv_base_b WHERE i = 2;
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery2 ORDER BY i, j;
--- EXISTS subquery with tuple duplication and DISTINCT
SELECT pgivm.create_immv('mv_ivm_exists_subquery_distinct', 'SELECT DISTINCT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
DELETE FROM mv_base_b WHERE i = 1 or i = 3;
INSERT INTO mv_base_b VALUES (1,100), (3,300);
SELECT * FROM mv_ivm_exists_subquery ORDER BY i, j;
SELECT * FROM mv_ivm_exists_subquery_distinct ORDER BY i, j;
ROLLBACK;
-- support simple subquery in FROM clause -- support simple subquery in FROM clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a,( SELECT * FROM mv_base_b) b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a,( SELECT * FROM mv_base_b) b WHERE a.i = b.i');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
SELECT * FROM mv_ivm_subquery ORDER BY i,j; SELECT * FROM mv_ivm_subquery ORDER BY i,j;
ROLLBACK; ROLLBACK;
-- disallow non-simple subqueries -- disallow non-simple subqueries
SELECT create_immv('mv_ivm_exists_subquery', 'SELECT a.i, a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) b WHERE a.i = b.i');
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT DISTINCT i FROM mv_base_b) b WHERE a.i = b.i');
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT DISTINCT i FROM mv_base_b) b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )');
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a');
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a');
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v');
SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v'); SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_a a2 WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a2.i = b.i))');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT EXISTS(SELECT 1 from mv_base_b) FROM mv_base_a a');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT false OR EXISTS(SELECT 1 FROM mv_base_a) FROM mv_base_b');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, CASE EXISTS(SELECT 1 FROM mv_base_a) WHEN true THEN 100 ELSE 10 END), mv_base_b');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM mv_base_a a WHERE CASE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) WHEN true THEN false ELSE true END');
SELECT pgivm.create_immv('mv_ivm_subquery', 'SELECT * FROM mv_base_a a WHERE true and CASE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) WHEN true THEN false ELSE true END');
-- support join subquery in FROM clause -- support join subquery in FROM clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp'); SELECT pgivm.create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp');
WITH WITH
ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0), ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0),
bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0), bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0),
@ -275,7 +329,7 @@ ROLLBACK;
BEGIN; BEGIN;
-- nested subquery -- nested subquery
SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN (SELECT * FROM mv_base_a) a USING(i)) tmp'); SELECT pgivm.create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN (SELECT * FROM mv_base_a) a USING(i)) tmp');
WITH WITH
ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0), ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0),
bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0), bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0),
@ -286,7 +340,7 @@ ROLLBACK;
-- support simple CTE -- support simple CTE
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); 'WITH b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
@ -294,7 +348,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i'); 'WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
@ -302,7 +356,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM mv_base_b) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'WITH b AS ( SELECT * FROM mv_base_b) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
@ -310,7 +364,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'SELECT * FROM (WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'SELECT * FROM (WITH a AS (SELECT * FROM mv_base_a), b AS ( SELECT * FROM mv_base_b) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
@ -318,7 +372,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH b AS ( SELECT * FROM (SELECT * FROM mv_base_b) b2) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v'); 'WITH b AS ( SELECT * FROM (SELECT * FROM mv_base_b) b2) SELECT v.i,v.j FROM (WITH a AS (SELECT * FROM mv_base_a) SELECT a.i,a.j FROM a, b WHERE a.i = b.i) v');
INSERT INTO mv_base_a VALUES(2,20); INSERT INTO mv_base_a VALUES(2,20);
INSERT INTO mv_base_b VALUES(3,300); INSERT INTO mv_base_b VALUES(3,300);
@ -326,7 +380,7 @@ SELECT * FROM mv_cte ORDER BY i,j;
ROLLBACK; ROLLBACK;
BEGIN; BEGIN;
SELECT create_immv('mv_cte', SELECT pgivm.create_immv('mv_cte',
'WITH x AS ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) SELECT * FROM x'); 'WITH x AS ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) SELECT * FROM x');
WITH WITH
ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0), ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0),
@ -338,7 +392,7 @@ ROLLBACK;
-- nested CTE -- nested CTE
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_nested_cte', 'WITH v AS ( WITH a AS (SELECT * FROM mv_base_a) SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN a USING(i)) SELECT * FROM v'); SELECT pgivm.create_immv('mv_ivm_nested_cte', 'WITH v AS ( WITH a AS (SELECT * FROM mv_base_a) SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN a USING(i)) SELECT * FROM v');
WITH WITH
ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0), ai AS (INSERT INTO mv_base_a VALUES (1,11),(2,22) RETURNING 0),
bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0), bi AS (INSERT INTO mv_base_b VALUES (1,111),(3,133) RETURNING 0),
@ -351,7 +405,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30); INSERT INTO base_t VALUES (1, 10), (2, 20), (3, 30);
SELECT create_immv('mv_cte_multi(v1, v2)', SELECT pgivm.create_immv('mv_cte_multi(v1, v2)',
'WITH t AS (SELECT * FROM base_t) SELECT t1.v, t2.v FROM t AS t1 JOIN t AS t2 ON t1.i = t2.i'); 'WITH t AS (SELECT * FROM base_t) SELECT t1.v, t2.v FROM t AS t1 JOIN t AS t2 ON t1.i = t2.i');
SELECT * FROM mv_cte_multi ORDER BY v1; SELECT * FROM mv_cte_multi ORDER BY v1;
INSERT INTO base_t VALUES (4,40); INSERT INTO base_t VALUES (4,40);
@ -368,17 +422,18 @@ SELECT * FROM mv_cte_multi ORDER BY v1;
ROLLBACK; ROLLBACK;
--- disallow not-simple CTE --- disallow not-simple CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
SELECT pgivm.create_immv('mv_cte_fail', 'WITH a AS (SELECT i, j FROM mv_base_a) SELECT a.i,a.j FROM a WHERE EXISTS(WITH b AS (SELECT i FROM mv_base_b) SELECT 1 FROM b WHERE a.i = b.i)');
-- unreferenced CTE -- unreferenced CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a'); SELECT pgivm.create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a');
-- views including NULL -- views including NULL
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (1,10),(2, NULL); INSERT INTO base_t VALUES (1,10),(2, NULL);
SELECT create_immv('mv', 'SELECT * FROM base_t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM base_t');
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
UPDATE base_t SET v = 20 WHERE i = 2; UPDATE base_t SET v = 20 WHERE i = 2;
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
@ -386,7 +441,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int); CREATE TABLE base_t (i int);
SELECT create_immv('mv', 'SELECT * FROM base_t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM base_t');
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
INSERT INTO base_t VALUES (1),(NULL); INSERT INTO base_t VALUES (1),(NULL);
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
@ -395,7 +450,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (1, 10), (1, 20); INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (1, 10), (1, 20);
SELECT create_immv('mv', 'SELECT i, sum(v) FROM base_t GROUP BY i'); SELECT pgivm.create_immv('mv', 'SELECT i, sum(v) FROM base_t GROUP BY i');
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
UPDATE base_t SET v = v * 10; UPDATE base_t SET v = v * 10;
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
@ -404,7 +459,7 @@ ROLLBACK;
BEGIN; BEGIN;
CREATE TABLE base_t (i int, v int); CREATE TABLE base_t (i int, v int);
INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4), (NULL, 5); INSERT INTO base_t VALUES (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4), (NULL, 5);
SELECT create_immv('mv', 'SELECT i, min(v), max(v) FROM base_t GROUP BY i'); SELECT pgivm.create_immv('mv', 'SELECT i, min(v), max(v) FROM base_t GROUP BY i');
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
DELETE FROM base_t WHERE v = 1; DELETE FROM base_t WHERE v = 1;
SELECT * FROM mv ORDER BY i; SELECT * FROM mv ORDER BY i;
@ -454,7 +509,7 @@ CREATE OPERATOR CLASS mytype_ops
FUNCTION 1 mytype_cmp(mytype,mytype); FUNCTION 1 mytype_cmp(mytype,mytype);
CREATE TABLE t_mytype (x mytype); CREATE TABLE t_mytype (x mytype);
SELECT create_immv('mv_mytype', SELECT pgivm.create_immv('mv_mytype',
'SELECT * FROM t_mytype'); 'SELECT * FROM t_mytype');
INSERT INTO t_mytype VALUES ('1'::mytype); INSERT INTO t_mytype VALUES ('1'::mytype);
SELECT * FROM mv_mytype; SELECT * FROM mv_mytype;
@ -462,95 +517,95 @@ SELECT * FROM mv_mytype;
ROLLBACK; ROLLBACK;
-- outer join is not supported -- outer join is not supported
SELECT create_immv('mv(a,b)', SELECT pgivm.create_immv('mv(a,b)',
'SELECT a.i, b.i FROM mv_base_a a LEFT JOIN mv_base_b b ON a.i=b.i'); 'SELECT a.i, b.i FROM mv_base_a a LEFT JOIN mv_base_b b ON a.i=b.i');
-- contain system column -- contain system column
SELECT create_immv('mv_ivm01', 'SELECT i,j,xmin FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm01', 'SELECT i,j,xmin FROM mv_base_a');
SELECT create_immv('mv_ivm02', 'SELECT i,j FROM mv_base_a WHERE xmin = ''610'''); SELECT pgivm.create_immv('mv_ivm02', 'SELECT i,j FROM mv_base_a WHERE xmin = ''610''');
SELECT create_immv('mv_ivm03', 'SELECT i,j,xmin::text AS x_min FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm03', 'SELECT i,j,xmin::text AS x_min FROM mv_base_a');
SELECT create_immv('mv_ivm04', 'SELECT i,j,xidsend(xmin) AS x_min FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm04', 'SELECT i,j,xidsend(xmin) AS x_min FROM mv_base_a');
-- contain ORDER BY -- contain ORDER BY
SELECT create_immv('mv_ivm07', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) ORDER BY i,j,k'); SELECT pgivm.create_immv('mv_ivm07', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) ORDER BY i,j,k');
-- contain HAVING -- contain HAVING
SELECT create_immv('mv_ivm08', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) GROUP BY i,j,k HAVING SUM(i) > 5'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT i,j,k FROM mv_base_a a INNER JOIN mv_base_b b USING(i) GROUP BY i,j,k HAVING SUM(i) > 5');
-- contain GROUP BY without aggregate -- contain GROUP BY without aggregate
SELECT create_immv('mv_ivm08', 'SELECT i,j FROM mv_base_a GROUP BY i,j'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT i,j FROM mv_base_a GROUP BY i,j');
-- contain view or materialized view -- contain view or materialized view
CREATE VIEW b_view AS SELECT i,k FROM mv_base_b; CREATE VIEW b_view AS SELECT i,k FROM mv_base_b;
CREATE MATERIALIZED VIEW b_mview AS SELECT i,k FROM mv_base_b; CREATE MATERIALIZED VIEW b_mview AS SELECT i,k FROM mv_base_b;
SELECT create_immv('mv_ivm07', 'SELECT a.i,a.j FROM mv_base_a a,b_view b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm07', 'SELECT a.i,a.j FROM mv_base_a a,b_view b WHERE a.i = b.i');
SELECT create_immv('mv_ivm08', 'SELECT a.i,a.j FROM mv_base_a a,b_mview b WHERE a.i = b.i'); SELECT pgivm.create_immv('mv_ivm08', 'SELECT a.i,a.j FROM mv_base_a a,b_mview b WHERE a.i = b.i');
-- contain mutable functions -- contain mutable functions
SELECT create_immv('mv_ivm12', 'SELECT i,j FROM mv_base_a WHERE i = random()::int'); SELECT pgivm.create_immv('mv_ivm12', 'SELECT i,j FROM mv_base_a WHERE i = random()::int');
-- LIMIT/OFFSET is not supported -- LIMIT/OFFSET is not supported
SELECT create_immv('mv_ivm13', 'SELECT i,j FROM mv_base_a LIMIT 10 OFFSET 5'); SELECT pgivm.create_immv('mv_ivm13', 'SELECT i,j FROM mv_base_a LIMIT 10 OFFSET 5');
-- DISTINCT ON is not supported -- DISTINCT ON is not supported
SELECT create_immv('mv_ivm14', 'SELECT DISTINCT ON(i) i, j FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm14', 'SELECT DISTINCT ON(i) i, j FROM mv_base_a');
-- TABLESAMPLE clause is not supported -- TABLESAMPLE clause is not supported
SELECT create_immv('mv_ivm15', 'SELECT i, j FROM mv_base_a TABLESAMPLE SYSTEM(50)'); SELECT pgivm.create_immv('mv_ivm15', 'SELECT i, j FROM mv_base_a TABLESAMPLE SYSTEM(50)');
-- window functions are not supported -- window functions are not supported
SELECT create_immv('mv_ivm16', 'SELECT *, cume_dist() OVER (ORDER BY i) AS rank FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm16', 'SELECT *, cume_dist() OVER (ORDER BY i) AS rank FROM mv_base_a');
-- aggregate function with some options is not supported -- aggregate function with some options is not supported
SELECT create_immv('mv_ivm17', 'SELECT COUNT(*) FILTER(WHERE i < 3) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm17', 'SELECT COUNT(*) FILTER(WHERE i < 3) FROM mv_base_a');
SELECT create_immv('mv_ivm18', 'SELECT COUNT(DISTINCT i) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm18', 'SELECT COUNT(DISTINCT i) FROM mv_base_a');
SELECT create_immv('mv_ivm19', 'SELECT array_agg(j ORDER BY i DESC) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm19', 'SELECT array_agg(j ORDER BY i DESC) FROM mv_base_a');
SELECT create_immv('mv_ivm20', 'SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),())'); SELECT pgivm.create_immv('mv_ivm20', 'SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),())');
-- inheritance parent is not supported -- inheritance parent is not supported
BEGIN; BEGIN;
CREATE TABLE parent (i int, v int); CREATE TABLE parent (i int, v int);
CREATE TABLE child_a(options text) INHERITS(parent); CREATE TABLE child_a(options text) INHERITS(parent);
SELECT create_immv('mv_ivm21', 'SELECT * FROM parent'); SELECT pgivm.create_immv('mv_ivm21', 'SELECT * FROM parent');
ROLLBACK; ROLLBACK;
-- UNION statement is not supported -- UNION statement is not supported
SELECT create_immv('mv_ivm22', 'SELECT i,j FROM mv_base_a UNION ALL SELECT i,k FROM mv_base_b'); SELECT pgivm.create_immv('mv_ivm22', 'SELECT i,j FROM mv_base_a UNION ALL SELECT i,k FROM mv_base_b');
-- DISTINCT clause in nested query are not supported -- DISTINCT clause in nested query are not supported
SELECT create_immv('mv_ivm23', 'SELECT * FROM (SELECT DISTINCT i,j FROM mv_base_a) AS tmp');; SELECT pgivm.create_immv('mv_ivm23', 'SELECT * FROM (SELECT DISTINCT i,j FROM mv_base_a) AS tmp');;
-- empty target list is not allowed with IVM -- empty target list is not allowed with IVM
SELECT create_immv('mv_ivm25', 'SELECT FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm25', 'SELECT FROM mv_base_a');
-- FOR UPDATE/SHARE is not supported -- FOR UPDATE/SHARE is not supported
SELECT create_immv('mv_ivm26', 'SELECT i,j FROM mv_base_a FOR UPDATE'); SELECT pgivm.create_immv('mv_ivm26', 'SELECT i,j FROM mv_base_a FOR UPDATE');
SELECT create_immv('mv_ivm27', 'SELECT * FROM (SELECT i,j FROM mv_base_a FOR UPDATE) AS tmp;'); SELECT pgivm.create_immv('mv_ivm27', 'SELECT * FROM (SELECT i,j FROM mv_base_a FOR UPDATE) AS tmp;');
-- tartget list cannot contain ivm column that start with '__ivm' -- tartget list cannot contain ivm column that start with '__ivm'
SELECT create_immv('mv_ivm28', 'SELECT i AS "__ivm_count__" FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm28', 'SELECT i AS "__ivm_count__" FROM mv_base_a');
-- expressions specified in GROUP BY must appear in the target list. -- expressions specified in GROUP BY must appear in the target list.
SELECT create_immv('mv_ivm29', 'SELECT COUNT(i) FROM mv_base_a GROUP BY i;'); SELECT pgivm.create_immv('mv_ivm29', 'SELECT COUNT(i) FROM mv_base_a GROUP BY i;');
-- experssions containing an aggregate is not supported -- experssions containing an aggregate is not supported
SELECT create_immv('mv_ivm30', 'SELECT sum(i)*0.5 FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm30', 'SELECT sum(i)*0.5 FROM mv_base_a');
SELECT create_immv('mv_ivm31', 'SELECT sum(i)/sum(j) FROM mv_base_a'); SELECT pgivm.create_immv('mv_ivm31', 'SELECT sum(i)/sum(j) FROM mv_base_a');
-- VALUES is not supported -- VALUES is not supported
SELECT create_immv('mv_ivm_only_values1', 'values(1)'); SELECT pgivm.create_immv('mv_ivm_only_values1', 'values(1)');
SELECT create_immv('mv_ivm_only_values2', 'SELECT * FROM (values(1)) AS tmp'); SELECT pgivm.create_immv('mv_ivm_only_values2', 'SELECT * FROM (values(1)) AS tmp');
-- views containing base tables with Row Level Security -- views containing base tables with Row Level Security
DROP USER IF EXISTS ivm_admin; DROP USER IF EXISTS regress_ivm_admin;
DROP USER IF EXISTS ivm_user; DROP USER IF EXISTS regress_ivm_user;
CREATE USER ivm_admin; CREATE USER regress_ivm_admin;
CREATE USER ivm_user; CREATE USER regress_ivm_user;
--- create a table with RLS --- create a table with RLS
SET SESSION AUTHORIZATION ivm_admin; SET SESSION AUTHORIZATION regress_ivm_admin;
CREATE TABLE rls_tbl(id int, data text, owner name); CREATE TABLE rls_tbl(id int, data text, owner name);
INSERT INTO rls_tbl VALUES INSERT INTO rls_tbl VALUES
(1,'foo','ivm_user'), (1,'foo','regress_ivm_user'),
(2,'bar','postgres'); (2,'bar','postgres');
CREATE TABLE num_tbl(id int, num text); CREATE TABLE num_tbl(id int, num text);
INSERT INTO num_tbl VALUES INSERT INTO num_tbl VALUES
@ -567,29 +622,29 @@ ALTER TABLE rls_tbl ENABLE ROW LEVEL SECURITY;
GRANT ALL on rls_tbl TO PUBLIC; GRANT ALL on rls_tbl TO PUBLIC;
GRANT ALL on num_tbl TO PUBLIC; GRANT ALL on num_tbl TO PUBLIC;
--- create a view owned by ivm_user --- create a view owned by regress_ivm_user
SET SESSION AUTHORIZATION ivm_user; SET SESSION AUTHORIZATION regress_ivm_user;
SELECT create_immv('ivm_rls', 'SELECT * FROM rls_tbl'); SELECT pgivm.create_immv('ivm_rls', 'SELECT * FROM rls_tbl');
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
RESET SESSION AUTHORIZATION; RESET SESSION AUTHORIZATION;
--- inserts rows owned by different users --- inserts rows owned by different users
INSERT INTO rls_tbl VALUES INSERT INTO rls_tbl VALUES
(3,'baz','ivm_user'), (3,'baz','regress_ivm_user'),
(4,'qux','postgres'); (4,'qux','postgres');
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
--- combination of diffent kinds of commands --- combination of diffent kinds of commands
WITH WITH
i AS (INSERT INTO rls_tbl VALUES(5,'quux','postgres'), (6,'corge','ivm_user')), i AS (INSERT INTO rls_tbl VALUES(5,'quux','postgres'), (6,'corge','regress_ivm_user')),
u AS (UPDATE rls_tbl SET owner = 'postgres' WHERE id = 1), u AS (UPDATE rls_tbl SET owner = 'postgres' WHERE id = 1),
u2 AS (UPDATE rls_tbl SET owner = 'ivm_user' WHERE id = 2) u2 AS (UPDATE rls_tbl SET owner = 'regress_ivm_user' WHERE id = 2)
SELECT; SELECT;
SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3; SELECT id, data, owner FROM ivm_rls ORDER BY 1,2,3;
--- ---
SET SESSION AUTHORIZATION ivm_user; SET SESSION AUTHORIZATION regress_ivm_user;
SELECT create_immv('ivm_rls2', 'SELECT * FROM rls_tbl JOIN num_tbl USING(id)'); SELECT pgivm.create_immv('ivm_rls2', 'SELECT * FROM rls_tbl JOIN num_tbl USING(id)');
RESET SESSION AUTHORIZATION; RESET SESSION AUTHORIZATION;
WITH WITH
@ -601,8 +656,8 @@ SELECT * FROM ivm_rls2 ORDER BY 1,2,3;
DROP TABLE rls_tbl CASCADE; DROP TABLE rls_tbl CASCADE;
DROP TABLE num_tbl CASCADE; DROP TABLE num_tbl CASCADE;
DROP USER ivm_user; DROP USER regress_ivm_user;
DROP USER ivm_admin; DROP USER regress_ivm_admin;
-- automatic index creation -- automatic index creation
BEGIN; BEGIN;
@ -610,20 +665,30 @@ CREATE TABLE base_a (i int primary key, j int);
CREATE TABLE base_b (i int primary key, j int); CREATE TABLE base_b (i int primary key, j int);
--- group by: create an index --- group by: create an index
SELECT create_immv('mv_idx1', 'SELECT i, sum(j) FROM base_a GROUP BY i'); SELECT pgivm.create_immv('mv_idx1', 'SELECT i, sum(j) FROM base_a GROUP BY i');
--- distinct: create an index --- distinct: create an index
SELECT create_immv('mv_idx2', 'SELECT DISTINCT j FROM base_a'); SELECT pgivm.create_immv('mv_idx2', 'SELECT DISTINCT j FROM base_a');
--- with all pkey columns: create an index --- with all pkey columns: create an index
SELECT create_immv('mv_idx3(i_a, i_b)', 'SELECT a.i, b.i FROM base_a a, base_b b'); SELECT pgivm.create_immv('mv_idx3(i_a, i_b)', 'SELECT a.i, b.i FROM base_a a, base_b b');
--- missing some pkey columns: no index --- missing some pkey columns: no index
SELECT create_immv('mv_idx4', 'SELECT j FROM base_a'); SELECT pgivm.create_immv('mv_idx4', 'SELECT j FROM base_a');
SELECT create_immv('mv_idx5', 'SELECT a.i, b.j FROM base_a a, base_b b'); SELECT pgivm.create_immv('mv_idx5', 'SELECT a.i, b.j FROM base_a a, base_b b');
--- subqueries --- subqueries: create an index
SELECT create_immv('mv_idx6(i_a, i_b)', 'SELECT a.i, b.i FROM (SELECT * FROM base_a) a, (SELECT * FROM base_b) b'); SELECT pgivm.create_immv('mv_idx6(i_a, i_b)', 'SELECT a.i, b.i FROM (SELECT * FROM base_a) a, (SELECT * FROM base_b) b');
--- with set-returning function: no index
SELECT pgivm.create_immv('mv_idx7', 'SELECT i FROM base_a, generate_series(1,10)');
ROLLBACK;
-- type that doesn't have default operator class for access method btree
BEGIN;
CREATE TABLE table_json (j json);
SELECT pgivm.create_immv('mv_json', 'SELECT * from table_json');
ROLLBACK; ROLLBACK;
-- prevent IMMV chanages -- prevent IMMV chanages
@ -633,9 +698,9 @@ DELETE FROM mv_ivm_1;
TRUNCATE mv_ivm_1; TRUNCATE mv_ivm_1;
-- get_immv_def function -- get_immv_def function
SELECT immvrelid, get_immv_def(immvrelid) FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, pgivm.get_immv_def(immvrelid) FROM pgivm.pg_ivm_immv ORDER BY 1;
-- mv_base_b is not immv -- mv_base_b is not immv
SELECT 'mv_base_b'::regclass, get_immv_def('mv_base_b'); SELECT 'mv_base_b'::regclass, pgivm.get_immv_def('mv_base_b');
DROP TABLE mv_base_b CASCADE; DROP TABLE mv_base_b CASCADE;
DROP TABLE mv_base_a CASCADE; DROP TABLE mv_base_a CASCADE;

View file

@ -1,19 +1,19 @@
CREATE TABLE t (i int PRIMARY KEY); CREATE TABLE t (i int PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 5); INSERT INTO t SELECT generate_series(1, 5);
SELECT create_immv('mv', 'SELECT * FROM t'); SELECT pgivm.create_immv('mv', 'SELECT * FROM t');
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
-- Refresh IMMV with data -- Refresh IMMV with data
SELECT refresh_immv('mv', true); SELECT pgivm.refresh_immv('mv', true);
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
INSERT INTO t VALUES(6); INSERT INTO t VALUES(6);
SELECT i FROM mv ORDER BY 1; SELECT i FROM mv ORDER BY 1;
-- Make IMMV unpopulated -- Make IMMV unpopulated
SELECT refresh_immv('mv', false); SELECT pgivm.refresh_immv('mv', false);
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
SELECT i FROM mv ORDER BY 1; SELECT i FROM mv ORDER BY 1;
-- Immediate maintenance is disabled. IMMV can be scannable and is empty. -- Immediate maintenance is disabled. IMMV can be scannable and is empty.
@ -21,8 +21,8 @@ INSERT INTO t VALUES(7);
SELECT i FROM mv ORDER BY 1; SELECT i FROM mv ORDER BY 1;
-- Refresh the IMMV and make it populated. -- Refresh the IMMV and make it populated.
SELECT refresh_immv('mv', true); SELECT pgivm.refresh_immv('mv', true);
SELECT immvrelid, ispopulated FROM pg_ivm_immv ORDER BY 1; SELECT immvrelid, ispopulated FROM pgivm.pg_ivm_immv ORDER BY 1;
SELECT i FROM mv ORDER BY 1; SELECT i FROM mv ORDER BY 1;
-- Immediate maintenance is enabled. -- Immediate maintenance is enabled.
@ -30,10 +30,10 @@ INSERT INTO t VALUES(8);
SELECT i FROM mv ORDER BY 1; SELECT i FROM mv ORDER BY 1;
-- Use qualified name -- Use qualified name
SELECT refresh_immv('public.mv', true); SELECT pgivm.refresh_immv('public.mv', true);
-- Use not existing IMMV -- Use not existing IMMV
SELECT refresh_immv('mv_not_existing', true); SELECT pgivm.refresh_immv('mv_not_existing', true);
-- Try to refresh a normal table -- error -- Try to refresh a normal table -- error
SELECT refresh_immv('t', true); SELECT pgivm.refresh_immv('t', true);