From: | "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com> |
---|---|
To: | CSN <cool_screen_name90001(at)yahoo(dot)com> |
Cc: | <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: lastval(seq) ? |
Date: | 2003-10-21 20:43:15 |
Message-ID: | Pine.LNX.4.33.0310211437390.10899-100000@css120.ihs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, 21 Oct 2003, CSN wrote:
>
> --- "scott.marlowe" <scott(dot)marlowe(at)ihs(dot)com> wrote:
> > On Mon, 20 Oct 2003, CSN wrote:
> >
> > > How do you get the last value of a sequence
> > without
> > > having called nextval? phppgadmin displays last
> > value
> > > for sequences (I haven't found out how yet rooting
> > > through the code).
> >
> > First, the mandatory, why would you want to do that?
>
> For updating sequences after copy importing data (with
> id's).
>
> select setval('table_id_seq', (select max(id) from
> table));
>
> vs.
>
> select setval('table_id_seq', (select last_value from
> table_id_seq));
>
> Is there a transaction-safe way?
Not sure. I don't think so.
That second one won't work, or at least shouldn't do much useful. I.e.
you're setting table_id_seq to be equal to itself.
The first one, the select max(id) one, is the standard way of doing this,
if you're afraid users might be diddling the data while you're trying to
import it, you can always set pg_hba.conf to only let you log in from
local or something and do it there. But mostly if the copy command and
the select setval are in a bacth file it should all happen fast enough to
escape notice by the users until it's already loaded and set.
From | Date | Subject | |
---|---|---|---|
Next Message | Christopher Browne | 2003-10-21 21:14:18 | Re: lastval(seq) ? |
Previous Message | Alvaro Herrera Munoz | 2003-10-21 20:12:36 | Re: lastval(seq) ? |