X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A419A9FA5C8 for ; Fri, 16 Jun 2006 03:28:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 31913-04 for ; Fri, 16 Jun 2006 03:28:24 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by postgresql.org (Postfix) with ESMTP id 9DC409F9CAA for ; Fri, 16 Jun 2006 03:28:24 -0300 (ADT) Received: from [213.162.97.75] (helo=mail.metronet.co.uk) by ptb-relay02.plus.net with esmtp (Exim) id 1Fr7oc-00065D-RZ; Fri, 16 Jun 2006 07:28:22 +0100 Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 7DB357365C; Fri, 16 Jun 2006 07:28:18 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id EF60715EA6; Fri, 16 Jun 2006 07:28:16 +0100 (BST) Message-ID: <44924F80.7060500@archonet.com> Date: Fri, 16 Jun 2006 07:28:16 +0100 From: Richard Huxton User-Agent: Thunderbird 1.5.0.4 (X11/20060516) MIME-Version: 1.0 To: sathish kumar shanmugavelu Cc: pgsql-sql@postgresql.org Subject: Re: concurrency problem References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/200 X-Sequence-Number: 25436 sathish kumar shanmugavelu wrote: > INSERT INTO rcp_patient_visit_monitor ( > entry_no, patient_id, visit_date, is_newpatient, > visit_type, is_medical, > is_review, is_labtest, is_scan, is_scopy, is_xray, > weight, height) > VALUES ((SELECT coalesce(max(entry_no)+1, 1) FROM > rcp_patient_visit_monitor),?,current_timestamp,?,?,?,?,?,?,?,?,?,?) > > The point to note here is the select statement which gets the max > entry_no and adds one to it and save the new value. entry_no is the primary > key of the above table. > > Now i run the same program (different instance) from two systems, save > the form simultaneously, only one entry is saved, in the other system the > error says - duplicate key violates. BEGIN; LOCK TABLE ... INSERT ... COMMIT; You'll need to handle possible errors where one client fails to get a lock and times out. It won't happen often, but you do need to consider the option. -- Richard Huxton Archonet Ltd