From 93f826b88d7310f8416103b7cd60f3855b37a930 Mon Sep 17 00:00:00 2001 From: Konstantina Skovola Date: Thu, 21 Apr 2022 14:22: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 | 68 +++++++++++++++++++++++++++ tsl/test/sql/continuous_aggs.sql | 55 ++++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1a95a18f23..0cce6de3e2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ accidentally triggering the load of a previous DB version.** * #3899 Fix segfault in Continuous Aggregates * #4225 Fix TRUNCATE error as non-owner on hypertable * #4259 Fix logic bug in extension update script +* #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..9ca3cf991ac 100644 --- a/tsl/test/expected/continuous_aggs.out +++ b/tsl/test/expected/continuous_aggs.out @@ -1738,3 +1738,71 @@ 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 t, round(random()*10), random()*5 +from generate_series('2020-01-01', '2020-02-25', INTERVAL '12 hours') t; +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; +-- see corresponding materialization_hypertables +select view_name, materialization_hypertable_name from timescaledb_information.continuous_aggregates ca +where view_name like 'cagg_index_%'; + view_name | materialization_hypertable_name +--------------------+--------------------------------- + cagg_index_default | _materialized_hypertable_51 + cagg_index_false | _materialized_hypertable_50 + cagg_index_true | _materialized_hypertable_49 +(3 rows) + +-- now make sure a group index has been created when explicitly asked for +select i.* +from pg_indexes i +join pg_class c + on schemaname = relnamespace::regnamespace::text + and tablename = relname +where tablename in (select materialization_hypertable_name from timescaledb_information.continuous_aggregates +where view_name like 'cagg_index_%') +order by tablename; + schemaname | tablename | indexname | tablespace | indexdef +-----------------------+-----------------------------+-----------------------------------------------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------- + _timescaledb_internal | _materialized_hypertable_49 | _materialized_hypertable_49_bucket_idx | | CREATE INDEX _materialized_hypertable_49_bucket_idx ON _timescaledb_internal._materialized_hypertable_49 USING btree (bucket DESC) + _timescaledb_internal | _materialized_hypertable_49 | _materialized_hypertable_49_symbol_bucket_idx | | CREATE INDEX _materialized_hypertable_49_symbol_bucket_idx ON _timescaledb_internal._materialized_hypertable_49 USING btree (symbol, bucket DESC) + _timescaledb_internal | _materialized_hypertable_50 | _materialized_hypertable_50_bucket_idx | | CREATE INDEX _materialized_hypertable_50_bucket_idx ON _timescaledb_internal._materialized_hypertable_50 USING btree (bucket DESC) + _timescaledb_internal | _materialized_hypertable_51 | _materialized_hypertable_51_bucket_idx | | CREATE INDEX _materialized_hypertable_51_bucket_idx ON _timescaledb_internal._materialized_hypertable_51 USING btree (bucket DESC) + _timescaledb_internal | _materialized_hypertable_51 | _materialized_hypertable_51_symbol_bucket_idx | | CREATE INDEX _materialized_hypertable_51_symbol_bucket_idx ON _timescaledb_internal._materialized_hypertable_51 USING btree (symbol, bucket DESC) +(5 rows) + diff --git a/tsl/test/sql/continuous_aggs.sql b/tsl/test/sql/continuous_aggs.sql index 96610595a03..1801e70e6a3 100644 --- a/tsl/test/sql/continuous_aggs.sql +++ b/tsl/test/sql/continuous_aggs.sql @@ -1226,3 +1226,58 @@ 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 t, round(random()*10), random()*5 +from generate_series('2020-01-01', '2020-02-25', INTERVAL '12 hours') t; + +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; + +-- see corresponding materialization_hypertables +select view_name, materialization_hypertable_name from timescaledb_information.continuous_aggregates ca +where view_name like 'cagg_index_%'; +-- now make sure a group index has been created when explicitly asked for +select i.* +from pg_indexes i +join pg_class c + on schemaname = relnamespace::regnamespace::text + and tablename = relname +where tablename in (select materialization_hypertable_name from timescaledb_information.continuous_aggregates +where view_name like 'cagg_index_%') +order by tablename;