Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ? - Mailing list pgsql-hackers

From Florian G. Pflug
Subject Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ?
Date
Msg-id 45E6AA4F.4030907@phlo.org
Whole thread Raw
In response to Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ?  (Zoltan Boszormenyi <zboszor@dunaweb.hu>)
Responses Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ?  (Zoltan Boszormenyi <zboszor@dunaweb.hu>)
Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ?  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-hackers
Zoltan Boszormenyi wrote:
> The GENERATED column is an easy of use feature
> with possibly having less work, whereas the IDENTITY
> column is mandatory for some applications (e.g. accounting
> and billing is stricter in some countries) where you simply
> cannot skip a value in the sequence, the strict monotonity is
> not enough.

But just postponing nextval() until after the uniqueness checks
only decreases the *probability* of non-monotonic values, and
*does not* preven them. Consindert two transactions

A: begin ;
B: Begin ;
A: insert ... -- IDENTITY generates value 1
B: insert .. -- IDENTITY generates value 2
A: rollback ;
B: commit ;

Now there is a record with IDENTITY 2, but not with 1. The *only*
way to fix this is to *not* use a sequence, but rather do
lock table t in exclusive mode ;
select max(identity)+1 from t ;
to generate the identity - but of course this prevents any concurrent
inserts, which will make this unuseable for any larger database.

Note that this is not a deficency of postgres sequences - there is no
way to guarantee stricly monotonic values while allowing concurrent
selects at the same time. (Other than lazyly assigning the values, but
this needs to be done by the application)

I agree that I'd be nice to generate the identity columns as late as
possible to prevents needless gaps, but not if price is a for more
intrusive patch, or much higher complexity.

greetings, Florian Pflug



pgsql-hackers by date:

Previous
From: Gregory Stark
Date:
Subject: Re: COMMIT NOWAIT Performance Option
Next
From: Zoltan Boszormenyi
Date:
Subject: Re: Is there a way to run heap_insert() AFTER ExecInsertIndexTuples() ?