pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
psql sequence question
5+ messages / 3 participants
[nested] [flat]

* psql sequence question
@ 2003-06-16 15:24  Jodi Kanter <jkanter@virginia.edu>
  0 siblings, 2 replies; 5+ messages in thread

From: Jodi Kanter @ 2003-06-16 15:24 UTC (permalink / raw)
  To: pgsql-admin

If I'm using transactions (not autocommit), are sequences atomic?

In other words, after inserting a record to a table that 
uses sequence A, am I guaranteed that select last_value on 
sequence A is atomic, and cannot be interfered with by other 
transactions using that same sequence? Sequence A is used by
several tables.

Thanks,
Jodi



-

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu <mailto:jkanter@virginia.edu>

 

 

 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: psql sequence question
@ 2003-06-16 16:04  Bruno Wolff III <bruno@wolff.to>
  parent: Jodi Kanter <jkanter@virginia.edu>
  1 sibling, 1 reply; 5+ messages in thread

From: Bruno Wolff III @ 2003-06-16 16:04 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-admin

On Mon, Jun 16, 2003 at 11:24:28 -0400,
  Jodi Kanter <jkanter@virginia.edu> wrote:
> If I'm using transactions (not autocommit), are sequences atomic?

Yes.

> In other words, after inserting a record to a table that 
> uses sequence A, am I guaranteed that select last_value on 
> sequence A is atomic, and cannot be interfered with by other 
> transactions using that same sequence? Sequence A is used by
> several tables.

Probably not in the way you mean. Every transaction is going to see
a consistant view of the sequence table. However transactions proceeding
in parallel may seem the same value for the last value. To make this
work you would need to use serializable mode to do any updates based
on the value of the last value or lock the table exclusively to prevent
concurrent updates. This defeats the function of sequences providing
unique values using light weight locking.

The right way to use sequences is to use nextval to get new values
and use currval to reuse the value you got from the latest call to
nextval in the same session.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: psql sequence question
@ 2003-06-16 18:26  Ernest E Vogelsinger <ernest@vogelsinger.at>
  parent: Jodi Kanter <jkanter@virginia.edu>
  1 sibling, 0 replies; 5+ messages in thread

From: Ernest E Vogelsinger @ 2003-06-16 18:26 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-admin

At 17:24 16.06.2003, Jodi Kanter said:
--------------------[snip]-------------------- 

>If I'm using transactions (not autocommit), are sequences atomic?

Yes

>In other words, after inserting a record to a table that 
>uses sequence A, am I guaranteed that select last_value on 
>sequence A is atomic, and cannot be interfered with by other 
>transactions using that same sequence? Sequence A is used by
>several tables.

Yes. But you should use currval('seq_name') since this guarantees to return
the last value returned to the current connection. Check the recent posts -
there was a discussion on just this topic.

-- 
   >O     Ernest E. Vogelsinger
   (\)    ICQ #13394035
    ^     http://www.vogelsinger.at/





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: psql sequence question
@ 2003-06-18 13:49  Jodi Kanter <jkanter@virginia.edu>
  parent: Bruno Wolff III <bruno@wolff.to>
  0 siblings, 1 reply; 5+ messages in thread

From: Jodi Kanter @ 2003-06-18 13:49 UTC (permalink / raw)
  To: Bruno Wolff III <bruno@wolff.to>; +Cc: pgsql-admin

so it sounds like I just need to tell my programmer friend to change his 
use of last value to currval instead?
I just hate silly mistakes like this! Thanks for the help.
Jodi


Bruno Wolff III wrote:

>On Mon, Jun 16, 2003 at 11:24:28 -0400,
>  Jodi Kanter <jkanter@virginia.edu> wrote:
>
>>If I'm using transactions (not autocommit), are sequences atomic?
>>
>
>Yes.
>
>>In other words, after inserting a record to a table that 
>>uses sequence A, am I guaranteed that select last_value on 
>>sequence A is atomic, and cannot be interfered with by other 
>>transactions using that same sequence? Sequence A is used by
>>several tables.
>>
>
>Probably not in the way you mean. Every transaction is going to see
>a consistant view of the sequence table. However transactions proceeding
>in parallel may seem the same value for the last value. To make this
>work you would need to use serializable mode to do any updates based
>on the value of the last value or lock the table exclusively to prevent
>concurrent updates. This defeats the function of sequences providing
>unique values using light weight locking.
>
>The right way to use sequences is to use nextval to get new values
>and use currval to reuse the value you got from the latest call to
>nextval in the same session.
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>

-- 

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu <mailto:jkanter@virginia.edu>

 

 

 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: psql sequence question
@ 2003-06-18 14:31  Bruno Wolff III <bruno@wolff.to>
  parent: Jodi Kanter <jkanter@virginia.edu>
  0 siblings, 0 replies; 5+ messages in thread

From: Bruno Wolff III @ 2003-06-18 14:31 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-admin

On Wed, Jun 18, 2003 at 09:49:58 -0400,
  Jodi Kanter <jkanter@virginia.edu> wrote:
> so it sounds like I just need to tell my programmer friend to change his 
> use of last value to currval instead?

This is most likely what you want to do.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2003-06-18 14:31 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-06-16 15:24 psql sequence question Jodi Kanter <jkanter@virginia.edu>
2003-06-16 16:04 ` Bruno Wolff III <bruno@wolff.to>
2003-06-18 13:49   ` Jodi Kanter <jkanter@virginia.edu>
2003-06-18 14:31     ` Bruno Wolff III <bruno@wolff.to>
2003-06-16 18:26 ` Ernest E Vogelsinger <ernest@vogelsinger.at>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox