X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4C51252906 for ; Mon, 13 Jun 2005 11:46:43 -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 34748-09 for ; Mon, 13 Jun 2005 14:46:39 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 6765452812 for ; Mon, 13 Jun 2005 11:46:38 -0300 (ADT) 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 877B4414FCF; Mon, 13 Jun 2005 15:46:29 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id E77D715ED7; Mon, 13 Jun 2005 15:43:44 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 31812-09; Mon, 13 Jun 2005 15:43:42 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id D76BD15ED5; Mon, 13 Jun 2005 15:43:41 +0100 (BST) Message-ID: <42AD9B9D.6080902@archonet.com> Date: Mon, 13 Jun 2005 15:43:41 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: "ON.KG" Cc: pgsql-general@postgresql.org Subject: Re: duplicate key violates unique constraint References: <42A95936.3060002@archonet.com> <20050610095655.0215752956@svr1.postgresql.org> <10813889593.20050613202239@on.kg> In-Reply-To: <10813889593.20050613202239@on.kg> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.054 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200506/623 X-Sequence-Number: 79331 ON.KG wrote: > > before inserting or updating this table there're some checkings, > logs, etc., so I'm using PL/PgSQL for that > > after all checkings and logs I have: > > UPDATE table1 > SET hits = hits + 1 > WHERE ip = some_ip; > > IF NOT FOUND THEN > INSERT INTO table1 > (ip) > VALUES > (some_ip); > END IF; > > when IP is not found in table it inserts new record into table > but in logs i see error > ERROR: duplicate key violates unique constraint "table1" > CONTEXT: PL/pgSQL function "insert_table1" line 68 at SQL statement If you can have more than one client running this at once you have a race condition here. The order runs something like: 1. client A tries to update SOME_IP, no rows affected 2. client B tries to update SOME_IP, no rows affected 3. client A tries the insert of SOME_IP 4. client B tries the insert of SOME_IP - fails! If you have more than one client, this can always happen. You have two choices: 1. Use a lock to stop two clients interacting like this 2. Catch the error on the insert and try the update again. This requires version 8.0 or higher. -- Richard Huxton Archonet Ltd