473,404 Members | 2,195 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,404 software developers and data experts.

Problems with transactions and sequences

Hello All!

Im suffering an appearantly common problem with psql, although I
haven't been able to locate the proper solution.
Using phpPgAdmin I've created a relation (note: this relation is
grossly simplified but the problem remains the same) :

CREATE TABLE Users
( bid SERIAL,
name CHAR(8) NOT NULL,
PRIMARY KEY (bid)
);

Now, the problem is that I wish to extract the sequence number from a
newly inserted user, like this:

INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');

This works perfectly(!), but my project involves multiple simultanous
users so I have to use transactions like this:

BEGIN;
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');
COMMIT;

This, however, doesn't work although I thought it should. It doesn't
return anything at all?!?! Could somebody explain me what I'm doing
wrong, or tell my how to do it instead?

Any help would be greatly appreciated!
--
Sune Nielsen su****@image.dk
Student at
Dept. of Computer Science
Copenhagen University
http://www.diku.dk
Nov 11 '05 #1
4 3191
Sune Nielsen <su****@image.dk> writes:
Now, the problem is that I wish to extract the sequence number from a
newly inserted user, like this: INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq'); This works perfectly(!), but my project involves multiple simultanous
users so I have to use transactions like this: BEGIN;
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');
COMMIT;
No, you don't need the BEGIN/COMMIT. Read the description of currval
again.
This, however, doesn't work although I thought it should. It doesn't
return anything at all?!?!


Surely it does. You didn't say exactly what client-side code you are
using, but I suspect it's looking at the result of the COMMIT step
(ie, nothing) instead of the result of the prior SELECT.

regards, tom lane

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

http://archives.postgresql.org

Nov 11 '05 #2
On Thu, 28 Aug 2003 10:51:21 -0400, in
comp.databases.postgresql.general you wrote:
Sune Nielsen <su****@image.dk> writes:
Now, the problem is that I wish to extract the sequence number from a
newly inserted user, like this:
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');

This works perfectly(!), but my project involves multiple simultanous
users so I have to use transactions like this:

BEGIN;
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');
COMMIT;


No, you don't need the BEGIN/COMMIT. Read the description of currval
again.

Okay, I think I get it. Because of the current connection-session I'm
safe?
This, however, doesn't work although I thought it should. It doesn't
return anything at all?!?!


Surely it does. You didn't say exactly what client-side code you are
using, but I suspect it's looking at the result of the COMMIT step
(ie, nothing) instead of the result of the prior SELECT.

Sorry about that. I was querying using SQL through phpPgAdmin's
SQL-interface although I was merely prototyping for some PHP-code I
couldn't get to work. But for the reference, how do I obtain the
results from an SELECT-statement if psql only returns the value of
COMMIT?

Thanks for you very speedy reply!
--
Sune Nielsen su****@image.dk
Student at
Dept. of Computer Science
Copenhagen University
http://www.diku.dk
Nov 11 '05 #3
On Thu, 28 Aug 2003 20:24:43 +0530,
sh*****************@persistent.co.in ("Shridhar Daithankar") wrote:
On 28 Aug 2003 at 14:35, Sune Nielsen wrote:
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');

This works perfectly(!), but my project involves multiple simultanous
users so I have to use transactions like this:

BEGIN;
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');
COMMIT;


You need to extract the current val first and explicitly use it in insert
statement. That way you won't have to produce work-arounds later..

As I could read from the reply from Tom Lane, it seemed that I only
needed to to remove the BEGIN and COMMIT in order to make it work.
What problems do you foresee if don't perform the queries in opposite
order? I'm kind of new to this stuff :-)

Thanks for your fast reply!
--
Sune Nielsen su****@image.dk
Student at
Dept. of Computer Science
Copenhagen University
http://www.diku.dk
Nov 11 '05 #4
> You need to extract the current val first and explicitly use it in insert
statement. That way you won't have to produce work-arounds later.. This statement is wrong. If you were going to use this method (
getting the value first - then setting it), I believe you would need
to call nextval not currval.

Lanette

sh*****************@persistent.co.in ("Shridhar Daithankar") wrote in message news:<3F4E650B.3698.795CE8@localhost>... On 28 Aug 2003 at 14:35, Sune Nielsen wrote:
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');

This works perfectly(!), but my project involves multiple simultanous
users so I have to use transactions like this:

BEGIN;
INSERT INTO Users (name) VALUES ('JohnDoe');
SELECT CURRVAL('users_bid_seq');
COMMIT;

Bye
Shridhar

Nov 11 '05 #5

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

Similar topics

11
by: Josh | last post by:
Hi, I am having a problem with Python. I am new to Python as a programming language, but I do have experience in other languages. I am experiencing strange problems with File handling and wonder...
7
by: Richard Maher | last post by:
Hi, I am seeking the help of volunteers to test some software that I've developed which facilitates distributed two-phase commit transactions, encompassing any resource manager (e.g. SQL/Server...
1
by: Rick | last post by:
I'm having problems with EnterpriseServices transactions running against Oracle 9iR2. I am inconsistently getting Oracle ORA-24761: Transaction Rolled Back results mid-transaction. If I start...
1
by: David McGeorge | last post by:
Dear Gurus, A Client has the following problems/requests for their Production databases, what is your professional/practical advises to tackle and resolve these issues: 1)Number of...
9
by: Lara | last post by:
Hello freaks, we have many problems with our online reorg and no idea how to resolve it. We had to do an online reorg beacause of 24 h online business. We start the reorg-statements (table by...
1
by: EDom | last post by:
Hi We are wanting to prepare a report which will show us month wise balances of ledgers. However on preparing the report only those months are showing which actually has transactions, even though...
2
by: Sridhar | last post by:
Hi, I am trying to implement sql transactions. But I am not knowing how to do that. I created a data access layer which contains methods to select/insert/update tables in a database. I have also...
12
by: Rami | last post by:
I have some requirement for an automated payment system. The system has four machines setup as follows: 1- Two machines have a clustered database. 2- Two machines have a .net business logic...
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.