3 -- Constraints can be specified with:
6 -- - PRIMARY KEY clauses
15 CREATE TABLE DEFAULT_TBL (i int DEFAULT 100,
16 x text DEFAULT 'vadim', f float8 DEFAULT 123.456);
18 INSERT INTO DEFAULT_TBL VALUES (1, 'thomas', 57.0613);
19 INSERT INTO DEFAULT_TBL VALUES (1, 'bruce');
20 INSERT INTO DEFAULT_TBL (i, f) VALUES (2, 987.654);
21 INSERT INTO DEFAULT_TBL (x) VALUES ('marc');
22 INSERT INTO DEFAULT_TBL VALUES (3, null, 1.0);
24 SELECT * FROM DEFAULT_TBL;
26 CREATE SEQUENCE DEFAULT_SEQ;
28 CREATE TABLE DEFAULTEXPR_TBL (i1 int DEFAULT 100 + (200-199) * 2,
29 i2 int DEFAULT nextval('default_seq'));
31 INSERT INTO DEFAULTEXPR_TBL VALUES (-1, -2);
32 INSERT INTO DEFAULTEXPR_TBL (i1) VALUES (-3);
33 INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (-4);
34 INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (NULL);
36 SELECT * FROM DEFAULTEXPR_TBL;
39 -- test for extraneous comma
40 CREATE TABLE error_tbl (i int DEFAULT (100, ));
41 -- this will fail because gram.y uses b_expr not a_expr for defaults,
42 -- to avoid a shift/reduce conflict that arises from NOT NULL being
43 -- part of the column definition syntax:
44 CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
45 -- this should work, however:
46 CREATE TABLE error_tbl (b1 bool DEFAULT (1 IN (1, 2)));
54 CREATE TABLE CHECK_TBL (x int,
55 CONSTRAINT CHECK_CON CHECK (x > 3));
57 INSERT INTO CHECK_TBL VALUES (5);
58 INSERT INTO CHECK_TBL VALUES (4);
59 INSERT INTO CHECK_TBL VALUES (3);
60 INSERT INTO CHECK_TBL VALUES (2);
61 INSERT INTO CHECK_TBL VALUES (6);
62 INSERT INTO CHECK_TBL VALUES (1);
64 SELECT * FROM CHECK_TBL;
66 CREATE SEQUENCE CHECK_SEQ;
68 CREATE TABLE CHECK2_TBL (x int, y text, z int,
69 CONSTRAINT SEQUENCE_CON
70 CHECK (x > 3 and y <> 'check failed' and z < 8));
72 INSERT INTO CHECK2_TBL VALUES (4, 'check ok', -2);
73 INSERT INTO CHECK2_TBL VALUES (1, 'x check failed', -2);
74 INSERT INTO CHECK2_TBL VALUES (5, 'z check failed', 10);
75 INSERT INTO CHECK2_TBL VALUES (0, 'check failed', -2);
76 INSERT INTO CHECK2_TBL VALUES (6, 'check failed', 11);
77 INSERT INTO CHECK2_TBL VALUES (7, 'check ok', 7);
79 SELECT * from CHECK2_TBL;
82 -- Check constraints on INSERT
85 CREATE SEQUENCE INSERT_SEQ;
87 CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
88 y TEXT DEFAULT '-NULL-',
89 z INT DEFAULT -1 * currval('insert_seq'),
90 CONSTRAINT INSERT_TBL_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
93 INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
95 SELECT * FROM INSERT_TBL;
97 SELECT 'one' AS one, nextval('insert_seq');
99 INSERT INTO INSERT_TBL(y) VALUES ('Y');
100 INSERT INTO INSERT_TBL(y) VALUES ('Y');
101 INSERT INTO INSERT_TBL(x,z) VALUES (1, -2);
102 INSERT INTO INSERT_TBL(z,x) VALUES (-7, 7);
103 INSERT INTO INSERT_TBL VALUES (5, 'check failed', -5);
104 INSERT INTO INSERT_TBL VALUES (7, '!check failed', -7);
105 INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
107 SELECT * FROM INSERT_TBL;
109 INSERT INTO INSERT_TBL(y,z) VALUES ('check failed', 4);
110 INSERT INTO INSERT_TBL(x,y) VALUES (5, 'check failed');
111 INSERT INTO INSERT_TBL(x,y) VALUES (5, '!check failed');
112 INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
114 SELECT * FROM INSERT_TBL;
116 SELECT 'seven' AS one, nextval('insert_seq');
118 INSERT INTO INSERT_TBL(y) VALUES ('Y');
120 SELECT 'eight' AS one, currval('insert_seq');
122 -- According to SQL, it is OK to insert a record that gives rise to NULL
123 -- constraint-condition results. Postgres used to reject this, but it
125 INSERT INTO INSERT_TBL VALUES (null, null, null);
127 SELECT * FROM INSERT_TBL;
130 -- Check constraints on system columns
133 CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool,
135 CHECK (NOT (is_capital AND tableoid::regclass::text = 'sys_col_check_tbl')));
137 INSERT INTO SYS_COL_CHECK_TBL VALUES ('Seattle', 'Washington', false, 100);
138 INSERT INTO SYS_COL_CHECK_TBL VALUES ('Olympia', 'Washington', true, 100);
140 SELECT *, tableoid::regclass::text FROM SYS_COL_CHECK_TBL;
142 DROP TABLE SYS_COL_CHECK_TBL;
145 -- Check constraints on system columns other then TableOid should return error
147 CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool,
149 CHECK (NOT (is_capital AND ctid::text = 'sys_col_check_tbl')));
152 -- Check inheritance of defaults and constraints
155 CREATE TABLE INSERT_CHILD (cx INT default 42,
156 cy INT CHECK (cy > x))
157 INHERITS (INSERT_TBL);
159 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,11);
160 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,6);
161 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (6,-7,7);
162 INSERT INTO INSERT_CHILD(x,y,z,cy) VALUES (6,'check failed',-6,7);
164 SELECT * FROM INSERT_CHILD;
166 DROP TABLE INSERT_CHILD;
169 -- Check NO INHERIT type of constraints and inheritance
172 CREATE TABLE ATACC1 (TEST INT
173 CHECK (TEST > 0) NO INHERIT);
175 CREATE TABLE ATACC2 (TEST2 INT) INHERITS (ATACC1);
176 -- check constraint is not there on child
177 INSERT INTO ATACC2 (TEST) VALUES (-3);
178 -- check constraint is there on parent
179 INSERT INTO ATACC1 (TEST) VALUES (-3);
180 DROP TABLE ATACC1 CASCADE;
182 CREATE TABLE ATACC1 (TEST INT, TEST2 INT
183 CHECK (TEST > 0), CHECK (TEST2 > 10) NO INHERIT);
185 CREATE TABLE ATACC2 () INHERITS (ATACC1);
186 -- check constraint is there on child
187 INSERT INTO ATACC2 (TEST) VALUES (-3);
188 -- check constraint is there on parent
189 INSERT INTO ATACC1 (TEST) VALUES (-3);
190 -- check constraint is not there on child
191 INSERT INTO ATACC2 (TEST2) VALUES (3);
192 -- check constraint is there on parent
193 INSERT INTO ATACC1 (TEST2) VALUES (3);
194 DROP TABLE ATACC1 CASCADE;
197 -- Check constraints on INSERT INTO
200 DELETE FROM INSERT_TBL;
202 ALTER SEQUENCE INSERT_SEQ RESTART WITH 4;
204 CREATE TEMP TABLE tmp (xd INT, yd TEXT, zd INT);
206 INSERT INTO tmp VALUES (null, 'Y', null);
207 INSERT INTO tmp VALUES (5, '!check failed', null);
208 INSERT INTO tmp VALUES (null, 'try again', null);
209 INSERT INTO INSERT_TBL(y) select yd from tmp;
211 SELECT * FROM INSERT_TBL;
213 INSERT INTO INSERT_TBL SELECT * FROM tmp WHERE yd = 'try again';
214 INSERT INTO INSERT_TBL(y,z) SELECT yd, -7 FROM tmp WHERE yd = 'try again';
215 INSERT INTO INSERT_TBL(y,z) SELECT yd, -8 FROM tmp WHERE yd = 'try again';
217 SELECT * FROM INSERT_TBL;
222 -- Check constraints on UPDATE
225 UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
226 UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
227 UPDATE INSERT_TBL SET x = -z, z = -x;
228 UPDATE INSERT_TBL SET x = z, z = x;
230 SELECT * FROM INSERT_TBL;
232 -- DROP TABLE INSERT_TBL;
235 -- Check constraints on COPY FROM
238 CREATE TABLE COPY_TBL (x INT, y TEXT, z INT,
240 CHECK (x > 3 AND y <> 'check failed' AND x < 7 ));
242 COPY COPY_TBL FROM '@abs_srcdir@/data/constro.data';
244 SELECT * FROM COPY_TBL;
246 COPY COPY_TBL FROM '@abs_srcdir@/data/constrf.data';
248 SELECT * FROM COPY_TBL;
254 CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text);
256 INSERT INTO PRIMARY_TBL VALUES (1, 'one');
257 INSERT INTO PRIMARY_TBL VALUES (2, 'two');
258 INSERT INTO PRIMARY_TBL VALUES (1, 'three');
259 INSERT INTO PRIMARY_TBL VALUES (4, 'three');
260 INSERT INTO PRIMARY_TBL VALUES (5, 'one');
261 INSERT INTO PRIMARY_TBL (t) VALUES ('six');
263 SELECT * FROM PRIMARY_TBL;
265 DROP TABLE PRIMARY_TBL;
267 CREATE TABLE PRIMARY_TBL (i int, t text,
270 INSERT INTO PRIMARY_TBL VALUES (1, 'one');
271 INSERT INTO PRIMARY_TBL VALUES (2, 'two');
272 INSERT INTO PRIMARY_TBL VALUES (1, 'three');
273 INSERT INTO PRIMARY_TBL VALUES (4, 'three');
274 INSERT INTO PRIMARY_TBL VALUES (5, 'one');
275 INSERT INTO PRIMARY_TBL (t) VALUES ('six');
277 SELECT * FROM PRIMARY_TBL;
279 DROP TABLE PRIMARY_TBL;
285 CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text);
287 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
288 INSERT INTO UNIQUE_TBL VALUES (2, 'two');
289 INSERT INTO UNIQUE_TBL VALUES (1, 'three');
290 INSERT INTO UNIQUE_TBL VALUES (4, 'four');
291 INSERT INTO UNIQUE_TBL VALUES (5, 'one');
292 INSERT INTO UNIQUE_TBL (t) VALUES ('six');
293 INSERT INTO UNIQUE_TBL (t) VALUES ('seven');
295 INSERT INTO UNIQUE_TBL VALUES (5, 'five-upsert-insert') ON CONFLICT (i) DO UPDATE SET t = 'five-upsert-update';
296 INSERT INTO UNIQUE_TBL VALUES (6, 'six-upsert-insert') ON CONFLICT (i) DO UPDATE SET t = 'six-upsert-update';
298 INSERT INTO UNIQUE_TBL VALUES (1, 'a'), (2, 'b'), (2, 'b') ON CONFLICT (i) DO UPDATE SET t = 'fails';
300 SELECT * FROM UNIQUE_TBL;
302 DROP TABLE UNIQUE_TBL;
304 CREATE TABLE UNIQUE_TBL (i int, t text,
307 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
308 INSERT INTO UNIQUE_TBL VALUES (2, 'two');
309 INSERT INTO UNIQUE_TBL VALUES (1, 'three');
310 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
311 INSERT INTO UNIQUE_TBL VALUES (5, 'one');
312 INSERT INTO UNIQUE_TBL (t) VALUES ('six');
314 SELECT * FROM UNIQUE_TBL;
316 DROP TABLE UNIQUE_TBL;
319 -- Deferrable unique constraints
322 CREATE TABLE unique_tbl (i int UNIQUE DEFERRABLE, t text);
324 INSERT INTO unique_tbl VALUES (0, 'one');
325 INSERT INTO unique_tbl VALUES (1, 'two');
326 INSERT INTO unique_tbl VALUES (2, 'tree');
327 INSERT INTO unique_tbl VALUES (3, 'four');
328 INSERT INTO unique_tbl VALUES (4, 'five');
332 -- default is immediate so this should fail right away
333 UPDATE unique_tbl SET i = 1 WHERE i = 0;
337 -- check is done at end of statement, so this should succeed
338 UPDATE unique_tbl SET i = i+1;
340 SELECT * FROM unique_tbl;
342 -- explicitly defer the constraint
345 SET CONSTRAINTS unique_tbl_i_key DEFERRED;
347 INSERT INTO unique_tbl VALUES (3, 'three');
348 DELETE FROM unique_tbl WHERE t = 'tree'; -- makes constraint valid again
350 COMMIT; -- should succeed
352 SELECT * FROM unique_tbl;
354 -- try adding an initially deferred constraint
355 ALTER TABLE unique_tbl DROP CONSTRAINT unique_tbl_i_key;
356 ALTER TABLE unique_tbl ADD CONSTRAINT unique_tbl_i_key
357 UNIQUE (i) DEFERRABLE INITIALLY DEFERRED;
361 INSERT INTO unique_tbl VALUES (1, 'five');
362 INSERT INTO unique_tbl VALUES (5, 'one');
363 UPDATE unique_tbl SET i = 4 WHERE i = 2;
364 UPDATE unique_tbl SET i = 2 WHERE i = 4 AND t = 'four';
365 DELETE FROM unique_tbl WHERE i = 1 AND t = 'one';
366 DELETE FROM unique_tbl WHERE i = 5 AND t = 'five';
370 SELECT * FROM unique_tbl;
372 -- should fail at commit-time
374 INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
375 COMMIT; -- should fail
377 -- make constraint check immediate
380 SET CONSTRAINTS ALL IMMEDIATE;
382 INSERT INTO unique_tbl VALUES (3, 'Three'); -- should fail
386 -- forced check when SET CONSTRAINTS is called
389 SET CONSTRAINTS ALL DEFERRED;
391 INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
393 SET CONSTRAINTS ALL IMMEDIATE; -- should fail
397 -- test deferrable UNIQUE with a partitioned table
398 CREATE TABLE parted_uniq_tbl (i int UNIQUE DEFERRABLE) partition by range (i);
399 CREATE TABLE parted_uniq_tbl_1 PARTITION OF parted_uniq_tbl FOR VALUES FROM (0) TO (10);
400 CREATE TABLE parted_uniq_tbl_2 PARTITION OF parted_uniq_tbl FOR VALUES FROM (20) TO (30);
401 SELECT conname, conrelid::regclass FROM pg_constraint
402 WHERE conname LIKE 'parted_uniq%' ORDER BY conname;
404 INSERT INTO parted_uniq_tbl VALUES (1);
406 INSERT INTO parted_uniq_tbl VALUES (1); -- unique violation
408 SET CONSTRAINTS parted_uniq_tbl_i_key DEFERRED;
409 INSERT INTO parted_uniq_tbl VALUES (1); -- OK now, fail at commit
411 DROP TABLE parted_uniq_tbl;
413 -- test a HOT update that invalidates the conflicting tuple.
414 -- the trigger should still fire and catch the violation
418 INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
419 UPDATE unique_tbl SET t = 'THREE' WHERE i = 3 AND t = 'Three';
421 COMMIT; -- should fail
423 SELECT * FROM unique_tbl;
425 -- test a HOT update that modifies the newly inserted tuple,
426 -- but should succeed because we then remove the other conflicting tuple.
430 INSERT INTO unique_tbl VALUES(3, 'tree'); -- should succeed for now
431 UPDATE unique_tbl SET t = 'threex' WHERE t = 'tree';
432 DELETE FROM unique_tbl WHERE t = 'three';
434 SELECT * FROM unique_tbl;
438 SELECT * FROM unique_tbl;
440 DROP TABLE unique_tbl;
443 -- EXCLUDE constraints
446 CREATE TABLE circles (
450 (c1 WITH &&, (c2::circle) WITH &&)
451 WHERE (circle_center(c1) <> '(0,0)')
454 -- these should succeed because they don't match the index predicate
455 INSERT INTO circles VALUES('<(0,0), 5>', '<(0,0), 5>');
456 INSERT INTO circles VALUES('<(0,0), 5>', '<(0,0), 4>');
459 INSERT INTO circles VALUES('<(10,10), 10>', '<(0,0), 5>');
461 INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>');
462 -- succeed, because violation is ignored
463 INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>')
464 ON CONFLICT ON CONSTRAINT circles_c1_c2_excl DO NOTHING;
465 -- fail, because DO UPDATE variant requires unique index
466 INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>')
467 ON CONFLICT ON CONSTRAINT circles_c1_c2_excl DO UPDATE SET c2 = EXCLUDED.c2;
468 -- succeed because c1 doesn't overlap
469 INSERT INTO circles VALUES('<(20,20), 1>', '<(0,0), 5>');
470 -- succeed because c2 doesn't overlap
471 INSERT INTO circles VALUES('<(20,20), 10>', '<(10,10), 5>');
473 -- should fail on existing data without the WHERE clause
474 ALTER TABLE circles ADD EXCLUDE USING gist
475 (c1 WITH &&, (c2::circle) WITH &&);
477 -- try reindexing an existing constraint
478 REINDEX INDEX circles_c1_c2_excl;
482 -- Check deferred exclusion constraint
484 CREATE TABLE deferred_excl (
487 CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
490 INSERT INTO deferred_excl VALUES(1);
491 INSERT INTO deferred_excl VALUES(2);
492 INSERT INTO deferred_excl VALUES(1); -- fail
493 INSERT INTO deferred_excl VALUES(1) ON CONFLICT ON CONSTRAINT deferred_excl_con DO NOTHING; -- fail
495 INSERT INTO deferred_excl VALUES(2); -- no fail here
496 COMMIT; -- should fail here
498 INSERT INTO deferred_excl VALUES(3);
499 INSERT INTO deferred_excl VALUES(3); -- no fail here
500 COMMIT; -- should fail here
502 -- bug #13148: deferred constraint versus HOT update
504 INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
505 DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
506 UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
507 COMMIT; -- should not fail
509 SELECT * FROM deferred_excl;
511 ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
513 -- This should fail, but worth testing because of HOT updates
514 UPDATE deferred_excl SET f1 = 3;
516 ALTER TABLE deferred_excl ADD EXCLUDE (f1 WITH =);
518 DROP TABLE deferred_excl;
521 -- Setup a low-level role to enforce non-superuser checks.
522 CREATE ROLE regress_constraint_comments;
523 SET SESSION AUTHORIZATION regress_constraint_comments;
525 CREATE TABLE constraint_comments_tbl (a int CONSTRAINT the_constraint CHECK (a > 0));
526 CREATE DOMAIN constraint_comments_dom AS int CONSTRAINT the_constraint CHECK (value > 0);
528 COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'yes, the comment';
529 COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS 'yes, another comment';
531 -- no such constraint
532 COMMENT ON CONSTRAINT no_constraint ON constraint_comments_tbl IS 'yes, the comment';
533 COMMENT ON CONSTRAINT no_constraint ON DOMAIN constraint_comments_dom IS 'yes, another comment';
535 -- no such table/domain
536 COMMENT ON CONSTRAINT the_constraint ON no_comments_tbl IS 'bad comment';
537 COMMENT ON CONSTRAINT the_constraint ON DOMAIN no_comments_dom IS 'another bad comment';
539 COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS NULL;
540 COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS NULL;
543 RESET SESSION AUTHORIZATION;
544 CREATE ROLE regress_constraint_comments_noaccess;
545 SET SESSION AUTHORIZATION regress_constraint_comments_noaccess;
546 COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'no, the comment';
547 COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS 'no, another comment';
548 RESET SESSION AUTHORIZATION;
550 DROP TABLE constraint_comments_tbl;
551 DROP DOMAIN constraint_comments_dom;
553 DROP ROLE regress_constraint_comments;
554 DROP ROLE regress_constraint_comments_noaccess;