From andre@webkr.de Wed Jul 13 13:35:54 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBcX6-0004ng-TD for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 13:36:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBcX4-00074q-1l for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 13:36:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBcX3-00074g-Iu for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 13:36:01 +0000 Received: from mail.webkr.de ([78.47.173.34]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBcX0-0004zh-Ek for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 13:36:00 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id DCE5D1A124A for ; Wed, 13 Jul 2022 15:35:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=webkr.de; s=dkim; t=1657719356; h=from:subject:date:message-id:to:mime-version:content-type; bh=XNy9tf+UPJAuJtKNXb5/XiQ8GA7AifHiVcIhxuGooLw=; b=rx+3UbQ4P6Hfpuw9RnREVN077oWda9njVqLOLqk+TTr6rVjuVQAWiboGVL9O9wpzD23peL wkp7AiPkI9R+Aqx4qYMPGCY7C8g2g0DyBKCOk+PCqW3+2xWJ4XcMi0cRbw7gEd/4KqM/CQ 0pU9CrG2/JXHHgA2rtfIwM24xppMNwE= From: =?iso-8859-1?Q?Andr=E9_H=E4nsel?= To: Subject: Issues with upserts Date: Wed, 13 Jul 2022 15:35:54 +0200 Message-ID: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0323_01D896CE.3E1DCF40" Content-Language: de Thread-Index: AdiWvXL7GQ5LOKjBQ3KN9FR2Wa8P4A== X-Last-TLS-Session-Version: TLSv1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_0323_01D896CE.3E1DCF40 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit The wiki entry https://wiki.postgresql.org/wiki/UPSERT kind of suggests that with the introduction of ON CONFLICT DO UPDATE the problem of upserts is solved. But is it? A common use case for upserts is to keep a table up to date from an external data source. So you might have a cron job that runs a relatively large batch of upserts every couple of minutes. I have found that this use case is not adequately covered by ON CONFLICT DO UPDATE for two reasons: - New versions are created for all rows, even if the data is identical. This quickly fills up the WAL and puts unnecessary load on the tablespace drives. - If the conflict target is not the serial column, the sequence backing the serial column gets incremented for every row. This quickly reaches the point where the serial for new rows exceeds the range of an integer. Does this mean I have to SELECT the data first and do the conflict check in the application or is there a better SQL-only way? ------=_NextPart_000_0323_01D896CE.3E1DCF40 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable

The wiki entry https://wiki.postgresql.= org/wiki/UPSERT kind of suggests that with the introduction of ON = CONFLICT DO UPDATE the problem of upserts is solved. But is = it?

 

A common use case for upserts is to keep a table up to date = from an external data source. So you might have a cron job that runs a = relatively large batch of upserts every couple of = minutes.

 

I have found that this use case is not adequately covered = by ON CONFLICT DO UPDATE for two reasons:

- New versions are created for all = rows, even if the data is identical. This quickly fills up the WAL and = puts unnecessary load on the tablespace drives.

- If the conflict target is not the = serial column, the sequence backing the serial column gets incremented = for every row. This quickly reaches the point where the serial for new = rows exceeds the range of an integer.

 

Does this mean I have to SELECT the = data first and do the conflict check in the application or is there a = better SQL-only way?

------=_NextPart_000_0323_01D896CE.3E1DCF40-- From jeremy@musicsmith.net Wed Jul 13 13:48:28 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBcjP-0005JG-Ir for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 13:48:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBcjN-0004sQ-Tr for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 13:48:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBcjN-0004om-Ed for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 13:48:45 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oBcjJ-000582-IM for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 13:48:44 +0000 Received: by mail-ej1-x631.google.com with SMTP id l23so19957586ejr.5 for ; Wed, 13 Jul 2022 06:48:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=musicsmith-net.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=klbkAJpFsFWu70kqu6JtA3+Bk43omkwGGFzW8zSeKMs=; b=Ajkv23kFw1az/lIv4BC16kHfk4gNGlIRHblVnTTqf70v4dTCvSloQ8c/8N/n6lwx1V aJVlpv9D2W2sqzdB9fNQ85ioHHMLHgQ3x8GDrkrKYRD/UuBL++PeOvSKkIHjKoLS9KXy XTjpSyfkC8kudcabi1VnfI2m+40r87WOgmgRxYkVipkDCgsUgd4scXeYarANkURNo8NF n60mxeAJTh4DEZKDme7NskAI2GDT/mLfoYz66Qad90aGewqeShbGGcVFd8S9hfoLvECW MTPTSYKjM47cZV1lYVi55QoyoUbx60qBkjruYabWQHF+24b28ZMbYFwYJEKmjjH4EJ5X U7YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=klbkAJpFsFWu70kqu6JtA3+Bk43omkwGGFzW8zSeKMs=; b=PzdVH+cd6gAKAJIOmUzW8aIocLdxMtv0vx3VzPAr5WtKvR9a9rsJlEI5h28qg5naYV FmFPtoaOGkYrdZY+gh4By0B5XvU13tBqPZujtHvZko9YvE3eXeRyNGbnLxXVmlmZYgco LCO3BBrvwkZ7Pm2/90/clClnkPrhhNAABa5it7yGvHuCMiT2Tjdoy2iZStpL65PjzwKF WyeM3fp42OJSIkitp6i131a3MnZmSUkw0pFJwow7/Lll9BcWmqRl66h0M85h73PA/rCV 0FYyjovB1xE6bSNLafwSV1T5YgrRNph9FkbUOvv+Bia4YfsxKOFWzKp316Snnhti59V+ i+NA== X-Gm-Message-State: AJIora/mICqHgc3Rc9YIE02Th9JmKfILCp2kekEsullt0OaBG/YV+ZBZ vdQz2HQoTaUFHAR83Mx5MMN4kEm8XFl8ZAouMasq1w== X-Google-Smtp-Source: AGRyM1sMZW45nyqfY4J/MguAGyvXQl14xgcHdmCOHex02CcCZHZmFuPkjlO7D+lBpBNC6mbw3qHQSO5G6zcCQXMAzAA= X-Received: by 2002:a17:907:7ea1:b0:72b:83cf:4ce9 with SMTP id qb33-20020a1709077ea100b0072b83cf4ce9mr3601991ejc.204.1657720119815; Wed, 13 Jul 2022 06:48:39 -0700 (PDT) MIME-Version: 1.0 References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> In-Reply-To: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> From: Jeremy Smith Date: Wed, 13 Jul 2022 09:48:28 -0400 Message-ID: Subject: Re: Issues with upserts To: =?UTF-8?B?QW5kcsOpIEjDpG5zZWw=?= Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000e1097d05e3b00ac3" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e1097d05e3b00ac3 Content-Type: text/plain; charset="UTF-8" - New versions are created for all rows, even if the data is identical. > This quickly fills up the WAL and puts unnecessary load on the tablespace > drives. > > - If the conflict target is not the serial column, the sequence backing > the serial column gets incremented for every row. This quickly reaches the > point where the serial for new rows exceeds the range of an integer. > > > It sounds like you aren't adding a WHERE clause to prevent the duplicate rows from being updated. It would help if you could share your query, but in general this could look like this: INSERT INTO my_table (col1, col2) SELECT col1, col2 FROM other_table ON CONFLICT (col1, col2) DO UPDATE SET (col1, col2) = (EXCLUDED.col1, EXCLUDED.col2) WHERE (my_table.col1, my_table.col2) IS DISTINCT FROM (EXCLUDED.col1, EXCLUDED.col2); --000000000000e1097d05e3b00ac3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=

- New versio= ns are created for all rows, even if the data is identical. This quickly fi= lls up the WAL and puts unnecessary load on the tablespace drives.

- If the confli= ct target is not the serial column, the sequence backing the serial column = gets incremented for every row. This quickly reaches the point where the se= rial for new rows exceeds the range of an integer.

=

=C2=A0

<= /div>

It sounds like you aren't adding = a WHERE clause to prevent the duplicate rows from being updated.=C2=A0 It w= ould help if you could share your query, but in general this could look lik= e this:

INSERT INTO my_table (col1, col2)
SELECT col1, col2 FROM other_table
ON CONFLICT (col1, col2) DO = UPDATE SET (col1, col2) =3D (EXCLUDED.col1, EXCLUDED.col2)
WHERE = (my_table.col1, my_table.col2) IS DISTINCT FROM (EXCLUDED.col1, EXCLUDED.co= l2);
--000000000000e1097d05e3b00ac3-- From david.g.johnston@gmail.com Wed Jul 13 14:28:15 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdLh-000725-RP for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 14:28:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBdLg-0006U0-MF for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 14:28:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdLg-0006Tr-9o for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 14:28:20 +0000 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oBdLd-0005VG-M2 for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 14:28:19 +0000 Received: by mail-ej1-x62e.google.com with SMTP id dn9so20157783ejc.7 for ; Wed, 13 Jul 2022 07:28:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=oyis0GOtLadkGxWxY7wWFtn44/hcmbnIiTHgRsux6ww=; b=NMpLRFiylrDNjbSTZDaOLhNQFj3BCufTO84+zJtD7I+HYYcvCcVy9l+L/gS0zaJkqO wzfOw8h1BN/cGP/rWIEKjeEtqHhEkn9G/N35vx38wN1SVZc0FZwk7WjXdO5jEsDjkoK/ 8sSBbZ3JtGc4qsq25N8B8sLxi+0nXay2s4IgepnWxIHT5XDtORQW8/zfMl5NMK3vRRNY /z2q8vGV9ibzf/4jOE9+8mTIjJUsm+yX7BsNoOKDkDw/4xF3tIblQY7H7yj1RzqhUnDD 5jX0ba4FC/otRQBHJZ+KMTLpXOaX76pZa02pqPNPGi/QraDd+y41UkDBhZk4sGhi9ehW SxWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=oyis0GOtLadkGxWxY7wWFtn44/hcmbnIiTHgRsux6ww=; b=VVRKdgR/sgKPzHctaE0O1yUP/sY5v5SimEfmsiMFQOpOiaMmjqjK2R/s1rJE1ie5YW GCwLnmp9rnbOXcKZXAEo/oqa3yNkjrWmn5z/I8EYHCClyC8VVNpjoG7cnIxGHvIx6FCr I3xYC6YrrCXpurJBK41CCVFZgF1FIjAjGXFMrEnxcf6rJROkge8LnoP8UvDpUjl6UH// ut9W9ADIRwdoGYNQKNJg8QVa/lnjelEShK0RTpmhUZj2qUhC2yusAhuKveGwyUOadHAo xOqpn7XX82SMYHeMYK3fovhkEsUWu1zYFZyj0bVM17KB02Y+d51UmgNg5ysgDsN7EsBw XBfw== X-Gm-Message-State: AJIora9+zl1xE6w/kQhBYNHKbjnI/3AP5hFHViKG6SGWF8QuirwihZ/w 1nz1ls1Ftv2ER7Bfy5GYK2kWDFQTx4YwYyAT5QvXtsXx X-Google-Smtp-Source: AGRyM1vhpKXAOq/eAdCY6E/YSmGGw1bov+tnoiarUk4133I0tR4fEInqznPul0KfzoPN3AoUXJHtQuImtTnIbwv5cOU= X-Received: by 2002:a17:907:2722:b0:72b:6a93:bf9a with SMTP id d2-20020a170907272200b0072b6a93bf9amr3657036ejl.424.1657722496139; Wed, 13 Jul 2022 07:28:16 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a98:9a97:0:b0:178:d706:5910 with HTTP; Wed, 13 Jul 2022 07:28:15 -0700 (PDT) In-Reply-To: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> From: "David G. Johnston" Date: Wed, 13 Jul 2022 07:28:15 -0700 Message-ID: Subject: Re: Issues with upserts To: =?UTF-8?B?QW5kcsOpIEjDpG5zZWw=?= Cc: "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="00000000000084c3fb05e3b09889" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000084c3fb05e3b09889 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wednesday, July 13, 2022, Andr=C3=A9 H=C3=A4nsel wrote: > The wiki entry https://wiki.postgresql.org/wiki/UPSERT kind of suggests > that with the introduction of ON CONFLICT DO UPDATE the problem of upsert= s > is solved. But is it? > > > > A common use case for upserts is to keep a table up to date from an > external data source. So you might have a cron job that runs a relatively > large batch of upserts every couple of minutes. > > > > I have found that this use case is not adequately covered by ON CONFLICT > DO UPDATE for two reasons: > > - New versions are created for all rows, even if the data is identical. > This quickly fills up the WAL and puts unnecessary load on the tablespace > drives. > > - If the conflict target is not the serial column, the sequence backing > the serial column gets incremented for every row. This quickly reaches th= e > point where the serial for new rows exceeds the range of an integer. > > > > Does this mean I have to SELECT the data first and do the conflict check > in the application or is there a better SQL-only way? > > Well, first of all, don=E2=80=99t use serial/auto-increment in this situati= on. Second, you need to figure out what the unique key in the provided data is and key off of that for detecting duplicates. Ideally it already contains some bigint key column which then makes the first point easy to accept. David J. --00000000000084c3fb05e3b09889 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wednesday, July 13, 2022, Andr=C3=A9 H=C3=A4nsel <andre@webkr.de> wrote:

The wiki entry https://wiki.postgresql.org/wi= ki/UPSERT kind of suggests that with the introduction of ON CONFLICT DO= UPDATE the problem of upserts is solved. But is it?

=C2=A0

A common use case for upserts= is to keep a table up to date from an external data source. So you might h= ave a cron job that runs a relatively large batch of upserts every couple o= f minutes.

=C2=A0

I have found that this use case is not adequately covered by ON CONFLIC= T DO UPDATE for two reasons:

- New versions are created for all rows, even if the = data is identical. This quickly fills up the WAL and puts unnecessary load = on the tablespace drives.

- If the conflict target is not the serial column, the s= equence backing the serial column gets incremented for every row. This quic= kly reaches the point where the serial for new rows exceeds the range of an= integer.

=C2=A0

Does this mean I have to SELECT the data first and do the conflict check= in the application or is there a better SQL-only way?=


Well, first of all, don=E2=80=99t use serial/auto-increment in this situat= ion.

Second, you need to figure out what the uniqu= e key in the provided data is and key off of that for detecting duplicates.= =C2=A0 Ideally it already contains some bigint key column which then makes = the first point easy to accept.

David J.
=C2=A0 --00000000000084c3fb05e3b09889-- From andre@webkr.de Wed Jul 13 14:58:20 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdou-00084C-9Y for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 14:58:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBdos-0007Ga-Ui for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 14:58:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdos-0007G9-Eo for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 14:58:30 +0000 Received: from mail.webkr.de ([2a01:4f8:c0c:102::2]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdop-0005jN-6Y for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 14:58:29 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 2F1FB1A253C for ; Wed, 13 Jul 2022 16:58:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=webkr.de; s=dkim; t=1657724301; h=from:subject:date:message-id:to:mime-version:content-type:in-reply-to: references; bh=EIj/NcCrV034s407xrA1qFHxYKkmNdjlLJP3RAAr+/g=; b=bjloVQWQC1Jv7WoX/nLrgrGeSunkRVi3D+GT5yQOkU22sQn8QcHKV0fegJjNGpF+7Y9nrp slmqMNDvM/9Fd/dYF2mnG68HUOozcggb5e4NgjwFRKLgLeVyjudG5EEd/GOKXUS6d+aDeG kcjIsLUtRQopqrwTDkA9jI/m9S+BwzU= From: =?utf-8?Q?Andr=C3=A9_H=C3=A4nsel?= To: References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> In-Reply-To: Subject: RE: Issues with upserts Date: Wed, 13 Jul 2022 16:58:20 +0200 Message-ID: <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0348_01D896D9.C1B89A00" Content-Language: de Thread-Index: AQLfbQLY9jQY6k1tqGbaDz66p4yUhgGKEuG2q2Jm6nA= X-Last-TLS-Session-Version: TLSv1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_0348_01D896D9.C1B89A00 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Jeremy Smith wrote:=20 =20 It sounds like you aren't adding a WHERE clause to prevent the duplicate = rows from being updated. It would help if you could share your query, = but in general this could look like this: =20 INSERT INTO my_table (col1, col2) SELECT col1, col2 FROM other_table ON CONFLICT (col1, col2) DO UPDATE SET (col1, col2) =3D (EXCLUDED.col1, = EXCLUDED.col2) WHERE (my_table.col1, my_table.col2) IS DISTINCT FROM (EXCLUDED.col1, = EXCLUDED.col2)=20 =20 Here=E2=80=99s an example: =20 https://dbfiddle.uk/?rdbms=3Dpostgres_14 = &fiddle=3Db48d062d2eedbab14157359694b16081 =20 CREATE TABLE t ( id serial PRIMARY KEY, name text NOT NULL UNIQUE, address text NOT NULL ); =20 INSERT INTO t(name, address) VALUES ('foo', 'Baker street'),('bar', 'Miller street') ON CONFLICT (name) DO UPDATE SET address =3D EXCLUDED.address WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address); =20 INSERT INTO t(name, address) VALUES ('foo', 'Baker street'),('bar', 'Miller street') ON CONFLICT (name) DO UPDATE SET address =3D EXCLUDED.address WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address); =20 INSERT INTO t(name, address) VALUES ('foo', 'Baker street'),('bar', 'Miller street') ON CONFLICT (name) DO UPDATE SET address =3D EXCLUDED.address WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address); =20 INSERT INTO t(name, address) VALUES ('foo', 'Baker street'),('bar', 'Miller street') ON CONFLICT (name) DO UPDATE SET address =3D EXCLUDED.address WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address); =20 SELECT last_value FROM t_id_seq; =20 This will yield =E2=80=9C8=E2=80=9D, showing that new sequence numbers = have been generated for each attempt. ------=_NextPart_000_0348_01D896D9.C1B89A00 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Jeremy Smith = wrote:

 

It sounds like you aren't adding a = WHERE clause to prevent the duplicate rows from being updated.  It = would help if you could share your query, but in general this could look = like this:

 

INSERT INTO my_table (col1, col2)

SELECT col1, col2 FROM = other_table

ON CONFLICT (col1, col2) DO UPDATE SET (col1, col2) =3D = (EXCLUDED.col1, EXCLUDED.col2)

WHERE (my_table.col1, = my_table.col2) IS DISTINCT FROM (EXCLUDED.col1, = EXCLUDED.col2) 

<= p class=3DMsoNormal> 

Here=E2=80=99s an example:

 

https://dbfiddle.uk/?rdbms=3Dpostgres_14&fiddl= e=3Db48d062d2eedbab14157359694b16081

 

CREATE TABLE t (

=C2=A0=C2=A0=C2=A0 id serial PRIMARY KEY,

=C2=A0=C2=A0=C2=A0 name text NOT NULL UNIQUE,

=C2=A0=C2=A0=C2=A0 address text NOT NULL

);

 

INSERT INTO t(name, address)

VALUES ('foo', 'Baker street'),('bar', 'Miller = street')

ON CONFLICT (name) DO UPDATE SET address =3D = EXCLUDED.address

WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address);

 

INSERT INTO t(name, address)

VALUES ('foo', 'Baker street'),('bar', 'Miller = street')

ON CONFLICT (name) DO UPDATE SET address =3D = EXCLUDED.address

WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address);

 

INSERT INTO t(name, address)

VALUES ('foo', 'Baker street'),('bar', 'Miller = street')

ON CONFLICT (name) DO UPDATE SET address =3D = EXCLUDED.address

WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address);

 

INSERT INTO t(name, address)

VALUES ('foo', 'Baker street'),('bar', 'Miller = street')

ON CONFLICT (name) DO UPDATE SET address =3D = EXCLUDED.address

WHERE (t.name, t.address) IS DISTINCT FROM (EXCLUDED.name, = EXCLUDED.address);

 

SELECT last_value FROM t_id_seq;

 

This will yield =E2=80=9C8=E2=80=9D, showing that new sequence numbers = have been generated for each = attempt.

------=_NextPart_000_0348_01D896D9.C1B89A00-- From tgl@sss.pgh.pa.us Wed Jul 13 15:06:20 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdwX-0008Na-KR for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:06:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBdwW-0001VY-7L for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:06:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdwV-0001Uw-TH for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:06:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdwT-0005na-J8 for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:06:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 26DF6KII2230613; Wed, 13 Jul 2022 11:06:20 -0400 From: Tom Lane To: =?utf-8?Q?Andr=C3=A9_H=C3=A4nsel?= cc: pgsql-general@lists.postgresql.org Subject: Re: Issues with upserts In-reply-to: <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> Comments: In-reply-to =?utf-8?Q?Andr=C3=A9_H=C3=A4nsel?= message dated "Wed, 13 Jul 2022 16:58:20 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2230611.1657724780.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 13 Jul 2022 11:06:20 -0400 Message-ID: <2230612.1657724780@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk =3D?utf-8?Q?Andr=3DC3=3DA9_H=3DC3=3DA4nsel?=3D writes: > This will yield =E2=80=9C8=E2=80=9D, showing that new sequence numbers h= ave been generated for each attempt. Well, yeah, because the INSERT has to be attempted first, and that includes forming the whole candidate row including the nextval() result. If you're expecting a serial ID column to not have holes in the sequence of values, you're going to be sadly disappointed, whether you use ON CONFLICT UPDATE or not. regards, tom lane From david.g.johnston@gmail.com Wed Jul 13 15:06:46 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdxE-0008Ox-S5 for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:07:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBdxD-0005df-PY for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:07:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBdxD-0005dU-Dx for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:07:07 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oBdxB-0005nx-81 for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:07:06 +0000 Received: by mail-ej1-x62c.google.com with SMTP id va17so20441657ejb.0 for ; Wed, 13 Jul 2022 08:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=gYxE7SJT2L0dxiY7O798FuhZ+51qCcbZjlA5AvG6D1Q=; b=ZPYECVR9JdTYAesq8yWWStcX+v7DFoBy7dnG2ioJlrfYbVhla7B+AvOYC/DajkgjCc lTP+Z/2yakXIj2CucSWKUWfq5ksN6zQo8OBZW5FvHCEGwPMRLXH/GvOIMFTNgzyII/X8 N6YNIw4fJh+ElSjH6yiNZPyeTs5Kb3cKtS9xW71ZJuVIRjFUA9JFB+R8uMn5RyWGIISj 8I48AxiYNiKR0ovhxjloM4ScvdbHhlSaddcay4iT/DnloX6kATvR1FqphyozFRRNDvEd 7V0wgeiGKim+FqrdWJluAl9rhGg/QNk/CNkd4yDMhKoixAvESQunJc2d111cf937qeEU URCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gYxE7SJT2L0dxiY7O798FuhZ+51qCcbZjlA5AvG6D1Q=; b=kKXbNiyGJ2wj/ojYe8P9bqfanuc59DYzseW6KOnDOdsxR7z21eVRPE62Koajxbek2l lT8xF5gwS/zV4Gb4x4AUtHJag5OHJPjgli8aCP3CWtGnyjJ5t7v5Vxmu3sCD4buly7UV Nqi1CnNV7DzpOSju6lFREojb9eE7pD/AzS5dQGQa/m8rAmqYCxLFLckcmm3HuMMjg9He HD6hcheEDeKLXdT42XElyFFieOLRTgAHDqx3w/k/6smXVkv7f+U8TxH4V/WPOBFd3o3N X1+fVfVMT9Q+BetBYArg5KNdC8BTcuTWseKSGBueBLLVZQUeONtdKGmSuFBPN+br1rzj ntDA== X-Gm-Message-State: AJIora9na+N1+cNHXGxFeTMIr3itBGrlsQNaYiPmp4jMJ5B2nlMXgwUf Xv0scrPpEZmlYN6yBPbfQK81U9Hxek2nOtZx12I= X-Google-Smtp-Source: AGRyM1tdQ1IocmHz/uWV72DWEX9k3D1DuvjENwzlRqrvyxOeeZNlAsUFq9JC0rXx3ZSLF/KETTOdU3ci0r6G54ge9mw= X-Received: by 2002:a17:907:3e18:b0:722:be7e:302c with SMTP id hp24-20020a1709073e1800b00722be7e302cmr3722288ejc.437.1657724823742; Wed, 13 Jul 2022 08:07:03 -0700 (PDT) MIME-Version: 1.0 References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> In-Reply-To: <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> From: "David G. Johnston" Date: Wed, 13 Jul 2022 08:06:46 -0700 Message-ID: Subject: Re: Issues with upserts To: =?UTF-8?B?QW5kcsOpIEjDpG5zZWw=?= Cc: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000412e6f05e3b12374" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000412e6f05e3b12374 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jul 13, 2022 at 7:58 AM Andr=C3=A9 H=C3=A4nsel wro= te: > > SELECT last_value FROM t_id_seq; > > > > This will yield =E2=80=9C8=E2=80=9D, showing that new sequence numbers ha= ve been generated > for each attempt. > Yep, an entire able-to-be-inserted tuple is formed every time. That requires evaluating defaults so that every column has a value. David J. --000000000000412e6f05e3b12374 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Wed, Jul 13, 2022 at 7:58 AM Andr=C3=A9 H=C3=A4nsel <= ;andre@webkr.de> wrote:
=


= SELECT last_value FROM t_id_seq;

=C2=A0

This will yield =E2=80= =9C8=E2=80=9D, showing that new sequence numbers have been generated for ea= ch attempt.


<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ">Yep, an entire able-to-be-inserted tuple is formed every time.=C2=A0 That= requires evaluating defaults so that every column has a value.
David J.

--000000000000412e6f05e3b12374-- From adrian.klaver@aklaver.com Wed Jul 13 15:13:43 2022 Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBe3h-0000AS-Qg for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:13:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBe3g-0008KD-Mt for pgsql-general@arkaria.postgresql.org; Wed, 13 Jul 2022 15:13:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBe3f-0008Id-Nt for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:13:48 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBe3d-0005qd-96 for pgsql-general@lists.postgresql.org; Wed, 13 Jul 2022 15:13:46 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 9F4A35C00A2; Wed, 13 Jul 2022 11:13:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 13 Jul 2022 11:13:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1657725224; x= 1657811624; bh=qbZt9LukT4VvYKjCVbEJXcSFDxWL64Z08ChroPwHZko=; b=o w+SMhE94i5fvk3sCkfJDbS9hyN6GoNEaxotYnJTz2ektPtB2VHdrpO41jsqinpDS gfFcdX5kfdidFByw5eLoL6TlhUQsYlww5rKUKetqb5FAoI/bd/ut9I0szZwCrb9b +gWMBdZyBCkWH51356+XzXrPkKJlwAGaJj9nduUmY+9J3ugHamAExgGyq2ovNbyh 7zTHkkd4cplR+QMVFHMXU1Q9lC2Cn8btjORLWE1ntZ8WfovzrXZCaIEIL75nSobm +JdTRksb/EeuHH9UIIQssAgMN8sg9gf00J+PBRP0hQBj5Vlb4lTaFfXIUMyZ43Sx 0wgLfmG8OsEF7TXwRGCnw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1657725224; x=1657811624; bh=q bZt9LukT4VvYKjCVbEJXcSFDxWL64Z08ChroPwHZko=; b=cJyaMewMomBliLNnZ FSz3Rs0LhstEKzkRLxjd/d4goESW2u0PcZEsx9okrbG3Z4PDvaDURv6TfjSGmmYu Hu/i5c740eZqYTzVLikKmMX3JpAcBXYImcGcyxxqzD4frGrNRjiD/UX+U/jJI/po RKss9hO9P/gzhgSlCBFV/F07cUsH5enyYyo72v41NSbfFVVAoHpovG2Jpbg/C0An /OMJYpBDMnEwQCI96bwJSBJBgUTFRmPBKh8FqEVgXBY7ph4GuKwi6NJkyQej71l6 6VMftQ+nM0qDbMLkKoYStYIaOAm7A87AQGUWZgODJAS7ai9094gRnYB+ReRnC27Q XUtew== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudejjedgkeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpedugfeuleegveffhfegleejhefhtedtgeeileeggfeh fefffeevgeejtdeutefftdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 13 Jul 2022 11:13:43 -0400 (EDT) Message-ID: Date: Wed, 13 Jul 2022 08:13:43 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Issues with upserts Content-Language: en-US To: =?UTF-8?B?QW5kcsOpIEjDpG5zZWw=?= , pgsql-general@lists.postgresql.org References: <032201d896bd$7a948a10$6fbd9e30$@webkr.de> <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> From: Adrian Klaver In-Reply-To: <034701d896c8$fe2edfa0$fa8c9ee0$@webkr.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/13/22 07:58, André Hänsel wrote: > Jeremy Smith wrote: > CREATE TABLE t ( > >     id serial PRIMARY KEY, > >     name text NOT NULL UNIQUE, > >     address text NOT NULL > > ); > > This will yield “8”, showing that new sequence numbers have been > generated for each attempt. > If running out of id's is a concern use bigserial instead of serial as it uses bigint: bigint -9223372036854775808 to +9223372036854775807 vs integer for serial: integer -2147483648 to +2147483647 -- Adrian Klaver adrian.klaver@aklaver.com