Skip to content

Commit 7cece1b

Browse files
committed
Tests for foreign keys into hypertable with OSM chunk
1 parent a1f8e48 commit 7cece1b

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

tsl/test/expected/chunk_utils_internal.out

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,56 @@ INSERT INTO osm_slice_update VALUES (1);
15671567
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
15681568
INSERT INTO osm_slice_update VALUES (1);
15691569
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
1570+
\set ON_ERROR_STOP 1
1571+
--TEST hypertable with foreign key into it
1572+
\c :TEST_DBNAME :ROLE_4
1573+
CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float);
1574+
SELECT table_name FROM create_hypertable('hyper_fk', 'ts');
1575+
table_name
1576+
------------
1577+
hyper_fk
1578+
(1 row)
1579+
1580+
INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00+00', 'd1', 1.0);
1581+
\c postgres_fdw_db :ROLE_4
1582+
CREATE TABLE fdw_hyper_fk(ts timestamptz NOT NULL, device text, value float);
1583+
INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00:00+00', 'd2', 2.0);
1584+
\c :TEST_DBNAME :ROLE_4
1585+
-- this is a stand-in for the OSM table
1586+
CREATE FOREIGN TABLE child_hyper_fk
1587+
(ts timestamptz NOT NULL, device text, value float)
1588+
SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_fk');
1589+
SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_fk', 'child_hyper_fk');
1590+
attach_osm_table_chunk
1591+
------------------------
1592+
t
1593+
(1 row)
1594+
1595+
--create table with fk into hypertable
1596+
CREATE TABLE event(ts timestamptz REFERENCES hyper_fk(ts) , info text);
1597+
\set ON_ERROR_STOP 0
1598+
-- NOTE: current behavior is to allow inserts/deletes from PG tables when data
1599+
-- references OSM table.
1600+
--insert referencing OSM chunk
1601+
INSERT INTO event VALUES( '2021-05-05 00:00:00+00' , 'osm_chunk_ts');
1602+
--insert referencing non-existent value
1603+
INSERT INTO event VALUES( '2020-01-02 00:00:00+00' , 'does_not_exist_ts');
1604+
ERROR: insert or update on table "event" violates foreign key constraint "event_ts_fkey"
1605+
INSERT INTO event VALUES( '2020-01-01 00:00:00+00' , 'chunk_ts');
1606+
SELECT * FROM event ORDER BY ts;
1607+
ts | info
1608+
------------------------------+--------------
1609+
Tue Dec 31 16:00:00 2019 PST | chunk_ts
1610+
Tue May 04 17:00:00 2021 PDT | osm_chunk_ts
1611+
(2 rows)
1612+
1613+
DELETE FROM event WHERE info = 'osm_chunk_ts';
1614+
DELETE FROM event WHERE info = 'chunk_ts';
1615+
SELECT * FROM event ORDER BY ts;
1616+
ts | info
1617+
----+------
1618+
(0 rows)
1619+
15701620
\set ON_ERROR_STOP 1
15711621
-- clean up databases created
15721622
\c :TEST_DBNAME :ROLE_SUPERUSER

tsl/test/sql/chunk_utils_internal.sql

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,10 @@ DROP INDEX hyper_constr_mid_idx;
656656
CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float);
657657
SELECT table_name FROM create_hypertable('hyper_fk', 'ts');
658658

659-
INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00', 'd1', 1.0);
659+
INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00+00', 'd1', 1.0);
660660
\c postgres_fdw_db :ROLE_4
661661
CREATE TABLE fdw_hyper_fk(ts timestamptz NOT NULL, device text, value float);
662-
INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00', 'd2', 2.0);
662+
INSERT INTO fdw_hyper_fk VALUES( '2021-05-05 00:00:00+00', 'd2', 2.0);
663663

664664
\c :TEST_DBNAME :ROLE_4
665665
-- 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
670670

671671
--create table with fk into hypertable
672672
CREATE TABLE event(ts timestamptz REFERENCES hyper_fk(ts) , info text);
673-
INSERT INTO event VALUES( '2021-05-05 00:00' , 'osm_chunk_ts');
674-
673+
\set ON_ERROR_STOP 0
674+
-- NOTE: current behavior is to allow inserts/deletes from PG tables when data
675+
-- references OSM table.
676+
--insert referencing OSM chunk
677+
INSERT INTO event VALUES( '2021-05-05 00:00:00+00' , 'osm_chunk_ts');
678+
--insert referencing non-existent value
679+
INSERT INTO event VALUES( '2020-01-02 00:00:00+00' , 'does_not_exist_ts');
680+
INSERT INTO event VALUES( '2020-01-01 00:00:00+00' , 'chunk_ts');
681+
SELECT * FROM event ORDER BY ts;
682+
683+
DELETE FROM event WHERE info = 'osm_chunk_ts';
684+
DELETE FROM event WHERE info = 'chunk_ts';
685+
SELECT * FROM event ORDER BY ts;
686+
\set ON_ERROR_STOP 1
675687
-- clean up databases created
676688
\c :TEST_DBNAME :ROLE_SUPERUSER
677689
DROP DATABASE postgres_fdw_db WITH (FORCE);

0 commit comments

Comments
 (0)