Re: Sequential non unique IDs - Mailing list pgsql-sql

From John Reeve
Subject Re: Sequential non unique IDs
Date
Msg-id E3BA5CEC8F665345BF4993E64A5F5C26C255E2@2exchange.pelagodesign.com
Whole thread Raw
In response to Re: Sequential non unique IDs  (Craig Ringer <craig@postnewspapers.com.au>)
List pgsql-sql
Thanks! I was able to resolve this by using SELECT FOR UPDATE on a table
row that both tasks had in common. In this case, the customer table.
Since only one transaction can lock the row at a time, the latter
transaction will be put on hold until the first one commits. This will
work perfectly.

Thanks again!


- John

-----Original Message-----
From: Craig Ringer [mailto:craig@postnewspapers.com.au]
Sent: Tuesday, April 01, 2008 9:24 PM
To: John Reeve
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Sequential non unique IDs

John Reeve wrote:

> I've already considered:
> 1. I can't lock the table, because there are too many inserts
> happening and it will slow down the app.

In a locking approach you may not need to lock the whole table. You
should only need to lock the entry for the customer being altered, eg:

BEGIN;

SELECT 1 FROM task WHERE customerid = 92 FOR UPDATE;

INSERT INTO task (id, customerid, localid) VALUES (nextval('task_id'),
92, (SELECT MAX(localid) + 1 FROM task WHERE customerid = 92));

If I'm not mistaken, that should ensure that for any given customer ID
there's only one transaction holding the locks on that customer.

It won't prevent SELECTs from reading the customer's records, but you
don't mind that so long as they're not using the customer's records to
determine the least free localid. That'll help reduce the hit on your
app's performance, too. If you do mind the , use SELECT ... FOR SHARE
and you'll wait on the FOR UPDATE lock if one is active (however, it
might prove hard to obtain a FOR UPDATE lock if there are lots of FOR
SHARE operations active).

I *think* that'll work, but you should of course test and investigate
before doing anything as crazy as taking my word for it.

> Anyone know a solution for this? This can't be the first time anyone
> has ever tried to do this. Thanks!

If I'm not mistaken about the similarity, you might want to search the
archives for the thread "Primary Key with serial". It might be
informative.

--
Craig Ringer


pgsql-sql by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Problem with now() in function pgsql
Next
From: "Sabin Coanda"
Date:
Subject: undefined relations in pg_locks