![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
There shouldn't be any gaps in the sequence jobno sequence either. Is there an easy way to do this? I thought of doing: SELECT max(jobno) from jobs where companyid=1; you could do something like this or using a lot of different sequence ... but I don't know if it's a clever idea ![]() |
|
ciao, bugant. |
#3
| |||
| |||
|
|
I thought of doing: SELECT max(jobno) from jobs where companyid=1; Then use the result as the job number, is there a way I can lock the table to do this or a better way of achieving the required result. |
#4
| |||
| |||
|
|
Jake Stride writes: I thought of doing: SELECT max(jobno) from jobs where companyid=1; I think SELECT FOR UPDATE should work fine here. |
#5
| |||
| |||
|
|
Andreas Seltenreich <seltenreich (AT) gmx (DOT) de> writes: Jake Stride writes: I thought of doing: SELECT max(jobno) from jobs where companyid=1; I think SELECT FOR UPDATE should work fine here. Nope; he'll get something like regression=# select max(unique1) from tenk1 for update; ERROR: SELECT FOR UPDATE is not allowed with aggregate functions His best bet is probably BEGIN; LOCK TABLE jobs; SELECT max(jobno) from jobs where companyid=1; INSERT INTO jobs ... COMMIT; This is pretty horrid from a concurrency point of view but I don't think there's any other way to meet the "no gaps" requirement. You could reduce the strength of the lock a bit, for instance LOCK TABLE jobs IN EXCLUSIVE MODE; which would allow readers of the jobs table to proceed concurrently, but not writers. If you were willing to assume that all inserters into jobs are cooperating by explicitly obtaining the correct lock, you could reduce it to LOCK TABLE jobs IN SHARE UPDATE EXCLUSIVE MODE; which is the lowest self-conflicting table lock type. This would allow unrelated updates to the jobs table to proceed concurrently too (though not VACUUMs). See http://www.postgresql.org/docs/7.4/s...t-locking.html regards, tom lane |
#6
| |||
| |||
|
|
See http://www.postgresql.org/docs/7.4/s...t-locking.html |
![]() |
| Thread Tools | |
| Display Modes | |
| |