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 1kHfiW-0001vv-7x for pgsql-general@arkaria.postgresql.org; Mon, 14 Sep 2020 04:03:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kHfiU-0004b9-TX for pgsql-general@arkaria.postgresql.org; Mon, 14 Sep 2020 04:03:46 +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 1kHfiU-0004b1-GG for pgsql-general@lists.postgresql.org; Mon, 14 Sep 2020 04:03:46 +0000 Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kHfiO-0008S2-Sy for pgsql-general@postgresql.org; Mon, 14 Sep 2020 04:03:44 +0000 Received: by mail-ed1-x542.google.com with SMTP id w1so16166074edr.3 for ; Sun, 13 Sep 2020 21:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=GiIH1+iFHUEMcU2aQqU64WP94IIynRkTK1b/8vtNHHQ=; b=Yg/0y26ayutdkxMNT5WjIWLZtW7VEDe6yIGk9miQxXYBl2fTZwFzMw/u8E42H5NEIc DCYF8IKKmSbaFMO3D7CG023XZdbFmBTkJD8gtM/3nIG2j/C5udaY04tzfEPO/Qf5BdyN Gl4gro5S/b79rQTIGa4plktP83v/GQSaa7gcMSoC+hLZ7OjFQVEXO/p1OBb3AOJaNpOW TY0zTW5aCi6/SYId4cCvXrZ2UTFhzBSRiActiRlUG1TiCahfEL61L6yPfu3igPRFRyEY JFa8RvZ4rieVVVZSG9Uqqz8LXnIz+MMB7LCMaY73D/ANn67lWjNuLMbiEVGdq/hkEkLF dKOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=GiIH1+iFHUEMcU2aQqU64WP94IIynRkTK1b/8vtNHHQ=; b=GNn4w6/9hmovEwbaYQZCu+7XfTO9TeWhsFG46RcTH7ekzx4sDaq1QFrmBMSU/ux/Ia gxN1OT9bKpB4qOVOonsj5OtLvfBGTJ7YK4LQFLzSfGEZkXvmH23fjuYJtSJn5lKvz41u IvjNXpGfeNtpPBMvI1nCZWE4b1/1fQmJU/J5KI0sLQTqXPZSd6ijfzLcE4agxKVh5ehH JD4h/MPAZ7Fb882/foLiz5OcN5NhH1Q54oVXbI/D8G0nZGdFKZG0dBC9m0EU6qZPEKg7 2mPGzvuFxnkJCOfEgeGHrHTTaYquJ7Ves7gMPcj4vQfG7iUKoZzi9xA9eAEFYRz/xc01 5MhQ== X-Gm-Message-State: AOAM533O3le8jrvBwiRatoxWkyAwIAbEGbe+CyUbuybiN2FKr5BLLnsJ Vunq3nBvN/huLlMzy1S5h7yRB9ECi+vX6g== X-Google-Smtp-Source: ABdhPJyowp8VN+4GKiqM2My9zoF4LQgZX9FtRvb59KeWy9IVPoov/OkQPIQmIvLryt9cFTwpz5wxrA== X-Received: by 2002:aa7:d6ce:: with SMTP id x14mr15544363edr.359.1600056219867; Sun, 13 Sep 2020 21:03:39 -0700 (PDT) Received: from dynamic-ekztd8rbzae5v639t-pd01.res.v6.highway.a1.net (dynamic-ekztd8rbzae5v639t-pd01.res.v6.highway.a1.net. [2001:871:60:13a:4d55:86b5:f064:6911]) by smtp.gmail.com with ESMTPSA id dt15sm6686356ejb.80.2020.09.13.21.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Sep 2020 21:03:39 -0700 (PDT) Message-ID: Subject: Re: Dirty buffers with suppress_redundant_updates_trigger From: Laurenz Albe To: Mike Noordermeer , pgsql-general@postgresql.org Date: Mon, 14 Sep 2020 06:03:38 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, 2020-09-13 at 13:49 +0200, Mike Noordermeer wrote: > Hi, > > I am currently working on a data sync solution, where data is synced > from external systems to Postgres. As I do not know what data changed > in the source data store, I have opted for the following flow: > > - Load all data in a temp table > - Update existing rows main table based on contents of temp table > - Insert new rows main table > - Delete obsolete rows main table > > Obviously, I want to minimize WAL logging and data writes. Therefore I > have added suppress_redundant_updates_trigger to the main table. > > Nevertheless, I am still seeing pages being dirtied and written to the > WAL, even if no rows changed. Is there anyone that could explain why > this is happening? And does anyone know of better ways to minimize > writes in such a 'data load/refresh' scenario, where data is the same > in 99.9% of the cases? > > Small test case on Postgres 12.4 below, as you can see it dirties 8334 blocksk: > > testdatabase=# create table testtable (id int not null, name > varchar(32), primary key(id)); > CREATE TABLE > testdatabase=# create temp table testtable_temp (id int not null, name > varchar(32), primary key(id)); > CREATE TABLE > testdatabase=# insert into testtable (id, name) select i, md5(i::text) > from generate_series(1,1000000) s(i); > INSERT 0 1000000 > testdatabase=# insert into testtable_temp select * from testtable; > INSERT 0 1000000 > testdatabase=# create trigger z_min_update before update on testtable > for each row execute procedure suppress_redundant_updates_trigger(); > CREATE TRIGGER > testdatabase=# checkpoint; > CHECKPOINT > testdatabase=# explain (analyze, buffers) update testtable as d set > name = s.name from testtable_temp as s where d.id = s.id; > > QUERY PLAN > ---------------------------------------------------------------------------------------------------------------------------------------------- > Update on testtable d (cost=30315.56..69708.58 rows=591714 width=98) > (actual time=4168.901..4168.903 rows=0 loops=1) > Buffers: shared hit=1008337 dirtied=8334, local read=8334 > dirtied=8334 written=8331, temp read=11144 written=11144 > -> Hash Join (cost=30315.56..69708.58 rows=591714 width=98) > (actual time=486.771..1429.637 rows=1000000 loops=1) > Hash Cond: (d.id = s.id) > Buffers: shared hit=8337, local read=8334 dirtied=8334 > written=8331, temp read=11144 written=11144 > -> Seq Scan on testtable d (cost=0.00..18334.00 > rows=1000000 width=10) (actual time=0.026..157.729 rows=1000000 > loops=1) > Buffers: shared hit=8334 > -> Hash (cost=14251.14..14251.14 rows=591714 width=92) > (actual time=486.128..486.129 rows=1000000 loops=1) > Buckets: 32768 Batches: 32 Memory Usage: 2583kB > Buffers: local read=8334 dirtied=8334 written=8331, > temp written=7549 > -> Seq Scan on testtable_temp s (cost=0.00..14251.14 > rows=591714 width=92) (actual time=0.035..226.642 rows=1000000 > loops=1) > Buffers: local read=8334 dirtied=8334 written=8331 > Planning Time: 0.429 ms > Trigger z_min_update: time=57.069 calls=1000000 > Execution Time: 4174.785 ms > (15 rows) These are probably the "hint bits" set on newly committed rows by the first reader. Note that te blocks are dirtied during the sequential scan, not during the update. You could try VACUUMing the tables before the update (which will set hint bits) and see if you still get the dirtied blocks. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com