From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: Efficient use of ON CONFLICT DO UPDATE with the PostgreSQL JDBC driver
Date: Fri, 13 Jan 2017 20:17:13 +0100
Message-ID: <o5b93k$6ps$1@blaine.gmane.org> (raw)
In-Reply-To: <CY1PR0201MB18972094A178B4D7295BB76CFF780@CY1PR0201MB1897.namprd02.prod.outlook.com>
References: <CAHL_zcOaWb2wuTX0eVnVXie0LhDjdZgvX0xR1PZ1GX2sM-8Ovw@mail.gmail.com>
<CY1PR0201MB18972094A178B4D7295BB76CFF780@CY1PR0201MB1897.namprd02.prod.outlook.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: spam_eater@gmx.net
Subject: Re: Efficient use of ON CONFLICT DO UPDATE with the PostgreSQL JDBC driver
In-Reply-To: <o5b93k$6ps$1@blaine.gmane.org>
References: <CAHL_zcOaWb2wuTX0eVnVXie0LhDjdZgvX0xR1PZ1GX2sM-8Ovw@mail.gmail.com> <CY1PR0201MB18972094A178B4D7295BB76CFF780@CY1PR0201MB1897.namprd02.prod.outlook.com> <o5b93k$6ps$1@blaine.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox