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 August 26th, 2012, 11:29 AM
littleK littleK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 littleK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 46 sec
Reputation Power: 0
Question about transactions and concurrent access

Hello,

I have a question about transactions. I am creating a shell script that will use psql to execute 8 queries that will SELECT data from a database and INSERT the result of the SELECT into a temporary table.

My concern is concurrent access to the same tables while the script is running. While this is unlikely scenario in my case, I'd prefer to be safe about it.

Originally, I had the script executing each of the 8 queries separately, one after another. Then, I modified the psql command to execute all of the queries, as one transaction. For example:

Code:
psql -h localhost -p 5432 -U postgres -1 -c "$SQL1; $SQL2; $SQL3;" mydb


However, it seems from the Postgres documentation that such a transaction will not guarantee a single snapshot of the database/tables while the query is executing. In other words, concurrent access could still affect the results.

Therefore, I don't really know where to proceed from here. What's the recommended option to ensure that concurrent access does not interfere with my 8 queries, every time the script is executed?

Thanks!

Reply With Quote
  #2  
Old August 26th, 2012, 03:00 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 1 h 55 m
Reputation Power: 284
You need to make sure everything is run in a single transaction. Either by turning off autocommit and running all scripts with a single call or by putting everything into a single script that encloses everything in a BEGIN TRANSACTION ... END TRANSACTION block.

Before starting the transaction set the transaction to REPEATABLE READ and you should be fine.
__________________
I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
http://forums.devshed.com/misc.php?do=bbcode#code

Tips on how to ask better questions:
http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
http://wiki.postgresql.org/wiki/SlowQueryQuestions
http://catb.org/esr/faqs/smart-questions.html

Reply With Quote
  #3  
Old August 26th, 2012, 03:23 PM
littleK littleK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 littleK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 46 sec
Reputation Power: 0
Thank you, shammat. I have updated the psql command to:

Code:
psql -h localhost -p 5432 -U postgres -1 -c "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN TRANSACTION; $SQL1; $SQL2; $SQL3; END TRANSACTION" mydb


Will the isolation level "end" after the "END TRANSACTION"? What if there was an error? Would I then need to perform an explicit ROLLBACK, or will that happen automatically?

Just out of curiosity, are there any disadvantages to using SERIALIZABLE instead of REPEATABLE READ?

Thanks!

Last edited by littleK : August 26th, 2012 at 03:37 PM. Reason: Additional Question

Reply With Quote
  #4  
Old August 26th, 2012, 03:47 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 1 h 55 m
Reputation Power: 284
Quote:
Originally Posted by littleK
Will the isolation level "end" after the "END TRANSACTION"?
Please see the manual. It's all explained there:

http://www.postgresql.org/docs/current/static/sql-set-transaction.html

You will also need to put the SET TRANSACTION after the BEGIN TRANSACTION!
Comments on this post
littleK agrees!

Reply With Quote
  #5  
Old August 26th, 2012, 03:53 PM
littleK littleK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 littleK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 46 sec
Reputation Power: 0
Ah, got it. Thanks for your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Question about transactions and concurrent access

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