diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/constraints.out | 23 | ||||
| -rw-r--r-- | src/test/regress/expected/create_index.out | 61 | ||||
| -rw-r--r-- | src/test/regress/sql/constraints.sql | 14 | ||||
| -rw-r--r-- | src/test/regress/sql/create_index.sql | 37 |
4 files changed, 135 insertions, 0 deletions
diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out index e32cf8bb574..36ccbb5f155 100644 --- a/src/test/regress/expected/constraints.out +++ b/src/test/regress/expected/constraints.out @@ -446,6 +446,29 @@ SELECT * FROM UNIQUE_TBL; (7 rows) DROP TABLE UNIQUE_TBL; +CREATE TABLE UNIQUE_TBL (i int UNIQUE NULLS NOT DISTINCT, t text); +INSERT INTO UNIQUE_TBL VALUES (1, 'one'); +INSERT INTO UNIQUE_TBL VALUES (2, 'two'); +INSERT INTO UNIQUE_TBL VALUES (1, 'three'); +ERROR: duplicate key value violates unique constraint "unique_tbl_i_key" +DETAIL: Key (i)=(1) already exists. +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'); +ERROR: duplicate key value violates unique constraint "unique_tbl_i_key" +DETAIL: Key (i)=(null) already exists. +SELECT * FROM UNIQUE_TBL; + i | t +---+------ + 1 | one + 2 | two + 4 | four + 5 | one + | six +(5 rows) + +DROP TABLE UNIQUE_TBL; CREATE TABLE UNIQUE_TBL (i int, t text, UNIQUE(i,t)); INSERT INTO UNIQUE_TBL VALUES (1, 'one'); diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out index daf75dd5c44..53c8e830ceb 100644 --- a/src/test/regress/expected/create_index.out +++ b/src/test/regress/expected/create_index.out @@ -1272,6 +1272,67 @@ SELECT count(*) FROM tenk1 WHERE stringu1 = 'TVAAAA'; DROP INDEX hash_tuplesort_idx; RESET maintenance_work_mem; -- +-- Test unique null behavior +-- +CREATE TABLE unique_tbl (i int, t text); +CREATE UNIQUE INDEX unique_idx1 ON unique_tbl (i) NULLS DISTINCT; +CREATE UNIQUE INDEX unique_idx2 ON unique_tbl (i) NULLS NOT DISTINCT; +INSERT INTO unique_tbl VALUES (1, 'one'); +INSERT INTO unique_tbl VALUES (2, 'two'); +INSERT INTO unique_tbl VALUES (3, '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'); -- error from unique_idx2 +ERROR: duplicate key value violates unique constraint "unique_idx2" +DETAIL: Key (i)=(null) already exists. +DROP INDEX unique_idx1, unique_idx2; +INSERT INTO unique_tbl (t) VALUES ('seven'); +-- build indexes on filled table +CREATE UNIQUE INDEX unique_idx3 ON unique_tbl (i) NULLS DISTINCT; -- ok +CREATE UNIQUE INDEX unique_idx4 ON unique_tbl (i) NULLS NOT DISTINCT; -- error +ERROR: could not create unique index "unique_idx4" +DETAIL: Key (i)=(null) is duplicated. +DELETE FROM unique_tbl WHERE t = 'seven'; +CREATE UNIQUE INDEX unique_idx4 ON unique_tbl (i) NULLS NOT DISTINCT; -- ok now +\d unique_tbl + Table "public.unique_tbl" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + i | integer | | | + t | text | | | +Indexes: + "unique_idx3" UNIQUE, btree (i) + "unique_idx4" UNIQUE, btree (i) NULLS NOT DISTINCT + +\d unique_idx3 + Index "public.unique_idx3" + Column | Type | Key? | Definition +--------+---------+------+------------ + i | integer | yes | i +unique, btree, for table "public.unique_tbl" + +\d unique_idx4 + Index "public.unique_idx4" + Column | Type | Key? | Definition +--------+---------+------+------------ + i | integer | yes | i +unique nulls not distinct, btree, for table "public.unique_tbl" + +SELECT pg_get_indexdef('unique_idx3'::regclass); + pg_get_indexdef +---------------------------------------------------------------------- + CREATE UNIQUE INDEX unique_idx3 ON public.unique_tbl USING btree (i) +(1 row) + +SELECT pg_get_indexdef('unique_idx4'::regclass); + pg_get_indexdef +----------------------------------------------------------------------------------------- + CREATE UNIQUE INDEX unique_idx4 ON public.unique_tbl USING btree (i) NULLS NOT DISTINCT +(1 row) + +DROP TABLE unique_tbl; +-- -- Test functional index -- CREATE TABLE func_index_heap (f1 text, f2 text); diff --git a/src/test/regress/sql/constraints.sql b/src/test/regress/sql/constraints.sql index 458f8057785..34de0c969ae 100644 --- a/src/test/regress/sql/constraints.sql +++ b/src/test/regress/sql/constraints.sql @@ -306,6 +306,20 @@ SELECT * FROM UNIQUE_TBL; DROP TABLE UNIQUE_TBL; +CREATE TABLE UNIQUE_TBL (i int UNIQUE NULLS NOT DISTINCT, 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'); + +SELECT * FROM UNIQUE_TBL; + +DROP TABLE UNIQUE_TBL; + CREATE TABLE UNIQUE_TBL (i int, t text, UNIQUE(i,t)); diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sql index 8b353be16e0..9003950a1f7 100644 --- a/src/test/regress/sql/create_index.sql +++ b/src/test/regress/sql/create_index.sql @@ -388,6 +388,43 @@ RESET maintenance_work_mem; -- +-- Test unique null behavior +-- +CREATE TABLE unique_tbl (i int, t text); + +CREATE UNIQUE INDEX unique_idx1 ON unique_tbl (i) NULLS DISTINCT; +CREATE UNIQUE INDEX unique_idx2 ON unique_tbl (i) NULLS NOT DISTINCT; + +INSERT INTO unique_tbl VALUES (1, 'one'); +INSERT INTO unique_tbl VALUES (2, 'two'); +INSERT INTO unique_tbl VALUES (3, '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'); -- error from unique_idx2 + +DROP INDEX unique_idx1, unique_idx2; + +INSERT INTO unique_tbl (t) VALUES ('seven'); + +-- build indexes on filled table +CREATE UNIQUE INDEX unique_idx3 ON unique_tbl (i) NULLS DISTINCT; -- ok +CREATE UNIQUE INDEX unique_idx4 ON unique_tbl (i) NULLS NOT DISTINCT; -- error + +DELETE FROM unique_tbl WHERE t = 'seven'; + +CREATE UNIQUE INDEX unique_idx4 ON unique_tbl (i) NULLS NOT DISTINCT; -- ok now + +\d unique_tbl +\d unique_idx3 +\d unique_idx4 +SELECT pg_get_indexdef('unique_idx3'::regclass); +SELECT pg_get_indexdef('unique_idx4'::regclass); + +DROP TABLE unique_tbl; + + +-- -- Test functional index -- CREATE TABLE func_index_heap (f1 text, f2 text); |
