pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedON CONFLICT with constraint name doesn't work
12+ messages / 4 participants
[nested] [flat]
* ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:34 Nikolay Samokhvalov <samokhvalov@gmail.com>
0 siblings, 2 replies; 12+ messages in thread
From: Nikolay Samokhvalov @ 2017-03-16 19:34 UTC (permalink / raw)
To: pgsql-bugs
Hi,
I'm trying to explicitly use the constraint name of my UNIQUE INDEX but it
doesn't work (Postgres 9.6.2):
[local]:5432 nikolay@test=# create table constr(id serial, msg text);
CREATE TABLE
[local]:5432 nikolay@test=# create unique index i_constr_msg on constr
using btree(md5(msg));
CREATE INDEX
[local]:5432 nikolay@test=# insert into constr (msg) values ('hoho') on
conflict on constraint i_constr_msg do nothing;
ERROR: constraint "i_constr_msg" for table "constr" does not exist
[local]:5432 nikolay@test=# \d constr
Table "public.constr"
Column | Type | Modifiers
--------+---------+-----------------------------------------------------
id | integer | not null default nextval('constr_id_seq'::regclass)
msg | text |
Indexes:
"i_constr_msg" UNIQUE, btree (md5(msg))
This works:
[local]:5432 nikolay@test=# insert into constr (msg) values ('hoho') on
conflict (md5(msg)) do nothing;
INSERT 0 1
I don't see anything in the current docs
https://www.postgresql.org/docs/9.6/static/sql-insert.html saying that I
cannot use the unique index' name here. So it definitely looks like a bug.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:40 Andres Freund <andres@anarazel.de>
parent: Nikolay Samokhvalov <samokhvalov@gmail.com>
1 sibling, 1 reply; 12+ messages in thread
From: Andres Freund @ 2017-03-16 19:40 UTC (permalink / raw)
To: Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-bugs
Hi,
On 2017-03-16 12:34:49 -0700, Nikolay Samokhvalov wrote:
> I'm trying to explicitly use the constraint name of my UNIQUE INDEX but it
> doesn't work (Postgres 9.6.2):
>
> [local]:5432 nikolay@test=# create table constr(id serial, msg text);
> CREATE TABLE
>
> [local]:5432 nikolay@test=# create unique index i_constr_msg on constr
> using btree(md5(msg));
> CREATE INDEX
A unique index isn't exactly the same as a unique constraint - you
really need to create a constraint.
- Andres
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:42 Heikki Linnakangas <hlinnaka@iki.fi>
parent: Nikolay Samokhvalov <samokhvalov@gmail.com>
1 sibling, 1 reply; 12+ messages in thread
From: Heikki Linnakangas @ 2017-03-16 19:42 UTC (permalink / raw)
To: Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-bugs
On 03/16/2017 09:34 PM, Nikolay Samokhvalov wrote:
> I don't see anything in the current docs
> https://www.postgresql.org/docs/9.6/static/sql-insert.html saying that I
> cannot use the unique index' name here. So it definitely looks like a bug.
This is by design. The docs on conflict_target says:
"Specifies which conflicts ON CONFLICT takes the alternative action on
by choosing arbiter indexes. Either performs unique index inference, or
names a *constraint* explicitly." (emphasis mine)
As it says, you can name a constraint explicitly. A unique index is not
a constraint.
We debated this for a long time when the ON CONFLICT feature was being
developed. In the end, we settled on this behavior, on the grounds that
a constraint is a logical concept, while an index is a physical
implementation detail. Note that the SQL standard also doesn't say
anything about indexes, but constraints are in the standard.
- Heikki
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:44 Peter Geoghegan <pg@bowt.ie>
parent: Heikki Linnakangas <hlinnaka@iki.fi>
0 siblings, 1 reply; 12+ messages in thread
From: Peter Geoghegan @ 2017-03-16 19:44 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-bugs
On Thu, Mar 16, 2017 at 12:42 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
> We debated this for a long time when the ON CONFLICT feature was being
> developed. In the end, we settled on this behavior, on the grounds that a
> constraint is a logical concept, while an index is a physical implementation
> detail. Note that the SQL standard also doesn't say anything about indexes,
> but constraints are in the standard.
Right. Besides, you really are only supposed to use the ON CONSTRAINT
syntax when inference won't work, as an escape hatch. This doesn't
look like an example of where inference won't work. That's limited to
ON CONFLICT DO NOTHING with exclusion constraints, which is fairly
limited.
--
Peter Geoghegan
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:49 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@bowt.ie>
0 siblings, 1 reply; 12+ messages in thread
From: Andres Freund @ 2017-03-16 19:49 UTC (permalink / raw)
To: Peter Geoghegan <pg@bowt.ie>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-bugs
On 2017-03-16 12:44:23 -0700, Peter Geoghegan wrote:
> On Thu, Mar 16, 2017 at 12:42 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
> > We debated this for a long time when the ON CONFLICT feature was being
> > developed. In the end, we settled on this behavior, on the grounds that a
> > constraint is a logical concept, while an index is a physical implementation
> > detail. Note that the SQL standard also doesn't say anything about indexes,
> > but constraints are in the standard.
>
> Right. Besides, you really are only supposed to use the ON CONSTRAINT
> syntax when inference won't work, as an escape hatch. This doesn't
> look like an example of where inference won't work. That's limited to
> ON CONFLICT DO NOTHING with exclusion constraints, which is fairly
> limited.
FWIW, I never was completely on board with this design goal, and I think
we should have (and still should) support using indexes directly.
- Andres
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 19:51 Peter Geoghegan <pg@bowt.ie>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 0 replies; 12+ messages in thread
From: Peter Geoghegan @ 2017-03-16 19:51 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-bugs
On Thu, Mar 16, 2017 at 12:49 PM, Andres Freund <andres@anarazel.de> wrote:
> FWIW, I never was completely on board with this design goal, and I think
> we should have (and still should) support using indexes directly.
FWIW I agree that we probably should have exposed indexes as a target
that can be named directly, while still generally discouraging the
practice. But, it hardly matters now.
--
Peter Geoghegan
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 20:08 Nikolay Samokhvalov <samokhvalov@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 12+ messages in thread
From: Nikolay Samokhvalov @ 2017-03-16 20:08 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs
On Thu, Mar 16, 2017 at 12:40 PM, Andres Freund <andres@anarazel.de> wrote:
>
> A unique index isn't exactly the same as a unique constraint - you
> really need to create a constraint.
Then we probably need to fix this message?
[local]:5432 nikolay@test=# insert into constr (msg) values ('hohoho') ;
INSERT 0 1
[local]:5432 nikolay@test=# insert into constr (msg) values ('hohoho') ;
ERROR: duplicate key value violates unique constraint "i_constr_msg"
DETAIL: Key (md5(msg))=(8b0dc2e34844337434b8475108a490ab) already exists.
-- it tells us explicitly, that we have a *constraint* named "i_constr_msg".
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 20:23 Andres Freund <andres@anarazel.de>
parent: Nikolay Samokhvalov <samokhvalov@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Andres Freund @ 2017-03-16 20:23 UTC (permalink / raw)
To: Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-bugs
On 2017-03-16 13:08:53 -0700, Nikolay Samokhvalov wrote:
> On Thu, Mar 16, 2017 at 12:40 PM, Andres Freund <andres@anarazel.de> wrote:
> >
> > A unique index isn't exactly the same as a unique constraint - you
> > really need to create a constraint.
>
>
> Then we probably need to fix this message?
>
> [local]:5432 nikolay@test=# insert into constr (msg) values ('hohoho') ;
> INSERT 0 1
>
> [local]:5432 nikolay@test=# insert into constr (msg) values ('hohoho') ;
> ERROR: duplicate key value violates unique constraint "i_constr_msg"
> DETAIL: Key (md5(msg))=(8b0dc2e34844337434b8475108a490ab) already exists.
>
> -- it tells us explicitly, that we have a *constraint* named "i_constr_msg".
Seems like a good idea to improve that message. I wouldn't vote for
backpatching it, however. Could you propose a patch for that?
Greetings,
Andres Freund
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 20:56 Nikolay Samokhvalov <samokhvalov@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 12+ messages in thread
From: Nikolay Samokhvalov @ 2017-03-16 20:56 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs
On Thu, Mar 16, 2017 at 1:23 PM, Andres Freund <andres@anarazel.de> wrote:
>
> Seems like a good idea to improve that message. I wouldn't vote for
> backpatching it, however. Could you propose a patch for that?
>
OK. Here it is.
But:
1) what's next with all the i18n stuff? I've changed RU version as well,
but unfortunately I don't the other languages.
2) it will definitely break many regression tests, should I patch them as
well?
Also, documentation explaining ON CONFLICT might be still not clear, at
least for some readers. Do you want me to propose a patch for that as well?
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
fix_messaging_unique_index_vs_constraint.patch (application/octet-stream, 1.8K) download
From 5896a26f506767be87aaeec2f847eaccc4377e65 Mon Sep 17 00:00:00 2001
From: NikolayS <github2@samokhvalov.com>
Date: Thu, 16 Mar 2017 23:48:27 +0300
Subject: [PATCH] Fix message that wrongly uses index' name as contraint's name
---
src/backend/access/nbtree/nbtinsert.c | 2 +-
src/backend/po/ru.po | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 6dca810..a4a582c 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -426,7 +426,7 @@ _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel,
ereport(ERROR,
(errcode(ERRCODE_UNIQUE_VIOLATION),
- errmsg("duplicate key value violates unique constraint \"%s\"",
+ errmsg("duplicate key value violates unique constraint associated with index \"%s\"",
RelationGetRelationName(rel)),
key_desc ? errdetail("Key %s already exists.",
key_desc) : 0,
diff --git a/src/backend/po/ru.po b/src/backend/po/ru.po
index 5bd8059..7b5549a 100644
--- a/src/backend/po/ru.po
+++ b/src/backend/po/ru.po
@@ -1049,8 +1049,8 @@ msgstr "\"%s\" - это не индекс"
#: access/nbtree/nbtinsert.c:428
#, c-format
-msgid "duplicate key value violates unique constraint \"%s\""
-msgstr "повторяющееся значение ключа нарушает ограничение уникальности \"%s\""
+msgid "duplicate key value violates unique constraint associated with index \"%s\""
+msgstr "повторяющееся значение ключа нарушает ограничение уникальности, связанное с индексом \"%s\""
#: access/nbtree/nbtinsert.c:430
#, c-format
--
2.1.4
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-16 20:59 Andres Freund <andres@anarazel.de>
parent: Nikolay Samokhvalov <samokhvalov@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Andres Freund @ 2017-03-16 20:59 UTC (permalink / raw)
To: Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-bugs
On 2017-03-16 13:56:27 -0700, Nikolay Samokhvalov wrote:
> On Thu, Mar 16, 2017 at 1:23 PM, Andres Freund <andres@anarazel.de> wrote:
> >
> > Seems like a good idea to improve that message. I wouldn't vote for
> > backpatching it, however. Could you propose a patch for that?
> >
>
> OK. Here it is.
I don't think that's an appropriate fix. ISTM we should say 'violates
unique index' when it's just an index and 'violates unique constraint'
when the index is backing a constraint.
> But:
> 1) what's next with all the i18n stuff? I've changed RU version as well,
> but unfortunately I don't the other languages.
You don't need to patch any (including RU). They're maintained
separately and the in-core stuff is periodically refreshed by the
translators.
> 2) it will definitely break many regression tests, should I patch them as
> well?
Yes, after above adaption, that'll probably reduce the size of the diff.
> Also, documentation explaining ON CONFLICT might be still not clear, at
> least for some readers. Do you want me to propose a patch for that as well?
Please feel free to give it a try.
Greetings,
Andres Freund
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-03-17 04:15 Nikolay Samokhvalov <samokhvalov@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 12+ messages in thread
From: Nikolay Samokhvalov @ 2017-03-17 04:15 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs
On Thu, Mar 16, 2017 at 1:59 PM, Andres Freund <andres@anarazel.de> wrote:
>
> I don't think that's an appropriate fix. ISTM we should say 'violates
> unique index' when it's just an index and 'violates unique constraint'
> when the index is backing a constraint.
>
To me, it now seems to be correct as well.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: ON CONFLICT with constraint name doesn't work
@ 2017-04-26 16:14 Nikolay Samokhvalov <samokhvalov@gmail.com>
parent: Nikolay Samokhvalov <samokhvalov@gmail.com>
0 siblings, 0 replies; 12+ messages in thread
From: Nikolay Samokhvalov @ 2017-04-26 16:14 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; pgsql-hackers@postgresql.org
This is a kindly reminder, that this problem (message about "constraint"
violation, while there is no such a constraint defined, just an index) is
still unresolved.
Let's fix that naming?
Patch is attached in the previous message (posted to -bugs list)
On Thu, Mar 16, 2017 at 9:15 PM, Nikolay Samokhvalov <samokhvalov@gmail.com>
wrote:
>
> Anyway, attached are 2 separate patches:
> 1) version 2 of patch fixing the message, including regression tests;
> 2) proposed change to the documentation https://www.postgresql.org/
> docs/current/static/sql-insert.html
>
>
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2017-04-26 16:14 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-03-16 19:34 ON CONFLICT with constraint name doesn't work Nikolay Samokhvalov <samokhvalov@gmail.com>
2017-03-16 19:40 ` Andres Freund <andres@anarazel.de>
2017-03-16 20:08 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
2017-03-16 20:23 ` Andres Freund <andres@anarazel.de>
2017-03-16 20:56 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
2017-03-16 20:59 ` Andres Freund <andres@anarazel.de>
2017-03-17 04:15 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
2017-04-26 16:14 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
2017-03-16 19:42 ` Heikki Linnakangas <hlinnaka@iki.fi>
2017-03-16 19:44 ` Peter Geoghegan <pg@bowt.ie>
2017-03-16 19:49 ` Andres Freund <andres@anarazel.de>
2017-03-16 19:51 ` Peter Geoghegan <pg@bowt.ie>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox