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 September 4th, 2002, 10:44 PM
merl merl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 55 merl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 48 m 2 sec
Reputation Power: 11
question about start transactions

Hi all,

I'm familiarising myself with postgresql syntax, and am running in some trial scripts. As a matter of course I always end with a commit or rollback (rollback while developing script, commit for actual execution).

I ran the following:
insert into test (test_id) values (1);
commit;

And it inserts the row, but committing gives the message "COMMIT: no transaction in progress".

When running:
insert into test (test_id) values (2);
rollback;
The row is not present during a subsequent query, but the message "ROLLBACK: no transaction in progress" is given.

So, the behaviour is what I would have expected, but the message seems counter intuitive (in particular, it looks like the rollback has not taken place).

Is there some setting that needs to be tweaked to give more meaningful messages, or is this just something to live with, and I'll take it on trust that postgresql is doing what I *think* it should be doing?

Cheers,
Merl

Reply With Quote
  #2  
Old September 6th, 2002, 07:19 PM
Ted Striker Ted Striker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 409 Ted Striker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Arrow

You're almost there, just need to tell Postgres you are starting a transaction. Oracle lets you commit and rollback without the BEGIN stuff, but apparently Postgres requires the BEGIN statement before you can use COMMIT or ROLLBACK.


Do the transaction like so:

begin work;
insert into test (test_id) values (1);
commit;

Reply With Quote
  #3  
Old September 8th, 2002, 11:32 AM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
You can also use END in place of the COMMIT. END is a synonym of COMMIT, for those who want their SQL to appear in nicely grouped blocks.

Note that without BEGIN/COMMIT (or END) statements, Postgres uses an auto-commit mode, that is, every one of your statements acts as though it has its own BEGIN/COMMIT wrapper. If, for example, you have multiple INSERT statements, wrapping in BEGIN/COMMIT can substantially decrease execution time.

Don

Reply With Quote
  #4  
Old September 9th, 2002, 07:49 PM
merl merl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 55 merl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 48 m 2 sec
Reputation Power: 11
Thanks Ted,

That was exactly the sort of response I was expecting. I'll use "begin work;" to explicitly start transactions in future.

Donarb, your comments confused me slightly. I understand you to say that without BEGIN/COMMIT it implicitly commits every statement. Certainly, you should ALWAYS use commit/rollback in conjunction with dml statements. But in my experience (see initial post), rollback still works correctly (i.e. as expected) even if the transaction is not explicitly started.

Merl

Reply With Quote
  #5  
Old September 9th, 2002, 10:33 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Postgres considers every statement outside of a BEGIN/COMMIT block as an individual transaction.

So, as I mentioned before, each statement will auto-commit, if successful. This also means that each statement will auto-rollback, if unsuccessful. This is why your COMMIT and ROLLBACK did not work, they had already completed.

Note that this feature is not a part of the SQL standard, but a few database vendors offer it.

Don

Reply With Quote
  #6  
Old September 10th, 2002, 04:52 PM
merl merl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 55 merl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 48 m 2 sec
Reputation Power: 11
Ok,

Thanks donarb for clearing that up. I make a mistake in my tests, and thought that it was rolling back successfully, just giving a screwy message as it did so. I just checked again, and the rollback is not occurring.

I understand now, no rollback possible unless within an explicitly started transaction.

Cheers,
Merl

Reply With Quote
  #7  
Old September 10th, 2002, 06:40 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Just to be clear, a rollback IS possible, that is, for a single statement not inside of a transaction. If the statement fails, it will rollback.

Don

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > question about start transactions

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