Skip to content

Commit 32ed50b

Browse files
committed
Fix issue with adding foreign key constraint in hte presence of OSM chunks
We cannot create a foreign key constraint on a foreign table.
1 parent d9a85fd commit 32ed50b

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/foreign_key.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,24 @@ propagate_fk(Relation ht_rel, HeapTuple fk_tuple, List *chunks)
8080
foreach (lc, chunks)
8181
{
8282
Chunk *chunk = lfirst(lc);
83-
clone_constraint_on_chunk(chunk,
84-
ht_rel,
85-
fk,
86-
numfks,
87-
conkey,
88-
confkey,
89-
conpfeqop,
90-
conppeqop,
91-
conffeqop,
83+
if (!chunk->fd.osm_chunk)
84+
{
85+
clone_constraint_on_chunk(chunk,
86+
ht_rel,
87+
fk,
88+
numfks,
89+
conkey,
90+
confkey,
91+
conpfeqop,
92+
conppeqop,
93+
conffeqop,
9294
#if PG15_GE
93-
numfkdelsetcols,
94-
confdelsetcols,
95+
numfkdelsetcols,
96+
confdelsetcols,
9597
#endif
96-
parentDelTrigger,
97-
parentUpdTrigger);
98+
parentDelTrigger,
99+
parentUpdTrigger);
100+
}
98101
}
99102
}
100103

tsl/test/sql/chunk_utils_internal.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,27 @@ DROP INDEX hyper_constr_mid_idx;
651651

652652
\i include/chunk_utils_internal_orderedappend.sql
653653

654+
--TEST hypertable with foreign key into it
655+
\c :TEST_DBNAME :ROLE_4
656+
CREATE TABLE hyper_fk(ts timestamptz primary key, device text, value float);
657+
SELECT table_name FROM create_hypertable('hyper_fk', 'ts');
658+
659+
INSERT INTO hyper_fk(ts, device, value) VALUES ('2020-01-01 00:00:00', 'd1', 1.0);
660+
\c postgres_fdw_db :ROLE_4
661+
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);
663+
664+
\c :TEST_DBNAME :ROLE_4
665+
-- this is a stand-in for the OSM table
666+
CREATE FOREIGN TABLE child_hyper_fk
667+
(ts timestamptz NOT NULL, device text, value float)
668+
SERVER s3_server OPTIONS ( schema_name 'public', table_name 'fdw_hyper_fk');
669+
SELECT _timescaledb_functions.attach_osm_table_chunk('hyper_fk', 'child_hyper_fk');
670+
671+
--create table with fk into hypertable
672+
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+
654675
-- clean up databases created
655676
\c :TEST_DBNAME :ROLE_SUPERUSER
656677
DROP DATABASE postgres_fdw_db WITH (FORCE);

0 commit comments

Comments
 (0)