RE: Sequences in transaction context

From: "Erik Pearson" <erik(at)cariboulake(dot)com>
To: "Doug McNaught" <doug(at)wireboard(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>, "Erik Pearson" <erik(at)cariboulake(dot)com>
Subject: RE: Sequences in transaction context
Date: 2001-06-06 03:33:08
Message-ID: DGEBLCNNPDPKMOGANADDEEFPCCAA.erik@cariboulake.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Thanks for the reply! I'm afraid I didn't test this thoroughly. I noticed
that nextval changed the sequence for all transactions, and assumed that
currval simply translated to the (nextval() - 1), which it does not, as I
now understand it. I was also unclear on the FAQ -- I guess I thought that
"race condition" referred to concurrent access to the underlying store,
either delivering duplicate values or something.

One last followup question -- what's MVCC?

-- Erik

> -----Original Message-----
> From: doug(at)belphigor(dot)mcnaught(dot)org [mailto:doug(at)belphigor(dot)mcnaught(dot)org]On
> Behalf Of Doug McNaught
> Sent: Tuesday, June 05, 2001 10:14 PM
> To: Erik Pearson
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] Sequences in transaction context
>
>
> "Erik Pearson" <erik(at)cariboulake(dot)com> writes:
>
> > I searched through mailing list archives but was unable to find full
> > coverage of this question -- my apologies if this is a reposted
> question.
> >
> > As in the FAQ, I am trying to retrieve the value of a sequence
> value from a
> > newly inserted row. So, first I call something like:
> >
> > insert into foobar (foo, bar)
> > values (nextval('foobar_foo_seq'), 'whatever');
> >
> > Then, I want to retrieve the value that generated from the sequence and
> > inserted into the table, so I use a call to currval:
> >
> > insert into foobar_rel_table(foo_fk, baz)
> > values (currval('foobar_foo_seq', 'something else');
> >
> > This is (one of the methods that is) prescribed in the FAQ.
> However, I'm
> > concerned that another transaction attempting to insert into
> the same table
> > might make a call to nextval('foobar_foo_seq') between the two
> operations
> > above. This would mean that my second statement would use the
> wrong value
> > from the sequence.
>
> This does not happen. I just tested it:
>
> [doug(at)shaggy doug]$ createdb foo
> CREATE DATABASE
> [doug(at)shaggy doug]$ psql foo
> Welcome to psql, the PostgreSQL interactive terminal.
>
> Type: \copyright for distribution terms
> \h for help with SQL commands
> \? for help on internal slash commands
> \g or terminate with semicolon to execute query
> \q to quit
>
> foo=# create sequence foo_seq;
> CREATE
> foo=# select nextval('foo_seq');
> nextval
> ---------
> 1
> (1 row)
>
> foo=# select currval('foo_seq');
> currval
> ---------
> 1
> (1 row)
>
> At this point, in another window, I do:
>
> foo=# select nextval('foo_seq');
> nextval
> ---------
> 2
> (1 row)
>
> Back to the first window:
>
> foo=# select currval('foo_seq');
> currval
> ---------
> 1
> (1 row)
>
> Are you not seeing this behavior? Since you mention the FAQ, question
> 4.16.3 addresses this very issue in very clear language.
>
> Welcome to MVCC...
>
> -Doug
> --
> The rain man gave me two cures; he said jump right in,
> The first was Texas medicine--the second was just railroad gin,
> And like a fool I mixed them, and it strangled up my mind,
> Now people just get uglier, and I got no sense of time... --Dylan
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Doug McNaught 2001-06-06 03:36:54 Re: Sequences in transaction context
Previous Message Stephan Szabo 2001-06-06 03:16:36 Re: Sequences in transaction context