pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSelect for update / deadlock possibility?
6+ messages / 4 participants
[nested] [flat]
* Select for update / deadlock possibility?
@ 2018-01-02 08:22 Durumdara <durumdara@gmail.com>
0 siblings, 2 replies; 6+ messages in thread
From: Durumdara @ 2018-01-02 08:22 UTC (permalink / raw)
To: pgsql-general
Dear Members!
I have to ask something that not clear for me from description, and I can't
simulate it.
Is "select for update" atomic (as transactions) or it isn't?
I want to avoid the deadlocks.
If it's atomic, then I don't need to worry about concurrent locks.
But I think it's not.
This is an example for deadlock:
a.) select * from test where id in (1, 3, 4)
b.) select * from test where id in (2, 4, 5)
c.) select * from test where id in (5, 1, 6)
If it's not atomic, then:
- a locks 1.
- b locks 2.
- c locks 5.
- a locks 3.
- b locks 4.
- c try to lock 1, but it locked by a
- a try to lock 4, but it locked by b
- b try to lock 5, but it locked by c
DEADLOCK!!!
As I read select for update doesn't support timeout.
I've found two timeout that could be affects on it.
Which one I need to redefine temporarily?
lock_timeout (integer)
statement_timeout (integer)
Somebody wrote statement_timeout, but why PG have lock_timeout then?
Thank you for your help!
Regards
dd
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Select for update / deadlock possibility?
@ 2018-01-02 11:31 Rakesh Kumar <rakeshkumar464@mail.com>
parent: Durumdara <durumdara@gmail.com>
1 sibling, 1 reply; 6+ messages in thread
From: Rakesh Kumar @ 2018-01-02 11:31 UTC (permalink / raw)
To: Durumdara <durumdara@gmail.com>; +Cc: pgsql-general
Shouldn't isolation level also matter ? What is the isolation level you are using ?
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Select for update / deadlock possibility?
@ 2018-01-02 12:22 Durumdara <durumdara@gmail.com>
parent: Rakesh Kumar <rakeshkumar464@mail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Durumdara @ 2018-01-02 12:22 UTC (permalink / raw)
To: Rakesh Kumar <rakeshkumar464@mail.com>; +Cc: pgsql-general
Hello!
Read Committed.
I extend the example: the concurrent connections are in transactions.
begin
select ... for update;
end;
Regards
dd
2018-01-02 12:31 GMT+01:00 Rakesh Kumar <rakeshkumar464@mail.com>:
>
> Shouldn't isolation level also matter ? What is the isolation level you
> are using ?
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Select for update / deadlock possibility?
@ 2018-01-02 14:02 Jeff Janes <jeff.janes@gmail.com>
parent: Durumdara <durumdara@gmail.com>
1 sibling, 2 replies; 6+ messages in thread
From: Jeff Janes @ 2018-01-02 14:02 UTC (permalink / raw)
To: Durumdara <durumdara@gmail.com>; +Cc: pgsql-general
On Tue, Jan 2, 2018 at 3:22 AM, Durumdara <durumdara@gmail.com> wrote:
> Dear Members!
>
> I have to ask something that not clear for me from description, and I
> can't simulate it.
>
> Is "select for update" atomic (as transactions) or it isn't?
>
> I want to avoid the deadlocks.
>
> If it's atomic, then I don't need to worry about concurrent locks.
> But I think it's not.
>
It is atomic, but you do have to worry about deadlocks. Being atomic
doesn't mean it can't deadlock, it just means that if it does deadlock, all
the work in the transaction is rolled back together.
>
>
> This is an example for deadlock:
>
> a.) select * from test where id in (1, 3, 4)
> b.) select * from test where id in (2, 4, 5)
> c.) select * from test where id in (5, 1, 6)
>
> If it's not atomic, then:
>
> - a locks 1.
> - b locks 2.
> - c locks 5.
> - a locks 3.
> - b locks 4.
> - c try to lock 1, but it locked by a
> - a try to lock 4, but it locked by b
> - b try to lock 5, but it locked by c
>
There is no obligation for it to lock rows in the order they appear in the
IN-list. Maybe that is why you can't simulate it.
>
> DEADLOCK!!!
>
> As I read select for update doesn't support timeout.
> I've found two timeout that could be affects on it.
> Which one I need to redefine temporarily?
>
> lock_timeout (integer)
> statement_timeout (integer)
>
Deadlocks are automatically detected and one session is dealt an ERROR to
resolve them. So deadlock_timeout is the only timeout you need care about.
>
> Somebody wrote statement_timeout, but why PG have lock_timeout then?
>
They do different things, and give you different error messages when they
fire so that you know more about what the problem was (I'm too slow, versus
I'm stuck behind someone else).
Cheers,
Jeff
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Select for update / deadlock possibility?
@ 2018-01-02 14:36 Durumdara <durumdara@gmail.com>
parent: Jeff Janes <jeff.janes@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: Durumdara @ 2018-01-02 14:36 UTC (permalink / raw)
To: ; +Cc: pgsql-general
Dear Jeff!
So. I start this question from more far.
I need to protect some resources.
All modifications started with StartTransaction.
Then I try to lock the articles by ids (to prevents other client's
modifications).
After that I insert / modify needed data.
Then I commit or rollback.
The locks will vanish on the end of the transaction, so resources
accessable again for different session.
If A session locks 1. articles, B session waits for the end of the
transaction of A.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Select for update / deadlock possibility?
@ 2018-01-02 15:23 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jeff Janes <jeff.janes@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: Tom Lane @ 2018-01-02 15:23 UTC (permalink / raw)
To: Jeff Janes <jeff.janes@gmail.com>; +Cc: Durumdara <durumdara@gmail.com>; pgsql-general
Jeff Janes <jeff.janes@gmail.com> writes:
> On Tue, Jan 2, 2018 at 3:22 AM, Durumdara <durumdara@gmail.com> wrote:
>> Is "select for update" atomic (as transactions) or it isn't?
> It is atomic, but you do have to worry about deadlocks.
I think by "atomic" the OP intends "all the row locks are magically
acquired at the same instant". Which they aren't, they're taken one
at a time. So if you have different transactions trying to lock
overlapping sets of rows, there's a risk of deadlock. Which will
be detected and one transaction will fail, but still you might wish
to avoid that.
The usual rule for that is "be sure all transactions acquire locks
in the same order". So just throw an "order by id" type of clause
into the SELECT FOR UPDATE, and you should be fine.
Personally, I'd still code the application to retry on deadlock
failures, just for robustness.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2018-01-02 15:23 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-02 08:22 Select for update / deadlock possibility? Durumdara <durumdara@gmail.com>
2018-01-02 11:31 ` Rakesh Kumar <rakeshkumar464@mail.com>
2018-01-02 12:22 ` Durumdara <durumdara@gmail.com>
2018-01-02 14:02 ` Jeff Janes <jeff.janes@gmail.com>
2018-01-02 14:36 ` Durumdara <durumdara@gmail.com>
2018-01-02 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>
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