X-Original-To: pgsql-patches-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 5D4B59DC95D; Tue, 10 Jan 2006 20:40:42 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 31130-08; Tue, 10 Jan 2006 20:40:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from linda-3.paradise.net.nz (bm-3a.paradise.net.nz [203.96.152.182]) by postgresql.org (Postfix) with ESMTP id C3F509DC838; Tue, 10 Jan 2006 20:40:37 -0400 (AST) Received: from smtp-2.paradise.net.nz (tclsnelb1-src-1.paradise.net.nz [203.96.152.172]) by linda-3.paradise.net.nz (Paradise.net.nz) with ESMTP id <0ISW000RBKJR59@linda-3.paradise.net.nz>; Wed, 11 Jan 2006 13:40:40 +1300 (NZDT) Received: from [192.168.1.11] (218-101-28-20.dsl.clear.net.nz [218.101.28.20]) by smtp-2.paradise.net.nz (Postfix) with ESMTP id 7E6596E8972; Wed, 11 Jan 2006 13:40:39 +1300 (NZDT) Date: Wed, 11 Jan 2006 13:40:37 +1300 From: Mark Kirkwood Subject: Re: Summary table trigger example race condition In-reply-to: <20060111000951.GQ3902@pervasive.com> To: "Jim C. Nasby" Cc: pgsql-patches@postgresql.org, pgsql-docs@postgresql.org Message-id: <43C45405.40002@paradise.net.nz> MIME-version: 1.0 Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7bit X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0.6 (X11/20051106) References: <20060105213645.GY43311@pervasive.com> <43BDC132.90007@paradise.net.nz> <20060106030415.GL43311@pervasive.com> <43BDE812.5000103@paradise.net.nz> <43C0833D.4040607@paradise.net.nz> <20060111000951.GQ3902@pervasive.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.284 required=5 tests=[AWL=0.284] X-Spam-Score: 0.284 X-Spam-Level: X-Archive-Number: 200601/131 X-Sequence-Number: 18467 Jim C. Nasby wrote: > On Sun, Jan 08, 2006 at 04:13:01PM +1300, Mark Kirkwood wrote: > >>After re-examining the original code, it looks like it was not actually >>vulnerable to a race condition! (it does the UPDATE, then if not found >>will do an INSERT, and handle unique violation with a repeat of the same >>UPDATE - i.e three DML statements, which are enough to handle the race >>in this case). > > > What happens if someone deletes the row between the failed insert and > the second update? :) > In this example the rows in the summary table never get deleted by DELETE operations on that main one - the trigger just decrements the various amounts - i.e DELETE becomes UPDATE, so no problem there. > AFAICT, example 36-1 is the only way to handle this without creating a > race condition. > For the general case indeed you are correct - a good reason for this change :-). In addition, that fact that it is actually quite difficult to be sure that any race condition is actually being handled makes (another) good reason for using the most robust method in the 'official' examples. Best wishes Mark