1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-10 14:22:35 +03:00

Fix handling of NULL constraint conditions: per SQL92 spec, a NULL result

from a constraint condition does not violate the constraint (cf. discussion
on pghackers 12/9/99).  Implemented by adding a parameter to ExecQual,
specifying whether to return TRUE or FALSE when the qual result is
really NULL in three-valued boolean logic.  Currently, ExecRelCheck is
the only caller that asks for TRUE, but if we find any other places that
have the wrong response to NULL, it'll be easy to fix them.
This commit is contained in:
Tom Lane
2000-01-19 23:55:03 +00:00
parent 08fb7375e3
commit 6d1efd76fb
20 changed files with 143 additions and 104 deletions

View File

@@ -89,7 +89,6 @@ CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
CONSTRAINT INSERT_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
CHECK (x + z = 0));
INSERT INTO INSERT_TBL VALUES (null, null, null);
INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
SELECT '' AS zero, * FROM INSERT_TBL;
@@ -119,6 +118,13 @@ INSERT INTO INSERT_TBL(y) VALUES ('Y');
SELECT 'eight' AS one, currval('insert_seq');
-- According to SQL92, it is OK to insert a record that gives rise to NULL
-- 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;
--
-- Check inheritance of defaults and constraints
--
@@ -166,7 +172,7 @@ DROP TABLE tmp;
-- Check constraints on UPDATE
--
UPDATE INSERT_TBL SET x = NULL WHERE x = 6;
UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
UPDATE INSERT_TBL SET x = -z, z = -x;
UPDATE INSERT_TBL SET x = z, z = x;

View File

@@ -106,8 +106,6 @@ CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
z INT DEFAULT -1 * currval('insert_seq'),
CONSTRAINT INSERT_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
CHECK (x + z = 0));
INSERT INTO INSERT_TBL VALUES (null, null, null);
ERROR: ExecAppend: rejected due to CHECK constraint $2
INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
ERROR: ExecAppend: rejected due to CHECK constraint insert_con
SELECT '' AS zero, * FROM INSERT_TBL;
@@ -171,6 +169,22 @@ SELECT 'eight' AS one, currval('insert_seq');
eight | 8
(1 row)
-- According to SQL92, it is OK to insert a record that gives rise to NULL
-- 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;
nine | x | y | z
------+---+---------------+----
| 3 | Y | -3
| 7 | -NULL- | -7
| 7 | !check failed | -7
| 4 | -!NULL- | -4
| 5 | !check failed | -5
| 6 | -!NULL- | -6
| | |
(7 rows)
--
-- Check inheritance of defaults and constraints
--
@@ -212,7 +226,6 @@ SELECT '' AS three, * FROM INSERT_TBL;
(3 rows)
INSERT INTO INSERT_TBL SELECT * FROM tmp WHERE yd = 'try again';
ERROR: ExecAppend: rejected due to CHECK constraint $2
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';
ERROR: ExecAppend: rejected due to CHECK constraint insert_con
@@ -222,15 +235,15 @@ SELECT '' AS four, * FROM INSERT_TBL;
| 4 | Y | -4
| 5 | !check failed | -5
| 6 | try again | -6
| | try again |
| 7 | try again | -7
(4 rows)
(5 rows)
DROP TABLE tmp;
--
-- Check constraints on UPDATE
--
UPDATE INSERT_TBL SET x = NULL WHERE x = 6;
ERROR: ExecReplace: rejected due to CHECK constraint $2
UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
UPDATE INSERT_TBL SET x = -z, z = -x;
UPDATE INSERT_TBL SET x = z, z = x;
@@ -239,10 +252,11 @@ SELECT * FROM INSERT_TBL;
x | y | z
---+---------------+----
4 | Y | -4
5 | !check failed | -5
| try again |
7 | try again | -7
5 | !check failed |
6 | try again | -6
(4 rows)
(5 rows)
-- DROP TABLE INSERT_TBL;
--