pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Can SELECT … NOWAIT “deadlock”?
3+ messages / 2 participants
[nested] [flat]

* Can SELECT … NOWAIT “deadlock”?
@ 2019-08-20 10:46  Toni Cárdenas <toni@tcardenas.me>
  0 siblings, 0 replies; 3+ messages in thread

From: Toni Cárdenas @ 2019-08-20 10:46 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

The docs say (emphasis mine):

> FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHARE are locking
clauses; they affect how SELECT locks rows **as they are obtained from the
table**.

> With NOWAIT, the statement reports an error, rather than waiting, if a
selected row cannot be locked immediately.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Can SELECT … NOWAIT “deadlock”?
@ 2019-08-20 10:53  Toni Cárdenas <toni@tcardenas.me>
  0 siblings, 1 reply; 3+ messages in thread

From: Toni Cárdenas @ 2019-08-20 10:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

The docs say (emphasis mine):

> FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHARE are locking
clauses; they affect how SELECT locks rows **as they are obtained from the
table**.

> With NOWAIT, the statement reports an error, rather than waiting, if a
selected row cannot be locked immediately.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Can SELECT … NOWAIT “deadlock”?
@ 2019-08-20 13:59  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Toni Cárdenas <toni@tcardenas.me>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2019-08-20 13:59 UTC (permalink / raw)
  To: Toni Cárdenas <toni@tcardenas.me>; +Cc: pgsql-general@lists.postgresql.org

=?UTF-8?Q?Toni_C=C3=A1rdenas?= <toni@tcardenas.me> writes:
> Imagine the same `SELECT ... NOWAIT` statement, which returns rows R1 and
> R2, is run from concurrent connections S1 and S2.

>  1. S1 obtains and locks R1.
>  2. S2 obtains and locks R2.
>  3. S1 tries to obtain R2, but it's locked by S2.
>  4. S2 tries to obtain R1, but it's locked by S1.
>  5. The lock on R1 is released due to S1 failing.

> The question is whether step 4 can actually happen between step 3 and 5, or
> else steps 3 and 5 are performed atomically with respect to concurrent
> selects.

I see no reason to think that 3 and 5 would appear to be atomic.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-08-20 13:59 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-20 10:46 Can SELECT … NOWAIT “deadlock”? Toni Cárdenas <toni@tcardenas.me>
2019-08-20 10:53 Can SELECT … NOWAIT “deadlock”? Toni Cárdenas <toni@tcardenas.me>
2019-08-20 13:59 ` 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