Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cS7MV-0008DR-85 for pgsql-jdbc@arkaria.postgresql.org; Fri, 13 Jan 2017 19:18:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cS7MU-0008RM-R4 for pgsql-jdbc@arkaria.postgresql.org; Fri, 13 Jan 2017 19:18:06 +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 1cS7M1-0007uB-0I for pgsql-jdbc@postgresql.org; Fri, 13 Jan 2017 19:17:37 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cS7Lx-0005Un-33 for pgsql-jdbc@postgresql.org; Fri, 13 Jan 2017 19:17:36 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1cS7Ll-0004Gk-VZ for pgsql-jdbc@postgresql.org; Fri, 13 Jan 2017 20:17:21 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: Efficient use of ON CONFLICT DO UPDATE with the PostgreSQL JDBC driver Date: Fri, 13 Jan 2017 20:17:13 +0100 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Brad DeJong schrieb am 13.01.2017 um 18:18: > I find it easiest to make the intention explicit like this. This is > also a more conventient syntax for handling multiple rows because you > just extend the "values (?, ?, ?)" to values "(?, ?, ?), (?, ?, ?), > (?, ?, ?)". > > with params (c_uuid, file_data, file_name) as (values (?, ?, ?)) > insert into foo (c_uuid, file_data, file_name) > select c_uuid, file_data, file_name from params > on conflict (c_uuid) do update set (file_data, file_name) = (select file_data, file_name from params) > > > I ran this statement in 9.6 through pgAdmin 4 in order to verify the > syntax (with the ?, ?, ? replaced with literals - I have not yet > figured out how to bind parameters to parameterized statements in > pgAdmin 4) and the messages were ... That is overly complicated because you can do a simple: INSERT INTO foo (c_uuid, file_data, file_name) VALUES (?, ?, ?) ON CONFLICT (c_uuid) DO UPDATE SET file_data = excluded.file_data, file_name = excluded.file_name; Plus: using EXCLUDED means this will also work for multi-row inserts which your workaround does not: INSERT INTO foo (c_uuid, file_data, file_name) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?) ON CONFLICT (c_uuid) DO UPDATE SET file_data = excluded.file_data, file_name = excluded.file_name; -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc