lock in access exclusive and sequence question

Started by Creager, Robert Sabout 25 years ago2 messagesgeneral
Jump to latest
#1Creager, Robert S
CreagRS@LOUISVILLE.STORTEK.COM

I'm creating a script which will re-claim sequence numbers in a table by
'packing' the existing sequence numbers. My questions is if I lock the
table in access exclusive mode, and an insert into that table occurs after
the lock, with the insert be blocked before or after the nextval is chosen?

Thanks,
Rob

Robert Creager
Senior Software Engineer
Client Server Library
303.673.2365 V
303.661.5379 F
888.912.4458 P
StorageTek
INFORMATION made POWERFUL

#2Richard Huxton
dev@archonet.com
In reply to: Creager, Robert S (#1)
Re: lock in access exclusive and sequence question

From: "Creager, Robert S" <CreagRS@LOUISVILLE.STORTEK.COM>

I'm creating a script which will re-claim sequence numbers in a table by
'packing' the existing sequence numbers. My questions is if I lock the
table in access exclusive mode, and an insert into that table occurs after
the lock, with the insert be blocked before or after the nextval is

chosen?

If you do something like INSERT INTO foo VALUES (nextval('foo_seq')) it
seems to be before the nextval is chosen (based on testing against 7.1). I
don't know that this is guaranteed to remain the case, but it would seem
unlikely to change.

However - just doing a SELECT nextval('foo_seq') is unblocked so it will
depend how inserts/updates are performed.

I suppose you might also have a case where rules/triggers or the like could
evaluate a nextval() before an insert (triggers yes, rules probably not
*although that's just guesswork*)

Have you considered just extending the sequence numbers so you don't run
out? There's an example in my PostgreSQL notes linked from
techdocs.postgresql.org and also IIRC might well be in the pgsql cookbook.

HTH

- Richard Huxton