Debian Codegraph @

contrib/btree_gist/expected/without_overlaps.out

raw
1 -- Core must test WITHOUT OVERLAPS
2 -- with an int4range + daterange,
3 -- so here we do some simple tests
4 -- to make sure int + daterange works too,
5 -- since that is the expected use-case.
6 CREATE TABLE temporal_rng (
7 id integer,
8 valid_at daterange,
9 CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
10 );
11 \d temporal_rng
12 Table "public.temporal_rng"
13 Column | Type | Collation | Nullable | Default
14 ----------+-----------+-----------+----------+---------
15 id | integer | | not null |
16 valid_at | daterange | | not null |
17 Indexes:
18 "temporal_rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
19
20 SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
21 pg_get_constraintdef
22 ---------------------------------------------
23 PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
24 (1 row)
25
26 SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
27 pg_get_indexdef
28 -------------------------------------------------------------------------------
29 CREATE UNIQUE INDEX temporal_rng_pk ON temporal_rng USING gist (id, valid_at)
30 (1 row)
31
32 INSERT INTO temporal_rng VALUES
33 (1, '[2000-01-01,2001-01-01)');
34 -- same key, doesn't overlap:
35 INSERT INTO temporal_rng VALUES
36 (1, '[2001-01-01,2002-01-01)');
37 -- overlaps but different key:
38 INSERT INTO temporal_rng VALUES
39 (2, '[2000-01-01,2001-01-01)');
40 -- should fail:
41 INSERT INTO temporal_rng VALUES
42 (1, '[2000-06-01,2001-01-01)');
43 ERROR: conflicting key value violates exclusion constraint "temporal_rng_pk"
44 DETAIL: Key (id, valid_at)=(1, [06-01-2000,01-01-2001)) conflicts with existing key (id, valid_at)=(1, [01-01-2000,01-01-2001)).
45 -- Foreign key
46 CREATE TABLE temporal_fk_rng2rng (
47 id integer,
48 valid_at daterange,
49 parent_id integer,
50 CONSTRAINT temporal_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS),
51 CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at)
52 REFERENCES temporal_rng (id, PERIOD valid_at)
53 );
54 \d temporal_fk_rng2rng
55 Table "public.temporal_fk_rng2rng"
56 Column | Type | Collation | Nullable | Default
57 -----------+-----------+-----------+----------+---------
58 id | integer | | not null |
59 valid_at | daterange | | not null |
60 parent_id | integer | | |
61 Indexes:
62 "temporal_fk_rng2rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
63 Foreign-key constraints:
64 "temporal_fk_rng2rng_fk" FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at)
65
66 SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2rng_fk';
67 pg_get_constraintdef
68 ---------------------------------------------------------------------------------------
69 FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at)
70 (1 row)
71
72 -- okay
73 INSERT INTO temporal_fk_rng2rng VALUES
74 (1, '[2000-01-01,2001-01-01)', 1);
75 -- okay spanning two parent records:
76 INSERT INTO temporal_fk_rng2rng VALUES
77 (2, '[2000-01-01,2002-01-01)', 1);
78 -- key is missing
79 INSERT INTO temporal_fk_rng2rng VALUES
80 (3, '[2000-01-01,2001-01-01)', 3);
81 ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
82 DETAIL: Key (parent_id, valid_at)=(3, [01-01-2000,01-01-2001)) is not present in table "temporal_rng".
83 -- key exist but is outside range
84 INSERT INTO temporal_fk_rng2rng VALUES
85 (4, '[2001-01-01,2002-01-01)', 2);
86 ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
87 DETAIL: Key (parent_id, valid_at)=(2, [01-01-2001,01-01-2002)) is not present in table "temporal_rng".
88 -- key exist but is partly outside range
89 INSERT INTO temporal_fk_rng2rng VALUES
90 (5, '[2000-01-01,2002-01-01)', 2);
91 ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
92 DETAIL: Key (parent_id, valid_at)=(2, [01-01-2000,01-01-2002)) is not present in table "temporal_rng".