Commit graph

32 commits

Author SHA1 Message Date
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
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
Kyungmin Kim
eab5195be5
fix some typos (#129) 2025-04-03 17:19:29 +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
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
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
reshke
b7be2aa7ff
Drop unused params from ExecCreateImmv function (#102) 2024-10-14 21:45:52 +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
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
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
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
Yugo Nagata
14bb84c599 Fix to use qualified function names during maintenance
Previously, functions names in pg_catalog schema that were used during
view maintenance were not qualified. This is problematic because
functions in other schema could be referenced unintentionally. Moreover,
that could result in privilege escalation that if a nefarious user who
can create a function, arbitrary functions could be executed under IMMV
owner's privilege.

CVE-2023-23554
2023-03-02 14:43:24 +09:00
Yugo Nagata
b928e32774
Add CTE support (#47)
Simple CTEs which does not contain aggregates or DISTINCT are 
now supported similarly to simple sub-queries.

Before a view is maintained, all CTEs are converted to corresponding
subqueries to enable to treat CTEs as same as subqueries. For this
end, codes of the static function inline_cte in the core
(optimizer/plan/subselect.c) was imported.

Prohibit Unreferenced CTE is prohibited.
When a table in a unreferenced CTE is TRUNCATEd, the contents
of the IMMV is not affected so it must not be truncated. For
confirming it at the maintenance time, we have to check if the
modified table used in a CTE is actually referenced. Although
it would possible, we just disallow to create such IMMVs for now
since such unreferenced CTE is useless unless it doesn't contain
modifying commands, that is already prohibited.
2023-01-30 11:28:27 +09:00
Yugo Nagata
7997f3e260 Remove a unnecessary function call
It was added by 3de95c09 for consistency with the patch version
proposed for PostgreSQL core, but the called function was not
one we intended. Fortunately, the behavior has not been affected.

Now, I decided to remove this funciton since I come to think it is
confusable.

Also, added some comments on an argument left for the similar
consistency.
2023-01-27 15:37:11 +09:00
Yugo Nagata
b4b5ea28fd
Add some qeury checks on subqueries (#49)
Some conditions on view definition that should be prohibited,
for example,

- SELECT ... FROM func(..., (SELECT ... FROM ...), ..) ...;
- SELECT expr(SELECT ... FROM ...) FROM ...;

were not checked. Also, some error messages and the order of
tests are improved.
2023-01-26 16:59:15 +09:00
Yugo Nagata
0e581c16e7 Fix automatic index creation to support subqueries
Previously, when a subquery is used a unique index could not be created
even if all primary key attributes appear in the target list.
2023-01-14 18:52:58 +09:00
Yugo Nagata
5db54f4669
Use exclusive lock for view maintenance caused by UPDATE or DELETE (#42)
When using DELETE or UPDATE, we must use exclusive lock for now
because apply_old_delta(_with_count) could result in wrong results
with concurrent transactions. We would like to improve it in future,
but we prevent it by using the lock for now.
2022-12-16 23:31:00 +09:00
Yugo Nagata
26f0b03b58
Fix bugs of IVM that occur when column names are specified in aggregate views (#41)
The names of additional columns for aggregate views are derived from column
names specified in the first parameter of create_immv, but when the number
was less than the length of the actual target list, segmentation fault occurred.
Furthermore, when the number of specified columns is more than the target list,
it overrode additional column names and it caused a failure of incremental
maintenance of an aggregate view.

To fix then, check the length of the specified column name list not to access
invalid area, and also prevent from overriding additional column names.
2022-12-16 19:13:08 +09:00
Yugo Nagata
326720874e
Fix a bug of automatic index creation (#40)
It is intended that a unique index is created only if all primary
keys of tables in FROM clause appear in the target list. For this
purpose, pull_varnos_of_level was used to extract relations in the
FROM clause, but it is incorrect and actually we should use
get_relids_in_jointree.

Due to this bug, an index could be created even even where there
is a pkey attribute from just one of relations in FROM clause.
2022-12-16 12:42:35 +09:00
thoshiai
24dc053659
Fix not to create IMMV including other IMMV(#30) (#31)
Previously, IMMV including IMMV in its definition can be created by
create_immv(), but it should not be supported by IMMV because we
cannot maintain it recursively for now. This patch prevents it by raising
an error for such view definition on create_immv().
2022-10-18 15:51:41 +09:00
Yugo Nagata
79d4b13ba1
Fix comment typo and test for aggregates without GROUP BY (#24)
We usually try to create a unique index on an IMMV for both
performance and constraint reasons. However, we don't need
such index for an aggregate view without GROUP BY because
it has always only one row. Also, we don't need to raise
NOTICE message to suggest to create an index.

This change was mistakenly introduced in cfe9491e6b,
but it should have been a separate commit. In this commit,
a typo and tests are fixed.
2022-09-28 16:02:53 +09:00
Yugo Nagata
cfe9491e6b Fix README for pg_ivm 1.2 2022-07-25 17:31:46 +09:00
Yugo Nagata
6faf0b3baa
Support min/max aggregates (#18)
In order to re-calculate min/max values for groups where the min
or max value is deleted, we need the view query definition in string
form. However, pg_get_viewdef cannot be used for this purpose because
IMMV's defenition is in pg_ivm_immv but not pg_rewrite.  Therefore,
we have to convert query definition in pg_ivm_immv to query
definition string. We can use pg_get_querydef in PG15, but we cannot
in PG14 or earlier, so we use codes in ruleutil.c copied from PG13
or PG14 depending versions.
2022-07-25 13:11:33 +09:00
thoshiai
790b0d2bd6
Support simple subquery (#17)
A simple subquery in FROM clause is supported.
DISTINCT and aggregate functions are not supported in subquery.
2022-07-25 09:24:51 +09:00
Yugo Nagata
53c9509163 Fix check for aggregate functions to work with PostgreSQL 13
In PostgreSQL 14 or later, OIDs of aggregate functions are
described in fmrgoids.h, but that in PostgreSQL 13 doesn't
contain aggregate function OIDs. Therefore, we get the OID
by passing the function name and arg type to to_regprocedure().
2022-06-21 23:08:54 +09:00
Yugo Nagata
d99aeb848e Allow TRUNCATE on base tables (#144)
When a base table is truncated, the view content will be empty if the
view definition query does not contain an aggregate without a GROUP clause.
Therefore, such views can be truncated.

Aggregate views without a GROUP clause always have one row. Therefore,
if a base table is truncated, the view will not be empty and will contain
a row with NULL value (or 0 for count()). So, in this case, we refresh the
view instead of truncating it.
2022-06-21 21:27:21 +09:00
Yugo Nagata
57c8bac1a0 Add aggregates support
Built-in count, sum, avg are supported. We have not supported min/max
yet, but will support in future.

When an IMMV with any aggregates are defined, additional columns
are created for each aggregate function. Although such columns are
"hidden" in pgsql-ivm version, they are always visible for users
in the extension version.
2022-06-21 20:50:45 +09:00
thoshiai
51a944b388 Add refresh_immv() function
refresh_immv(immv_name, with_data) is a function to refresh IMMV like
 REFRESH MATERIALIZED VIEW command. It has two argument.
immv_name is incrementally maintainable materialized view's name, and
with_data is an option that is corresponding to the WITH [NO] DATA option.
When with_data is set false, the IMMV gets unpopulated.

One of differences between IMMVs unpopulated by this function and
normal materialized views unpopulated by REFRESH ... WITH NO DATA
is that such IMMVs can be referenced by SELECT but return no rows,
while unpopulated materialized views are not scanable.

The behaviour may be changed in future to raise an error when unpopulated
an IMMV is scanned.
2022-06-16 03:06:47 +09:00
Tatsuo Ishii
612b59694e Allow to build pg_ivm on PostgreSQL 13. 2022-05-17 21:32:04 +09:00
Yugo Nagata
eed6271128 Split files to make it easier to follow the core code 2022-04-27 14:45:47 +09:00