Re: A problem with sequences... - Mailing list pgsql-general

From Dmitry Tkach
Subject Re: A problem with sequences...
Date
Msg-id b35qve$vh5$1@news.hub.org
Whole thread Raw
In response to Re: A problem with sequences...  (Dmitry Tkach <dmitry@openratings.com>)
Responses Re: A problem with sequences...
List pgsql-general
I got it!

For those, who are still wonderring, the actual problem had nothing to do with that rule.

I was loading some stuff into the database through a sql script (copy from etc...), and in the end of it I did:

select setval('answer_id_seq', id) from answer order by id desc limit 1;

Now, for some reason this reports a correct value, but what actually gets set is wrong!
Here is an example:

rapidb=# select max(id) from answer;
   max
-------
  25000
(1 row)

rapidb=# select last_value from answer_id_seq;
  last_value
------------
       22124
(1 row)

rapidb=# select setval ('answer_id_seq', id) from answer order by id desc limit 1;
  setval
--------
   25000
(1 row)

rapidb=# select currval('answer_id_seq');
  currval
---------
    21452
(1 row)

So, the question I have now is - what's going on?
How come setval() reports 25000, but the currval changes to 21452
If my query is wrong (and I can't really see what's wrong with it), then why does it report the correct value?

I have rewritten that script to do
select setval ('answer_id_seq', max(id)) from answer
instead, and that works fine... But I'd still love to find out what is wrong with that original query's behaviour...

Thanks!

Dima


pgsql-general by date:

Previous
From: Jan Wieck
Date:
Subject: Re: Foreign Key with Constant
Next
From: Doug McNaught
Date:
Subject: Re: transactions