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 1m4c7y-0007fY-Ar for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jul 2021 04:40:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m4c7w-0004bU-Dg for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jul 2021 04:40:36 +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 1m4c7v-0004bK-PD for pgsql-performance@lists.postgresql.org; Sat, 17 Jul 2021 04:40:35 +0000 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m4c7p-0001tw-HC for pgsql-performance@postgresql.org; Sat, 17 Jul 2021 04:40:33 +0000 Received: by mail-il1-x131.google.com with SMTP id r16so10241431ilt.11 for ; Fri, 16 Jul 2021 21:40:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=mw1kwRRLgDr8e562jXna1+1grF6kSzp+DQJDDarkGCw=; b=TcNpf5oiyCLUllAxHbIT9HWmrSfImmAj+7DLxVWstDXf6RPMW3um2GUFgKH2G59p/6 hbO4DeeF/bvrZ1Ck4R/CIqSQf9QcaK1whzWiYRk6kOV8ew4FIMaOx9JKgNQrsWy7ujZE gZmcENwUiyobMqo3EyUsUnNX0WVIBa12zys74lsC8oDsm+geDY5d2L/ZOfMiPrl0FxDL BMOoC4G3sT7SDmvNXZnTXQQzJwZhZ/RffH7T4lDIKM1gteQNBJ3qpKedEO/MqsJWahMw EyzZmZYAYUJfSvF0tScxHiXIxgMkM8euwBaApQONM0Nq/xuwEDj3rmdNTwwaWf2mNWy6 n7CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=mw1kwRRLgDr8e562jXna1+1grF6kSzp+DQJDDarkGCw=; b=SMTq9PqBUAUOsf4pqoYqllPcAPzCOgrR8D9NHvZvmgH7HQ0Nl8cGiT25NbkcQuQv4K jXCS8gPgXfEIWMwAt9Bv8Xsii05eqoPOpw1Th0vJFhRBowQJsYSEntD4IaGszlXLGFUy 9GQ7SVGI3DnQkV2ey05HedH3Tj634pVjJU2UsVYIEoPm9KgmeKoNlZXXYf0oDJ8z1VT+ RDquFtymJjY4UIZLQ2/vaOMQYTpX0Uszs2qoEcBcl8MtbdThjMln6tto5sCR9soeLhJW 1VT6x4kxDHoV0rQ3u/eUjXLHBu/SsfmDuiVMZ6Oz5T1+C16A/iqeYtKBYhikMlYrPZgL ZxBg== X-Gm-Message-State: AOAM533uEWwHBYcL+Til95PuMvCkL3yn2fjI/HRdMv0/k7ugb06krqnJ 7Y5ljFkojVxfUJXD9tpn2cSmVECQROQ0ww== X-Google-Smtp-Source: ABdhPJxyieKq3vyWxYX7NjCVVKLMW3XJ8lUixUYWkW7sxIOnEwtT2eENNhjjYFag2XS8un64kAhNKw== X-Received: by 2002:a05:6e02:66e:: with SMTP id l14mr8691349ilt.211.1626496828610; Fri, 16 Jul 2021 21:40:28 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id n5sm6151639ilo.78.2021.07.16.21.40.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Jul 2021 21:40:28 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 160E5800A3C; Fri, 16 Jul 2021 23:40:27 -0500 (CDT) Date: Fri, 16 Jul 2021 23:40:26 -0500 From: Justin Pryzby To: Tobias Gierke Cc: pgsql-performance@postgresql.org Subject: Re: Linear slow-down while inserting into a table with an ON INSERT trigger ? Message-ID: <20210717044026.GA19498@telsasoft.com> References: <62070f97-13e7-df73-bed1-f8e48c12bf2d@code-sourcery.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <62070f97-13e7-df73-bed1-f8e48c12bf2d@code-sourcery.de> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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. -- Justin