Commit graph

148 commits

Author SHA1 Message Date
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