X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 442D6D9B21 for ; Tue, 18 Oct 2005 20:13:51 -0300 (ADT) 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 24580-02 for ; Tue, 18 Oct 2005 23:13:46 +0000 (GMT) Received: from smtp-v.fe.bosch.de (smtp-v.fe.bosch.de [139.15.237.2]) by svr1.postgresql.org (Postfix) with ESMTP id 33495D9A7C for ; Tue, 18 Oct 2005 20:13:47 -0300 (ADT) Received: from mta3.fe.internet.bosch.com (unknown [10.4.98.30]) by imta5.fe.bosch.de (Postfix) with ESMTP id A4BD43C00C for ; Wed, 19 Oct 2005 01:13:50 +0200 (CEST) Received: from mailgate.cl.internet.bosch.com (virusscan6.fe.internet.bosch.com [10.4.98.11]) by mta3.fe.internet.bosch.com (Postfix) with ESMTP id B85D28C062 for ; Wed, 19 Oct 2005 01:13:49 +0200 (CEST) Received: from cl-mail01.au.bosch.com (unknown [10.23.5.53]) by mailgate.cl.internet.bosch.com (Postfix) with ESMTP id B5BA15282E for ; Wed, 19 Oct 2005 09:13:47 +1000 (EST) X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Problem while using start transaction ans commit; Date: Wed, 19 Oct 2005 09:13:46 +1000 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Problem while using start transaction ans commit; Thread-Index: AcXUORLysVx9sz6lQ3GPmvJouPXGvQAAAeqQ From: "Stewart Ben (RBAU/EQS4) *" To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.557 required=5 tests=[AWL=-0.024, DNS_FROM_RFC_ABUSE=0.374, UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200510/198 X-Sequence-Number: 23051 Sri, > I have a small problem in using nested transactions while=20 > working on Postgres 8.0. This is a known problem with Postgres 8.0 - there is no support for nested transactions (which occurs when calling functions). Your best bet would be to raise an exception within B or C - this will cause a rollback to wherever the exception is caught. If you surround the calls to B and C in a block to catch the exception, this will provide transaction-like semantics. An example: ---------- CREATE OR REPLACE FUNCTION tr_addcourse(employeeno, int4, coursename, float4, text, timestamptz, int4) RETURNS int4 AS $BODY$-- Use case: 10.2.9: Add a course DECLARE transid int4; cid int4; errcode int4; BEGIN -- Setup default return code. This is used if we hit an -- exception that we didn't throw. SELECT -32767 into errcode; -- E_UNKNOWN -- Start the transaction, lock tables LOCK TABLE backend.courses IN ACCESS EXCLUSIVE MODE; LOCK TABLE backend.courseareas IN SHARE MODE; -- Access to administrators only IF NOT tt_user_access(actor, 'a') THEN SELECT -1 into errcode; -- Return E_NO_ACCESS RAISE EXCEPTION 'User % does not have access.', actor; END IF; -- Check for a duplicate course name. IF tt_course_name_active(cname) THEN SELECT -2000 INTO errcode; -- E_DUP_COURSE RAISE EXCEPTION 'Course "%" already exists.', cname; END IF; -- Check for course area status SELECT tt_coursearea_status(carea) INTO errcode; IF NOT errcode =3D 0 THEN -- NOT errcode =3D SUCCESS RAISE EXCEPTION 'Error finding active course area %', carea; END IF; -- Grab a transaction ID SELECT tt_acquire_transaction(actor, 'tr_addcourse') INTO transid; IF transid < 0 THEN SELECT transid into errcode; -- Return the error code. RAISE EXCEPTION 'Could not acquire transaction.'; END IF; -- Get the next course ID SELECT nextval('backend.courses_courseid_seq') INTO cid; -- Insert the row INSERT INTO backend.courses (transactionid, courseid, coursearea, coursename, active, duration, description, contentdate, valid_months) VALUES (transid, cid, carea, cname, TRUE, dur, desc, cdate, valid_mths); -- Success RETURN cid; EXCEPTION WHEN RAISE_EXCEPTION THEN RETURN errcode; WHEN OTHERS THEN RETURN -32767; -- E_UNKNOWN END;$BODY$ LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER; ---------- In this code, whenever an exception is raised, the system will rollback to the start of the block (BEGIN). Best regards, Ben Stewart -- Robert Bosch (Australia) Pty. Ltd. Engineering Quality Services, Student Software Engineer (RBAU/EQS4) Locked Bag 66 - Clayton South, VIC 3169 - AUSTRALIA mailto:ben.stewart@au.bosch.com http://www.bosch.com.au/=20