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 1m51B2-0007sf-7T for pgsql-performance@arkaria.postgresql.org; Sun, 18 Jul 2021 07:25:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m51B0-0004oq-Ae for pgsql-performance@arkaria.postgresql.org; Sun, 18 Jul 2021 07:25:26 +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 1m51B0-0004oi-2P for pgsql-performance@lists.postgresql.org; Sun, 18 Jul 2021 07:25:26 +0000 Received: from mail2.code-sourcery.de ([138.201.66.53]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m51Av-0005JY-JY for pgsql-performance@postgresql.org; Sun, 18 Jul 2021 07:25:24 +0000 Received: from [192.168.188.22] (dynamic-077-003-037-239.77.3.pool.telefonica.de [77.3.37.239]) by mail2.code-sourcery.de (Postfix) with ESMTPSA id 4E0861FD5A; Sun, 18 Jul 2021 09:25:19 +0200 (CEST) Subject: Re: Linear slow-down while inserting into a table with an ON INSERT trigger ? To: David Rowley , Justin Pryzby Cc: pgsql-performance References: <62070f97-13e7-df73-bed1-f8e48c12bf2d@code-sourcery.de> <20210717044026.GA19498@telsasoft.com> From: Tobias Gierke Message-ID: <9f2df5fb-50a4-fefd-810a-d3b915717190@code-sourcery.de> Date: Sun, 18 Jul 2021 09:25:19 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Great idea ! This brought the time per INSERT into the parent table down to a consistent ~0.065ms again (compared to 0.05ms when completely removing the trigger, so penalty for the trigger is roughly ~20%). > 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 prune 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=CURRENT_TIMESTAMP WHERE > last_parent_table_change <> CURRENT_TIMESTAMP; > > That should reduce the number of actual updates to 1 per transaction. > > David