Weird insert issue

From: Larry Meadors <larry(dot)meadors(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Weird insert issue
Date: 2015-06-28 04:37:44
Message-ID: CAFkMvDF9niWNiRefs+ZMvb+fWvMo+YhouGtcqzTcfvJkjdaTTw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm running this SQL statement:

insert into Favorite (patronId, titleId)
select 123, 234
where not exists (
select 1 from Favorite where patronId = 123 and titleId = 234
)

It normally runs perfectly, but will rarely fail and I just can't see
any way that it could. :-|

The exception I get is that the unique key (patronid+titleid) was violated.

Is it possible that the statement is getting run twice and that the
timing is such that the first one succeeds and the second tries to do
the insert and fails because the select part of the SQL ran before the
first insert completed? I'd expected that each of the two would be
single operations, but this error is making me rethink that.

Any thoughts?

Larry

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2015-06-28 04:47:42 Re: Weird insert issue
Previous Message Luca Arzeni 2015-06-27 22:05:26 varchar sort ordering ignore blanks - SOLVED!