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 1m4lNk-0004EK-R6 for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jul 2021 14:33: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 1m4lNi-0005bI-Kx for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jul 2021 14:33: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 1m4lNi-0005bA-Ck for pgsql-performance@lists.postgresql.org; Sat, 17 Jul 2021 14:33:30 +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 1m4lNc-0006FP-2u for pgsql-performance@postgresql.org; Sat, 17 Jul 2021 14:33:29 +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 16HEX94p062963; Sat, 17 Jul 2021 10:33:09 -0400 From: Tom Lane To: David Rowley cc: Justin Pryzby , Tobias Gierke , pgsql-performance Subject: Re: Linear slow-down while inserting into a table with an ON INSERT trigger ? In-reply-to: References: <62070f97-13e7-df73-bed1-f8e48c12bf2d@code-sourcery.de> <20210717044026.GA19498@telsasoft.com> Comments: In-reply-to David Rowley message dated "Sat, 17 Jul 2021 19:02:31 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <62961.1626532389.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 17 Jul 2021 10:33:09 -0400 Message-ID: <62962.1626532389@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Sat, 17 Jul 2021 at 16:40, Justin Pryzby wrote= : >> You could run a single UPDATE rather than 30k triggers. >> Or switch to an INSERT on the table, with an index on it, and call >> max(last_parent_table_change) from whatever needs to ingest it. And pr= une the >> old entries and vacuum it outside the transaction. Maybe someone else = will >> have a better suggestion. > Maybe just change the UPDATE statement to: > UPDATE data_sync SET last_parent_table_change=3DCURRENT_TIMESTAMP WHERE > last_parent_table_change <> CURRENT_TIMESTAMP; > That should reduce the number of actual updates to 1 per transaction. Or, if it's impractical to make the application do that for itself, this could be a job for suppress_redundant_updates_trigger(). https://www.postgresql.org/docs/current/functions-trigger.html regards, tom lane