Re: Locking & concurrency - best practices

From: "Adam Rich" <adam(dot)r(at)indigodynamic(dot)com>
To: "'Merlin Moncure'" <mmoncure(at)gmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Locking & concurrency - best practices
Date: 2008-01-15 05:03:05
Message-ID: 00a401c85733$ea26f770$be74e650$@r@indigodynamic.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Advisory locks would work here (better that than table lock), but I
> don't think that's the right approach. Transaction 2 should simply do
> a
> select * from parent_tbl
> where id=1 for update;
>
> at the start of the transaction.

That's actually what I'm doing (just forgot to include it in the
simplified example). What I'm struggling with is that since these locks
aren't enforced in one central place, so I have to run the "for update"
query in every far corner of my code that touches data, whether or not
it reads or writes to parent_tbl. If any of the developers forget
to add it, the data can become corrupted. And since I'm essentially
using row-level locks as advisory locks, I wondered if just using
advisory locks directly would benefit us somehow, in quicker
transactions, CPU/memory overhead, WAL, etc.

In my real application, there are lots of "parent_tbl" and when I try
to "for update" the appropriate ones, I get deadlocks. I know in
theory, I only need to lock things in the same order, everywhere.
But in practice, it seems hard to achieve.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Johanson 2008-01-15 05:31:02 Re: Patch for Statement.getGeneratedKeys()
Previous Message Merlin Moncure 2008-01-15 04:08:58 Re: Locking & concurrency - best practices