From 868411cbd2f040d906379e9d0389643fdf41361d Mon Sep 17 00:00:00 2001 From: Rebecca Taft Date: Thu, 17 Dec 2020 11:30:37 -0600 Subject: [PATCH] sql: update unique violation error to be Postgres compatible This commit fixes the violation error for unique constraints to match the message used by Postres. Previously, the error would have been printed as: duplicate key value (k)=(1) violates unique constraint "primary" Now this message is printed as: duplicate key value violates unique constraint "primary" DETAIL: Key (k)=(1) already exists\. Release note (sql change): The error message for unique constraint violations now matches the error used by Postgres. For example, the new error message looks like this: ERROR: duplicate key value violates unique constraint "primary" DETAIL: Key (k)=(1) already exists. --- pkg/sql/logictest/testdata/logic_test/alter_table | 2 +- pkg/sql/logictest/testdata/logic_test/array | 2 +- pkg/sql/logictest/testdata/logic_test/cascade | 6 +++--- .../testdata/logic_test/collatedstring_constraint | 2 +- pkg/sql/logictest/testdata/logic_test/datetime | 2 +- pkg/sql/logictest/testdata/logic_test/fk | 2 +- .../testdata/logic_test/hash_sharded_index | 2 +- pkg/sql/logictest/testdata/logic_test/insert | 8 ++++---- pkg/sql/logictest/testdata/logic_test/interleaved | 6 +++--- .../testdata/logic_test/materialized_view | 2 +- .../logictest/testdata/logic_test/multi_statement | 4 ++-- .../testdata/logic_test/parallel_stmts_compat | 12 ++++++------ .../logictest/testdata/logic_test/partial_index | 12 ++++++------ pkg/sql/logictest/testdata/logic_test/txn | 2 +- pkg/sql/logictest/testdata/logic_test/update | 8 ++++---- pkg/sql/logictest/testdata/logic_test/upsert | 6 +++--- pkg/sql/mutation_test.go | 6 +++--- pkg/sql/opt/exec/execbuilder/testdata/insert | 4 ++-- pkg/sql/opt/exec/execbuilder/testdata/show_trace | 4 ++-- pkg/sql/opt/exec/execbuilder/testdata/update | 4 ++-- pkg/sql/opt/exec/execbuilder/testdata/upsert | 2 +- pkg/sql/row/errors.go | 15 +++++++++------ pkg/sql/testdata/savepoints | 2 +- 23 files changed, 59 insertions(+), 56 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/alter_table b/pkg/sql/logictest/testdata/logic_test/alter_table index 3d9f0600de0a..75d3944f7e9d 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_table +++ b/pkg/sql/logictest/testdata/logic_test/alter_table @@ -226,7 +226,7 @@ ALTER TABLE t ADD d INT UNIQUE statement ok INSERT INTO t VALUES (4, 9, 1) -statement error duplicate key value \(d\)=\(1\) violates unique constraint \"t_d_key\" +statement error duplicate key value violates unique constraint \"t_d_key\"\nDETAIL: Key \(d\)=\(1\) already exists\. INSERT INTO t VALUES (5, 9, 1) # Add a column with no default value diff --git a/pkg/sql/logictest/testdata/logic_test/array b/pkg/sql/logictest/testdata/logic_test/array index 01c86c12884a..b9987d58757a 100644 --- a/pkg/sql/logictest/testdata/logic_test/array +++ b/pkg/sql/logictest/testdata/logic_test/array @@ -1362,7 +1362,7 @@ INSERT INTO t VALUES # Test that the unique index rejects bad inserts. # Disabled until #50659 is resolved. -#statement error pq: duplicate key value \(x\)=\(ARRAY\[1,NULL,10\]\) violates unique constraint "primary" +#statement error pq: duplicate key value violates unique constraint "primary"\nDETAIL: Key \(x\)=\(ARRAY\[1,NULL,10\]\) already exists\. #INSERT INTO t VALUES (ARRAY[1, NULL, 10]) query T diff --git a/pkg/sql/logictest/testdata/logic_test/cascade b/pkg/sql/logictest/testdata/logic_test/cascade index 6233daf881cd..a1bb3af7255c 100644 --- a/pkg/sql/logictest/testdata/logic_test/cascade +++ b/pkg/sql/logictest/testdata/logic_test/cascade @@ -331,7 +331,7 @@ SELECT * FROM b; 1 2 3 4 5 1006 7 8 9 10 # Also ensure that normal errors are still correctly wrapped even if cascading. -statement error pq: duplicate key value \(id\)=\(1\) violates unique constraint "primary" +statement error pq: duplicate key value violates unique constraint "primary"\nDETAIL: Key \(id\)=\(1\) already exists\. UPDATE a SET id = 1 WHERE id = 1006; # 7. ON DELETE SET NULL @@ -3293,7 +3293,7 @@ statement oK INSERT INTO a VALUES ('original'), ('default'); INSERT INTO b VALUES ('b1', 'original'), ('b2', 'default'); -statement error pq: duplicate key value \(a_id\)=\('default'\) violates unique constraint "b_a_id_key" +statement error pq: duplicate key value violates unique constraint "b_a_id_key"\nDETAIL: Key \(a_id\)=\('default'\) already exists\. DELETE FROM a WHERE id = 'original'; # Clean up after the test. @@ -3676,7 +3676,7 @@ statement oK INSERT INTO a VALUES ('original'), ('default'); INSERT INTO b VALUES ('b1', 'original'), ('b2', 'default'); -statement error pq: duplicate key value \(a_id\)=\('default'\) violates unique constraint "b_a_id_key" +statement error pq: duplicate key value violates unique constraint "b_a_id_key"\nDETAIL: Key \(a_id\)=\('default'\) already exists\. UPDATE a SET id = 'updated' WHERE id = 'original'; # Clean up after the test. diff --git a/pkg/sql/logictest/testdata/logic_test/collatedstring_constraint b/pkg/sql/logictest/testdata/logic_test/collatedstring_constraint index 6d7d2ad36a3d..b078c59a69af 100644 --- a/pkg/sql/logictest/testdata/logic_test/collatedstring_constraint +++ b/pkg/sql/logictest/testdata/logic_test/collatedstring_constraint @@ -8,7 +8,7 @@ CREATE TABLE p ( statement ok INSERT INTO p VALUES ('a' COLLATE en_u_ks_level1) -statement error duplicate key value \(a\)=\('a' COLLATE en_u_ks_level1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(a\)=\('a' COLLATE en_u_ks_level1\) already exists\. INSERT INTO p VALUES ('A' COLLATE en_u_ks_level1) statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/datetime b/pkg/sql/logictest/testdata/logic_test/datetime index 9be122382f72..5764c2e07f68 100644 --- a/pkg/sql/logictest/testdata/logic_test/datetime +++ b/pkg/sql/logictest/testdata/logic_test/datetime @@ -30,7 +30,7 @@ SELECT * FROM t WHERE a = '2015-08-25 04:45:45.53453+01:00'::timestamp 2015-08-25 04:45:45.53453 +0000 +0000 2015-08-25 00:00:00 +0000 +0000 02:45:02.234 # insert duplicate value with different time zone offset -statement error duplicate key value \(a\)=\('2015-08-30 03:34:45\.34567'\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(a\)=\('2015-08-30 03:34:45\.34567'\) already exists\. INSERT INTO t VALUES ('2015-08-30 03:34:45.34567-07:00', '2015-08-31', '35h2s') diff --git a/pkg/sql/logictest/testdata/logic_test/fk b/pkg/sql/logictest/testdata/logic_test/fk index 22a1bfb25335..0a526a3559e1 100644 --- a/pkg/sql/logictest/testdata/logic_test/fk +++ b/pkg/sql/logictest/testdata/logic_test/fk @@ -2824,7 +2824,7 @@ INSERT INTO child (c, p) VALUES (200, 2) # These two test cases are sort of undefined behavior, since their # success/failure depends on the order in which the updates are performed. -statement error duplicate key value \(p\)=\(3\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(p\)=\(3\) already exists\. UPDATE parent SET p = p + 1 statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/hash_sharded_index b/pkg/sql/logictest/testdata/logic_test/hash_sharded_index index f9bcbd8e308f..9b544f3f038a 100644 --- a/pkg/sql/logictest/testdata/logic_test/hash_sharded_index +++ b/pkg/sql/logictest/testdata/logic_test/hash_sharded_index @@ -49,7 +49,7 @@ sharded_primary CREATE TABLE public.sharded_primary ( statement ok INSERT INTO sharded_primary values (1), (2), (3) -query error pq: duplicate key value \(crdb_internal_a_shard_10,a\)=\(6,1\) violates unique constraint "primary" +query error pq: duplicate key value violates unique constraint "primary"\nDETAIL: Key \(crdb_internal_a_shard_10,a\)=\(6,1\) already exists\. INSERT INTO sharded_primary values (1) # Ensure that the shard column is assigned into the column family of the first column in diff --git a/pkg/sql/logictest/testdata/logic_test/insert b/pkg/sql/logictest/testdata/logic_test/insert index 8c5ee2d9348a..2119a79b469a 100644 --- a/pkg/sql/logictest/testdata/logic_test/insert +++ b/pkg/sql/logictest/testdata/logic_test/insert @@ -41,13 +41,13 @@ SELECT v || 'hello' FROM [INSERT INTO kv VALUES ('e', 'f'), ('g', '') RETURNING fhello hello -statement error pgcode 23505 duplicate key value \(v\)=\('f'\) violates unique constraint "a" +statement error pgcode 23505 duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('f'\) already exists\. INSERT INTO kv VALUES ('h', 'f') statement ok INSERT INTO kv VALUES ('f', 'g') -statement error duplicate key value \(v\)=\('g'\) violates unique constraint "a" +statement error duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('g'\) already exists\. INSERT INTO kv VALUES ('h', 'g') query TT @@ -330,7 +330,7 @@ INSERT INTO abc VALUES (1, 2, 10) # Verify we get the correct message, even though internally the ConditionalPut # for the index key will also fail. -statement error pgcode 23505 duplicate key value \(a,b\)=\(1,2\) violates unique constraint "primary" +statement error pgcode 23505 duplicate key value violates unique constraint "primary"\nDETAIL: Key \(a,b\)=\(1,2\) already exists\. INSERT INTO abc VALUES (1, 2, 20) statement ok @@ -352,7 +352,7 @@ CREATE TABLE blindcput ( # The optimization thresholds at 10 k/v operations, so we need at least that # many in one batch to trigger it. -statement error duplicate key value \(x\)=\(1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(x\)=\(1\) already exists\. INSERT INTO blindcput values (1, 1), (2, 2), (3, 3), (4, 4), (1, 5) statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/interleaved b/pkg/sql/logictest/testdata/logic_test/interleaved index 6a03e75cb1d4..5ef1497f61b4 100644 --- a/pkg/sql/logictest/testdata/logic_test/interleaved +++ b/pkg/sql/logictest/testdata/logic_test/interleaved @@ -369,17 +369,17 @@ INSERT INTO p20067 VALUES (1, 'John Doe'); INSERT INTO c20067 VALUES (1, 1, 'John Doe Junior'); COMMIT; -statement error duplicate key value \(name\)=\('John Doe Junior'\) violates unique constraint "uq_name" +statement error duplicate key value violates unique constraint "uq_name"\nDETAIL: Key \(name\)=\('John Doe Junior'\) already exists\. INSERT INTO c20067 VALUES (2, 1, 'John Doe Junior') -statement error duplicate key value \(name\)=\('John Doe Junior'\) violates unique constraint "uq_name" +statement error duplicate key value violates unique constraint "uq_name"\nDETAIL: Key \(name\)=\('John Doe Junior'\) already exists\. BEGIN; INSERT INTO p20067 VALUES (2, 'John Doe'); INSERT INTO c20067 VALUES (2, 1, 'John Doe Junior'); END; # End the last transaction. statement ok END -statement error duplicate key value \(p_id,c_id\)=\(1,1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(p_id,c_id\)=\(1,1\) already exists\. INSERT INTO c20067 VALUES (1, 1, 'John Doe') # Regression test for #26756: ensure that interleaved table joins don't get diff --git a/pkg/sql/logictest/testdata/logic_test/materialized_view b/pkg/sql/logictest/testdata/logic_test/materialized_view index 48214ac41bf3..b7653df957b4 100644 --- a/pkg/sql/logictest/testdata/logic_test/materialized_view +++ b/pkg/sql/logictest/testdata/logic_test/materialized_view @@ -107,7 +107,7 @@ CREATE MATERIALIZED VIEW v_dup AS SELECT x FROM dup; CREATE UNIQUE INDEX i ON v_dup (x); INSERT INTO dup VALUES (1), (1); -statement error pq: duplicate key value \(x\)=\(1\) violates unique constraint "i" +statement error pq: duplicate key value violates unique constraint "i"\nDETAIL: Key \(x\)=\(1\) already exists\. REFRESH MATERIALIZED VIEW v_dup # We shouldn't be able to mix materialized and non materialized views in DDLs. diff --git a/pkg/sql/logictest/testdata/logic_test/multi_statement b/pkg/sql/logictest/testdata/logic_test/multi_statement index 8833a7edbdc4..40270962ca33 100644 --- a/pkg/sql/logictest/testdata/logic_test/multi_statement +++ b/pkg/sql/logictest/testdata/logic_test/multi_statement @@ -19,7 +19,7 @@ c d # error if either statement returns an error # first statement returns an error. Second stmt shouldn't execute. -statement error duplicate key value \(k\)=\('a'\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\('a'\) already exists\. INSERT INTO kv (k,v) VALUES ('a', 'b'); INSERT INTO kv (k,v) VALUES ('e', 'f') query TT rowsort @@ -29,7 +29,7 @@ a b c d # second statement returns an error -statement error duplicate key value \(k\)=\('a'\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\('a'\) already exists\. INSERT INTO kv (k,v) VALUES ('g', 'h'); INSERT INTO kv (k,v) VALUES ('a', 'b') query TT rowsort diff --git a/pkg/sql/logictest/testdata/logic_test/parallel_stmts_compat b/pkg/sql/logictest/testdata/logic_test/parallel_stmts_compat index ecadac108236..ec95a771de25 100644 --- a/pkg/sql/logictest/testdata/logic_test/parallel_stmts_compat +++ b/pkg/sql/logictest/testdata/logic_test/parallel_stmts_compat @@ -21,7 +21,7 @@ CREATE TABLE fk( statement ok INSERT INTO kv VALUES (1, 2) RETURNING NOTHING -statement error duplicate key value \(k\)=\(1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(1\) already exists\. INSERT INTO kv VALUES (1, 2) RETURNING NOTHING statement ok @@ -33,7 +33,7 @@ UPSERT INTO kv VALUES (2, 500) RETURNING NOTHING statement ok UPDATE kv SET v = k WHERE k = 3 RETURNING NOTHING -statement error duplicate key value \(k\)=\(1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(1\) already exists\. UPDATE kv SET k = 1 WHERE k = 2 RETURNING NOTHING statement ok @@ -84,7 +84,7 @@ BEGIN statement ok INSERT INTO kv VALUES (4, 5) RETURNING NOTHING -statement error duplicate key value \(k\)=\(2\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(2\) already exists\. INSERT INTO kv VALUES (2, 3) RETURNING NOTHING statement error current transaction is aborted, commands ignored until end of transaction block @@ -190,7 +190,7 @@ BEGIN statement ok UPDATE kv SET k = 9 WHERE k = 1 RETURNING NOTHING -statement error duplicate key value \(k\)=\(3\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(3\) already exists\. UPDATE kv SET k = 3 WHERE k = 2 RETURNING NOTHING statement error current transaction is aborted, commands ignored until end of transaction block @@ -316,7 +316,7 @@ SELECT k, v FROM kv ORDER BY k statement ok BEGIN -statement error duplicate key value \(k\)=\(1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(1\) already exists\. INSERT INTO kv VALUES (1, 2) RETURNING NOTHING statement error current transaction is aborted, commands ignored until end of transaction block @@ -361,7 +361,7 @@ BEGIN statement ok EXECUTE x(1, 2) -statement error duplicate key value \(k\)=\(1\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(1\) already exists\. EXECUTE x(1, 2) statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/partial_index b/pkg/sql/logictest/testdata/logic_test/partial_index index f5996dab02bc..3d13892617e5 100644 --- a/pkg/sql/logictest/testdata/logic_test/partial_index +++ b/pkg/sql/logictest/testdata/logic_test/partial_index @@ -660,7 +660,7 @@ CREATE TABLE u ( ) # Inserting multiple rows that conflicts fails. -statement error pgcode 23505 duplicate key value \(a\)=\(1\) violates unique constraint \"i\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i\"\nDETAIL: Key \(a\)=\(1\) already exists\. INSERT INTO u VALUES (1, 1), (1, 2) # Inserting multiple rows that don't conflict succeeds. @@ -668,7 +668,7 @@ statement ok INSERT INTO u VALUES (1, 1), (2, 2), (1, -1) # Inserting a row that conflicts with an existing row fails. -statement error pgcode 23505 duplicate key value \(a\)=\(1\) violates unique constraint \"i\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i\"\nDETAIL: Key \(a\)=\(1\) already exists\. INSERT INTO u VALUES (1, 3) query II rowsort @@ -684,12 +684,12 @@ DELETE FROM u WHERE a = 2; INSERT INTO u VALUES (2, 2); # Updating a row in the unique partial index to conflict with another row fails. -statement error pgcode 23505 duplicate key value \(a\)=\(2\) violates unique constraint \"i\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i\"\nDETAIL: Key \(a\)=\(2\) already exists\. UPDATE u SET a = 2 WHERE b = 1 # Updating a row not in the unique partial index to conflict with a row in the # index fails. -statement error pgcode 23505 duplicate key value \(a\)=\(2\) violates unique constraint \"i\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i\"\nDETAIL: Key \(a\)=\(2\) already exists\. UPDATE u SET a = 2, b = 1 WHERE b = -1 # Updating a row not in the unique index to remain out of the unique index @@ -885,7 +885,7 @@ statement ok CREATE UNIQUE INDEX i2 ON u (a) WHERE b < 0; INSERT INTO u VALUES (-1, -1); -statement error pgcode 23505 duplicate key value \(a\)=\(-1\) violates unique constraint \"i2\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i2\"\nDETAIL: Key \(a\)=\(-1\) already exists\. INSERT INTO u VALUES (-1, -1) ON CONFLICT (a) WHERE b > 0 DO NOTHING # Two arbiters can be used to detect conflicts and avoid duplicate key errors. @@ -1005,7 +1005,7 @@ CREATE UNIQUE INDEX i2 ON u (a) WHERE b < 0; # There can be duplicate key errors from unique partial indexes that are not # arbiters. -statement error pgcode 23505 duplicate key value \(a\)=\(1\) violates unique constraint \"i2\" +statement error pgcode 23505 duplicate key value violates unique constraint \"i2\"\nDETAIL: Key \(a\)=\(1\) already exists\. INSERT INTO u VALUES (1, -1) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET a = 100 statement ok diff --git a/pkg/sql/logictest/testdata/logic_test/txn b/pkg/sql/logictest/testdata/logic_test/txn index f3e577640848..b25d54efff40 100644 --- a/pkg/sql/logictest/testdata/logic_test/txn +++ b/pkg/sql/logictest/testdata/logic_test/txn @@ -112,7 +112,7 @@ a c statement ok BEGIN -statement error duplicate key value \(k\)=\('a'\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\('a'\) already exists\. INSERT INTO kv VALUES('unique_key', 'some value'); INSERT INTO kv VALUES('a', 'c'); INSERT INTO kv VALUES('unique_key2', 'some value'); diff --git a/pkg/sql/logictest/testdata/logic_test/update b/pkg/sql/logictest/testdata/logic_test/update index 7dfe307bc892..476807d84191 100644 --- a/pkg/sql/logictest/testdata/logic_test/update +++ b/pkg/sql/logictest/testdata/logic_test/update @@ -95,7 +95,7 @@ c d e f f g -statement error duplicate key value \(v\)=\('g'\) violates unique constraint "a" +statement error duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('g'\) already exists\. UPDATE kv2 SET v = 'g' WHERE k IN ('a') statement count 1 @@ -255,10 +255,10 @@ SELECT * FROM abc statement count 1 INSERT INTO abc VALUES (4, 5, 6) -statement error duplicate key value \(a\)=\(4\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(a\)=\(4\) already exists\. UPDATE abc SET a = 4, b = 3 -statement error duplicate key value \(c\)=\(6\) violates unique constraint "d" +statement error duplicate key value violates unique constraint "d"\nDETAIL: Key \(c\)=\(6\) already exists\. UPDATE abc SET a = 2, c = 6 query III @@ -347,7 +347,7 @@ CREATE TABLE pks ( statement count 2 INSERT INTO pks VALUES (1, 2, 3), (4, 5, 3) -statement error duplicate key value \(k2,v\)=\(5,3\) violates unique constraint "i" +statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2,v\)=\(5,3\) already exists\. UPDATE pks SET k2 = 5 where k1 = 1 # Test updating only one of the columns of a multi-column primary key. diff --git a/pkg/sql/logictest/testdata/logic_test/upsert b/pkg/sql/logictest/testdata/logic_test/upsert index 9f38630edb1f..285ec85bbd7e 100644 --- a/pkg/sql/logictest/testdata/logic_test/upsert +++ b/pkg/sql/logictest/testdata/logic_test/upsert @@ -120,7 +120,7 @@ INSERT INTO kv VALUES (4, 10) ON CONFLICT (k) DO UPDATE SET v = v + 1 statement count 1 INSERT INTO kv VALUES (4, 10) ON CONFLICT (k) DO UPDATE SET v = kv.v + 20 -statement error duplicate key value \(k\)=\(3\) violates unique constraint "primary" +statement error duplicate key value violates unique constraint "primary"\nDETAIL: Key \(k\)=\(3\) already exists\. INSERT INTO kv VALUES (2, 10) ON CONFLICT (k) DO UPDATE SET k = 3, v = 10 statement count 1 @@ -1030,7 +1030,7 @@ statement error pq: UPSERT or INSERT...ON CONFLICT command cannot affect row a s INSERT INTO tdup VALUES (1, 2, 1), (1, 3, 1) ON CONFLICT (x) DO UPDATE SET z=1 # Verify that duplicate insert into secondary fails with regular conflict error. -statement error pq: duplicate key value \(y\)=\(2\) violates unique constraint "tdup_y_key" +statement error pq: duplicate key value violates unique constraint "tdup_y_key"\nDETAIL: Key \(y\)=\(2\) already exists\. INSERT INTO tdup VALUES (2, 2, 2), (3, 2, 2) ON CONFLICT (x) DO UPDATE SET z=1 statement ok @@ -1073,7 +1073,7 @@ SELECT * FROM tdup@tdup_y_z_key 3 2 NULL # Verify that duplicate secondary key fails with regular conflict error. -statement error pq: duplicate key value \(y,z\)=\(1,2\) violates unique constraint "tdup_y_z_key" +statement error pq: duplicate key value violates unique constraint "tdup_y_z_key"\nDETAIL: Key \(y,z\)=\(1,2\) already exists\. INSERT INTO tdup VALUES (6, 1, 1), (7, 1, 2) ON CONFLICT (y, z) DO UPDATE SET z=2 # With constant grouping columns (no error). diff --git a/pkg/sql/mutation_test.go b/pkg/sql/mutation_test.go index 7d7fba2e1d5c..5b07b0d07602 100644 --- a/pkg/sql/mutation_test.go +++ b/pkg/sql/mutation_test.go @@ -54,7 +54,7 @@ INSERT INTO d.a(a) VALUES (1); for i, step := range []func() (*gosql.Rows, error){step1, step2} { rows, err := step() if err != nil { - if !testutils.IsError(err, `duplicate key value \(a\)=\(1\)`) { + if !testutils.IsError(err, `duplicate key value`) { t.Errorf("%d: %v", i, err) } } else { @@ -68,7 +68,7 @@ INSERT INTO d.a(a) VALUES (1); err := rows.Scan(&val) if err != nil { - if !testutils.IsError(err, `duplicate key value \(a\)=\(1\)`) { + if !testutils.IsError(err, `duplicate key value`) { t.Errorf("%d: %v", i, err) } } else { @@ -80,7 +80,7 @@ INSERT INTO d.a(a) VALUES (1); for rows.Next() { err := rows.Scan(&val) if err != nil { - if !testutils.IsError(err, `duplicate key value \(a\)=\(1\)`) { + if !testutils.IsError(err, `duplicate key value`) { t.Errorf("%d: %v", i, err) } } diff --git a/pkg/sql/opt/exec/execbuilder/testdata/insert b/pkg/sql/opt/exec/execbuilder/testdata/insert index ca1a9ef154ec..6fe6d60130ea 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/insert +++ b/pkg/sql/opt/exec/execbuilder/testdata/insert @@ -81,7 +81,7 @@ output row: ['a' 'b'] output row: ['c' 'd'] output row: ['e' 'f'] -statement error pgcode 23505 duplicate key value \(v\)=\('f'\) violates unique constraint "a" +statement error pgcode 23505 duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('f'\) already exists\. INSERT INTO kv VALUES ('h', 'f') statement ok @@ -208,7 +208,7 @@ output row: ['c' 'd'] output row: ['e' 'f'] output row: ['f' 'g'] -statement error duplicate key value \(v\)=\('g'\) violates unique constraint "a" +statement error duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('g'\) already exists\. INSERT INTO kv VALUES ('h', 'g') statement ok diff --git a/pkg/sql/opt/exec/execbuilder/testdata/show_trace b/pkg/sql/opt/exec/execbuilder/testdata/show_trace index e2bf66a56de0..603ee06cdcd3 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/show_trace +++ b/pkg/sql/opt/exec/execbuilder/testdata/show_trace @@ -95,7 +95,7 @@ SELECT operation, message FROM [SHOW KV TRACE FOR SESSION] flow CPut /Table/54/1/1/0 -> /TUPLE/2:2:Int/2 flow InitPut /Table/54/2/2/0 -> /BYTES/0x89 kv.DistSender: sending partial batch r28: sending batch 1 CPut, 1 EndTxn to (n1,s1):1 -exec stmt execution failed after 0 rows: duplicate key value (k)=(1) violates unique constraint "primary" +exec stmt execution failed after 0 rows: duplicate key value violates unique constraint "primary"\nDETAIL: Key (k)=(1) already exists\. statement error duplicate key value SET tracing = on,kv,results; INSERT INTO t.kv(k, v) VALUES (2,2); SET tracing = off @@ -108,7 +108,7 @@ SELECT operation, message FROM [SHOW KV TRACE FOR SESSION] flow CPut /Table/54/1/2/0 -> /TUPLE/2:2:Int/2 flow InitPut /Table/54/2/2/0 -> /BYTES/0x8a kv.DistSender: sending partial batch r28: sending batch 1 CPut, 1 EndTxn to (n1,s1):1 -exec stmt execution failed after 0 rows: duplicate key value (v)=(2) violates unique constraint "woo" +exec stmt execution failed after 0 rows: duplicate key value violates unique constraint "woo"\nDETAIL: Key (v)=(2) already exists\. statement ok SET tracing = on,kv,results; CREATE TABLE t.kv2 AS TABLE t.kv; SET tracing = off diff --git a/pkg/sql/opt/exec/execbuilder/testdata/update b/pkg/sql/opt/exec/execbuilder/testdata/update index 4a819543a803..09c650eb7e4a 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/update +++ b/pkg/sql/opt/exec/execbuilder/testdata/update @@ -50,7 +50,7 @@ output row: ['c' 'd'] output row: ['e' 'f'] output row: ['f' 'g'] -statement error duplicate key value \(v\)=\('g'\) violates unique constraint "a" +statement error duplicate key value violates unique constraint "a"\nDETAIL: Key \(v\)=\('g'\) already exists\. UPDATE kv2 SET v = 'g' WHERE k IN ('a') statement ok @@ -212,7 +212,7 @@ CREATE TABLE pks ( statement count 2 INSERT INTO pks VALUES (1, 2, 3), (4, 5, 3) -statement error duplicate key value \(k2,v\)=\(5,3\) violates unique constraint "i" +statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2,v\)=\(5,3\) already exists\. UPDATE pks SET k2 = 5 where k1 = 1 # Test updating only one of the columns of a multi-column primary key. diff --git a/pkg/sql/opt/exec/execbuilder/testdata/upsert b/pkg/sql/opt/exec/execbuilder/testdata/upsert index b59036943c0c..616b7e908f75 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/upsert +++ b/pkg/sql/opt/exec/execbuilder/testdata/upsert @@ -372,7 +372,7 @@ flow Put /Table/57/1/2/0 -> /TUPLE/2:2:Int/2 flow Del /Table/57/2/3/0 flow CPut /Table/57/2/2/0 -> /BYTES/0x8a (expecting does not exist) kv.DistSender: sending partial batch r35: sending batch 1 Put, 1 EndTxn to (n1,s1):1 -exec stmt execution failed after 0 rows: duplicate key value (v)=(2) violates unique constraint "woo" +exec stmt execution failed after 0 rows: duplicate key value violates unique constraint "woo"\nDETAIL: Key (v)=(2) already exists\. subtest regression_32473 diff --git a/pkg/sql/row/errors.go b/pkg/sql/row/errors.go index cae0f3e8a8d9..4303f1caebc3 100644 --- a/pkg/sql/row/errors.go +++ b/pkg/sql/row/errors.go @@ -12,6 +12,7 @@ package row import ( "context" + "fmt" "strings" "github.com/cockroachdb/cockroach/pkg/keys" @@ -95,12 +96,14 @@ func NewUniquenessConstraintViolationError( "duplicate key value: decoding err=%s", err) } - return pgerror.WithConstraintName(pgerror.Newf(pgcode.UniqueViolation, - "duplicate key value (%s)=(%s) violates unique constraint %q", - strings.Join(names, ","), - strings.Join(values, ","), - index.Name, - ), index.Name) + return errors.WithDetail( + pgerror.WithConstraintName(pgerror.Newf(pgcode.UniqueViolation, + "duplicate key value violates unique constraint %q", index.Name, + ), index.Name), + fmt.Sprintf( + "Key (%s)=(%s) already exists.", strings.Join(names, ","), strings.Join(values, ","), + ), + ) } // NewLockNotAvailableError creates an error that represents an inability to diff --git a/pkg/sql/testdata/savepoints b/pkg/sql/testdata/savepoints index 6d08298bf4e1..24fb390a5ffc 100644 --- a/pkg/sql/testdata/savepoints +++ b/pkg/sql/testdata/savepoints @@ -172,7 +172,7 @@ COMMIT -- NoTxn -> Open ###..... (none) 4: SAVEPOINT foo -- 0 rows -- Open -> Open ####.... foo -5: INSERT INTO t(x) VALUES (1) -- pq: duplicate key value (x)=(1) violates unique constraint "t_x_key" +5: INSERT INTO t(x) VALUES (1) -- pq: duplicate key value violates unique constraint "t_x_key"\nDETAIL: Key (x)=(1) already exists\. -- Open -> Aborted XXXXXXXX foo 6: ROLLBACK TO SAVEPOINT foo -- 0 rows -- Aborted -> Open ####.... foo