Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eAMBp-00031G-5S for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Nov 2017 20:34:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eAMBo-0007Ty-Jq for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Nov 2017 20:34:12 +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 1eAMAG-0004gH-VQ for pgsql-hackers@postgresql.org; Thu, 02 Nov 2017 20:32:37 +0000 Received: from sub4.mail.dreamhost.com ([69.163.253.135] helo=homiemail-a55.g.dreamhost.com) by magus.postgresql.org with esmtps (TLS1.1:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1eAMAA-0001X5-Km for pgsql-hackers@postgresql.org; Thu, 02 Nov 2017 20:32:36 +0000 Received: from homiemail-a55.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a55.g.dreamhost.com (Postfix) with ESMTP id 68A7D68003C1B; Thu, 2 Nov 2017 13:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=cryptonector.com; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=cryptonector.com; bh=MsNicL6lJI6+y4 jwMxTEQMVuNnc=; b=SMIaBvr7Rlxqy9sisw12R4WDzJGeUbFPNGs2RfK2WCKn74 +h6SSjFszrI58OBBmPbemKJyuZVnX/rtqg81f9Q0QbgZbrbaJ1ghZEZnudObbkPI EXpHDCKapUB0MRrr7DsnmhxSQBabAxHI5oa5+Ejo1WkulhHLdO7TMPlgGsMe8= Received: from localhost (cpe-70-123-158-140.austin.res.rr.com [70.123.158.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nico@cryptonector.com) by homiemail-a55.g.dreamhost.com (Postfix) with ESMTPSA id 043E468003C1A; Thu, 2 Nov 2017 13:32:27 -0700 (PDT) Date: Thu, 2 Nov 2017 15:32:24 -0500 From: Nico Williams To: Peter Geoghegan Cc: Simon Riggs , Robert Haas , Michael Paquier , PostgreSQL-development Subject: Re: MERGE SQL Statement for PG11 Message-ID: <20171102203223.GT4496@localhost> References: <20171101182032.GA7104@marmot> <20171102191612.GR4496@localhost> <20171102195145.GB27644@marmot> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171102195145.GB27644@marmot> 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 Thu, Nov 02, 2017 at 12:51:45PM -0700, Peter Geoghegan wrote: > Nico Williams wrote: > >If you want to ignore conflicts arising from concurrency you could > >always add an ON CONFLICT DO NOTHING to the INSERT DML in the mapping I > >proposed earlier. Thus a MERGE CONCURRENTLY could just do that. > > > >Is there any reason not to map MERGE as I proposed? > > Performance, for one. MERGE generally has a join that can be optimized > like an UPDATE FROM join. Ah, right, I think my mapping was pessimal. How about this mapping instead then: WITH updated AS ( UPDATE SET ... WHERE RETURNING ) , inserted AS ( INSERT INTO SELECT ... WHERE NOT IN (SELECT FROM updated) AND .. /* * Add ON CONFLICT DO NOTHING here to avoid conflicts in the face * of concurrency. */ RETURNING ) DELETE FROM WHERE NOT IN (SELECT FROM updated) AND NOT IN (SELECT FROM inserted) AND ...; ? If a MERGE has no delete clause, then the mapping would be: WITH updated AS ( UPDATE SET ... WHERE RETURNING ) INSERT INTO SELECT ... WHERE NOT IN (SELECT FROM updated) AND .. /* * Add ON CONFLICT DO NOTHING here to avoid conflicts in the face * of concurrency. */ ; > I haven't studied this question in any detail, but FWIW I think that > using CTEs for merging is morally equivalent to a traditional MERGE > implementation. [...] I agree. So why not do that initially? Optimize later. Such a MERGE mapping could be implemented entirely within src/backend/parser/gram.y ... Talk about cheap to implement, review, and maintain! Also, this would be notionally very simple. Any optimizations to CTE query/DML execution would be generic and applicable to MERGE and other things besides. If mapping MERGE to CTE-using DMLs motivates such optimizations, all the better. > [...]. It may actually be possible to map from CTEs to a MERGE > statement, but I don't think that that's a good approach to implementing > MERGE. Surely not every DML with CTEs can map to MERGE. Maybe I misunderstood your comment? > Most of the implementation time will probably be spent doing things like > making sure MERGE behaves appropriately with triggers, RLS, updatable > views, and so on. That will take quite a while, but isn't particularly > technically challenging IMV. Note that mapping to a DML with CTEs as above gets triggers, RLS, and updateable views right from the get-go, because DMLs with CTEs, and DMLs as CTEs, surely do as well. Nico -- -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers