By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Nested transaction - I am a bank ??

P: n/a
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.

Question:
if my first update succeeds and second fails (say due to space
errors .. I have inconsistancy ..

Per the thread below stored procedures/functions cannot have commits. I
assume that means that they will be implicitly commited ??

How do I approach this simple requirment using psql ?

Thx
Deep

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An*************@loteco.ru
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] Parse error help needed...
On Tuesday 13 January 2004 12:01, An*************@loteco.ru wrote:
RH> Remove the "commit" line - functions cannot define their own
transactions RH> anyway.
Do you know if it will be solved sometime? Or this is architecture
dependend problem? I mean that transactions are rulez and very helpful
rulez when working with large databases.


Nested transactions are on the todo list, but I don't know when they will
appear.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.

Question:
if my first update succeeds and second fails (say due to space
errors .. I have inconsistancy ..


Not if you run the queries as a single transaction.

Per the thread below stored procedures/functions cannot have commits. I
assume that means that they will be implicitly commited ??

How do I approach this simple requirment using psql ?

Thx
Deep

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Richard Huxton
Sent: Tuesday, January 13, 2004 4:32 AM
To: An*************@loteco.ru
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] Parse error help needed...
On Tuesday 13 January 2004 12:01, An*************@loteco.ru wrote:

RH> Remove the "commit" line - functions cannot define their own
transactions RH> anyway.
Do you know if it will be solved sometime? Or this is architecture
dependend problem? I mean that transactions are rulez and very helpful
rulez when working with large databases.


Nested transactions are on the todo list, but I don't know when they will
appear.

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #2

P: n/a
On Tuesday 13 January 2004 17:47, Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.


In database terms, the two operations together are one transaction. You do
something like:

BEGIN;
INSERT INTO detail (acct_num,trans_type,trans_time,notes) VALUES
(1,'CASHOUT',now(),'blah');
UPDATE account_summary SET amount=amount-10 WHERE acct_num = 1;
COMMIT;

Now, if one (or both) of those were written as a function, that function's
effects would still be bound by the transaction. All operations(*) take place
within a transaction in PG, either explicitly as above or implicitly with one
per statement.

What you can't do is have a function that does something like:

LOOP 1..10
BEGIN;
-- do something ten times, each time in its own transaction
COMMIT;
END LOOP

(*) except for a couple of bits like vacuum, truncate(?) and similar.
--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #3

P: n/a
On Tue, 13 Jan 2004, Joshua D. Drake wrote:
Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.

Question:
if my first update succeeds and second fails (say due to space
errors .. I have inconsistancy ..


Not if you run the queries as a single transaction.

Per the thread below stored procedures/functions cannot have commits. I
assume that means that they will be implicitly commited ??

How do I approach this simple requirment using psql ?


Joshua, aren't functions run within their own transactions if they don't
inherit one? Wouldn't that take care of this part as well?
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #4

P: n/a
On Tue, 2004-01-13 at 13:34, scott.marlowe wrote:
On Tue, 13 Jan 2004, Joshua D. Drake wrote:
Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.

Question:
if my first update succeeds and second fails (say due to space
errors .. I have inconsistancy ..


Not if you run the queries as a single transaction.

Per the thread below stored procedures/functions cannot have commits. I
assume that means that they will be implicitly commited ??

How do I approach this simple requirment using psql ?


Joshua, aren't functions run within their own transactions if they don't
inherit one? Wouldn't that take care of this part as well?


sort of... in order to call a function you have to do "select foo()"
which creates an implicit transaction block based on the select
statement that commits when the "select statement" finishes and
everything within foo will be treated as a single transaction.

Robert Treat
--
Build A Brighter Lamp :: Linux Apache {middleware} PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #5

P: n/a

On Tue, 13 Jan 2004, Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.

Question:
if my first update succeeds and second fails (say due to space
errors .. I have inconsistancy ..

Per the thread below stored procedures/functions cannot have commits. I
assume that means that they will be implicitly commited ??

How do I approach this simple requirment using psql ?


I know others have answered this but I don't recall a specific answer to the
psql question. So...simple:

mydb=> begin;
mydb=> update [whatever];
mydb=> update [whatever];
mydb=> [whatever]
mydb=> [...etc.]
mydb=> commit;
As has been mentioned, put all the operations in a function you could replace
all those lines with one:

select myfunc();

which will either complete and commit (if autocommit is on) or not commit. If
autocommit is off then it will still need the commit statement in order to
commit and also a rollback (or may be a commit works as well I can't
remember) in order to clear the aborted transaction in case of error (so that
more statements can be issued).
--
Nigel Andrews
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #6

P: n/a
Thapliyal, Deepak wrote:
Hi,

Assume I have a bank app.. When customer withdraws $10 from his accouint I
have to do following
--> update account_summary table [subtract $10 from his account]
--> update account detail_table [with other transaction details]

Requirement:
either both transactions should succeed or both transactions should
be rolled back in case of failure.


Both actions you mentioned are not [or are unlikely to be implemented
as] two separate transactions, but a single transaction (and thus the
subject "nested transaction" has nothing to do with this.

Nested transaction are usually used in complex operations.

Save points can be used to implement nested transaction.

Since we're using a bank as example, consider a bank with 1 million
accounts. At the end of the month, it needs to calculate and post
interest for each account. The whole operation takes 10 hours. If we use
a single transaction for this, then if the machine/database crashes, the
whole unfinished transaction will be rolled back. If the db is back up,
but then fails again in the middle of this giant transaction, it will be
rolled back again. And perhaps again... and again... and thus it will
never finishes.

With save points (and nested transactions) we can save in the middle of
transaction and later rolls back to the last save point instead of
beginning the transaction all over.

--
dave
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.