X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2261ED1C4EE for ; Tue, 29 Jul 2003 20:10:26 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 94859-03 for ; Tue, 29 Jul 2003 17:09:28 -0300 (ADT) Received: from mail.seaworthysystems.com (216-238-112-88.dsl.ct.thebiz.net [216.238.112.88]) by svr1.postgresql.org (Postfix) with ESMTP id E0D5BD1C509 for ; Tue, 29 Jul 2003 17:10:24 -0300 (ADT) Received: from seaworthysystems.com (localhost.localdomain [127.0.0.1]) by mail.seaworthysystems.com (Postfix) with SMTP id DC1CA1B690 for ; Tue, 29 Jul 2003 16:16:02 -0400 (EDT) Received: from 216.238.112.88 (SquirrelMail authenticated user btober) by $HOSTNAME with HTTP; Tue, 29 Jul 2003 16:16:02 -0400 (EDT) Message-ID: <64821.216.238.112.88.1059509762.squirrel@$HOSTNAME> Date: Tue, 29 Jul 2003 16:16:02 -0400 (EDT) Subject: Does a the block of code within a stored procedure constitute a transaction? From: To: X-Priority: 3 Importance: Normal X-Mailer: SquirrelMail (version 1.2.9) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/494 X-Sequence-Number: 14608 I need to "manually" keep a sequence for each row of the employee table, i.e., I don't want to use postgres's built-in sequences for this application because the sequence numbers are used to generate expense report numbers on a "per-employee" basis. That is, each employee has a separate sequence counter, starting at one, to number their expense reports. Since employees will come and go, I don't want to keep having to create and delete postgres sequence objects as employees come and go. Instead, I have a column of the employee table store the "last value" of the corresponding expense report sequence counter, and in an ON INSERT trigger to the expense report table, I call the following function to get and increment the new sequence value: CREATE OR REPLACE FUNCTION paid.expense_report_next(int4) RETURNS integer AS ' DECLARE l_employee_pk ALIAS FOR $1; l_expense_report_seq INTEGER; BEGIN SELECT INTO l_expense_report_seq expense_report_seq+1 FROM employee WHERE employee_pk = l_employee_pk; UPDATE employee SET expense_report_seq = l_expense_report_seq WHERE employee_pk = l_employee_pk; RETURN l_expense_report_seq; END;' LANGUAGE 'plpgsql' VOLATILE; What I need to know is whether or not this is multi-user safe, i.e., will the block of code in the procedure execute as a transaction so that if more than one clerk creates an expense report for the same employee simultaneously is it possible or impossible that value of the employee.expense_report_seq gets updated by the second clerk between the SELECT and UPDATE statements invoked by the first clerk? And as a follow-up, should I add the FOR UPDATE clause to the SELECT statement? ~Berend Tober