472,353 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

lastval(seq) ?

CSN
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).

CSN

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
7 7271
On Mon, 20 Oct 2003 21:52:17 -0700 (PDT)
CSN <co*******************@yahoo.com> 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).

CSN


select curval('my_sequence')
--
Jeff Trout <je**@jefftrout.com>
http://www.jefftrout.com/
http://www.stuarthamm.net/

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
On Tuesday 21 October 2003 13:10, Jeff wrote:
On Mon, 20 Oct 2003 21:52:17 -0700 (PDT)

CSN <co*******************@yahoo.com> 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).

CSN


select curval('my_sequence')


Only works if you've called nextval() first.

I'm guessing phppgadmin uses SELECT * FROM my_seq_name

What were you planning to use this value for?

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #3
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?

Second: select last_value from table_id_seq;

It's not transaction safe, so don't expect it to work right on busy
multi-user systems.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #4
CSN

--- "scott.marlowe" <sc***********@ihs.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?

CSN



Second: select last_value from table_id_seq;

It's not transaction safe, so don't expect it to
work right on busy
multi-user systems.

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #5
On Tue, Oct 21, 2003 at 12:49:39PM -0700, CSN wrote:
For updating sequences after copy importing data (with
id's).

Is there a transaction-safe way?


Sure:

begin;
lock table table;
select setval('table_id_seq', (select max(id) from table));
commit;

--
Alvaro Herrera (<alvherre[@]dcc.uchile.cl>)
"El sabio habla porque tiene algo que decir;
el tonto, porque tiene que decir algo" (Platon).

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #6
On Tue, 21 Oct 2003, CSN wrote:

--- "scott.marlowe" <sc***********@ihs.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.
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #7
co*******************@yahoo.com (CSN) writes:
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?


There's not likely to be.

For any given potential value of currval('table_id_seq'), it is always
possible that a transaction could be held open that is using that
value.

The only really _safe_ way to reset sequences is to do so when there
are no transactions active on the system.

In practice, we have to live with that potential for lack of safety,
and I would be inclined to set the value to the maximum visible value
plus some reasonable constant, say 1000, on the assumption that unless
someone is trying to do something actively pathologically bad, that
should be "good enough."

But my preference would be to do so with applications that might be
doing potentially-evil things SHUT DOWN.
--
output = ("cbbrowne" "@" "libertyrms.info")
<http://dev6.int.libertyrms.com/>
Christopher Browne
(416) 646 3304 x124 (land)
Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: joh12005 | last post by:
hello, i'm looking for a way to have a list of number grouped by consecutive interval, after a search, for example : => , , , ]
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.