Skip to content

Commit

Permalink
Prevent range overlap for chunks using constraint
Browse files Browse the repository at this point in the history
This exclusion constraint prevents a single (time) value to exist in
multiple dimension slices.
We use `int4range(dimension_id, dimension_id, '[]')` as opposed to a
direct compariso of `dimension_id` to not create a depenbency on
`btree_gist`.
  • Loading branch information
feikesteenbergen committed Apr 15, 2024
1 parent ecf6bea commit 415c14a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions sql/pre_install/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ CREATE TABLE _timescaledb_catalog.dimension_slice (
-- table constraints
CONSTRAINT dimension_slice_pkey PRIMARY KEY (id),
CONSTRAINT dimension_slice_dimension_id_range_start_range_end_key UNIQUE (dimension_id, range_start, range_end),
CONSTRAINT dimension_slice_dimension_id_exclude EXCLUDE USING gist(int4range(dimension_id, dimension_id, '[]') WITH =, int8range(range_start, range_end, '[)') WITH &&),
CONSTRAINT dimension_slice_check CHECK (range_start <= range_end),
CONSTRAINT dimension_slice_dimension_id_fkey FOREIGN KEY (dimension_id) REFERENCES _timescaledb_catalog.dimension (id) ON DELETE CASCADE
);
Expand Down
13 changes: 13 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,16 @@ DROP FUNCTION IF EXISTS _timescaledb_functions.policy_job_error_retention_check(
--
-- END bgw_job_stat_history
--

--
-- START add exclusion constraint to dimension_slice
--
ALTER TABLE _timescaledb_catalog.dimension_slice
ADD CONSTRAINT dimension_slice_dimension_id_exclude
EXCLUDE USING gist(
int4range(dimension_id, dimension_id, '[]') WITH =,
int8range(range_start, range_end, '[)') WITH &&
);
--
-- END add exclusion constraint to dimension_slice
--
3 changes: 3 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,6 @@ SET
check_name = 'policy_job_error_retention_check'
WHERE
id = 2;

ALTER TABLE _timescaledb_catalog.dimension_slice
DROP CONSTRAINT dimension_slice_dimension_id_exclude;

0 comments on commit 415c14a

Please sign in to comment.