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

Error on create continuous aggregate with data #2389

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/process_utility.c
Expand Up @@ -3307,6 +3307,11 @@ process_create_table_as(ProcessUtilityArgs *args)
"parameters."),
errhint("Use only parameters with the \"timescaledb.\" prefix when "
"creating a continuous aggregate.")));

if (!stmt->into->skipData)
PreventInTransactionBlock(args->context == PROCESS_UTILITY_TOPLEVEL,
"CREATE MATERIALIZED VIEW ... WITH DATA");

return ts_cm_functions->process_cagg_viewstmt(args->parsetree,
args->query_string,
args->pstmt,
Expand Down
46 changes: 46 additions & 0 deletions tsl/test/expected/continuous_aggs_refresh.out
Expand Up @@ -423,3 +423,49 @@ SELECT * FROM weekly_temp_without_data;
Sun May 03 17:00:00 2020 PDT | 3 | 19
(8 rows)

-- These should fail since we do not allow refreshing inside a
-- transaction, not even as part of CREATE MATERIALIZED VIEW.
\set ON_ERROR_STOP 0
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH DATA;
END $$;
ERROR: CREATE MATERIALIZED VIEW ... WITH DATA cannot be executed from a function
Copy link
Contributor

@pmwkaa pmwkaa Sep 14, 2020

Choose a reason for hiding this comment

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

This is slightly odd message, why we had to wrap it into the DO statement?

Copy link
Contributor Author

@mkindahl mkindahl Sep 14, 2020

Choose a reason for hiding this comment

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

Agree, but it's the standard message that PreventInTransactionBlock prints. It digs out the context itself, and uses the format %s cannot be .....

BEGIN;
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH DATA;
ERROR: CREATE MATERIALIZED VIEW ... WITH DATA cannot run inside a transaction block
COMMIT;
\set ON_ERROR_STOP 1
-- This should not fail since we do not refresh the continuous
-- aggregate.
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions_1
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH NO DATA;
END $$;
NOTICE: adding index _materialized_hypertable_13_device_day_idx ON _timescaledb_internal._materialized_hypertable_13 USING BTREE(device, day)
BEGIN;
CREATE MATERIALIZED VIEW weekly_conditions_2
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH NO DATA;
NOTICE: adding index _materialized_hypertable_14_device_day_idx ON _timescaledb_internal._materialized_hypertable_14 USING BTREE(device, day)
COMMIT;
47 changes: 47 additions & 0 deletions tsl/test/sql/continuous_aggs_refresh.sql
Expand Up @@ -240,3 +240,50 @@ SELECT * FROM weekly_temp_with_data;
CALL refresh_continuous_aggregate('weekly_temp_without_data', NULL, NULL);

SELECT * FROM weekly_temp_without_data;

-- These should fail since we do not allow refreshing inside a
-- transaction, not even as part of CREATE MATERIALIZED VIEW.
\set ON_ERROR_STOP 0
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH DATA;
END $$;

BEGIN;
CREATE MATERIALIZED VIEW weekly_conditions
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH DATA;
COMMIT;

\set ON_ERROR_STOP 1

-- This should not fail since we do not refresh the continuous
-- aggregate.
DO LANGUAGE PLPGSQL $$ BEGIN
CREATE MATERIALIZED VIEW weekly_conditions_1
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH NO DATA;
END $$;

BEGIN;
CREATE MATERIALIZED VIEW weekly_conditions_2
WITH (timescaledb.continuous,
timescaledb.materialized_only=true)
AS
SELECT time_bucket('7 days', time) AS day, device, avg(temp) AS avg_temp
FROM conditions
GROUP BY 1,2 WITH NO DATA;
COMMIT;