X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BF2473A4505 for ; Thu, 11 Nov 2004 17:38:36 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53446-10 for ; Thu, 11 Nov 2004 17:38:31 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 081413A4BD0 for ; Thu, 11 Nov 2004 15:54:05 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id iABFs4g5012427 for ; Thu, 11 Nov 2004 15:54:04 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id iABFhifC007941 for pgsql-sql@postgresql.org; Thu, 11 Nov 2004 15:43:44 GMT From: Gaetano Mendola X-Newsgroups: comp.databases.postgresql.sql Subject: Re: A transaction in transaction? Possible? Date: Thu, 11 Nov 2004 09:23:24 +0100 Organization: PYRENET Midi-pyrenees Provider Lines: 100 Message-ID: <4193217C.9080900@bigfoot.com> References: <13824.1100107697@sss.pgh.pa.us> <200411102236.41971.spaceman@foo.at> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@pyrenet.fr To: Stefan Weiss User-Agent: Mozilla Thunderbird 0.9 (Windows/20041103) X-Accept-Language: en-us, en In-Reply-To: <200411102236.41971.spaceman@foo.at> X-Enigmail-Version: 0.86.1.0 X-Enigmail-Supports: pgp-inline, pgp-mime To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.6 tagged_above=0.0 required=5.0 tests=DATE_IN_PAST_06_12 X-Spam-Level: X-Archive-Number: 200411/118 X-Sequence-Number: 19437 Stefan Weiss wrote: > On Wednesday, 10 November 2004 18:28, Tom Lane wrote: > >>Achilleus Mantzios writes: >> >>>Just a very naive thought.... >>>Wouldn't make more sense to allow nested begin/commit/rollback blocks? >> >>We actually had it working that way initially, but changed to the >>spec-defined behavior, because (a) it wasn't standard, and (b) it >>was confusing. See the pghackers archives. > > > We used to run into problems with nested transactions in scenarios > like this: > > Imagine a database where you have a table for customers, and > each customer can have (in a seperate table) several contacts; a > contact can have one or more addresses, phone numbers, etc. These > tables are connected by foreign keys, but without "on delete" > triggers. Why "without" ? Are you looking to solve a problem introduced by yourself ? > The frontend application has a function for deleting a contact, > which works something like this: > > * begin transaction > * delete the contact's addresses, phone numbers, etc > * ... > * delete the contact record itself > * commit > > Then there is a function for deleting a customer: > > * begin transaction > * for all contacts, call the "delete contact" function > * ... > * delete the customer record itself > * commit > > At the moment the application is "simulating" support for nested > transactions: We use a wrapper for the BEGIN and COMMIT calls, > and an internal counter, which is incremented for each BEGIN. > Only the first BEGIN gets sent to the backend. When COMMIT has > been called as many times as BEGIN, we send a real commit (errors > and ROLLBACK are handled too, of course). > > It's not perfect, but it does what we need. Savepoints are a nice > feature, but I don't think they could help us here. You can handle this task using the new functionality introduced with savepoint: the exception. For more information look at: http://candle.pha.pa.us/main/writings/pgsql/sgml/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING Your delete customer can do: * BEGIN * for all contacts call delete contact * ... * EXCEPTION * handle your exception * END; * * delete the customer record itself Regards Gaetano Mendola