mirror of
https://github.com/postgres/postgres.git
synced 2025-04-21 12:05:57 +03:00
Add PERIOD clause to foreign key constraint definitions. This is supported for range and multirange types. Temporal foreign keys check for range containment instead of equality. This feature matches the behavior of the SQL standard temporal foreign keys, but it works on PostgreSQL's native ranges instead of SQL's "periods", which don't exist in PostgreSQL (yet). Reference actions ON {UPDATE,DELETE} {CASCADE,SET NULL,SET DEFAULT} are not supported yet. (previously committed as 34768ee3616, reverted by 8aee330af55; this is essentially unchanged from those) Author: Paul A. Jungwirth <pj@illuminatedcomputing.com> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Reviewed-by: jian he <jian.universality@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com
54 lines
1.8 KiB
SQL
54 lines
1.8 KiB
SQL
-- Core must test WITHOUT OVERLAPS
|
|
-- with an int4range + daterange,
|
|
-- so here we do some simple tests
|
|
-- to make sure int + daterange works too,
|
|
-- since that is the expected use-case.
|
|
CREATE TABLE temporal_rng (
|
|
id integer,
|
|
valid_at daterange,
|
|
CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
|
|
);
|
|
\d temporal_rng
|
|
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
|
|
SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
|
|
|
|
INSERT INTO temporal_rng VALUES
|
|
(1, '[2000-01-01,2001-01-01)');
|
|
-- same key, doesn't overlap:
|
|
INSERT INTO temporal_rng VALUES
|
|
(1, '[2001-01-01,2002-01-01)');
|
|
-- overlaps but different key:
|
|
INSERT INTO temporal_rng VALUES
|
|
(2, '[2000-01-01,2001-01-01)');
|
|
-- should fail:
|
|
INSERT INTO temporal_rng VALUES
|
|
(1, '[2000-06-01,2001-01-01)');
|
|
|
|
-- Foreign key
|
|
CREATE TABLE temporal_fk_rng2rng (
|
|
id integer,
|
|
valid_at daterange,
|
|
parent_id integer,
|
|
CONSTRAINT temporal_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS),
|
|
CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at)
|
|
REFERENCES temporal_rng (id, PERIOD valid_at)
|
|
);
|
|
\d temporal_fk_rng2rng
|
|
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2rng_fk';
|
|
|
|
-- okay
|
|
INSERT INTO temporal_fk_rng2rng VALUES
|
|
(1, '[2000-01-01,2001-01-01)', 1);
|
|
-- okay spanning two parent records:
|
|
INSERT INTO temporal_fk_rng2rng VALUES
|
|
(2, '[2000-01-01,2002-01-01)', 1);
|
|
-- key is missing
|
|
INSERT INTO temporal_fk_rng2rng VALUES
|
|
(3, '[2000-01-01,2001-01-01)', 3);
|
|
-- key exist but is outside range
|
|
INSERT INTO temporal_fk_rng2rng VALUES
|
|
(4, '[2001-01-01,2002-01-01)', 2);
|
|
-- key exist but is partly outside range
|
|
INSERT INTO temporal_fk_rng2rng VALUES
|
|
(5, '[2000-01-01,2002-01-01)', 2);
|