Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9xd5-0003c7-H7 for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Nov 2017 18:20:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e9xd5-00069l-0c for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Nov 2017 18:20:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e9xd2-00068k-QU for pgsql-hackers@postgresql.org; Wed, 01 Nov 2017 18:20:41 +0000 Received: from mail-pg0-x234.google.com ([2607:f8b0:400e:c05::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e9xcz-0000Gf-78 for pgsql-hackers@postgresql.org; Wed, 01 Nov 2017 18:20:39 +0000 Received: by mail-pg0-x234.google.com with SMTP id r25so2786963pgn.4 for ; Wed, 01 Nov 2017 11:20:36 -0700 (PDT) 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=mVoHveCqDuvaoXjmSpGgK0wKM/QZBRxqonqZCNsiM00=; b=A6M5YCst74CYIyuB6rfwL4b5gSr+zWnrfqQocCSvdrhka85MOqdOO2niRTnX873ZBS hXJQHiFDDgnZWZdTpS0GFpyHGvseWj8CdmBX4+l0BCbso93GN15YkXp1lilsqeYh8710 QXqkMEoJS/Bih94U3Gb5msSAzKrRXcdBYJJNN5CiHNMs0kTTkyJEYjpMiqHCGMApSD2E qo6WDifQ6oU7QGKL43vMd5HyGAwp/Juid4VI+08LWVv2ASqjpChq6YIQD8wOA0THASl9 nRZgpjXzkj7I6pN5qb1UQ0c8bkftOejEvxCLVMvYTXpQht2gnbqvVuhpztuLw+H60n2K +RIg== 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=mVoHveCqDuvaoXjmSpGgK0wKM/QZBRxqonqZCNsiM00=; b=ja2Vw7+hOs52ewbs5ZLMc81gZ2uI3L9cXZcWlsrGr3EuukwXx0UoaQ3Yl5oQxs55gz 4Gkzd4erLDPuDyasLYHnfnlXJZ7zZa/zIhLGdWDKjhcIdY0iMfeFCwYMAJHZMMugftrg r+vhJIV2/Np+lbNkUnS8hxcGXQgHsGesjDzZheAJ+0WIUJsYrvHgwYQ+qhrSdppMFkQl SbtMgLQPL+ItOLLNjGy3hJMnhAVzK7gD7n7W3HEqYJm5USFwUmD2LO1FHn0CGFCmdn1o 3fziB8O+iaJNmrWbWsMu4f4BrQvfjT+XWYkU0BA86yxGDAfNJuCxM29vKpaFgvqMkL+q U6+Q== X-Gm-Message-State: AMCzsaUHPAhhW1GgK2PNUsWhd+eKYAloNmbMhzl+zug+HrR8Xltr7Zdw 1W9Do8sJQZqOL8Y5tjG6a0BF+Q86RFM= X-Google-Smtp-Source: ABhQp+QMNVtCpoQjoRhdfzUrjVdvxrZK9alx7m0rxV9E0DJabv9ZLFU5e/oE6HI9r+xaF7AbiaZqYA== X-Received: by 10.99.179.5 with SMTP id i5mr688454pgf.199.1509560435146; Wed, 01 Nov 2017 11:20:35 -0700 (PDT) Received: from marmot ([162.217.72.7]) by smtp.gmail.com with ESMTPSA id a70sm2316641pfg.176.2017.11.01.11.20.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Nov 2017 11:20:34 -0700 (PDT) Date: Wed, 1 Nov 2017 11:20:32 -0700 From: Peter Geoghegan To: Simon Riggs Cc: Robert Haas , Michael Paquier , PostgreSQL-development Subject: Re: MERGE SQL Statement for PG11 Message-ID: <20171101182032.GA7104@marmot> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Received: by 10.157.29.66 with HTTP; Wed, 1 Nov 2017 10:47:03 -0700 (PDT) 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 On Wed, Nov 1, 2017 at 10:19 AM, Simon Riggs wrote: >> The problem here is: Iff the first statement uses ON CONFLICT >> infrastructure, doesn't the absence of WHEN NOT MATCHED imply >> different semantics for the remaining updates and deletes in the >> second version of the query? > > Not according to the SQL Standard, no. I have no plans for such > differences to exist. > > Spec says: If we hit HeapTupleSelfUpdated then we throw an ERROR. Your documentation said that the MERGE was driven by a speculative insertion (BTW, I don't think that this internal implementation detail should be referenced in user-facing docs). I inferred that that could not always be true, since there won't always be an INSERT/WHEN NOT MATCHED case, assuming that you allow that at all (I now gather that you will). >> You've removed what seems like a neat >> adjunct to the MERGE, but it actually changes everything else too when >> using READ COMMITTED. Isn't that pretty surprising? > > I think you're presuming things I haven't said and don't mean, so > we're both surprised. You're right -- I'm surmising what I think might be true, because I don't have the information available to know one way or the other. As far as this issue with using speculative insertions in one context but not in another goes, I still don't really know where you stand. I can still only surmise that you must want both implementations, and will use one or the other as circumstances dictate (to avoid dup violations in the style of ON CONFLICT where that's possible). This seems true because you now say that it will be possible to omit WHEN NOT MATCHED, and yet there is no such thing as a speculative insertion without the insertion. You haven't said that that conclusion is true yourself, but it's the only conclusion that I can draw based on what you have said. > I think we need some way of expressing the problems clearly. It's certainly hard to talk about these problems. I know this from experience. > "a general purpose solution is one that more or > less works like an UPDATE FROM, with an outer join, whose ModifyTable > node is capable of insert, update, or delete (and accepts quals for > MATCHED and NOT matched cases, etc). You could still get duplicate > violations due to concurrent activity in READ COMMITTED mode". > > Surely the whole point of this is to avoid duplicate violations due to > concurrent activity? Now we're getting somewhere. I *don't* think that that's the whole point of MERGE. No other MERGE implementation does that, or claims to do that. The SQL standard says nothing about this. Heikki found this to be acceptable when working on the GSoC MERGE implementation that went nowhere. My position is that we ought to let MERGE be MERGE, and let ON CONFLICT be ON CONFLICT. In Postgres, you can avoid duplicate violations with MERGE by using a higher isolation level (these days, those are turned into a serialization error at higher isolation levels when no duplicate is visible to the xact's snapshot). MERGE isn't and shouldn't be special when it comes to concurrency. > I'm not seeing how either design sketch rigorously avoids live locks, > but those are fairly unlikely and easy to detect and abort. My MERGE semantics (which really are not mine at all) avoid live lock/lock starvation by simply never retrying anything without making forward progress. MERGE doesn't take any special interest in concurrency, just like any other DML statement that isn't INSERT with ON CONFLICT. ON CONFLICT would have live locks if it didn't always have the choice of inserting [1]. In many ways, the syntax of INSERT ON CONFLICT DO UPDATE is restricted in exactly the way it needs to be in order to function correctly. It wasn't an accident that it didn't end up being UPDATE ... ON NOUPDATE DO INSERT, or something like that, which Robert proposed at one point. ON CONFLICT plays by its own rules to a certain extent, because that's what you need in order to get the desired guarantees in READ COMMITTED mode [2]. This is the main reason why it was as painful a project as it was. Further generalizing that seems fraught with difficulties. It seems logically impossible to generalize it in a way where you don't end up with two behaviors masquerading as one. [1] https://wiki.postgresql.org/wiki/UPSERT#Theoretical_lock_starvation_hazards [2] https://wiki.postgresql.org/wiki/UPSERT#Goals_for_implementation -- 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