From e9e19ca6a8289fdd94026a1a599d6f47d773d015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADzio=20de=20Royes=20Mello?= Date: Thu, 8 Aug 2024 17:11:28 -0300 Subject: [PATCH] Fix create default indexes on chunks When creating a hypertable with default indexes and migrating data by setting `migrate_data=>true` is not creating the default indexes on chunks. Fixed it by changing the internal order or operations by first create the default indexes and after it migrate the data. Fixes #7190 --- .unreleased/pr_7191 | 1 + src/hypertable.c | 6 ++-- test/expected/create_hypertable.out | 46 +++++++++++++++++++++++++++++ test/sql/create_hypertable.sql | 20 +++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 .unreleased/pr_7191 diff --git a/.unreleased/pr_7191 b/.unreleased/pr_7191 new file mode 100644 index 00000000000..23437da124a --- /dev/null +++ b/.unreleased/pr_7191 @@ -0,0 +1 @@ +Fixes: #7191 Fix creating default indexes on chunks when migrating data diff --git a/src/hypertable.c b/src/hypertable.c index a8aba2dcef4..a0e6be8a5cb 100644 --- a/src/hypertable.c +++ b/src/hypertable.c @@ -2000,6 +2000,9 @@ ts_hypertable_create_from_info(Oid table_relid, int32 hypertable_id, uint32 flag ts_tablespace_attach_internal(&tspc_name, table_relid, false); } + if ((flags & HYPERTABLE_CREATE_DISABLE_DEFAULT_INDEXES) == 0) + ts_indexing_create_default_indexes(ht); + /* * Migrate data from the main table to chunks * @@ -2019,9 +2022,6 @@ ts_hypertable_create_from_info(Oid table_relid, int32 hypertable_id, uint32 flag insert_blocker_trigger_add(table_relid); - if ((flags & HYPERTABLE_CREATE_DISABLE_DEFAULT_INDEXES) == 0) - ts_indexing_create_default_indexes(ht); - ts_cache_release(hcache); return true; diff --git a/test/expected/create_hypertable.out b/test/expected/create_hypertable.out index 3ef4c728084..1e77e31ccb9 100644 --- a/test/expected/create_hypertable.out +++ b/test/expected/create_hypertable.out @@ -1069,3 +1069,49 @@ select create_hypertable ('test_schema.fk_parent', 'time'); ERROR: cannot have FOREIGN KEY constraints to hypertable "fk_parent" HINT: Remove all FOREIGN KEY constraints to table "fk_parent" before making it a hypertable. \set ON_ERROR_STOP 1 +-- create default indexes on chunks when migrating data +CREATE TABLE test(time TIMESTAMPTZ, val BIGINT); +CREATE INDEX test_val_idx ON test(val); +INSERT INTO test VALUES('2024-01-01 00:00:00-03', 500); +SELECT FROM create_hypertable('test', 'time', migrate_data=>TRUE); +-- +(1 row) + +-- should return ALL indexes for hypertable and chunk +SELECT * FROM test.show_indexes('test') ORDER BY 1; + Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace +---------------+---------+------+--------+---------+-----------+------------ + test_val_idx | {val} | | f | f | f | + test_time_idx | {time} | | f | f | f | +(2 rows) + +SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1, 2; + ch | Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace +------------------------------------------+--------------------------------------------------------+---------+------+--------+---------+-----------+------------ + _timescaledb_internal._hyper_23_23_chunk | _timescaledb_internal._hyper_23_23_chunk_test_val_idx | {val} | | f | f | f | + _timescaledb_internal._hyper_23_23_chunk | _timescaledb_internal._hyper_23_23_chunk_test_time_idx | {time} | | f | f | f | +(2 rows) + +DROP TABLE test; +-- don't create default indexes on chunks when migrating data +CREATE TABLE test(time TIMESTAMPTZ, val BIGINT); +CREATE INDEX test_val_idx ON test(val); +INSERT INTO test VALUES('2024-01-01 00:00:00-03', 500); +SELECT FROM create_hypertable('test', 'time', create_default_indexes => FALSE, migrate_data=>TRUE); +-- +(1 row) + +-- should NOT return default indexes for hypertable and chunk +-- only user indexes should be returned +SELECT * FROM test.show_indexes('test') ORDER BY 1; + Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace +--------------+---------+------+--------+---------+-----------+------------ + test_val_idx | {val} | | f | f | f | +(1 row) + +SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1, 2; + ch | Index | Columns | Expr | Unique | Primary | Exclusion | Tablespace +------------------------------------------+-------------------------------------------------------+---------+------+--------+---------+-----------+------------ + _timescaledb_internal._hyper_24_24_chunk | _timescaledb_internal._hyper_24_24_chunk_test_val_idx | {val} | | f | f | f | +(1 row) + diff --git a/test/sql/create_hypertable.sql b/test/sql/create_hypertable.sql index bef13e57d4b..af4fa200a07 100644 --- a/test/sql/create_hypertable.sql +++ b/test/sql/create_hypertable.sql @@ -645,3 +645,23 @@ create table test_schema.fk_child( ); select create_hypertable ('test_schema.fk_parent', 'time'); \set ON_ERROR_STOP 1 + +-- create default indexes on chunks when migrating data +CREATE TABLE test(time TIMESTAMPTZ, val BIGINT); +CREATE INDEX test_val_idx ON test(val); +INSERT INTO test VALUES('2024-01-01 00:00:00-03', 500); +SELECT FROM create_hypertable('test', 'time', migrate_data=>TRUE); +-- should return ALL indexes for hypertable and chunk +SELECT * FROM test.show_indexes('test') ORDER BY 1; +SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1, 2; +DROP TABLE test; + +-- don't create default indexes on chunks when migrating data +CREATE TABLE test(time TIMESTAMPTZ, val BIGINT); +CREATE INDEX test_val_idx ON test(val); +INSERT INTO test VALUES('2024-01-01 00:00:00-03', 500); +SELECT FROM create_hypertable('test', 'time', create_default_indexes => FALSE, migrate_data=>TRUE); +-- should NOT return default indexes for hypertable and chunk +-- only user indexes should be returned +SELECT * FROM test.show_indexes('test') ORDER BY 1; +SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1, 2;