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 1m51Lr-0008FL-AD for pgsql-performance@arkaria.postgresql.org; Sun, 18 Jul 2021 07:36:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m51Lp-0001SH-Tt for pgsql-performance@arkaria.postgresql.org; Sun, 18 Jul 2021 07:36:37 +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 1m51Lp-0001S8-LJ for pgsql-performance@lists.postgresql.org; Sun, 18 Jul 2021 07:36:37 +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 1m51Ln-0005O4-Ik for pgsql-performance@postgresql.org; Sun, 18 Jul 2021 07:36:36 +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 344E21FD61; Sun, 18 Jul 2021 09:36:34 +0200 (CEST) Subject: Re: Linear slow-down while inserting into a table with an ON INSERT trigger ? To: Justin Pryzby Cc: pgsql-performance@postgresql.org References: <62070f97-13e7-df73-bed1-f8e48c12bf2d@code-sourcery.de> <20210717044026.GA19498@telsasoft.com> From: Tobias Gierke Message-ID: Date: Sun, 18 Jul 2021 09:36:33 +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: <20210717044026.GA19498@telsasoft.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you for the detailed explanation ! Just one more question: I've did an experiment and reduced the fillfactor on the table updated by the trigger to 50%, hopingĀ  the HOT feature would kick in and each subsequent INSERT would clean up the "HOT chain" of the previous INSERT ... but execution times did not change at all compared to 100% fillfactor, why is this ? Does the HOT feature only work if a different backend accesses the table concurrently ? Thanks, Tobias > On Fri, Jul 16, 2021 at 11:27:24PM +0200, Tobias Gierke wrote: >> CREATE OR REPLACE FUNCTION parent_table_changed() RETURNS trigger LANGUAGE plpgsql >> AS $function$ >> BEGIN >> UPDATE data_sync SET last_parent_table_change=CURRENT_TIMESTAMP; >> RETURN NEW; >> END; >> $function$ >> >> I'm trying to insert 30k rows (inside a single transaction) into the parent > The problem is because you're doing 30k updates of data_sync within a txn. > Ideally it starts with 1 tuple in 1 page but every row updated requires > scanning the previous N rows, which haven't been vacuumed (and cannot). > Update is essentially delete+insert, and the table will grow with each update > until the txn ends and it's vacuumed. > > pages: 176 removed, 1 remain, 0 skipped due to pins, 0 skipped frozen > tuples: 40000 removed, 1 remain, 0 are dead but not yet removable, oldest xmin: 2027 > > 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. >