Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-21 Thread Jorge Godoy
elein <[EMAIL PROTECTED]> writes: > The technique for a single part gapless sequence and a two-part gapless > sequence has been published today at http://www.varlena.com/GeneralBits. > > I'd be interested to hear of high concurrency usage that would break it > or be notably slow. > > --elein > [EM

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-21 Thread elein
The technique for a single part gapless sequence and a two-part gapless sequence has been published today at http://www.varlena.com/GeneralBits. I'd be interested to hear of high concurrency usage that would break it or be notably slow. --elein [EMAIL PROTECTED] ---(end o

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Dawid Kuroczko
On 8/17/06, Merlin Moncure <[EMAIL PROTECTED]> wrote: On 8/17/06, Brad Nicholson <[EMAIL PROTECTED]> wrote: > > > Hmm, I think you are wrong. There is a SELECT ... FOR UPDATE; > > > The first-to-obtain the gapless sequence transaction will establish > > > a lock onthe "tax_id" row. The other tr

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Brad Nicholson
On Thu, 2006-08-17 at 15:13 -0500, Scott Marlowe wrote: > On Thu, 2006-08-17 at 15:07, Merlin Moncure wrote: > > On 8/17/06, Brad Nicholson <[EMAIL PROTECTED]> wrote: > > > > > > > Hmm, I think you are wrong. There is a SELECT ... FOR UPDATE; > > > > > The first-to-obtain the gapless sequence tra

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Brad Nicholson
On Thu, 2006-08-17 at 16:07 -0400, Merlin Moncure wrote: > On 8/17/06, Brad Nicholson <[EMAIL PROTECTED]> wrote: > > > > > Hmm, I think you are wrong. There is a SELECT ... FOR UPDATE; > > > > The first-to-obtain the gapless sequence transaction will establish > > > > a lock onthe "tax_id" row.

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Scott Marlowe
On Thu, 2006-08-17 at 15:07, Merlin Moncure wrote: > On 8/17/06, Brad Nicholson <[EMAIL PROTECTED]> wrote: > > > > > Hmm, I think you are wrong. There is a SELECT ... FOR UPDATE; > > > > The first-to-obtain the gapless sequence transaction will establish > > > > a lock onthe "tax_id" row. The ot

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Merlin Moncure
On 8/17/06, Brad Nicholson <[EMAIL PROTECTED]> wrote: > > Hmm, I think you are wrong. There is a SELECT ... FOR UPDATE; > > The first-to-obtain the gapless sequence transaction will establish > > a lock onthe "tax_id" row. The other transaction will block until > > the first transaction finish

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Brad Nicholson
On Thu, 2006-08-17 at 12:12 -0400, Merlin Moncure wrote: > On 8/17/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: > > On 8/17/06, Merlin Moncure <[EMAIL PROTECTED]> wrote: > > > On 8/16/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: > > > > -- then create a function to retrieve the values: > > > > CR

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Merlin Moncure
On 8/17/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: On 8/17/06, Merlin Moncure <[EMAIL PROTECTED]> wrote: > On 8/16/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: > > -- then create a function to retrieve the values: > > CREATE FUNCTION gseq_nextval(t text) RETURNS integer AS $$ > > DECLARE

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread AgentM
Just in case no one else has brought it up- 8.1+ supports 2PC and savepoints, so one alternative would be to run your standard insertion operations in a prepared transaction or savepoint block. If you get so far as being able to prepare the transaction/complete the savepoint block, you shou

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Berend Tober
Jorge Godoy wrote: Berend Tober <[EMAIL PROTECTED]> writes: A business requirement is to generate table rows that have uniformly increasing, whole number sequences, i.e., the "gap-less" sequence. This is something that I'll also have to code ;-) But the sequence for "employees" would also b

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Dawid Kuroczko
On 8/17/06, Merlin Moncure <[EMAIL PROTECTED]> wrote: On 8/16/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: > -- then create a function to retrieve the values: > CREATE FUNCTION gseq_nextval(t text) RETURNS integer AS $$ > DECLARE >n integer; > BEGIN >SELECT INTO n gseq_va

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Jorge Godoy
Berend Tober <[EMAIL PROTECTED]> writes: > A business requirement is to generate table rows that have uniformly > increasing, whole number sequences, i.e., the "gap-less" sequence. In this > particular case the situation requires multiple such sequences within the same > table -- for each employee

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Jorge Godoy
elein <[EMAIL PROTECTED]> writes: > If this is true the solution for a transactional, gapless sequence is possible > for table.gl_id where updated from count.gl_id. It is simple. However, it > *depends* on the fact that the second transaction getting the newly updated > record from the first tr

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-17 Thread Jorge Godoy
"Dawid Kuroczko" <[EMAIL PROTECTED]> writes: > I did not test the code right now, but I've written something similar to > it some time ago, and it worked fine. Remember to vacuum gapless_seq > table frequently and don't expect stellar performance from it. Interesting approach... And I don't exp

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-16 Thread Adrian Klaver
On Wednesday 16 August 2006 10:59 am, elein wrote: > On Mon, Aug 14, 2006 at 02:46:17PM -0700, Adrian Klaver wrote: > > On Monday 14 August 2006 01:59 pm, Brad Nicholson wrote: > > > On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: > > > > Jorge Godoy wrote: > > > > > Chris <[EMAIL PROTECTED]

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-16 Thread Dawid Kuroczko
On 8/16/06, Dawid Kuroczko <[EMAIL PROTECTED]> wrote: -- then create a function to retrieve the values: CREATE FUNCTION gseq_nextval(t text) RETURNS integer AS $$ DECLARE n integer; BEGIN SELECT INTO n gseq_value+1 FROM gapless_seq WHERE gseq_name = t FOR UPDATE; UPDA

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-16 Thread Dawid Kuroczko
On 8/12/06, Jorge Godoy <[EMAIL PROTECTED]> wrote: I was trying to solve a problem on an old system and realized that there might be some better approach for doing what I need. We have some documents that need to be ordered sequentially and without gaps. I could use a sequence, but if the transa

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-16 Thread Berend Tober
elein wrote: On Mon, Aug 14, 2006 at 02:46:17PM -0700, Adrian Klaver wrote: On Monday 14 August 2006 01:59 pm, Brad Nicholson wrote: On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: Wouldn't SELECT ... FOR UPDATE give you the row lock you need without locking the table? If this is t

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-16 Thread elein
On Mon, Aug 14, 2006 at 02:46:17PM -0700, Adrian Klaver wrote: > On Monday 14 August 2006 01:59 pm, Brad Nicholson wrote: > > On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: > > > Jorge Godoy wrote: > > > > Chris <[EMAIL PROTECTED]> writes: > > > >>I'm not sure what type of lock you'd need t

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Adrian Klaver
On Monday 14 August 2006 02:46 pm, Adrian Klaver wrote: > > Let current max id = x > > > > Transaction 1 (t1) does a select max(id) for update, gets a lock on the > > last tuple at the time of the select, and gets x as a value for max id > > > > Transaction 2 (t2) does a select max(id) for update,

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Berend Tober
Brad Nicholson wrote: On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: Jorge Godoy wrote: Chris <[EMAIL PROTECTED]> writes: I'm not sure what type of lock you'd need to make sure no other transactions updated the table (see http://www.postgresql.org/docs/8.1/interactive/sql-lock.h

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Adrian Klaver
On Monday 14 August 2006 01:59 pm, Brad Nicholson wrote: > On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: > > Jorge Godoy wrote: > > > Chris <[EMAIL PROTECTED]> writes: > > >>I'm not sure what type of lock you'd need to make sure no other > > >> transactions updated the table (see > > >>htt

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Brad Nicholson
On Mon, 2006-08-14 at 16:08 -0400, Berend Tober wrote: > Jorge Godoy wrote: > > > Chris <[EMAIL PROTECTED]> writes: > > > > > >>I'm not sure what type of lock you'd need to make sure no other transactions > >>updated the table (see > >>http://www.postgresql.org/docs/8.1/interactive/sql-lock.html

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Jorge Godoy
Harald Fuchs <[EMAIL PROTECTED]> writes: > In article <[EMAIL PROTECTED]>, > Jorge Godoy <[EMAIL PROTECTED]> writes: > >> Harald Fuchs <[EMAIL PROTECTED]> writes: >>> Why putting gapless numbers into the database at all? Just calculate them >>> at >>> query time. > >> And how would you retrieve

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Harald Fuchs
In article <[EMAIL PROTECTED]>, Scott Ribe <[EMAIL PROTECTED]> writes: >> Why putting gapless numbers into the database at all? Just >> calculate them at query time. > There is ABSOLUTELY NO WAY that would be acceptable for accounting or legal > purposes. It would be the same as fabricating the

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Harald Fuchs
In article <[EMAIL PROTECTED]>, Jorge Godoy <[EMAIL PROTECTED]> writes: > Harald Fuchs <[EMAIL PROTECTED]> writes: >> Why putting gapless numbers into the database at all? Just calculate them at >> query time. > And how would you retrieve the record that corresponds to invoice number > #16355, f

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Harald Fuchs
In article <[EMAIL PROTECTED]>, Richard Broersma Jr <[EMAIL PROTECTED]> writes: > I am curious, can you calculate something like this using only sql? Or you > you need to employee a > procedural language like plpsgql? You could use something like SELECT (SELECT count(*) FROM tbl t2 WHERE t2.i

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Berend Tober
Jorge Godoy wrote: Chris <[EMAIL PROTECTED]> writes: I'm not sure what type of lock you'd need to make sure no other transactions updated the table (see http://www.postgresql.org/docs/8.1/interactive/sql-lock.html) but "in theory" something like this should work: begin; select id from table

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Scott Ribe
> Why putting gapless numbers into the database at all? Just calculate them at > query time. There is ABSOLUTELY NO WAY that would be acceptable for accounting or legal purposes. It would be the same as fabricating the numbers during an audit. -- Scott Ribe [EMAIL PROTECTED] http://www.killerby

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Jorge Godoy
Harald Fuchs <[EMAIL PROTECTED]> writes: > Why putting gapless numbers into the database at all? Just calculate them at > query time. And how would you retrieve the record that corresponds to invoice number #16355, for example? Recalculating few records is fine, but millions of them everytime y

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Richard Broersma Jr
> > AgentM <[EMAIL PROTECTED]> writes: > >> Since the gapless numbers are purely for the benefit of the tax people, you > >> could build your db with regular sequences as primary keys and then > >> regularly > >> (or just before tax-time) insert into a table which maps the gapless > >> sequence

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Harald Fuchs
In article <[EMAIL PROTECTED]>, Jorge Godoy <[EMAIL PROTECTED]> writes: > AgentM <[EMAIL PROTECTED]> writes: >> Since the gapless numbers are purely for the benefit of the tax people, you >> could build your db with regular sequences as primary keys and then >> regularly >> (or just before tax-t

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Jorge Godoy
AgentM <[EMAIL PROTECTED]> writes: > Since the gapless numbers are purely for the benefit of the tax people, you > could build your db with regular sequences as primary keys and then regularly > (or just before tax-time) insert into a table which maps the gapless sequence > to the real primary k

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread AgentM
Since the gapless numbers are purely for the benefit of the tax people, you could build your db with regular sequences as primary keys and then regularly (or just before tax-time) insert into a table which maps the gapless sequence to the real primary key. -M ---(

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Alvaro Herrera
Jorge Godoy wrote: > Chris <[EMAIL PROTECTED]> writes: > > > I'm not sure what type of lock you'd need to make sure no other transactions > > updated the table (see > > http://www.postgresql.org/docs/8.1/interactive/sql-lock.html) but "in > > theory" > > something like this should work: > > > > b

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Jorge Godoy
Michael Fuhr <[EMAIL PROTECTED]> writes: > Automatically use indexes for MIN() and MAX() (Tom) > > In previous releases, the only way to use an index for MIN() > or MAX() was to rewrite the query as SELECT col FROM tab ORDER > BY col LIMIT 1. Index usage now happens automatica

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Michael Fuhr
On Mon, Aug 14, 2006 at 09:09:51AM -0300, Jorge Godoy wrote: > Chris <[EMAIL PROTECTED]> writes: > > P.S. I'm sure in older versions this query wouldn't use an index: > > select max(id) from table; > > It doesn't. You'd have to do what you did: "order by desc limit 1" to > have it using indexes.

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-14 Thread Jorge Godoy
Chris <[EMAIL PROTECTED]> writes: > I'm not sure what type of lock you'd need to make sure no other transactions > updated the table (see > http://www.postgresql.org/docs/8.1/interactive/sql-lock.html) but "in theory" > something like this should work: > > begin; > select id from table order by id

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Chris
Jorge Godoy wrote: Jorge Godoy <[EMAIL PROTECTED]> writes: Is there a better way to guarantee that there will be no gaps in my sequence if something goes wrong with my transaction? From the overwhelming feedback I assume there isn't a better way yet... Thanks. I'll see how I can improve

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Ron Johnson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Jorge Godoy wrote: > Ron Johnson <[EMAIL PROTECTED]> writes: > >> Pre-allocate records. The (primary key?) field would have the >> numbers already filled in, but all the rest of the fields in each >> record be NULL, blanks, zeros or indicator values

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Jorge Godoy
Ron Johnson <[EMAIL PROTECTED]> writes: > Pre-allocate records. The (primary key?) field would have the > numbers already filled in, but all the rest of the fields in each > record be NULL, blanks, zeros or indicator values ("~~", > -9, etc). > > Then create a single-field table c

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Ron Johnson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Jorge Godoy wrote: > Jorge Godoy <[EMAIL PROTECTED]> writes: > >> Is there a better way to guarantee that there will be no gaps in my sequence >> if something goes wrong with my transaction? > > From the overwhelming feedback I assume there isn't a

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Christian Kratzer
Hi, On Sun, 13 Aug 2006, Jorge Godoy wrote: Christian Kratzer <[EMAIL PROTECTED]> writes: I would at least try to assign multiple such numbers in batches to mimize contention on the row you store the counter in. What do you mean here? How would you guarantee that on of the receiver transac

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Jorge Godoy
Christian Kratzer <[EMAIL PROTECTED]> writes: > I would at least try to assign multiple such numbers in batches to mimize > contention on the row you store the counter in. What do you mean here? How would you guarantee that on of the receiver transactions didn't rollback and left a gap in the "s

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Christian Kratzer
Hi, On Sat, 12 Aug 2006, chris smith wrote: On 8/12/06, Jorge Godoy <[EMAIL PROTECTED]> wrote: Is there a better way to guarantee that there will be no gaps in my sequence if something goes wrong with my transaction? Why does it matter? I assume there is a reason you need it like this..

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-13 Thread Jorge Godoy
Jorge Godoy <[EMAIL PROTECTED]> writes: > Is there a better way to guarantee that there will be no gaps in my sequence > if something goes wrong with my transaction? >From the overwhelming feedback I assume there isn't a better way yet... Thanks. I'll see how I can improve the model then to se

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-12 Thread Jorge Godoy
Thomas Kellerer <[EMAIL PROTECTED]> writes: > What do you do if a document gets deleted? Renumber the "following" documents > so that no gaps are present in the already used ids? There's no deletion possibility. A RULE sets a column named "active" to "False" instead (I can set it manually or let

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-12 Thread Jorge Godoy
"chris smith" <[EMAIL PROTECTED]> writes: > Why does it matter? > > I assume there is a reason you need it like this.. Of course there is. It is a project requirement and also a law requirement that there's no unused number and that they be chronologically ordered as well. This is also part of

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-12 Thread Thomas Kellerer
Jorge Godoy wrote on 12.08.2006 01:33: I was trying to solve a problem on an old system and realized that there might be some better approach for doing what I need. We have some documents that need to be ordered sequentially and without gaps. I could use a sequence, but if the transaction fails

Re: [GENERAL] Best approach for a "gap-less" sequence

2006-08-12 Thread chris smith
On 8/12/06, Jorge Godoy <[EMAIL PROTECTED]> wrote: Hi! I was trying to solve a problem on an old system and realized that there might be some better approach for doing what I need. We have some documents that need to be ordered sequentially and without gaps. I could use a sequence, but if the

[GENERAL] Best approach for a "gap-less" sequence

2006-08-11 Thread Jorge Godoy
Hi! I was trying to solve a problem on an old system and realized that there might be some better approach for doing what I need. We have some documents that need to be ordered sequentially and without gaps. I could use a sequence, but if the transaction fails then when I rollback the sequence