Commit graph

15 commits

Author SHA1 Message Date
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
e00ff9dd32
Fix to allow generate_sereis in FROM clause (#50)
Previously, it caused an error due to an ambiguous reference
at the maintenance time because generate_series is used internally.
This is fixed by using an alias name for the internal genearet_series.
2023-01-26 16:58:20 +09:00
Yugo Nagata
b6702f9a3a
Use snapshot to check tuple visibility in pre-update state (#28)
When multiple tables are updated or the view contains a self-join,
we need to calculate table states that was before it is modified
during incremental view maintenance. For get the pre-update state,
tuples inserted in a command must be removed when the table is scanned.

Previously, we used xmin and cmin system columns for this purpose,
but this way is problematic because after a tuple is frozen, its xmin
no longer has any meaning. Actually, we will get inconsistent view
results after XID wraparound.

Also, we can see similar similar inconsistency when using sub-transaction
because xmin values do not always monotonically increasing by command
executions.

To fix this, we use a snapshot that taken just before the table is
modified for checking tuple visibility in pre-state table instead of
using xmin and cmin system columns. A new function returning boolean,
ivm_visible_in_prestate, is added, and this is called in WHERE clause
of sub-queries to calculate pre-state table. This function check if a
specified tuple in the post-update table is visible or not using the
snapshot and return true if the tuple is visible.
2022-09-30 11:53:12 +09:00
Yugo Nagata
4c6016999d
Fix to get view definition string with correct column names (#26)
Previously, a query string returned from pg_ivm_get_querydef
did not include column names specified when IMMV was defined
by create_immv. This caused failures in maintenance of MIN/MAX
aggregate views whose columns had alias names.

It is fixed by rewriting the result column name in the parse tree
using the view's tuple descriptor prior to calling pg_get_querydef
for PG15 or higher, or specifying the tuple descriptor to
get_query_def for PG14 or earlier.
2022-09-29 22:24:28 +09:00
Marco Slot
046b323ce5 Fix compiler warning in ExecRefreshImmv 2022-07-28 18:05:39 +02: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
thoshiai
2ead2e207e
Add support for PostgreSQL 15 (#15) 2022-07-14 22:54:33 +09:00
Yugo Nagata
3de95c09fa Improve refresh_immv behavior a bit
- Allow to use qualified name
- Confirm if executed by the owner of the IMMV
- Improve the message when specified relation is not an IMMV
- Create a unique index at refresh with no dat if possible
  This is actually required, but we want it behave as same
  as the pgsql-ivm version for now.
2022-06-23 11:33:06 +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