From 6ba70c8813226d1e48b1d9b8793dda16c3c560f6 Mon Sep 17 00:00:00 2001 From: Konstantina Skovola Date: Wed, 20 Apr 2022 20:27:58 +0300 Subject: [PATCH] Fix option "timescaledb.create_group_indexes" Previously this option was ignored when creating a continuous aggregate, even when explicitly set to true. Fixes #4249 --- CHANGELOG.md | 1 + tsl/src/continuous_aggs/create.c | 3 +- tsl/test/expected/continuous_aggs.out | 104 ++++++++++++++++++++++++++ tsl/test/sql/continuous_aggs.sql | 68 +++++++++++++++++ 4 files changed, 175 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c7d814e18..cb20a2a52c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ accidentally triggering the load of a previous DB version.** **Bugfixes** * #4225 Fix TRUNCATE error as non-owner on hypertable * #3899 Fix segfault in Continuous Aggregates +* #4255 Fix option "timescaledb.create_group_indexes" ## 2.6.1 (2022-04-11) This release is patch release. We recommend that you upgrade at the next available opportunity. diff --git a/tsl/src/continuous_aggs/create.c b/tsl/src/continuous_aggs/create.c index 9ce3675005c..6a78bf73746 100644 --- a/tsl/src/continuous_aggs/create.c +++ b/tsl/src/continuous_aggs/create.c @@ -2128,7 +2128,8 @@ cagg_create(const CreateTableAsStmt *create_stmt, ViewStmt *stmt, Query *panquer ts_catalog_restore_user(&sec_ctx); PRINT_MATINTERNAL_NAME(relnamebuf, "_materialized_hypertable_%d", materialize_hypertable_id); mat_rel = makeRangeVar(pstrdup(INTERNAL_SCHEMA_NAME), pstrdup(relnamebuf), -1); - is_create_mattbl_index = with_clause_options[ContinuousViewOptionCreateGroupIndex].is_default; + is_create_mattbl_index = + DatumGetBool(with_clause_options[ContinuousViewOptionCreateGroupIndex].parsed); mattablecolumninfo_create_materialization_table(&mattblinfo, materialize_hypertable_id, mat_rel, diff --git a/tsl/test/expected/continuous_aggs.out b/tsl/test/expected/continuous_aggs.out index 2f3927a20c0..19c2d759742 100644 --- a/tsl/test/expected/continuous_aggs.out +++ b/tsl/test/expected/continuous_aggs.out @@ -1738,3 +1738,107 @@ SELECT -- (0 rows) +-- test that option create_group_indexes is taken into account +SET client_min_messages = ERROR; +CREATE TABLE test_group_idx ( +time timestamptz, +symbol int, +value numeric +); +select create_hypertable('test_group_idx', 'time'); + create_hypertable +------------------------------ + (48,public,test_group_idx,t) +(1 row) + +insert into test_group_idx +(select generate_series( + '2020-01-01', + '2020-02-25', INTERVAL '30 sec' + ), + round(random()*10), + random()*5); +create materialized view cagg_index_true +with (timescaledb.continuous, timescaledb.create_group_indexes = true) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; +create materialized view cagg_index_false +with (timescaledb.continuous, timescaledb.create_group_indexes = false) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; +create materialized view cagg_index_default +with (timescaledb.continuous) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; +-- make sure a group index has been created when explicitly asked for +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_true +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_true' +\gset +\d+ :mat_hyper_cagg_index_true + Table "_timescaledb_internal._materialized_hypertable_49" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +----------+--------------------------+-----------+----------+---------+----------+--------------+------------- + bucket | timestamp with time zone | | not null | | plain | | + agg_2_2 | bytea | | | | extended | | + symbol | integer | | | | plain | | + chunk_id | integer | | | | plain | | +Indexes: + "_materialized_hypertable_49_bucket_idx" btree (bucket DESC) + "_materialized_hypertable_49_symbol_bucket_idx" btree (symbol, bucket DESC) +Triggers: + ts_insert_blocker BEFORE INSERT ON _timescaledb_internal._materialized_hypertable_49 FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker() +Child tables: _timescaledb_internal._hyper_49_106_chunk, + _timescaledb_internal._hyper_49_107_chunk + +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_false +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_false' +\gset +\d+ :mat_hyper_cagg_index_false + Table "_timescaledb_internal._materialized_hypertable_50" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +----------+--------------------------+-----------+----------+---------+----------+--------------+------------- + bucket | timestamp with time zone | | not null | | plain | | + agg_2_2 | bytea | | | | extended | | + symbol | integer | | | | plain | | + chunk_id | integer | | | | plain | | +Indexes: + "_materialized_hypertable_50_bucket_idx" btree (bucket DESC) +Triggers: + ts_insert_blocker BEFORE INSERT ON _timescaledb_internal._materialized_hypertable_50 FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker() +Child tables: _timescaledb_internal._hyper_50_108_chunk, + _timescaledb_internal._hyper_50_109_chunk + +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_default +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_default' +\gset +\d+ :mat_hyper_cagg_index_default + Table "_timescaledb_internal._materialized_hypertable_51" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +----------+--------------------------+-----------+----------+---------+----------+--------------+------------- + bucket | timestamp with time zone | | not null | | plain | | + agg_2_2 | bytea | | | | extended | | + symbol | integer | | | | plain | | + chunk_id | integer | | | | plain | | +Indexes: + "_materialized_hypertable_51_bucket_idx" btree (bucket DESC) + "_materialized_hypertable_51_symbol_bucket_idx" btree (symbol, bucket DESC) +Triggers: + ts_insert_blocker BEFORE INSERT ON _timescaledb_internal._materialized_hypertable_51 FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker() +Child tables: _timescaledb_internal._hyper_51_110_chunk, + _timescaledb_internal._hyper_51_111_chunk + diff --git a/tsl/test/sql/continuous_aggs.sql b/tsl/test/sql/continuous_aggs.sql index 96610595a03..c92f5962079 100644 --- a/tsl/test/sql/continuous_aggs.sql +++ b/tsl/test/sql/continuous_aggs.sql @@ -1226,3 +1226,71 @@ FROM issue3248 GROUP BY 1,2; SELECT FROM issue3248 AS m, LATERAL(SELECT m FROM issue3248_cagg WHERE avg IS NULL LIMIT 1) AS lat; + +-- test that option create_group_indexes is taken into account +SET client_min_messages = ERROR; + +CREATE TABLE test_group_idx ( +time timestamptz, +symbol int, +value numeric +); + +select create_hypertable('test_group_idx', 'time'); + +insert into test_group_idx +(select generate_series( + '2020-01-01', + '2020-02-25', INTERVAL '30 sec' + ), + round(random()*10), + random()*5); + +create materialized view cagg_index_true +with (timescaledb.continuous, timescaledb.create_group_indexes = true) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; + +create materialized view cagg_index_false +with (timescaledb.continuous, timescaledb.create_group_indexes = false) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; + +create materialized view cagg_index_default +with (timescaledb.continuous) as +select + time_bucket('1 day', "time") as bucket, + sum(value), + symbol +from test_group_idx +group by bucket, symbol; + +-- make sure a group index has been created when explicitly asked for +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_true +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_true' +\gset + +\d+ :mat_hyper_cagg_index_true + +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_false +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_false' +\gset + +\d+ :mat_hyper_cagg_index_false + +SELECT format('%I.%I', materialization_hypertable_schema, +materialization_hypertable_name) AS mat_hyper_cagg_index_default +FROM timescaledb_information.continuous_aggregates where view_name like 'cagg_index_default' +\gset + +\d+ :mat_hyper_cagg_index_default