Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eBmFW-0004Pl-Cw for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2017 18:35:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eBmFV-0004Q7-CR for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2017 18:35:53 +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_2) (envelope-from ) id 1eBmFT-0004Ox-3x for pgsql-hackers@postgresql.org; Mon, 06 Nov 2017 18:35:51 +0000 Received: from mail-pf0-x231.google.com ([2607:f8b0:400e:c00::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1eBmFL-0003p3-FD for pgsql-hackers@postgresql.org; Mon, 06 Nov 2017 18:35:49 +0000 Received: by mail-pf0-x231.google.com with SMTP id i5so8349941pfe.6 for ; Mon, 06 Nov 2017 10:35:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bowt-ie.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=mRqSZ3gcJakzA/UWkvIPwQpHIX3BVoLeto/N71ddgtw=; b=vI7xwQWppqiTYMVbSRER/8i/BC6gJW8dORCe+SQC8vdG8kOHXxoqpEnHltKMh0z6J9 G8WdIPDzxG4B1V00sd+FAoza12yH8/fEWxqz1glT47eLHFWnrjDdBZAKdjfDGWI092sS hTkNZgtkiT09A0SXG1TSQ+Xyw+VYGFcgqInOtLCXEfJoUksMWUdpYg55StDRbb83aWFi YKssv/D/5cMqoCX2bXvBilPgKh7TuVn1Etsif2Oqu2wd9r+Wxw4S4t/ejh6XDCM7eyhx XfgVbKAIq2A/KVqC3aIH3GD4fU/H/WwwjI2eDW+YGH1qO+3qiij9cZ4ZHyJz46I7dQc1 PuSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=mRqSZ3gcJakzA/UWkvIPwQpHIX3BVoLeto/N71ddgtw=; b=PyQsNJoIqcnWp8tTDzjJSyKAky00xvvUUDp3Zn4+sYmashekGs73YGgGywo/HpcX+0 xSrZkw9rEC7cXoFLjeLHFj3c6pHt8WBglEm+07Cm6UUwlIXFhq1TPygpysJhm59tzCLx eR8KIB8/2FUOTEnWpcyZG6w8oYaeQeWKGBSd00pSzQUo6JF7JYYBRuh+H6YrwFk4uWE6 7aXz1aBkZFr3QEfk2HpVJgUcdaeq3NiLiNeYfHTHkGGm+JGqDNFBeD5qm/5kjedpf1xA fsSR6lcbnKSH4FVJauTMWIAPOCXZkCZURbzt+r6QZIfoR0nN2z19eJQyEK69qoCdlD2n w9rg== X-Gm-Message-State: AMCzsaWmrhUtg48QEg8I0BubM2Oj43Mp/erZF3+rtA3RX+sROjthzOJS 8p6cWpH0Xbu8XySypYLXte0mCw== X-Google-Smtp-Source: ABhQp+Trfo7Tjb9XR3HdX8ObMD/37IoRZ4Ax+yudQyPgs5G1YQBg5hV+Mga4AsjhJGXQkYahlV/a6g== X-Received: by 10.101.99.199 with SMTP id n7mr16413231pgv.247.1509993340834; Mon, 06 Nov 2017 10:35:40 -0800 (PST) Received: from marmot ([162.217.72.7]) by smtp.gmail.com with ESMTPSA id t18sm26183996pfi.98.2017.11.06.10.35.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Nov 2017 10:35:39 -0800 (PST) Date: Mon, 6 Nov 2017 10:35:37 -0800 From: Peter Geoghegan To: Simon Riggs Cc: Robert Haas , Stephen Frost , Michael Paquier , PostgreSQL-development Subject: Re: MERGE SQL Statement for PG11 Message-ID: <20171106183537.GA22811@marmot> References: <20171030185511.GJ4628@tamriel.snowman.net> <20171103163539.GA17026@marmot> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org Simon Riggs wrote: >APPROACH1 >1. Join to produce results based upon snapshot at start of query >2. Apply results for INSERT, UPDATE or DELETE >Such failures are of great concern in practice because the time >between 1 and 2 could be very long for large statements, or for >smaller statements we might have sufficiently high concurrency to >allow us to see regular failures. I'm not sure that they're a *great* concern in a world with something that targets UPSERT use cases, which is a situation that does not exist in DBMSs with MERGE (with the notable exception of Teradata). But it's clearly a concern that users may expect to avoid duplicate violations in READ COMMITTED, since this caused confusion among users of other database systems with MERGE. >APPROACH2 (modified from my original proposal slightly) This write-up actually begins to confront the issues that I've raised. I'm glad to see this. >1. Join... >2. Apply results for UPDATE, if present not visible via the snapshot >taken at 1, do EPQ to ensure we locate current live tuple >3. If still not visible, do speculative insertion if we have a unique >index available, otherwise ERROR. If spec insertion fails, go to 2 > >The loop created above can live-lock, meaning that an infinite loop >could be created. The loop is *guaranteed* to live-lock once you "goto 2". So you might as well just throw an error at that point, which is the behavior that I've been arguing for all along! If this isn't guaranteed to live-lock at "goto 2", then it's not clear why. The outcome of step 2 is clearly going to be identical if you don't acquire a new MVCC snapshot, but you don't address that. You might have meant "apply an equivalent ON CONFLICT DO UPDATE", or something like that, despite the fact that the use of ON CONFLICT DO NOTHING was clearly implied by the "goto 2". I also see problems with that, but I'll wait for you to clarify what you meant before going into what they are. >In practice, such live-locks are rare and we could detect them by >falling out of the loop after a few tries. Approach2's purpose is to >alleviate errors in Approach1, so falling out of the loop merely takes >us back to the error we would have got if we didn't try, so Approach2 >has considerable benefit over Approach1. I don't hate the idea of retrying a fixed number of times for things like this, but I don't like it either. I'm going to assume that it's fine for now. >I read that step 3 in Approach2 is some kind of problem in MVCC >semantics. My understanding is that SQL Standard allows us to define >what the semantics of the statement are in relation to concurrency, so >any semantic issue can be handled by defining it to work the way we >want. My only concern is that our choices here should be good ones, based on practical considerations. We both more or less agree on how this should be assessed, I think; we just reach different conclusions. >As you point out, whichever we choose, we will be bound by those >semantics. So if we take Approach1, as has been indicated currently, >what is the written explanation for that, so we can show that to the >people who ask in the future about our decisions? Well, Approach1 is what other systems implement. I think that it would be important to point out that MERGE with Approach1 isn't special, but ON CONFLICT DO UPDATE is special. We'd also say that higher isolation levels will not have duplicate violations. -- Peter Geoghegan -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers