Skip to content

Commit

Permalink
Chunk_create must add existing table or fail
Browse files Browse the repository at this point in the history
Earlier this function have completed successfully if the requested
range already existed - regardless an existing table was supplied
or not.
  • Loading branch information
kgyrtkirk committed Jun 15, 2023
1 parent 9c7ae3e commit 10245dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .unreleased/bugfix_chunk_create
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #5788 Chunk_create must add existing table or fail
2 changes: 1 addition & 1 deletion src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ ts_chunk_find_or_create_without_cuts(const Hypertable *ht, Hypercube *hc, const

/* We can only use an existing chunk if it has identical dimensional
* constraints. Otherwise, throw an error */
if (!ts_hypercube_equal(stub->cube, hc))
if (OidIsValid(chunk_table_relid) || !ts_hypercube_equal(stub->cube, hc))
ereport(ERROR,
(errcode(ERRCODE_TS_CHUNK_COLLISION),
errmsg("chunk creation failed due to collision")));
Expand Down
7 changes: 7 additions & 0 deletions tsl/test/expected/chunk_api.out
Original file line number Diff line number Diff line change
Expand Up @@ -936,12 +936,19 @@ ERROR: child table is missing constraint "chunkapi_temp_check"
-- Add the missing CHECK constraint. Note that the name must be the
-- same as on the parent table.
ALTER TABLE newchunk ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
CREATE TABLE newchunk2 as select * from newchunk;
ALTER TABLE newchunk2 ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
SELECT * FROM _timescaledb_internal.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk');
chunk_id | hypertable_id | schema_name | table_name | relkind | slices | created
----------+---------------+-----------------------+--------------------+---------+------------------------------------------------+---------
13 | 11 | _timescaledb_internal | _hyper_10_10_chunk | r | {"time": [1514419200000000, 1515024000000000]} | t
(1 row)

-- adding an existing table to an exiting range must fail
\set ON_ERROR_STOP 0
SELECT * FROM _timescaledb_internal.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk2');
ERROR: chunk creation failed due to collision
\set ON_ERROR_STOP 1
-- Show the chunk and that names are what we'd expect
SELECT
:'CHUNK_SCHEMA' AS expected_schema,
Expand Down
6 changes: 6 additions & 0 deletions tsl/test/sql/chunk_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,13 @@ SELECT * FROM _timescaledb_internal.create_chunk('chunkapi', :'SLICES', :'CHUNK_
-- Add the missing CHECK constraint. Note that the name must be the
-- same as on the parent table.
ALTER TABLE newchunk ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
CREATE TABLE newchunk2 as select * from newchunk;
ALTER TABLE newchunk2 ADD CONSTRAINT chunkapi_temp_check CHECK (temp > 0);
SELECT * FROM _timescaledb_internal.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk');
-- adding an existing table to an exiting range must fail
\set ON_ERROR_STOP 0
SELECT * FROM _timescaledb_internal.create_chunk('chunkapi', :'SLICES', :'CHUNK_SCHEMA', :'CHUNK_NAME', 'newchunk2');
\set ON_ERROR_STOP 1

-- Show the chunk and that names are what we'd expect
SELECT
Expand Down

0 comments on commit 10245dd

Please sign in to comment.