Re: Nested transaction - I am a bank ?? - Mailing list pgsql-general

From Richard Huxton
Subject Re: Nested transaction - I am a bank ??
Date
Msg-id 200401131848.23299.dev@archonet.com
Whole thread Raw
In response to Nested transaction - I am a bank ??  ("Thapliyal, Deepak" <dthapliyal@soe.sony.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Pl/Perl speed
Next
From: "Keith G. Murphy"
Date:
Subject: Re: Best practice? Web application: single PostgreSQL