Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPt2c-0002w2-FE for pgsql-sql@arkaria.postgresql.org; Tue, 18 Mar 2014 12:22:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WPt2b-0007Lq-Qn for pgsql-sql@arkaria.postgresql.org; Tue, 18 Mar 2014 12:22:45 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPt2b-0007Lk-1S for pgsql-sql@postgresql.org; Tue, 18 Mar 2014 12:22:45 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPt2X-0000yJ-2B for pgsql-sql@postgresql.org; Tue, 18 Mar 2014 12:22:44 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WPt2V-0007Dy-JR for pgsql-sql@postgresql.org; Tue, 18 Mar 2014 05:22:39 -0700 Date: Tue, 18 Mar 2014 05:22:39 -0700 (PDT) From: ssylla To: pgsql-sql@postgresql.org Message-ID: <1395145359428-5796557.post@n5.nabble.com> Subject: problem with update order (?) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.9 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This seems like a mystery to me. I have the following table "project": id [integer], project_code [text] 1;"03.0104.1" 2;"03.0104.2" 3;"03.0104.3" 4;"03.0104.4" with a UNIQUE constraint on the column 'project_code' and the following trigger function (it is called after delete or update on "project") in order to recount the last digit of the project code: CREATE OR REPLACE FUNCTION project_update_delete_after() RETURNS trigger AS $BODY$ begin -- if project_code changed... if (TG_OP='UPDATE' and new.project_code!=old.project_code) -- ... or if project was deleted or (TG_OP='DELETE') then -- recount the last digit of project_code... -- ...that are higher than updated/deleted: execute format(' update %I.project set project_code= substr($1.project_code,1,8) ||cast(cast(substr(project_code,9,1) as integer)-1 as text) where substr(project_code,1,7)=substr($1.project_code,1,7) and (cast(substr(project_code,9,1) as integer) > cast(substr($1.project_code,9,1) as integer)); ', TG_TABLE_SCHEMA) using old; end if; return NEW; end; $BODY$ LANGUAGE plpgsql; Now, when I try to delete the first row of the table (1;"03.0104.1") I get the following error message: ERROR: duplicate key value violates unique constraint "pcode_unique" DETAIL: Key (project_code)=(03.0104.2) already exists. CONTEXT: SQL statement " update public.project set project_code= substr($1.project_code,1,8) ||cast(cast(substr(project_code,9,1) as integer)-1 as text) where substr(project_code,1,7)=substr($1.project_code,1,7) and (cast(substr(project_code,9,1) as integer) > cast(substr($1.project_code,9,1) as integer)); If I delete the 2nd row (2;"03.0104.2") it is working fine. Obviously, in the case of deleting the 1st row, Postgres tries to update the project_code of the 3rd row before the 2nd row and that creates the unique constraint violation. I tried to avoid that by creating an index of the id and clustering the table , but I get the same error message. I have no idea what this problem is caused by, so I feel forced to post this here. Stefan -- View this message in context: http://postgresql.1045698.n5.nabble.com/problem-with-update-order-tp5796557.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql