-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FK constraints for compressed chunks
When foreign key support for compressed chunks was added we moved the FK constraint from the uncompressed chunk to the compressed chunk as part of compress_chunk and moved it back as part of decompress_chunk. With the addition of partially compressed chunks in 2.10.x this approach was no longer sufficient and the FK constraint needs to be present on both the uncompressed and the compressed chunk.
- Loading branch information
Showing
6 changed files
with
87 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-- This file and its contents are licensed under the Timescale License. | ||
-- Please see the included NOTICE for copyright information and | ||
-- LICENSE-TIMESCALE for a copy of the license. | ||
-- test foreign key constraints with compression | ||
CREATE TABLE keys(time timestamptz unique); | ||
CREATE TABLE ht_with_fk(time timestamptz); | ||
SELECT create_hypertable('ht_with_fk','time'); | ||
NOTICE: adding not-null constraint to column "time" | ||
create_hypertable | ||
------------------------- | ||
(1,public,ht_with_fk,t) | ||
(1 row) | ||
|
||
ALTER TABLE ht_with_fk ADD CONSTRAINT keys FOREIGN KEY (time) REFERENCES keys(time) ON DELETE CASCADE; | ||
ALTER TABLE ht_with_fk SET (timescaledb.compress,timescaledb.compress_segmentby='time'); | ||
-- no keys added yet so any insert into ht_with_fk should fail | ||
\set ON_ERROR_STOP 0 | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
ERROR: insert or update on table "_hyper_1_1_chunk" violates foreign key constraint "1_1_keys" | ||
\set ON_ERROR_STOP 1 | ||
-- create a key in the referenced table | ||
INSERT INTO keys SELECT '2000-01-01'; | ||
-- now the insert should succeed | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
SELECT compress_chunk(ch) FROM show_chunks('ht_with_fk') ch; | ||
compress_chunk | ||
---------------------------------------- | ||
_timescaledb_internal._hyper_1_2_chunk | ||
(1 row) | ||
|
||
-- insert should still succeed after compression | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
-- inserting key not present in keys should fail | ||
\set ON_ERROR_STOP 0 | ||
INSERT INTO ht_with_fk SELECT '2000-01-01 0:00:01'; | ||
ERROR: insert or update on table "_hyper_1_2_chunk" violates foreign key constraint "2_2_keys" | ||
\set ON_ERROR_STOP 1 | ||
SELECT conrelid::regclass,conname,confrelid::regclass FROM pg_constraint WHERE contype = 'f' AND confrelid = 'keys'::regclass ORDER BY conrelid::regclass::text,conname; | ||
conrelid | conname | confrelid | ||
------------------------------------------------+----------+----------- | ||
ht_with_fk | keys | keys | ||
_timescaledb_internal.compress_hyper_2_3_chunk | keys | keys | ||
_timescaledb_internal._hyper_1_2_chunk | 2_2_keys | keys | ||
(3 rows) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-- This file and its contents are licensed under the Timescale License. | ||
-- Please see the included NOTICE for copyright information and | ||
-- LICENSE-TIMESCALE for a copy of the license. | ||
|
||
-- test foreign key constraints with compression | ||
CREATE TABLE keys(time timestamptz unique); | ||
CREATE TABLE ht_with_fk(time timestamptz); | ||
SELECT create_hypertable('ht_with_fk','time'); | ||
|
||
ALTER TABLE ht_with_fk ADD CONSTRAINT keys FOREIGN KEY (time) REFERENCES keys(time) ON DELETE CASCADE; | ||
ALTER TABLE ht_with_fk SET (timescaledb.compress,timescaledb.compress_segmentby='time'); | ||
|
||
-- no keys added yet so any insert into ht_with_fk should fail | ||
\set ON_ERROR_STOP 0 | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
\set ON_ERROR_STOP 1 | ||
|
||
-- create a key in the referenced table | ||
INSERT INTO keys SELECT '2000-01-01'; | ||
|
||
-- now the insert should succeed | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
|
||
SELECT compress_chunk(ch) FROM show_chunks('ht_with_fk') ch; | ||
|
||
-- insert should still succeed after compression | ||
INSERT INTO ht_with_fk SELECT '2000-01-01'; | ||
|
||
-- inserting key not present in keys should fail | ||
\set ON_ERROR_STOP 0 | ||
INSERT INTO ht_with_fk SELECT '2000-01-01 0:00:01'; | ||
\set ON_ERROR_STOP 1 | ||
|
||
SELECT conrelid::regclass,conname,confrelid::regclass FROM pg_constraint WHERE contype = 'f' AND confrelid = 'keys'::regclass ORDER BY conrelid::regclass::text,conname; | ||
|