Skip to content

Commit

Permalink
Fix create default indexes on chunks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabriziomello committed Aug 12, 2024
1 parent 4ebbd85 commit e9e19ca
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions .unreleased/pr_7191
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #7191 Fix creating default indexes on chunks when migrating data
6 changes: 3 additions & 3 deletions src/hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions test/expected/create_hypertable.out
Original file line number Diff line number Diff line change
Expand Up @@ -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)

20 changes: 20 additions & 0 deletions test/sql/create_hypertable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit e9e19ca

Please sign in to comment.