Skip to content

Commit

Permalink
Add more foreign key tests for OSM chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
gayyappan committed Jul 31, 2024
1 parent 3572635 commit 01b5de5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
57 changes: 46 additions & 11 deletions tsl/test/expected/chunk_utils_internal.out
Original file line number Diff line number Diff line change
Expand Up @@ -867,9 +867,20 @@ ORDER BY table_name;
CREATE TABLE measure( id integer PRIMARY KEY, mname varchar(10));
INSERT INTO measure VALUES( 1, 'temp');
INSERT INTO measure VALUES( 2, 'osmtemp');
CREATE TABLE devices( id integer PRIMARY KEY, devname varchar(10) );
INSERT INTO devices VALUES( 111, 'dev1');
INSERT INTO devices VALUES( 222, 'osmdev');
CREATE TABLE devicesref( id integer PRIMARY KEY, devname varchar(10) );
INSERT INTO devicesref VALUES( 44, 'devref');
INSERT INTO devicesref VALUES( 55, 'osmdevref');
CREATE TABLE hyper_constr ( id integer, time bigint, temp float, mid integer
,dev integer
,devref integer
,PRIMARY KEY (id, time)
,FOREIGN KEY ( mid) REFERENCES measure(id)
,FOREIGN KEY ( dev ) REFERENCES devices(id) ON DELETE CASCADE
,FOREIGN KEY ( devref ) REFERENCES devicesref(id) ON DELETE
RESTRICT
,CHECK ( temp > 10)
);
SELECT create_hypertable('hyper_constr', 'time', chunk_time_interval => 10);
Expand All @@ -878,14 +889,14 @@ SELECT create_hypertable('hyper_constr', 'time', chunk_time_interval => 10);
(7,public,hyper_constr,t)
(1 row)

INSERT INTO hyper_constr VALUES( 10, 200, 22, 1);
INSERT INTO hyper_constr VALUES( 10, 200, 22, 1, 111, 44);
\c postgres_fdw_db :ROLE_4
CREATE TABLE fdw_hyper_constr(id integer, time bigint, temp float, mid integer);
INSERT INTO fdw_hyper_constr VALUES( 10, 100, 33, 2);
CREATE TABLE fdw_hyper_constr(id integer, time bigint, temp float, mid integer, dev integer, devref integer);
INSERT INTO fdw_hyper_constr VALUES( 10, 100, 33, 2, 222, 55);
\c :TEST_DBNAME :ROLE_4
-- this is a stand-in for the OSM table
CREATE FOREIGN TABLE child_hyper_constr
( id integer NOT NULL, time bigint NOT NULL, temp float, mid integer)
( id integer NOT NULL, time bigint NOT NULL, temp float, mid integer, dev integer, devref integer)
SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_constr');
--check constraints are automatically added for the foreign table
SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_constr', 'child_hyper_constr');
Expand Down Expand Up @@ -913,10 +924,10 @@ ORDER BY table_name;
(2 rows)

SELECT * FROM hyper_constr order by time;
id | time | temp | mid
----+------+------+-----
10 | 100 | 33 | 2
10 | 200 | 22 | 1
id | time | temp | mid | dev | devref
----+------+------+-----+-----+--------
10 | 100 | 33 | 2 | 222 | 55
10 | 200 | 22 | 1 | 111 | 44
(2 rows)

--verify the check constraint exists on the OSM chunk
Expand All @@ -934,11 +945,35 @@ BEGIN;
DELETE FROM measure where id = 1;
ERROR: update or delete on table "measure" violates foreign key constraint "hyper_constr_mid_fkey" on table "hyper_constr"
ROLLBACK;
--this touches osm chunk. should succeed silently without deleteing any data
\set ON_ERROR_STOP 1
--this touches osm chunk. should succeed silently without deleting any data
BEGIN;
DELETE FROM measure where id = 2;
SELECT * FROM measure order by id;
id | mname
----+-------
1 | temp
(1 row)

ROLLBACK;
BEGIN;
DELETE FROM devices where id = 222;
SELECT * FROM devices order by id;
id | devname
-----+---------
111 | dev1
(1 row)

ROLLBACK;
BEGIN;
DELETE FROM devicesref where id = 55;
SELECT * FROM devicesref order by id;
id | devname
----+---------
44 | devref
(1 row)

ROLLBACK;
\set ON_ERROR_STOP 1
--TEST retention policy is applied on OSM chunk by calling registered callback
CREATE OR REPLACE FUNCTION dummy_now_smallint() RETURNS BIGINT LANGUAGE SQL IMMUTABLE as 'SELECT 500::bigint' ;
SELECT set_integer_now_func('hyper_constr', 'dummy_now_smallint');
Expand Down Expand Up @@ -1191,7 +1226,7 @@ _timescaledb_catalog.chunk c, _timescaledb_catalog.chunk_constraint cc WHERE c.h
AND c.id = cc.chunk_id;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | dropped | status | osm_chunk | chunk_id | dimension_slice_id | constraint_name | hypertable_constraint_name
----+---------------+-----------------------+--------------------+---------------------+---------+--------+-----------+----------+--------------------+-----------------------------+----------------------------
25 | 15 | _timescaledb_internal | _hyper_15_25_chunk | | f | 0 | f | 25 | | 25_3_test_multicon_time_key | test_multicon_time_key
25 | 15 | _timescaledb_internal | _hyper_15_25_chunk | | f | 0 | f | 25 | | 25_5_test_multicon_time_key | test_multicon_time_key
25 | 15 | _timescaledb_internal | _hyper_15_25_chunk | | f | 0 | f | 25 | 22 | constraint_22 |
(2 rows)

Expand Down
36 changes: 30 additions & 6 deletions tsl/test/sql/chunk_utils_internal.sql
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,36 @@ CREATE TABLE measure( id integer PRIMARY KEY, mname varchar(10));
INSERT INTO measure VALUES( 1, 'temp');
INSERT INTO measure VALUES( 2, 'osmtemp');

CREATE TABLE devices( id integer PRIMARY KEY, devname varchar(10) );
INSERT INTO devices VALUES( 111, 'dev1');
INSERT INTO devices VALUES( 222, 'osmdev');

CREATE TABLE devicesref( id integer PRIMARY KEY, devname varchar(10) );
INSERT INTO devicesref VALUES( 44, 'devref');
INSERT INTO devicesref VALUES( 55, 'osmdevref');

CREATE TABLE hyper_constr ( id integer, time bigint, temp float, mid integer
,dev integer
,devref integer
,PRIMARY KEY (id, time)
,FOREIGN KEY ( mid) REFERENCES measure(id)
,FOREIGN KEY ( dev ) REFERENCES devices(id) ON DELETE CASCADE
,FOREIGN KEY ( devref ) REFERENCES devicesref(id) ON DELETE
RESTRICT
,CHECK ( temp > 10)
);

SELECT create_hypertable('hyper_constr', 'time', chunk_time_interval => 10);
INSERT INTO hyper_constr VALUES( 10, 200, 22, 1);
INSERT INTO hyper_constr VALUES( 10, 200, 22, 1, 111, 44);

\c postgres_fdw_db :ROLE_4
CREATE TABLE fdw_hyper_constr(id integer, time bigint, temp float, mid integer);
INSERT INTO fdw_hyper_constr VALUES( 10, 100, 33, 2);
CREATE TABLE fdw_hyper_constr(id integer, time bigint, temp float, mid integer, dev integer, devref integer);
INSERT INTO fdw_hyper_constr VALUES( 10, 100, 33, 2, 222, 55);

\c :TEST_DBNAME :ROLE_4
-- this is a stand-in for the OSM table
CREATE FOREIGN TABLE child_hyper_constr
( id integer NOT NULL, time bigint NOT NULL, temp float, mid integer)
( id integer NOT NULL, time bigint NOT NULL, temp float, mid integer, dev integer, devref integer)
SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_constr');

--check constraints are automatically added for the foreign table
Expand All @@ -517,12 +530,23 @@ where conrelid = 'child_hyper_constr'::regclass ORDER BY 1;
BEGIN;
DELETE FROM measure where id = 1;
ROLLBACK;
\set ON_ERROR_STOP 1

--this touches osm chunk. should succeed silently without deleteing any data
--this touches osm chunk. should succeed silently without deleting any data
BEGIN;
DELETE FROM measure where id = 2;
SELECT * FROM measure order by id;
ROLLBACK;

BEGIN;
DELETE FROM devices where id = 222;
SELECT * FROM devices order by id;
ROLLBACK;

BEGIN;
DELETE FROM devicesref where id = 55;
SELECT * FROM devicesref order by id;
ROLLBACK;
\set ON_ERROR_STOP 1

--TEST retention policy is applied on OSM chunk by calling registered callback
CREATE OR REPLACE FUNCTION dummy_now_smallint() RETURNS BIGINT LANGUAGE SQL IMMUTABLE as 'SELECT 500::bigint' ;
Expand Down

0 comments on commit 01b5de5

Please sign in to comment.