PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2003, 09:13 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 11
Send a message via ICQ to zapa
Arrow A question on postgresql sequences

Hi there ,
I have a question about using postgreSQL sequences .
Whenever I create a sequence and set it's value , after starting another session with postgreSQL that value is lost and the sequence has to be set again .

Is there any way of setting the sequence to retain their values even after the session is reset ?
__________________
FreeBSD , dooing more with less since 10 years ago

Reply With Quote
  #2  
Old June 4th, 2003, 09:59 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Can you go into more depth about what you're doing? Are you saying that you create a sequence and then after successfully updating it with setval() it resets after you disconnect and reconnect to the DB?

-b
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #3  
Old June 4th, 2003, 10:20 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 11
Send a message via ICQ to zapa
here is some terminal output to demonstrate my problem
Code:
mydb=# create sequence cont_seq_nr;
CREATE SEQUENCE
mydb=# select setval('cont_seq_nr' , 13 );
 setval
--------
     13
(1 row)

mydb=# select currval('cont_seq_nr');
 currval
---------
      13
(1 row)

mydb=# \q

$ psql mydb

Welcome to psql 7.3.2, the PostgreSQL interactive terminal.

mydb=# select currval('cont_seq_nr');
ERROR:  cont_seq_nr.currval is not yet defined in this session
mydb=#

Reply With Quote
  #4  
Old June 4th, 2003, 10:43 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thx for the additional info,
Basically, the value of the actual sequence isn't lost it's just that the you can't use the currval until you've selected from the sequence during the current session.

This newsgroup posting probably explains it better than I have.

HTH,
-b

Reply With Quote
  #5  
Old June 4th, 2003, 10:57 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 11
Send a message via ICQ to zapa
ah ok , i understand

thanks for clearing it all up . It's a bit odd thought , that you can't get currval from a sequence like this .

I was trying to use this as a cheap trick to improve performance on an application that has to show a listing in the format :
"current_record_nr of last_record_nr" . Considering currval takes 0.09 miliseconds as oposed to the full second of count(*) or max(*) it was a good trick ...

oh well i'll work it out somehow

Reply With Quote
  #6  
Old June 4th, 2003, 11:15 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 17
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
If you're using max(*) then that means that there's no deletions allowed, correct? If so, then I'm not sure if it will be more efficient but how about trying to see if a
SELECT your_id_field FROM your_table ORDER BY your_id_field DESC LIMIT 1

How does that compare?
HTH
-b

Reply With Quote
  #7  
Old June 4th, 2003, 11:40 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 11
Send a message via ICQ to zapa
I used this :
Code:
int currval;
ResultSet rs = stmt.executeQuery("Select nextval('cont_seq_nr') as c");
rs.next();
currval = Integer.parseInt(rs.getString("c"));
currval--;
rs = stmt.executeQuery("Select setval('cont_seq_nr' , " + currval + ")");
return currval;   


just as I was done testing it i refreshed this page , saw your sugestion which seems to be 0.05 miliseconds faster in SQL code , my guess is that i'll gain some more speed by simplifying the java code too . Thanks , that was exactly what I needed

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > A question on postgresql sequences

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap