From a1f8e48572d19dd97fdbbed007eb4f9d9de9e397 Mon Sep 17 00:00:00 2001 From: gayyappan Date: Wed, 31 Jul 2024 17:34:24 -0400 Subject: [PATCH 1/2] Fix issue with adding foreign key constraint in hte presence of OSM chunks We cannot create a foreign key constraint on a foreign table. --- src/foreign_key.c | 29 +++++++++++++++------------ tsl/test/sql/chunk_utils_internal.sql | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/foreign_key.c b/src/foreign_key.c index f7c4b6f862e..00c969eb5cc 100644 --- a/src/foreign_key.c +++ b/src/foreign_key.c @@ -80,21 +80,24 @@ propagate_fk(Relation ht_rel, HeapTuple fk_tuple, List *chunks) foreach (lc, chunks) { Chunk *chunk = lfirst(lc); - clone_constraint_on_chunk(chunk, - ht_rel, - fk, - numfks, - conkey, - confkey, - conpfeqop, - conppeqop, - conffeqop, + if (!chunk->fd.osm_chunk) + { + clone_constraint_on_chunk(chunk, + ht_rel, + fk, + numfks, + conkey, + confkey, + conpfeqop, + conppeqop, + conffeqop, #if PG15_GE - numfkdelsetcols, - confdelsetcols, + numfkdelsetcols, + confdelsetcols, #endif - parentDelTrigger, - parentUpdTrigger); + parentDelTrigger, + parentUpdTrigger); + } } } diff --git a/tsl/test/sql/chunk_utils_internal.sql b/tsl/test/sql/chunk_utils_internal.sql index 3d4a7fd1c10..ab150a85d6a 100644 --- a/tsl/test/sql/chunk_utils_internal.sql +++ b/tsl/test/sql/chunk_utils_internal.sql @@ -651,6 +651,27 @@ DROP INDEX hyper_constr_mid_idx; \i include/chunk_utils_internal_orderedappend.sql +--TEST hypertable with foreign key into it +\c :TEST_DBNAME :ROLE_4 +CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float); +SELECT table_name FROM create_hypertable('hyper_fk', 'ts'); + +INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00', 'd1', 1.0); +\c postgres_fdw_db :ROLE_4 +CREATE TABLE fdw_hyper_fk(ts timestamptz NOT NULL, device text, value float); +INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00', 'd2', 2.0); + +\c :TEST_DBNAME :ROLE_4 +-- this is a stand-in for the OSM table +CREATE FOREIGN TABLE child_hyper_fk +(ts timestamptz NOT NULL, device text, value float) + SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_fk'); +SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_fk', 'child_hyper_fk'); + +--create table with fk into hypertable +CREATE TABLE event(ts timestamptz REFERENCES hyper_fk(ts) , info text); +INSERT INTO event VALUES( '2021-05-05 00:00' , 'osm_chunk_ts'); + -- clean up databases created \c :TEST_DBNAME :ROLE_SUPERUSER DROP DATABASE postgres_fdw_db WITH (FORCE); From 7cece1b233ec2a70bb42e0cc686c165d6ba2383e Mon Sep 17 00:00:00 2001 From: gayyappan Date: Tue, 6 Aug 2024 10:05:53 -0400 Subject: [PATCH 2/2] Tests for foreign keys into hypertable with OSM chunk --- tsl/test/expected/chunk_utils_internal.out | 50 ++++++++++++++++++++++ tsl/test/sql/chunk_utils_internal.sql | 20 +++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/tsl/test/expected/chunk_utils_internal.out b/tsl/test/expected/chunk_utils_internal.out index 6fa4bf31c8f..e89357f2efb 100644 --- a/tsl/test/expected/chunk_utils_internal.out +++ b/tsl/test/expected/chunk_utils_internal.out @@ -1567,6 +1567,56 @@ INSERT INTO osm_slice_update VALUES (1); psql:include/chunk_utils_internal_orderedappend.sql:178: ERROR: Cannot insert into tiered chunk range of public.osm_slice_update - attempt to create new chunk with range [0 10] failed INSERT INTO osm_slice_update VALUES (1); psql:include/chunk_utils_internal_orderedappend.sql:179: ERROR: Cannot insert into tiered chunk range of public.osm_slice_update - attempt to create new chunk with range [0 10] failed +\set ON_ERROR_STOP 1 +--TEST hypertable with foreign key into it +\c :TEST_DBNAME :ROLE_4 +CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float); +SELECT table_name FROM create_hypertable('hyper_fk', 'ts'); + table_name +------------ + hyper_fk +(1 row) + +INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00+00', 'd1', 1.0); +\c postgres_fdw_db :ROLE_4 +CREATE TABLE fdw_hyper_fk(ts timestamptz NOT NULL, device text, value float); +INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00:00+00', 'd2', 2.0); +\c :TEST_DBNAME :ROLE_4 +-- this is a stand-in for the OSM table +CREATE FOREIGN TABLE child_hyper_fk +(ts timestamptz NOT NULL, device text, value float) + SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_fk'); +SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_fk', 'child_hyper_fk'); + attach_osm_table_chunk +------------------------ + t +(1 row) + +--create table with fk into hypertable +CREATE TABLE event(ts timestamptz REFERENCES hyper_fk(ts) , info text); +\set ON_ERROR_STOP 0 +-- NOTE: current behavior is to allow inserts/deletes from PG tables when data +-- references OSM table. +--insert referencing OSM chunk +INSERT INTO event VALUES( '2021-05-05 00:00:00+00' , 'osm_chunk_ts'); +--insert referencing non-existent value +INSERT INTO event VALUES( '2020-01-02 00:00:00+00' , 'does_not_exist_ts'); +ERROR: insert or update on table "event" violates foreign key constraint "event_ts_fkey" +INSERT INTO event VALUES( '2020-01-01 00:00:00+00' , 'chunk_ts'); +SELECT * FROM event ORDER BY ts; + ts | info +------------------------------+-------------- + Tue Dec 31 16:00:00 2019 PST | chunk_ts + Tue May 04 17:00:00 2021 PDT | osm_chunk_ts +(2 rows) + +DELETE FROM event WHERE info = 'osm_chunk_ts'; +DELETE FROM event WHERE info = 'chunk_ts'; +SELECT * FROM event ORDER BY ts; + ts | info +----+------ +(0 rows) + \set ON_ERROR_STOP 1 -- clean up databases created \c :TEST_DBNAME :ROLE_SUPERUSER diff --git a/tsl/test/sql/chunk_utils_internal.sql b/tsl/test/sql/chunk_utils_internal.sql index ab150a85d6a..320210a2694 100644 --- a/tsl/test/sql/chunk_utils_internal.sql +++ b/tsl/test/sql/chunk_utils_internal.sql @@ -656,10 +656,10 @@ DROP INDEX hyper_constr_mid_idx; CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float); SELECT table_name FROM create_hypertable('hyper_fk', 'ts'); -INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00', 'd1', 1.0); +INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00+00', 'd1', 1.0); \c postgres_fdw_db :ROLE_4 CREATE TABLE fdw_hyper_fk(ts timestamptz NOT NULL, device text, value float); -INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00', 'd2', 2.0); +INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00:00+00', 'd2', 2.0); \c :TEST_DBNAME :ROLE_4 -- this is a stand-in for the OSM table @@ -670,8 +670,20 @@ SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_fk', 'child_hyper_fk --create table with fk into hypertable CREATE TABLE event(ts timestamptz REFERENCES hyper_fk(ts) , info text); -INSERT INTO event VALUES( '2021-05-05 00:00' , 'osm_chunk_ts'); - +\set ON_ERROR_STOP 0 +-- NOTE: current behavior is to allow inserts/deletes from PG tables when data +-- references OSM table. +--insert referencing OSM chunk +INSERT INTO event VALUES( '2021-05-05 00:00:00+00' , 'osm_chunk_ts'); +--insert referencing non-existent value +INSERT INTO event VALUES( '2020-01-02 00:00:00+00' , 'does_not_exist_ts'); +INSERT INTO event VALUES( '2020-01-01 00:00:00+00' , 'chunk_ts'); +SELECT * FROM event ORDER BY ts; + +DELETE FROM event WHERE info = 'osm_chunk_ts'; +DELETE FROM event WHERE info = 'chunk_ts'; +SELECT * FROM event ORDER BY ts; +\set ON_ERROR_STOP 1 -- clean up databases created \c :TEST_DBNAME :ROLE_SUPERUSER DROP DATABASE postgres_fdw_db WITH (FORCE);