473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 7333
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.