Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing syntax to use MATERIALIZED VIEW for continuous aggregates #2243

Merged
merged 1 commit into from Aug 27, 2020

Conversation

mkindahl
Copy link
Contributor

@mkindahl mkindahl commented Aug 21, 2020

We change the syntax for defining continuous aggregates to use CREATE MATERIALIZED VIEW rather than CREATE VIEW. The command still creates
a view, while CREATE MATERIALIZED VIEW creates a table. Raise an
error if CREATE VIEW is used to create a continuous aggregate and
redirect to CREATE MATERIALIZED VIEW.

In a similar vein, DROP MATERIALIZED VIEW is used for continuous
aggregates and continuous aggregates cannot be dropped with DROP VIEW.

Continuous aggregates are altered using ALTER MATERIALIZED VIEW
rather than ALTER VIEW, so we ensure that it works for ALTER MATERIALIZED VIEW and gives an error if you try to use ALTER VIEW to
change a continuous aggregate.

Note that we allow ALTER VIEW ... SET SCHEMA to be used with the
partial view as well as with the direct view, so this is handled as a
special case.

Fixes #2233

@mkindahl mkindahl requested a review from a team as a code owner August 21, 2020 13:31
@mkindahl mkindahl requested review from mfreed, svenklemm, gayyappan and erimatnor and removed request for a team and mfreed August 21, 2020 13:31
@mkindahl mkindahl self-assigned this Aug 21, 2020
Copy link
Contributor

@erimatnor erimatnor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick pass with high-level feedback. Might do a more thorough review later.

src/continuous_agg.c Outdated Show resolved Hide resolved
src/continuous_agg.c Outdated Show resolved Hide resolved
src/continuous_agg.h Outdated Show resolved Hide resolved
src/process_utility.c Show resolved Hide resolved
@erimatnor erimatnor added this to the 2.0.0 milestone Aug 21, 2020
src/process_utility.c Outdated Show resolved Hide resolved
cagg = ts_continuous_agg_find_by_view_name(NameStr(view_schema), NameStr(view_name));
vtyp = ts_continuous_agg_view_type(&cagg->data, NameStr(view_schema), NameStr(view_name));

if (vtyp == ContinuousAggPartialView || vtyp == ContinuousAggDirectView)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message has a typo: it states that ALTER VIEW is allowed with partial and direct views.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it is allowed to use ALTER VIEW to change some things, such as the schema for the view. Not sure why, but there are tests for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll clarify the situation when squashing them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote it to mention that it is ALTER VIEW ... SET SCHEMA that we handle.

@mkindahl mkindahl force-pushed the create_mat_view branch 3 times, most recently from 67dd652 to eee6b85 Compare August 24, 2020 11:11
@mkindahl mkindahl force-pushed the create_mat_view branch 4 times, most recently from fe989bd to 2de3f06 Compare August 25, 2020 14:34
Copy link
Contributor

@erimatnor erimatnor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, but there are a number of nits, issues to fix. Most pressing are several places where the return type and return values do not match.

I also think there's a test case missing for CREATE MATERIALIZED VIEW IF NOT EXISTS

Please also squash a number of the nit commits (7 commits seems a bit excessive for this change).

src/process_utility.c Outdated Show resolved Hide resolved
src/process_utility.c Outdated Show resolved Hide resolved
src/process_utility.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/create.c Outdated Show resolved Hide resolved
if (get_relname_relid(stmt->view->relname, nspid))
nspid = RangeVarGetCreationNamespace(stmt->into->rel);

if (get_relname_relid(stmt->into->rel->relname, nspid))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this honor IF NOT EXISTS? (Looks like we are always failing here in case of an existing aggregate). Do we have a test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check for it working in a similar way to how it works for normal tables. And added test case.

tsl/src/continuous_aggs/create.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/create.c Outdated Show resolved Hide resolved
tsl/src/continuous_aggs/create.c Outdated Show resolved Hide resolved
@mkindahl mkindahl force-pushed the create_mat_view branch 5 times, most recently from 1707c17 to f63fbf8 Compare August 26, 2020 13:17
@codecov
Copy link

codecov bot commented Aug 26, 2020

Codecov Report

Merging #2243 into master will increase coverage by 0.13%.
The diff coverage is 92.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2243      +/-   ##
==========================================
+ Coverage   90.11%   90.24%   +0.13%     
==========================================
  Files         212      212              
  Lines       34347    34341       -6     
==========================================
+ Hits        30952    30992      +40     
+ Misses       3395     3349      -46     
Impacted Files Coverage Δ
src/cross_module_fn.c 57.48% <0.00%> (ø)
src/hypertable.c 88.61% <ø> (ø)
src/continuous_agg.c 93.33% <85.71%> (-0.29%) ⬇️
tsl/src/continuous_aggs/create.c 95.80% <93.33%> (+<0.01%) ⬆️
src/process_utility.c 93.67% <95.58%> (-0.01%) ⬇️
src/import/planner.c 70.30% <0.00%> (+11.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba93cbf...466e236. Read the comment docs.

Copy link
Contributor

@gayyappan gayyappan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some unexpected diffs in the isolation test output. Other changes look good.

@@ -94,7 +94,8 @@ step Setup2:
END; $$ LANGUAGE plpgsql;

step AlterLag1: alter view continuous_view_1 set (timescaledb.refresh_lag = 10);
R1: LOG: materializing continuous aggregate public.continuous_view_1: nothing to invalidate, new range up to 15
ERROR: cannot alter continuous aggregate using ALTER VIEW
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error needs to be fixed. It alters the output for the .out file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -119,11 +120,12 @@ step Refresh2: REFRESH MATERIALIZED VIEW continuous_view_2; <waiting ...>
step UnlockCompleted: ROLLBACK;
step Refresh2: <... completed>
step UnlockMat1: ROLLBACK;
R1: LOG: materializing continuous aggregate public.continuous_view_1: nothing to invalidate, new range up to 30
R1: LOG: materializing continuous aggregate public.continuous_view_1: nothing to invalidate, new range up to 45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this diff also related to the alter view fialing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Fixed.

@mkindahl mkindahl force-pushed the create_mat_view branch 2 times, most recently from fd39737 to f80b5ed Compare August 27, 2020 10:49
@mkindahl mkindahl force-pushed the create_mat_view branch 6 times, most recently from db6946c to 9c8f941 Compare August 27, 2020 14:12
We change the syntax for defining continuous aggregates to use `CREATE
MATERIALIZED VIEW` rather than `CREATE VIEW`. The command still creates
a view, while `CREATE MATERIALIZED VIEW` creates a table.  Raise an
error if `CREATE VIEW` is used to create a continuous aggregate and
redirect to `CREATE MATERIALIZED VIEW`.

In a similar vein, `DROP MATERIALIZED VIEW` is used for continuous
aggregates and continuous aggregates cannot be dropped with `DROP
VIEW`.

Continuous aggregates are altered using `ALTER MATERIALIZED VIEW`
rather than `ALTER VIEW`, so we ensure that it works for `ALTER
MATERIALIZED VIEW` and gives an error if you try to use `ALTER VIEW` to
change a continuous aggregate.

Note that we allow `ALTER VIEW ... SET SCHEMA` to be used with the
partial view as well as with the direct view, so this is handled as a
special case.

Fixes timescale#2233

Co-authored-by: =?UTF-8?q?Erik=20Nordstr=C3=B6m?= <erik@timescale.com>
Co-authored-by: Mats Kindahl <mats@timescale.com>
@mkindahl mkindahl merged commit c054b38 into timescale:master Aug 27, 2020
@mkindahl mkindahl deleted the create_mat_view branch August 27, 2020 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use materialized view for continuous aggregates
4 participants