Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent range overlap for chunks using constraint #6828

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

feikesteenbergen
Copy link
Member

This exclusion constraint prevents a single (time) value to exist in multiple dimension slices.

@@ -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),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this unique constraint could be dropped, its index though is still useful, and the UNIQUE isn't wrong, so we should just let it be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unique is at least misleading so we should drop it, but we need to keep the index as that is used internally

@@ -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 &&),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage here:

https://www.postgresql.org/docs/current/btree-gist.html#BTREE-GIST-EXAMPLE-USAGE

Using int4range(dimension_id, dimension_id, '[]') to not create a dependency on the btree_gist extension. This works just as well, with the downside of some more storage space required.

@feikesteenbergen feikesteenbergen marked this pull request as ready for review April 15, 2024 08:04
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`.
@fabriziomello
Copy link
Contributor

Unfortunately it will not work because we use low level functions to update catalog tables and we only check for nulls, so no other constraints will be enforced when updating tsdb catalog tables.

ADD CONSTRAINT dimension_slice_dimension_id_exclude
EXCLUDE USING gist(
int4range(dimension_id, dimension_id, '[]') WITH =,
int8range(range_start, range_end, '[]') WITH &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int8range(range_start, range_end, '[]') WITH &&
int8range(range_start, range_end, '[)') WITH &&

range_end should be exclusive

@@ -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 &&),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CONSTRAINT dimension_slice_dimension_id_exclude EXCLUDE USING gist(int4range(dimension_id, dimension_id, '[]') WITH =, int8range(range_start, range_end, '[]') WITH &&),
CONSTRAINT dimension_slice_dimension_id_exclude EXCLUDE USING gist(int4range(dimension_id, dimension_id, '[]') WITH =, int8range(range_start, range_end, '[)') WITH &&),

range_end should be exclusive

@svenklemm
Copy link
Member

Unfortunately it will not work because we use low level functions to update catalog tables and we only check for nulls, so no other constraints will be enforced when updating tsdb catalog tables.

Constraints enforced by indexes will work

@mkindahl mkindahl removed the request for review from jnidzwetzki April 30, 2024 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants