Tuple insert missing query in ongoing transaction

From: Szűcs Gábor <surrano(at)gmail(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Tuple insert missing query in ongoing transaction
Date: 2005-08-23 15:09:42
Message-ID: 430B3C36.3060909@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dear Gurus,

I know this is the typical case of transaction use, I just seem to lack the
appropriate education of what exactly happens and whether I may be able to
detect it.

I have two queries, one affecting the other.

1. INSERT INTO barcode.
A BEFORE INSERT/UPDATE trigger checks if there's an appropriate tuple in
table "shift" for this tuple (matching day, shift-of-the-day and workplace)
and denormalizes fields.

2. INSERT INTO shift.
An AFTER INSERT/UPDATE/DELETE trigger updates rows in table barcode, forcing
the abovementioned check for shift.

The second one is a long process, taking about 20 sec to finish. Imagine the
following scenario:

x:xx:00 INSERT INTO shift.
Transaction "S" begins.
It updates several rows, but not the not-yet-inserted row.
x:xx:10 INSERT INTO barcode (... appropriate for above-inserted shift ...)
Transaction "B" begins.
It checks but does not find the corresponding shift.
x:xx:11 Transaction "B" ends.
x:xx:20 Transaction "S" ends.

In such scenarios, sometimes we get "Deadlock detected." That's OK since the
transactions actually cross each other's way.

But not always. In about 100 inserts, now we have the first case that did
not show any trace of that something went wrong.

1. Is there a way to detect such "crossing" transactions?
2. Is there a thorough article on deadlocks, how and when do they happen?
3. Maybe a section of the postgresql doc clarifying when do tuples get locked?

TIA,

--
G.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message neil.saunders 2005-08-23 15:25:19 Re: Problem calling stored procedure
Previous Message Tom Lane 2005-08-23 14:06:55 Re: Problem calling stored procedure