You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It isn't clear how these should behave, so let's wait to implement them
until we are sure how to do it.
This feature was initially added by commit 89f908a, so it hasn't
been released yet.
Author: Paul A. Jungwirth <pj@illuminatedcomputing.com>
Discussion: https://postgr.es/m/e773bc11-4ac1-40de-bb91-814e02f05b6d%40eisentraut.org
SET valid_at = CASE WHEN lower(valid_at) = '2018-01-01' THEN daterange('2018-01-01', '2018-01-05')
1793
-
WHEN lower(valid_at) = '2018-02-01' THEN daterange('2018-01-05', '2018-03-01') END
1794
-
WHERE id = '[6,7)';
1795
-
ERROR: update or delete on table "temporal_rng" violates RESTRICT setting of foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng"
1796
-
DETAIL: Key (id, valid_at)=([6,7), [2018-01-01,2018-02-01)) is referenced from table "temporal_fk_rng2rng".
1797
-
-- a PK update that fails because both are referenced (even before commit):
1798
-
BEGIN;
1799
-
ALTER TABLE temporal_fk_rng2rng
1800
-
ALTER CONSTRAINT temporal_fk_rng2rng_fk
1801
-
DEFERRABLE INITIALLY DEFERRED;
1802
-
UPDATE temporal_rng SET valid_at = daterange('2016-01-01', '2016-02-01')
1803
-
WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
1804
-
ERROR: update or delete on table "temporal_rng" violates RESTRICT setting of foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng"
1805
-
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is referenced from table "temporal_fk_rng2rng".
1806
-
ROLLBACK;
1807
-
-- changing the scalar part fails:
1808
-
UPDATE temporal_rng SET id = '[7,8)'
1809
-
WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
1810
-
ERROR: update or delete on table "temporal_rng" violates RESTRICT setting of foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng"
1811
-
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is referenced from table "temporal_fk_rng2rng".
1812
-
-- then delete the objecting FK record and the same PK update succeeds:
1813
-
DELETE FROM temporal_fk_rng2rng WHERE id = '[3,4)';
1814
-
UPDATE temporal_rng SET valid_at = daterange('2016-01-01', '2016-02-01')
1815
-
WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
1773
+
ERROR: unsupported ON UPDATE action for foreign key constraint using PERIOD
1816
1774
--
1817
1775
-- test FK referenced deletes NO ACTION
1818
1776
--
1819
1777
TRUNCATE temporal_rng, temporal_fk_rng2rng;
1820
-
ALTER TABLE temporal_fk_rng2rng
1821
-
DROP CONSTRAINT temporal_fk_rng2rng_fk;
1822
1778
ALTER TABLE temporal_fk_rng2rng
1823
1779
ADD CONSTRAINT temporal_fk_rng2rng_fk
1824
1780
FOREIGN KEY (parent_id, PERIOD valid_at)
@@ -1860,35 +1816,14 @@ ALTER TABLE temporal_fk_rng2rng
1860
1816
FOREIGN KEY (parent_id, PERIOD valid_at)
1861
1817
REFERENCES temporal_rng
1862
1818
ON DELETE RESTRICT;
1863
-
INSERT INTO temporal_rng (id, valid_at) VALUES ('[5,6)', daterange('2018-01-01', '2018-02-01'));
1864
-
DELETE FROM temporal_rng WHERE id = '[5,6)';
1865
-
-- a PK delete that succeeds even though the numeric id is referenced because the range isn't:
1866
-
INSERT INTO temporal_rng (id, valid_at) VALUES
1867
-
('[5,6)', daterange('2018-01-01', '2018-02-01')),
1868
-
('[5,6)', daterange('2018-02-01', '2018-03-01'));
1869
-
INSERT INTO temporal_fk_rng2rng (id, valid_at, parent_id) VALUES
DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-02-01', '2018-03-01');
1872
-
-- a PK delete that fails because both are referenced (even before commit):
1873
-
BEGIN;
1874
-
ALTER TABLE temporal_fk_rng2rng
1875
-
ALTER CONSTRAINT temporal_fk_rng2rng_fk
1876
-
DEFERRABLE INITIALLY DEFERRED;
1877
-
DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
1878
-
ERROR: update or delete on table "temporal_rng" violates RESTRICT setting of foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng"
1879
-
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is referenced from table "temporal_fk_rng2rng".
1880
-
ROLLBACK;
1881
-
-- then delete the objecting FK record and the same PK delete succeeds:
1882
-
DELETE FROM temporal_fk_rng2rng WHERE id = '[3,4)';
1883
-
DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
1819
+
ERROR: unsupported ON DELETE action for foreign key constraint using PERIOD
1884
1820
--
1885
1821
-- test ON UPDATE/DELETE options
1886
1822
--
1887
1823
-- test FK referenced updates CASCADE
1888
1824
INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', daterange('2018-01-01', '2021-01-01'));
SET valid_at = CASE WHEN lower(valid_at) = '2018-01-01' THEN datemultirange(daterange('2018-01-01', '2018-01-05'))
2312
-
WHEN lower(valid_at) = '2018-02-01' THEN datemultirange(daterange('2018-01-05', '2018-03-01')) END
2313
-
WHERE id = '[6,7)';
2314
-
ERROR: update or delete on table "temporal_mltrng" violates RESTRICT setting of foreign key constraint "temporal_fk_mltrng2mltrng_fk" on table "temporal_fk_mltrng2mltrng"
2315
-
DETAIL: Key (id, valid_at)=([6,7), {[2018-01-01,2018-02-01)}) is referenced from table "temporal_fk_mltrng2mltrng".
2316
-
-- a PK update that fails because both are referenced (even before commit):
2317
-
BEGIN;
2318
-
ALTER TABLE temporal_fk_mltrng2mltrng
2319
-
ALTER CONSTRAINT temporal_fk_mltrng2mltrng_fk
2320
-
DEFERRABLE INITIALLY DEFERRED;
2321
-
UPDATE temporal_mltrng SET valid_at = datemultirange(daterange('2016-01-01', '2016-02-01'))
2322
-
WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-01-01', '2018-02-01'));
2323
-
ERROR: update or delete on table "temporal_mltrng" violates RESTRICT setting of foreign key constraint "temporal_fk_mltrng2mltrng_fk" on table "temporal_fk_mltrng2mltrng"
2324
-
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is referenced from table "temporal_fk_mltrng2mltrng".
2325
-
ROLLBACK;
2326
-
-- changing the scalar part fails:
2327
-
UPDATE temporal_mltrng SET id = '[7,8)'
2328
-
WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-01-01', '2018-02-01'));
2329
-
ERROR: update or delete on table "temporal_mltrng" violates RESTRICT setting of foreign key constraint "temporal_fk_mltrng2mltrng_fk" on table "temporal_fk_mltrng2mltrng"
2330
-
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is referenced from table "temporal_fk_mltrng2mltrng".
2225
+
ERROR: unsupported ON UPDATE action for foreign key constraint using PERIOD
DELETE FROM temporal_mltrng WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-02-01', '2018-03-01'));
2382
-
-- a PK delete that fails because both are referenced (even before commit):
2383
-
BEGIN;
2384
-
ALTER TABLE temporal_fk_mltrng2mltrng
2385
-
ALTER CONSTRAINT temporal_fk_mltrng2mltrng_fk
2386
-
DEFERRABLE INITIALLY DEFERRED;
2387
-
DELETE FROM temporal_mltrng WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-01-01', '2018-02-01'));
2388
-
ERROR: update or delete on table "temporal_mltrng" violates RESTRICT setting of foreign key constraint "temporal_fk_mltrng2mltrng_fk" on table "temporal_fk_mltrng2mltrng"
2389
-
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is referenced from table "temporal_fk_mltrng2mltrng".
2390
-
ROLLBACK;
2391
-
--
2392
2257
-- FK between partitioned tables: ranges
2393
2258
--
2394
2259
CREATE TABLE temporal_partitioned_rng (
@@ -2469,40 +2334,6 @@ DELETE FROM temporal_partitioned_rng WHERE id = '[5,6)' AND valid_at = daterange
2469
2334
ERROR: update or delete on table "tp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_parent_id_valid_at_fkey" on table "temporal_partitioned_fk_rng2rng"
2470
2335
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_partitioned_fk_rng2rng".
UPDATE temporal_partitioned_rng SET valid_at = daterange('2016-02-01', '2016-03-01')
2487
-
WHERE id = '[5,6)' AND valid_at = daterange('2018-02-01', '2018-03-01');
2488
-
-- should fail:
2489
-
UPDATE temporal_partitioned_rng SET valid_at = daterange('2016-01-01', '2016-02-01')
2490
-
WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
2491
-
ERROR: update or delete on table "tp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_parent_id_valid_at_fkey" on table "temporal_partitioned_fk_rng2rng"
2492
-
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_partitioned_fk_rng2rng".
DELETE FROM temporal_partitioned_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-02-01', '2018-03-01');
2501
-
-- should fail:
2502
-
DELETE FROM temporal_partitioned_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01');
2503
-
ERROR: update or delete on table "tp1" violates RESTRICT setting of foreign key constraint "temporal_partitioned_fk_rng2rng_parent_id_valid_at_fkey" on table "temporal_partitioned_fk_rng2rng"
2504
-
DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is referenced from table "temporal_partitioned_fk_rng2rng".
2505
-
--
2506
2337
-- partitioned FK referenced updates CASCADE
2507
2338
--
2508
2339
ALTER TABLE temporal_partitioned_fk_rng2rng
@@ -2625,40 +2456,6 @@ DELETE FROM temporal_partitioned_mltrng WHERE id = '[5,6)' AND valid_at = datemu
2625
2456
ERROR: update or delete on table "tp1" violates foreign key constraint "temporal_partitioned_fk_mltrng2mltrng_parent_id_valid_at_fkey1" on table "temporal_partitioned_fk_mltrng2mltrng"
2626
2457
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is still referenced from table "temporal_partitioned_fk_mltrng2mltrng".
UPDATE temporal_partitioned_mltrng SET valid_at = datemultirange(daterange('2016-02-01', '2016-03-01'))
2643
-
WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-02-01', '2018-03-01'));
2644
-
-- should fail:
2645
-
UPDATE temporal_partitioned_mltrng SET valid_at = datemultirange(daterange('2016-01-01', '2016-02-01'))
2646
-
WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-01-01', '2018-02-01'));
2647
-
ERROR: update or delete on table "tp1" violates foreign key constraint "temporal_partitioned_fk_mltrng2mltrng_parent_id_valid_at_fkey1" on table "temporal_partitioned_fk_mltrng2mltrng"
2648
-
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is still referenced from table "temporal_partitioned_fk_mltrng2mltrng".
DELETE FROM temporal_partitioned_mltrng WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-02-01', '2018-03-01'));
2657
-
-- should fail:
2658
-
DELETE FROM temporal_partitioned_mltrng WHERE id = '[5,6)' AND valid_at = datemultirange(daterange('2018-01-01', '2018-02-01'));
2659
-
ERROR: update or delete on table "tp1" violates RESTRICT setting of foreign key constraint "temporal_partitioned_fk_mltrng2mltrng_parent_id_valid_at_fkey1" on table "temporal_partitioned_fk_mltrng2mltrng"
2660
-
DETAIL: Key (id, valid_at)=([5,6), {[2018-01-01,2018-02-01)}) is referenced from table "temporal_partitioned_fk_mltrng2mltrng".
0 commit comments