Deterministic locking in PostgreSQL

From: Robert Hodges <robert(dot)hodges(at)continuent(dot)com>
To: "pgsql-hackers(at)postgreSQL(dot)org" <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Deterministic locking in PostgreSQL
Date: 2008-05-09 23:24:28
Message-ID: C44A2B3C.6F6D%robert.hodges@continuent.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi everyone,

This question may have an obvious answer I have somehow missed, but to what
extent is locking order deterministic in PostgreSQL? For example, if
requests from multiple transactions arrive in some deterministic order and
acquire locks, can one assume that locks will be granted in the same order
if the requests are repeated at different times or on different servers?

Lock determinism is an important issue for replication algorithms that
depend on database instances to behave as state machines. Here's a simple
example of the behavior I'm seeking. Suppose you have transactions T1, T2,
and T3 that execute as shown below. Each line represents an "increment" of
time.

T1, T2, T3: begin
T1: update foo set value='x' where id=25; <-- Grabs row lock
T2: update foo set value='y' where id=25; <-- Blocked
T3: update foo set value='z' where id=25; <-- Blocked
T1: update foo set value='x1' where id=25;
T1: commit
T2: commit
T3: commit

T2 and T3 are both blocked until T1 commits. At that point, is the row lock
granted to T2 and T3 in some deterministic order? Or can it vary based on
load, lock manager state, etc., so that sometimes you get 'y' and sometimes
'z' as the final result?

If this case turns out to be deterministic, are there other cases that come
to mind that would turn out to be non-deterministic?

Thanks, Robert

--
Robert Hodges, CTO, Continuent, Inc.
Email: robert(dot)hodges(at)continuent(dot)com
Mobile: +1-510-501-3728 Skype: hodgesrm

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2008-05-09 23:37:35 Re: [badalex@gmail.com: Re: [BUGS] Problem identifying constraints which should not be inherited]
Previous Message Kevin Grittner 2008-05-09 21:56:44 Re: psql wrapped format default for backslash-d commands