Locking & concurrency - best practices

From: Adam Rich <adam(dot)r(at)indigodynamic(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Locking & concurrency - best practices
Date: 2008-01-14 20:43:07
Message-ID: 20080114204325.5DB0B2E014F@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


I have a "parent_tbl" and dozens of data tables, with foreign keys
referencing the PK of "parent_tbl" (one-to-many). There are 100+
users accessing the application, usually (but not always) each user
is working on a different record in parent_tbl. (this would seem like a pretty standard scenario for a lot of apps)

Each user performs multiple queries in a transaction, reading and
modifying the data in parent_tbl and multipe data tables before
commiting. I need the data to be consistent during and after the
transaction. (I basically need a way to lock a row in parent_tbl,
and all rows in the data tables referencing that row, and prevent
new rows from being inserted that reference that row).

To guard against this, I added "FOR UPDATE" to queries against the
parent_tbl and "LOCK TABLE IN EXCLUSIVE MODE" before queries against
all of the data tables. This works, except it slows down the entire
application because all transactions are serialized. Even users who
are working on seperate records in parent_tbl are not allowed to
proceed simultaneously. This is not ideal, the vast majority of
access to this database is users working on separate records.

Should I drop the "LOCK TABLE" statements completely? As long as
*every* part of the application that modifies data obtains a
"FOR UPDATE" lock on the parent table's record first, there shouldn't
be any concurrency issues. But, I realize I'm really only implementing
advisory locking, and there's nothing preventing data corruption from
any application that forgets or leaves out the "FOR UPDATE".

Is this the best practice for dealing with this situation? Should I
be using real advisory locks instead of "FOR UPDATE" ? What are the
pros & cons of each?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gurjeet Singh 2008-01-14 20:57:18 Re: Forgot to dump old data before re-installing machine
Previous Message Tom Lane 2008-01-14 19:43:37 Re: oddly slow query