Skip to content

Commit

Permalink
Fix segfault in cagg creation
Browse files Browse the repository at this point in the history
When trying to create cagg on top of any relation that is a neither
a hypertable nor a continuous aggregate the command would segfault.
This patch changes the code to handle this case gracefully and error
out when trying to create a cagg on top of a relation that is not
supported. Found by coverity.
  • Loading branch information
svenklemm committed Nov 28, 2022
1 parent 35c9120 commit 47e7741
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
31 changes: 20 additions & 11 deletions tsl/src/continuous_aggs/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ cagg_validate_query(const Query *query, const bool finalized, const char *cagg_s
StringInfo detail = makeStringInfo();
bool is_nested = false;
Query *prev_query = NULL;
ContinuousAgg *cagg_source = NULL;
ContinuousAgg *cagg_parent = NULL;

if (!cagg_query_supported(query, hint, detail, finalized))
{
Expand Down Expand Up @@ -1153,26 +1153,35 @@ cagg_validate_query(const Query *query, const bool finalized, const char *cagg_s
ht = ts_hypertable_cache_get_cache_and_entry(rte->relid, CACHE_FLAG_NONE, &hcache);
else
{
cagg_source = ts_continuous_agg_find_by_relid(rte->relid);
cagg_parent = ts_continuous_agg_find_by_relid(rte->relid);

if (!ContinuousAggIsFinalized(cagg_source))
if (!cagg_parent)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("invalid continuous aggregate query"),
errhint("continuous aggregate needs to query hypertable or another "
"continuous aggregate")));
}

if (!ContinuousAggIsFinalized(cagg_parent))
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("old format of continuous aggregate is not supported"),
errhint("Run \"CALL cagg_migrate('%s.%s');\" to migrate to the new "
"format.",
NameStr(cagg_source->data.user_view_schema),
NameStr(cagg_source->data.user_view_name))));
NameStr(cagg_parent->data.user_view_schema),
NameStr(cagg_parent->data.user_view_name))));
}

parent_mat_hypertable_id = cagg_source->data.mat_hypertable_id;
parent_mat_hypertable_id = cagg_parent->data.mat_hypertable_id;
hcache = ts_hypertable_cache_pin();
ht = ts_hypertable_cache_get_entry_by_id(hcache, cagg_source->data.mat_hypertable_id);
ht = ts_hypertable_cache_get_entry_by_id(hcache, cagg_parent->data.mat_hypertable_id);

/* get the querydef for the source cagg */
is_nested = true;
prev_query = ts_continuous_agg_get_query(cagg_source);
prev_query = ts_continuous_agg_get_query(cagg_parent);
}

if (TS_HYPERTABLE_IS_INTERNAL_COMPRESSION_TABLE(ht))
Expand Down Expand Up @@ -1248,7 +1257,7 @@ cagg_validate_query(const Query *query, const bool finalized, const char *cagg_s
part_dimension->column_attno,
part_dimension->fd.column_type,
part_dimension->fd.interval_length,
cagg_source->data.parent_mat_hypertable_id);
cagg_parent->data.parent_mat_hypertable_id);
}

ts_cache_release(hcache);
Expand Down Expand Up @@ -1347,8 +1356,8 @@ cagg_validate_query(const Query *query, const bool finalized, const char *cagg_s
cagg_name,
width_out,
message,
NameStr(cagg_source->data.user_view_schema),
NameStr(cagg_source->data.user_view_name),
NameStr(cagg_parent->data.user_view_schema),
NameStr(cagg_parent->data.user_view_name),
width_out_parent)));
}
}
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/expected/cagg_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,11 @@ ERROR: hypertable is an internal compressed hypertable
--Check error handling for this case
SELECT compress_chunk(ch) FROM show_chunks('i2980') ch;
ERROR: compression not enabled on "i2980"
-- cagg on normal view should error out
CREATE VIEW v1 AS SELECT now() AS time;
CREATE MATERIALIZED VIEW cagg1 WITH (timescaledb.continuous) AS SELECT time_bucket('1h',time) FROM v1 GROUP BY 1;
ERROR: invalid continuous aggregate query
-- cagg on normal view should error out
CREATE MATERIALIZED VIEW matv1 AS SELECT now() AS time;
CREATE MATERIALIZED VIEW cagg1 WITH (timescaledb.continuous) AS SELECT time_bucket('1h',time) FROM matv1 GROUP BY 1;
ERROR: invalid continuous aggregate view
9 changes: 9 additions & 0 deletions tsl/test/sql/cagg_errors.sql
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,12 @@ CREATE MATERIALIZED VIEW cagg1 WITH(timescaledb.continuous) AS SELECT time_bucke
--TEST ht + cagg, do not enable compression on ht and try to compress chunk on ht.
--Check error handling for this case
SELECT compress_chunk(ch) FROM show_chunks('i2980') ch;

-- cagg on normal view should error out
CREATE VIEW v1 AS SELECT now() AS time;
CREATE MATERIALIZED VIEW cagg1 WITH (timescaledb.continuous) AS SELECT time_bucket('1h',time) FROM v1 GROUP BY 1;

-- cagg on normal view should error out
CREATE MATERIALIZED VIEW matv1 AS SELECT now() AS time;
CREATE MATERIALIZED VIEW cagg1 WITH (timescaledb.continuous) AS SELECT time_bucket('1h',time) FROM matv1 GROUP BY 1;

0 comments on commit 47e7741

Please sign in to comment.