Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eANw0-0007Q6-Ka for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Nov 2017 22:26:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eANvz-0003re-LU for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Nov 2017 22:25:59 +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 1eANvw-0003pY-Tr for pgsql-hackers@postgresql.org; Thu, 02 Nov 2017 22:25:57 +0000 Received: from mail-pf0-x233.google.com ([2607:f8b0:400e:c00::233]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1eANvt-0002lD-H7 for pgsql-hackers@postgresql.org; Thu, 02 Nov 2017 22:25:55 +0000 Received: by mail-pf0-x233.google.com with SMTP id b79so735913pfk.5 for ; Thu, 02 Nov 2017 15:25:52 -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=oGBXb2wu7a8LL0OQuROhGUiXV5yAO/3MOcFiFKmQI2M=; b=OTUAeR4tXbuu+fwG3BPiClV7YGHVSlaLFHpHjvBz1MDUt/xShYMiNvUXC43ma2jbcg ECGOvP+k6djO9xqiuLe9x77Q3OEqPuyV2wpfL4ZisunJYfUTMq9sW+UB660Kr/MSCVQs b/SXt7Q+9/Z1TtWEB7W8KqfiJWKbbm0PHolHOm5b1LWuIDiUt6iIQIUOpKXxhMIOtXLT vNmFj/i1EAugERgwQgKY1UIM/FVaOA0Gh6kbBm/ZnTNvsjRlJu6vKgeDrWtJ6JWz+ilq o4349ryVj6+jdN0G5FOzoXTsav7+oBOm62uAbMnd/sz7pf5CDt3iwZ7wAiNq9H34vPWj vMPQ== 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=oGBXb2wu7a8LL0OQuROhGUiXV5yAO/3MOcFiFKmQI2M=; b=Zd6/AEbQD1rjvugs5fiam5ha0qcbUJEKiVsfxdUL+MSaoYfCxL8JXX5h3IS9iC9ape zLj0pOv8TdszWQK+0VZ0+EG4qnF9DVG7Bd1IPniFasl+nZwFNv1sQe8v8QpkGuDX4zlh DCDZkuXofdv/JyxNzyGemHRKDHl+aiYIwtJy/xEInZI3ml3TwfUn7AeIwwnhR9WwO4GA uMfeKWzcDda3ih4skP2O7HbWvE96fl1Cp7GQKfEUkvPFPCowFLGwOS5YOVeYRHrFDbi1 5/fwrYpnx/4i4SQey9Uoam/z2//SW91z0M2L+dmr5xFRiCY6w2XUpRWQ8zBTfHNynXL6 bpPA== X-Gm-Message-State: AMCzsaUN8HlPoi73NZtTrJQIQtJQVVt0WfU27AzvHbs13wxbRgsaLMn/ u1yo0X2U1ivPoGvEwYFANCipFA== X-Google-Smtp-Source: ABhQp+StWDbeN/9+RLISIzpbvhPLITDzMvYHwGhxX4ZNFOrnEdMFDrhXuv45dV82ieN9L14tDxa1bg== X-Received: by 10.99.123.14 with SMTP id w14mr4934093pgc.387.1509661551931; Thu, 02 Nov 2017 15:25:51 -0700 (PDT) Received: from marmot ([162.217.72.7]) by smtp.gmail.com with ESMTPSA id r78sm9172328pfb.29.2017.11.02.15.25.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Nov 2017 15:25:51 -0700 (PDT) Date: Thu, 2 Nov 2017 15:25:48 -0700 From: Peter Geoghegan To: Nico Williams Cc: Simon Riggs , Robert Haas , Michael Paquier , PostgreSQL-development Subject: Re: MERGE SQL Statement for PG11 Message-ID: <20171102222548.GA3581@marmot> References: <20171101182032.GA7104@marmot> <20171102191636.GA27644@marmot> <20171102210519.GA31574@marmot> <20171102220034.GV4496@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20171102220034.GV4496@localhost> 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 Nico Williams wrote: >A MERGE mapped to a DML like this: > > WITH > updated AS ( > UPDATE > SET ... > WHERE > RETURNING > ) > , inserted AS ( > INSERT INTO > SELECT ... > WHERE NOT IN (SELECT FROM updated) AND .. > ON CONFLICT DO NOTHING -- see below! > RETURNING > ) > DELETE FROM > WHERE NOT IN (SELECT FROM updated) AND > NOT IN (SELECT FROM inserted) AND ...; > This is a bad idea. An implementation like this is not at all maintainable. >can handle concurrency via ON CONFLICT DO NOTHING in the INSERT CTE. That's not handling concurrency -- it's silently ignoring an error. Who is to say that the conflict that IGNORE ignored is associated with a row visible to the MVCC snapshot of the statement? IOW, why should the DELETE affect any row? There are probably a great many reasons why you need a ModifyTable executor node that keeps around state, and explicitly indicates that a MERGE is a MERGE. For example, we'll probably want statement level triggers to execute in a fixed order, regardless of the MERGE, RLS will probably require explicitly knowledge of MERGE semantics, and so on. FWIW, your example doesn't actually have a source (just a target), so it isn't actually like MERGE. -- 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