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--