1997-08-28 06:49:34 +02:00
|
|
|
--
|
2000-01-05 18:32:29 +01:00
|
|
|
-- CONSTRAINTS
|
1997-12-05 01:01:22 +01:00
|
|
|
-- Constraints can be specified with:
|
|
|
|
-- - DEFAULT clause
|
|
|
|
-- - CHECK clauses
|
|
|
|
-- - PRIMARY KEY clauses
|
|
|
|
-- - UNIQUE clauses
|
2009-12-07 06:22:23 +01:00
|
|
|
-- - EXCLUDE clauses
|
1997-08-28 06:49:34 +02:00
|
|
|
--
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
|
|
|
-- DEFAULT syntax
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE DEFAULT_TBL (i int DEFAULT 100,
|
1997-12-05 01:01:22 +01:00
|
|
|
x text DEFAULT 'vadim', f float8 DEFAULT 123.456);
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
INSERT INTO DEFAULT_TBL VALUES (1, 'thomas', 57.0613);
|
|
|
|
INSERT INTO DEFAULT_TBL VALUES (1, 'bruce');
|
|
|
|
INSERT INTO DEFAULT_TBL (i, f) VALUES (2, 987.654);
|
1997-12-05 01:01:22 +01:00
|
|
|
INSERT INTO DEFAULT_TBL (x) VALUES ('marc');
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO DEFAULT_TBL VALUES (3, null, 1.0);
|
1997-09-16 18:15:52 +02:00
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
SELECT '' AS five, * FROM DEFAULT_TBL;
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
CREATE SEQUENCE DEFAULT_SEQ;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
CREATE TABLE DEFAULTEXPR_TBL (i1 int DEFAULT 100 + (200-199) * 2,
|
1997-12-05 01:01:22 +01:00
|
|
|
i2 int DEFAULT nextval('default_seq'));
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
INSERT INTO DEFAULTEXPR_TBL VALUES (-1, -2);
|
|
|
|
INSERT INTO DEFAULTEXPR_TBL (i1) VALUES (-3);
|
|
|
|
INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (-4);
|
|
|
|
INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (NULL);
|
1997-12-05 01:01:22 +01:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
SELECT '' AS four, * FROM DEFAULTEXPR_TBL;
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
-- syntax errors
|
|
|
|
-- test for extraneous comma
|
1997-09-16 18:15:52 +02:00
|
|
|
CREATE TABLE error_tbl (i int DEFAULT (100, ));
|
1999-10-04 01:55:40 +02:00
|
|
|
-- this will fail because gram.y uses b_expr not a_expr for defaults,
|
|
|
|
-- to avoid a shift/reduce conflict that arises from NOT NULL being
|
|
|
|
-- part of the column definition syntax:
|
1999-11-20 22:41:31 +01:00
|
|
|
CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
|
1999-10-04 01:55:40 +02:00
|
|
|
-- this should work, however:
|
1999-11-20 22:41:31 +01:00
|
|
|
CREATE TABLE error_tbl (b1 bool DEFAULT (1 IN (1, 2)));
|
1999-10-04 01:55:40 +02:00
|
|
|
|
|
|
|
DROP TABLE error_tbl;
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- CHECK syntax
|
|
|
|
--
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
CREATE TABLE CHECK_TBL (x int,
|
|
|
|
CONSTRAINT CHECK_CON CHECK (x > 3));
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
INSERT INTO CHECK_TBL VALUES (5);
|
|
|
|
INSERT INTO CHECK_TBL VALUES (4);
|
|
|
|
INSERT INTO CHECK_TBL VALUES (3);
|
|
|
|
INSERT INTO CHECK_TBL VALUES (2);
|
|
|
|
INSERT INTO CHECK_TBL VALUES (6);
|
|
|
|
INSERT INTO CHECK_TBL VALUES (1);
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
SELECT '' AS three, * FROM CHECK_TBL;
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
CREATE SEQUENCE CHECK_SEQ;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
CREATE TABLE CHECK2_TBL (x int, y text, z int,
|
|
|
|
CONSTRAINT SEQUENCE_CON
|
|
|
|
CHECK (x > 3 and y <> 'check failed' and z < 8));
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
INSERT INTO CHECK2_TBL VALUES (4, 'check ok', -2);
|
|
|
|
INSERT INTO CHECK2_TBL VALUES (1, 'x check failed', -2);
|
|
|
|
INSERT INTO CHECK2_TBL VALUES (5, 'z check failed', 10);
|
|
|
|
INSERT INTO CHECK2_TBL VALUES (0, 'check failed', -2);
|
|
|
|
INSERT INTO CHECK2_TBL VALUES (6, 'check failed', 11);
|
|
|
|
INSERT INTO CHECK2_TBL VALUES (7, 'check ok', 7);
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
SELECT '' AS two, * from CHECK2_TBL;
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
-- Check constraints on INSERT
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
CREATE SEQUENCE INSERT_SEQ;
|
|
|
|
|
|
|
|
CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
|
2000-01-16 20:57:48 +01:00
|
|
|
y TEXT DEFAULT '-NULL-',
|
|
|
|
z INT DEFAULT -1 * currval('insert_seq'),
|
Apply table and domain CHECK constraints in name order.
Previously, CHECK constraints of the same scope were checked in whatever
order they happened to be read from pg_constraint. (Usually, but not
reliably, this would be creation order for domain constraints and reverse
creation order for table constraints, because of differing implementation
details.) Nondeterministic results of this sort are problematic at least
for testing purposes, and in discussion it was agreed to be a violation of
the principle of least astonishment. Therefore, borrow the principle
already established for triggers, and apply such checks in name order
(using strcmp() sort rules). This lets users control the check order
if they have a mind to.
Domain CHECK constraints still follow the rule of checking lower nested
domains' constraints first; the name sort only applies to multiple
constraints attached to the same domain.
In passing, I failed to resist the temptation to wordsmith a bit in
create_domain.sgml.
Apply to HEAD only, since this could result in a behavioral change in
existing applications, and the potential regression test failures have
not actually been observed in our buildfarm.
2015-03-23 21:59:29 +01:00
|
|
|
CONSTRAINT INSERT_TBL_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
|
2000-01-16 20:57:48 +01:00
|
|
|
CHECK (x + z = 0));
|
1997-09-16 18:15:52 +02:00
|
|
|
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
|
1997-09-16 18:15:52 +02:00
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
SELECT '' AS zero, * FROM INSERT_TBL;
|
|
|
|
|
|
|
|
SELECT 'one' AS one, nextval('insert_seq');
|
1997-09-16 18:15:52 +02:00
|
|
|
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO INSERT_TBL(y) VALUES ('Y');
|
|
|
|
INSERT INTO INSERT_TBL(y) VALUES ('Y');
|
|
|
|
INSERT INTO INSERT_TBL(x,z) VALUES (1, -2);
|
|
|
|
INSERT INTO INSERT_TBL(z,x) VALUES (-7, 7);
|
1997-09-16 18:15:52 +02:00
|
|
|
INSERT INTO INSERT_TBL VALUES (5, 'check failed', -5);
|
|
|
|
INSERT INTO INSERT_TBL VALUES (7, '!check failed', -7);
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT '' AS four, * FROM INSERT_TBL;
|
|
|
|
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO INSERT_TBL(y,z) VALUES ('check failed', 4);
|
|
|
|
INSERT INTO INSERT_TBL(x,y) VALUES (5, 'check failed');
|
|
|
|
INSERT INTO INSERT_TBL(x,y) VALUES (5, '!check failed');
|
|
|
|
INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT '' AS six, * FROM INSERT_TBL;
|
|
|
|
|
|
|
|
SELECT 'seven' AS one, nextval('insert_seq');
|
|
|
|
|
1997-10-17 11:59:09 +02:00
|
|
|
INSERT INTO INSERT_TBL(y) VALUES ('Y');
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT 'eight' AS one, currval('insert_seq');
|
1997-09-16 18:15:52 +02:00
|
|
|
|
2013-04-20 17:04:41 +02:00
|
|
|
-- According to SQL, it is OK to insert a record that gives rise to NULL
|
2000-01-20 00:55:03 +01:00
|
|
|
-- constraint-condition results. Postgres used to reject this, but it
|
|
|
|
-- was wrong:
|
|
|
|
INSERT INTO INSERT_TBL VALUES (null, null, null);
|
|
|
|
|
|
|
|
SELECT '' AS nine, * FROM INSERT_TBL;
|
|
|
|
|
Don't allow system columns in CHECK constraints, except tableoid.
Previously, arbitray system columns could be mentioned in table
constraints, but they were not correctly checked at runtime, because
the values weren't actually set correctly in the tuple. Since it
seems easy enough to initialize the table OID properly, do that,
and continue allowing that column, but disallow the rest unless and
until someone figures out a way to make them work properly.
No back-patch, because this doesn't seem important enough to take the
risk of destabilizing the back branches. In fact, this will pose a
dump-and-reload hazard for those upgrading from previous versions:
constraints that were accepted before but were not correctly enforced
will now either be enforced correctly or not accepted at all. Either
could result in restore failures, but in practice I think very few
users will notice the difference, since the use case is pretty
marginal anyway and few users will be relying on features that have
not historically worked.
Amit Kapila, reviewed by Rushabh Lathia, with doc changes by me.
2013-09-23 19:31:22 +02:00
|
|
|
--
|
|
|
|
-- Check constraints on system columns
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool,
|
|
|
|
altitude int,
|
|
|
|
CHECK (NOT (is_capital AND tableoid::regclass::text = 'sys_col_check_tbl')));
|
|
|
|
|
|
|
|
INSERT INTO SYS_COL_CHECK_TBL VALUES ('Seattle', 'Washington', false, 100);
|
|
|
|
INSERT INTO SYS_COL_CHECK_TBL VALUES ('Olympia', 'Washington', true, 100);
|
|
|
|
|
|
|
|
SELECT *, tableoid::regclass::text FROM SYS_COL_CHECK_TBL;
|
|
|
|
|
|
|
|
DROP TABLE SYS_COL_CHECK_TBL;
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Check constraints on system columns other then TableOid should return error
|
|
|
|
--
|
|
|
|
CREATE TABLE SYS_COL_CHECK_TBL (city text, state text, is_capital bool,
|
|
|
|
altitude int,
|
|
|
|
CHECK (NOT (is_capital AND ctid::text = 'sys_col_check_tbl')));
|
|
|
|
|
2000-01-16 20:57:48 +01:00
|
|
|
--
|
|
|
|
-- Check inheritance of defaults and constraints
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE INSERT_CHILD (cx INT default 42,
|
|
|
|
cy INT CHECK (cy > x))
|
|
|
|
INHERITS (INSERT_TBL);
|
|
|
|
|
|
|
|
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,11);
|
|
|
|
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,6);
|
|
|
|
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (6,-7,7);
|
|
|
|
INSERT INTO INSERT_CHILD(x,y,z,cy) VALUES (6,'check failed',-6,7);
|
|
|
|
|
|
|
|
SELECT * FROM INSERT_CHILD;
|
|
|
|
|
|
|
|
DROP TABLE INSERT_CHILD;
|
|
|
|
|
2012-04-21 04:46:20 +02:00
|
|
|
--
|
|
|
|
-- Check NO INHERIT type of constraints and inheritance
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE ATACC1 (TEST INT
|
2012-07-24 21:49:54 +02:00
|
|
|
CHECK (TEST > 0) NO INHERIT);
|
2012-04-21 04:46:20 +02:00
|
|
|
|
|
|
|
CREATE TABLE ATACC2 (TEST2 INT) INHERITS (ATACC1);
|
|
|
|
-- check constraint is not there on child
|
|
|
|
INSERT INTO ATACC2 (TEST) VALUES (-3);
|
|
|
|
-- check constraint is there on parent
|
|
|
|
INSERT INTO ATACC1 (TEST) VALUES (-3);
|
|
|
|
DROP TABLE ATACC1 CASCADE;
|
|
|
|
|
|
|
|
CREATE TABLE ATACC1 (TEST INT, TEST2 INT
|
2012-07-24 21:49:54 +02:00
|
|
|
CHECK (TEST > 0), CHECK (TEST2 > 10) NO INHERIT);
|
2012-04-21 04:46:20 +02:00
|
|
|
|
|
|
|
CREATE TABLE ATACC2 () INHERITS (ATACC1);
|
|
|
|
-- check constraint is there on child
|
|
|
|
INSERT INTO ATACC2 (TEST) VALUES (-3);
|
|
|
|
-- check constraint is there on parent
|
|
|
|
INSERT INTO ATACC1 (TEST) VALUES (-3);
|
|
|
|
-- check constraint is not there on child
|
|
|
|
INSERT INTO ATACC2 (TEST2) VALUES (3);
|
|
|
|
-- check constraint is there on parent
|
|
|
|
INSERT INTO ATACC1 (TEST2) VALUES (3);
|
|
|
|
DROP TABLE ATACC1 CASCADE;
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
-- Check constraints on INSERT INTO
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
DELETE FROM INSERT_TBL;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
ALTER SEQUENCE INSERT_SEQ RESTART WITH 4;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
CREATE TABLE tmp (xd INT, yd TEXT, zd INT);
|
1997-08-28 06:49:34 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
INSERT INTO tmp VALUES (null, 'Y', null);
|
|
|
|
INSERT INTO tmp VALUES (5, '!check failed', null);
|
|
|
|
INSERT INTO tmp VALUES (null, 'try again', null);
|
|
|
|
INSERT INTO INSERT_TBL(y) select yd from tmp;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT '' AS three, * FROM INSERT_TBL;
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
INSERT INTO INSERT_TBL SELECT * FROM tmp WHERE yd = 'try again';
|
|
|
|
INSERT INTO INSERT_TBL(y,z) SELECT yd, -7 FROM tmp WHERE yd = 'try again';
|
|
|
|
INSERT INTO INSERT_TBL(y,z) SELECT yd, -8 FROM tmp WHERE yd = 'try again';
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT '' AS four, * FROM INSERT_TBL;
|
1997-08-28 06:49:34 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
DROP TABLE tmp;
|
1997-08-28 06:49:34 +02:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
-- Check constraints on UPDATE
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
|
2000-01-20 00:55:03 +01:00
|
|
|
UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
|
1997-09-16 18:15:52 +02:00
|
|
|
UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
|
|
|
|
UPDATE INSERT_TBL SET x = -z, z = -x;
|
1997-10-17 11:59:09 +02:00
|
|
|
UPDATE INSERT_TBL SET x = z, z = x;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
SELECT * FROM INSERT_TBL;
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
-- DROP TABLE INSERT_TBL;
|
|
|
|
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
1997-08-28 06:49:34 +02:00
|
|
|
-- Check constraints on COPY FROM
|
1997-09-16 18:15:52 +02:00
|
|
|
--
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
CREATE TABLE COPY_TBL (x INT, y TEXT, z INT,
|
|
|
|
CONSTRAINT COPY_CON
|
|
|
|
CHECK (x > 3 AND y <> 'check failed' AND x < 7 ));
|
1997-09-16 18:15:52 +02:00
|
|
|
|
2000-10-27 22:00:22 +02:00
|
|
|
COPY COPY_TBL FROM '@abs_srcdir@/data/constro.data';
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
SELECT '' AS two, * FROM COPY_TBL;
|
|
|
|
|
2000-10-27 22:00:22 +02:00
|
|
|
COPY COPY_TBL FROM '@abs_srcdir@/data/constrf.data';
|
1997-09-16 18:15:52 +02:00
|
|
|
|
|
|
|
SELECT * FROM COPY_TBL;
|
1997-12-05 01:01:22 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Primary keys
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text);
|
|
|
|
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (1, 'one');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (2, 'two');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (1, 'three');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (4, 'three');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
|
|
|
|
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
|
|
|
|
|
|
|
|
SELECT '' AS four, * FROM PRIMARY_TBL;
|
|
|
|
|
|
|
|
DROP TABLE PRIMARY_TBL;
|
|
|
|
|
|
|
|
CREATE TABLE PRIMARY_TBL (i int, t text,
|
|
|
|
PRIMARY KEY(i,t));
|
|
|
|
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (1, 'one');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (2, 'two');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (1, 'three');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (4, 'three');
|
|
|
|
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
|
|
|
|
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
|
|
|
|
|
|
|
|
SELECT '' AS three, * FROM PRIMARY_TBL;
|
|
|
|
|
|
|
|
DROP TABLE PRIMARY_TBL;
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Unique keys
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text);
|
|
|
|
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (2, 'two');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'three');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (4, 'four');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (5, 'one');
|
|
|
|
INSERT INTO UNIQUE_TBL (t) VALUES ('six');
|
|
|
|
INSERT INTO UNIQUE_TBL (t) VALUES ('seven');
|
|
|
|
|
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
The newly added ON CONFLICT clause allows to specify an alternative to
raising a unique or exclusion constraint violation error when inserting.
ON CONFLICT refers to constraints that can either be specified using a
inference clause (by specifying the columns of a unique constraint) or
by naming a unique or exclusion constraint. DO NOTHING avoids the
constraint violation, without touching the pre-existing row. DO UPDATE
SET ... [WHERE ...] updates the pre-existing tuple, and has access to
both the tuple proposed for insertion and the existing tuple; the
optional WHERE clause can be used to prevent an update from being
executed. The UPDATE SET and WHERE clauses have access to the tuple
proposed for insertion using the "magic" EXCLUDED alias, and to the
pre-existing tuple using the table name or its alias.
This feature is often referred to as upsert.
This is implemented using a new infrastructure called "speculative
insertion". It is an optimistic variant of regular insertion that first
does a pre-check for existing tuples and then attempts an insert. If a
violating tuple was inserted concurrently, the speculatively inserted
tuple is deleted and a new attempt is made. If the pre-check finds a
matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
If the insertion succeeds without detecting a conflict, the tuple is
deemed inserted.
To handle the possible ambiguity between the excluded alias and a table
named excluded, and for convenience with long relation names, INSERT
INTO now can alias its target table.
Bumps catversion as stored rules change.
Author: Peter Geoghegan, with significant contributions from Heikki
Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
Dean Rasheed, Stephen Frost and many others.
2015-05-08 05:31:36 +02:00
|
|
|
INSERT INTO UNIQUE_TBL VALUES (5, 'five-upsert-insert') ON CONFLICT (i) DO UPDATE SET t = 'five-upsert-update';
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (6, 'six-upsert-insert') ON CONFLICT (i) DO UPDATE SET t = 'six-upsert-update';
|
|
|
|
-- should fail
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'a'), (2, 'b'), (2, 'b') ON CONFLICT (i) DO UPDATE SET t = 'fails';
|
|
|
|
|
1997-12-05 01:01:22 +01:00
|
|
|
SELECT '' AS five, * FROM UNIQUE_TBL;
|
|
|
|
|
|
|
|
DROP TABLE UNIQUE_TBL;
|
|
|
|
|
|
|
|
CREATE TABLE UNIQUE_TBL (i int, t text,
|
|
|
|
UNIQUE(i,t));
|
|
|
|
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (2, 'two');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'three');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
|
|
|
|
INSERT INTO UNIQUE_TBL VALUES (5, 'one');
|
|
|
|
INSERT INTO UNIQUE_TBL (t) VALUES ('six');
|
|
|
|
|
|
|
|
SELECT '' AS five, * FROM UNIQUE_TBL;
|
|
|
|
|
|
|
|
DROP TABLE UNIQUE_TBL;
|
|
|
|
|
2009-07-29 22:56:21 +02:00
|
|
|
--
|
|
|
|
-- Deferrable unique constraints
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE unique_tbl (i int UNIQUE DEFERRABLE, t text);
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (0, 'one');
|
|
|
|
INSERT INTO unique_tbl VALUES (1, 'two');
|
|
|
|
INSERT INTO unique_tbl VALUES (2, 'tree');
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'four');
|
|
|
|
INSERT INTO unique_tbl VALUES (4, 'five');
|
|
|
|
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
-- default is immediate so this should fail right away
|
|
|
|
UPDATE unique_tbl SET i = 1 WHERE i = 0;
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
|
|
|
|
-- check is done at end of statement, so this should succeed
|
|
|
|
UPDATE unique_tbl SET i = i+1;
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
-- explicitly defer the constraint
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
SET CONSTRAINTS unique_tbl_i_key DEFERRED;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'three');
|
|
|
|
DELETE FROM unique_tbl WHERE t = 'tree'; -- makes constraint valid again
|
|
|
|
|
|
|
|
COMMIT; -- should succeed
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
-- try adding an initially deferred constraint
|
|
|
|
ALTER TABLE unique_tbl DROP CONSTRAINT unique_tbl_i_key;
|
|
|
|
ALTER TABLE unique_tbl ADD CONSTRAINT unique_tbl_i_key
|
|
|
|
UNIQUE (i) DEFERRABLE INITIALLY DEFERRED;
|
|
|
|
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (1, 'five');
|
|
|
|
INSERT INTO unique_tbl VALUES (5, 'one');
|
|
|
|
UPDATE unique_tbl SET i = 4 WHERE i = 2;
|
|
|
|
UPDATE unique_tbl SET i = 2 WHERE i = 4 AND t = 'four';
|
|
|
|
DELETE FROM unique_tbl WHERE i = 1 AND t = 'one';
|
|
|
|
DELETE FROM unique_tbl WHERE i = 5 AND t = 'five';
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
-- should fail at commit-time
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
|
|
|
|
COMMIT; -- should fail
|
|
|
|
|
|
|
|
-- make constraint check immediate
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
SET CONSTRAINTS ALL IMMEDIATE;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'Three'); -- should fail
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
-- forced check when SET CONSTRAINTS is called
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
SET CONSTRAINTS ALL DEFERRED;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
|
|
|
|
|
|
|
|
SET CONSTRAINTS ALL IMMEDIATE; -- should fail
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
-- test a HOT update that invalidates the conflicting tuple.
|
|
|
|
-- the trigger should still fire and catch the violation
|
|
|
|
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES (3, 'Three'); -- should succeed for now
|
|
|
|
UPDATE unique_tbl SET t = 'THREE' WHERE i = 3 AND t = 'Three';
|
|
|
|
|
|
|
|
COMMIT; -- should fail
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
-- test a HOT update that modifies the newly inserted tuple,
|
|
|
|
-- but should succeed because we then remove the other conflicting tuple.
|
|
|
|
|
|
|
|
BEGIN;
|
|
|
|
|
|
|
|
INSERT INTO unique_tbl VALUES(3, 'tree'); -- should succeed for now
|
|
|
|
UPDATE unique_tbl SET t = 'threex' WHERE t = 'tree';
|
|
|
|
DELETE FROM unique_tbl WHERE t = 'three';
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
|
|
|
|
SELECT * FROM unique_tbl;
|
|
|
|
|
|
|
|
DROP TABLE unique_tbl;
|
2009-12-07 06:22:23 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- EXCLUDE constraints
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE circles (
|
|
|
|
c1 CIRCLE,
|
|
|
|
c2 TEXT,
|
|
|
|
EXCLUDE USING gist
|
2010-01-02 18:53:57 +01:00
|
|
|
(c1 WITH &&, (c2::circle) WITH &&)
|
2009-12-07 06:22:23 +01:00
|
|
|
WHERE (circle_center(c1) <> '(0,0)')
|
|
|
|
);
|
|
|
|
|
|
|
|
-- these should succeed because they don't match the index predicate
|
|
|
|
INSERT INTO circles VALUES('<(0,0), 5>', '<(0,0), 5>');
|
2010-01-02 18:53:57 +01:00
|
|
|
INSERT INTO circles VALUES('<(0,0), 5>', '<(0,0), 4>');
|
2009-12-07 06:22:23 +01:00
|
|
|
|
|
|
|
-- succeed
|
|
|
|
INSERT INTO circles VALUES('<(10,10), 10>', '<(0,0), 5>');
|
|
|
|
-- fail, overlaps
|
2010-01-02 18:53:57 +01:00
|
|
|
INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>');
|
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
The newly added ON CONFLICT clause allows to specify an alternative to
raising a unique or exclusion constraint violation error when inserting.
ON CONFLICT refers to constraints that can either be specified using a
inference clause (by specifying the columns of a unique constraint) or
by naming a unique or exclusion constraint. DO NOTHING avoids the
constraint violation, without touching the pre-existing row. DO UPDATE
SET ... [WHERE ...] updates the pre-existing tuple, and has access to
both the tuple proposed for insertion and the existing tuple; the
optional WHERE clause can be used to prevent an update from being
executed. The UPDATE SET and WHERE clauses have access to the tuple
proposed for insertion using the "magic" EXCLUDED alias, and to the
pre-existing tuple using the table name or its alias.
This feature is often referred to as upsert.
This is implemented using a new infrastructure called "speculative
insertion". It is an optimistic variant of regular insertion that first
does a pre-check for existing tuples and then attempts an insert. If a
violating tuple was inserted concurrently, the speculatively inserted
tuple is deleted and a new attempt is made. If the pre-check finds a
matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
If the insertion succeeds without detecting a conflict, the tuple is
deemed inserted.
To handle the possible ambiguity between the excluded alias and a table
named excluded, and for convenience with long relation names, INSERT
INTO now can alias its target table.
Bumps catversion as stored rules change.
Author: Peter Geoghegan, with significant contributions from Heikki
Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
Dean Rasheed, Stephen Frost and many others.
2015-05-08 05:31:36 +02:00
|
|
|
-- succeed, because violation is ignored
|
|
|
|
INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>')
|
|
|
|
ON CONFLICT ON CONSTRAINT circles_c1_c2_excl DO NOTHING;
|
|
|
|
-- fail, because DO UPDATE variant requires unique index
|
|
|
|
INSERT INTO circles VALUES('<(20,20), 10>', '<(0,0), 4>')
|
|
|
|
ON CONFLICT ON CONSTRAINT circles_c1_c2_excl DO UPDATE SET c2 = EXCLUDED.c2;
|
2009-12-07 06:22:23 +01:00
|
|
|
-- succeed because c1 doesn't overlap
|
|
|
|
INSERT INTO circles VALUES('<(20,20), 1>', '<(0,0), 5>');
|
2010-01-02 18:53:57 +01:00
|
|
|
-- succeed because c2 doesn't overlap
|
|
|
|
INSERT INTO circles VALUES('<(20,20), 10>', '<(10,10), 5>');
|
2009-12-07 06:22:23 +01:00
|
|
|
|
|
|
|
-- should fail on existing data without the WHERE clause
|
|
|
|
ALTER TABLE circles ADD EXCLUDE USING gist
|
2010-01-02 18:53:57 +01:00
|
|
|
(c1 WITH &&, (c2::circle) WITH &&);
|
2009-12-07 06:22:23 +01:00
|
|
|
|
2011-06-06 04:30:04 +02:00
|
|
|
-- try reindexing an existing constraint
|
|
|
|
REINDEX INDEX circles_c1_c2_excl;
|
|
|
|
|
2009-12-07 06:22:23 +01:00
|
|
|
DROP TABLE circles;
|
|
|
|
|
|
|
|
-- Check deferred exclusion constraint
|
|
|
|
|
|
|
|
CREATE TABLE deferred_excl (
|
|
|
|
f1 int,
|
2015-05-11 18:25:28 +02:00
|
|
|
f2 int,
|
2009-12-07 06:22:23 +01:00
|
|
|
CONSTRAINT deferred_excl_con EXCLUDE (f1 WITH =) INITIALLY DEFERRED
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO deferred_excl VALUES(1);
|
|
|
|
INSERT INTO deferred_excl VALUES(2);
|
|
|
|
INSERT INTO deferred_excl VALUES(1); -- fail
|
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
The newly added ON CONFLICT clause allows to specify an alternative to
raising a unique or exclusion constraint violation error when inserting.
ON CONFLICT refers to constraints that can either be specified using a
inference clause (by specifying the columns of a unique constraint) or
by naming a unique or exclusion constraint. DO NOTHING avoids the
constraint violation, without touching the pre-existing row. DO UPDATE
SET ... [WHERE ...] updates the pre-existing tuple, and has access to
both the tuple proposed for insertion and the existing tuple; the
optional WHERE clause can be used to prevent an update from being
executed. The UPDATE SET and WHERE clauses have access to the tuple
proposed for insertion using the "magic" EXCLUDED alias, and to the
pre-existing tuple using the table name or its alias.
This feature is often referred to as upsert.
This is implemented using a new infrastructure called "speculative
insertion". It is an optimistic variant of regular insertion that first
does a pre-check for existing tuples and then attempts an insert. If a
violating tuple was inserted concurrently, the speculatively inserted
tuple is deleted and a new attempt is made. If the pre-check finds a
matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
If the insertion succeeds without detecting a conflict, the tuple is
deemed inserted.
To handle the possible ambiguity between the excluded alias and a table
named excluded, and for convenience with long relation names, INSERT
INTO now can alias its target table.
Bumps catversion as stored rules change.
Author: Peter Geoghegan, with significant contributions from Heikki
Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
Dean Rasheed, Stephen Frost and many others.
2015-05-08 05:31:36 +02:00
|
|
|
INSERT INTO deferred_excl VALUES(1) ON CONFLICT ON CONSTRAINT deferred_excl_con DO NOTHING; -- fail
|
2009-12-07 06:22:23 +01:00
|
|
|
BEGIN;
|
|
|
|
INSERT INTO deferred_excl VALUES(2); -- no fail here
|
|
|
|
COMMIT; -- should fail here
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO deferred_excl VALUES(3);
|
|
|
|
INSERT INTO deferred_excl VALUES(3); -- no fail here
|
|
|
|
COMMIT; -- should fail here
|
|
|
|
|
2015-05-11 18:25:28 +02:00
|
|
|
-- bug #13148: deferred constraint versus HOT update
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO deferred_excl VALUES(2, 1); -- no fail here
|
|
|
|
DELETE FROM deferred_excl WHERE f1 = 2 AND f2 IS NULL; -- remove old row
|
|
|
|
UPDATE deferred_excl SET f2 = 2 WHERE f1 = 2;
|
|
|
|
COMMIT; -- should not fail
|
|
|
|
|
|
|
|
SELECT * FROM deferred_excl;
|
|
|
|
|
2009-12-07 06:22:23 +01:00
|
|
|
ALTER TABLE deferred_excl DROP CONSTRAINT deferred_excl_con;
|
|
|
|
|
|
|
|
-- This should fail, but worth testing because of HOT updates
|
|
|
|
UPDATE deferred_excl SET f1 = 3;
|
|
|
|
|
|
|
|
ALTER TABLE deferred_excl ADD EXCLUDE (f1 WITH =);
|
|
|
|
|
|
|
|
DROP TABLE deferred_excl;
|
2014-12-23 13:06:44 +01:00
|
|
|
|
|
|
|
-- Comments
|
|
|
|
CREATE TABLE constraint_comments_tbl (a int CONSTRAINT the_constraint CHECK (a > 0));
|
|
|
|
CREATE DOMAIN constraint_comments_dom AS int CONSTRAINT the_constraint CHECK (value > 0);
|
|
|
|
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'yes, the comment';
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS 'yes, another comment';
|
|
|
|
|
|
|
|
-- no such constraint
|
|
|
|
COMMENT ON CONSTRAINT no_constraint ON constraint_comments_tbl IS 'yes, the comment';
|
|
|
|
COMMENT ON CONSTRAINT no_constraint ON DOMAIN constraint_comments_dom IS 'yes, another comment';
|
|
|
|
|
|
|
|
-- no such table/domain
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON no_comments_tbl IS 'bad comment';
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON DOMAIN no_comments_dom IS 'another bad comment';
|
|
|
|
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS NULL;
|
|
|
|
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS NULL;
|
|
|
|
|
|
|
|
DROP TABLE constraint_comments_tbl;
|
|
|
|
DROP DOMAIN constraint_comments_dom;
|