Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZDF9C-0001kq-Qk for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Jul 2015 16:58:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZDF9A-0000jf-D2 for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Jul 2015 16:58:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZDF5R-0005C9-FH for pgsql-bugs@postgresql.org; Thu, 09 Jul 2015 16:54:13 +0000 Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZDF5M-0003UU-M8 for pgsql-bugs@postgresql.org; Thu, 09 Jul 2015 16:54:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20140207; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date; bh=RoUW5HtMvJdB3aNApuuMG6p0WHgYF0+y9ESrEeKw+hY=; b=Cjr68ZGruRmSQW2z1IzlieeMLF YmD2ealyHhKACTG0+7CdTlzRJnWkqxjq7d3NYl2zrg4Bk7ut498nFJ6WMs2iRXhKWPeQviZAZOuFx kg0LxBPdqBohAQ1/EaL/s/K7N; Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.86_RC4) (envelope-from ) id 1ZDF5K-0003tz-M3; Thu, 09 Jul 2015 18:54:06 +0200 Date: Thu, 9 Jul 2015 18:54:06 +0200 From: hubert depesz lubaczewski To: ilies.ovidiu@googlemail.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #13496: INSERT WHERE NOT EXISTS error Message-ID: <20150709165406.GA13875@depesz.com> Reply-To: depesz@depesz.com References: <20150709155304.15752.16284@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20150709155304.15752.16284@wrigleys.postgresql.org> User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.3 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On Thu, Jul 09, 2015 at 03:53:04PM +0000, ilies.ovidiu@googlemail.com wrote: > The insert query is like: > INSERT INTO "website"."search_terms" SELECT 'someID', 'someSearchTerm' WHERE > NOT EXISTS ( SELECT 1 FROM "website"."search_terms" WHERE (("id" = 'someID') > OR ("term" = 'someSearchTerm')) LIMIT 1 OFFSET 0 ) > It appears this "upsert" is not safe in high concurency mode ~ 20 users / > second as I see in Google analytics for that period of time. Of course it's not safe. Whoever told you it's safe was lying. There is some time between select check, and the insertion. Hence - race condition. More information here: http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/ depesz -- The best thing about modern society is how easy it is to avoid contact with it. http://depesz.com/ -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs