Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tnE2E-003Jwa-5n for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Feb 2025 09:48:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tnE2C-002HDo-Sp for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Feb 2025 09:48:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tnE2C-002HDg-GG for pgsql-hackers@lists.postgresql.org; Wed, 26 Feb 2025 09:48:56 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tnE29-000AyT-1Y for pgsql-hackers@lists.postgresql.org; Wed, 26 Feb 2025 09:48:56 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-439846bc7eeso41758325e9.3 for ; Wed, 26 Feb 2025 01:48:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740563333; x=1741168133; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=b1rUxZuMf3zH1hc2PpwHnv/E/jevAUz3hks+aXqqJ/Q=; b=cjxZ6sJwJDR5y4Hk2V6gSvuC6o5TUQ5rnSK5qQ8sO45CHswZ/lNJO6HKNUQZ4owGqr q67aWajwoEoinUk4TctyrCmKt7p8Iz8l35tz4ixMqs3363qRipvG58o971DduaKSx7Vg br/nAiFrjF+dNixOkg7dNifmLvzH7NB2c9yjnVWozvrgfwJTRCEMYfzAi/d26GlMEjtj gbcm4XMqSd+VO2/LUwYu/ErFJtmC/ihGXT37IU/rPTDbpS1j9Z2LXdwci/dn1+Dj0pU6 DC67ZvXQv47XAM6Y5Eb9xyq2/ime44T54c36b+2iW6awyiiBo7EOAZo5jMKKEV4mm2h+ aQbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740563333; x=1741168133; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=b1rUxZuMf3zH1hc2PpwHnv/E/jevAUz3hks+aXqqJ/Q=; b=a3XWnMuX/BO6UWQwxjC0jQGgoNawoK8AjajBpsathomjP555fuRo480OUaHEEDRQb2 5XKJP4Z2s4m3SPTHSnz1nJeZ4vnvFJ8y2AoiahvO0pgOYqeFXazBFSKmCCj4tU4M+oKW W4nGR6d0pUrurS9i6Nm1pFeetCN+VBPI2i+8ob1tB3n7FOWL5awo49Pm2mByb5NfLosC xZ32pZPcfbhJRkWZaj97r7EyhRFXLDEjSZJZlDe1ptfLs0os+uglRbHUeP+8v77Bu/G6 atPHJA96FkiEYIdPXEn61oxcy7GC2HU5jRGIda7QYF6mhp9cen2uSjttQdbiWkZk3/ii Xk4g== X-Gm-Message-State: AOJu0YyRfX6gx7PM3G4OJLtrJZ9Bx04tOTEpYQN2IeW0hRorCuyalU1L gwINFsnoUVY93KyP0Z4L4hR0pv7J7e5+KIdZWvrHgvyfZiI5rUhG X-Gm-Gg: ASbGncvB4G5O9oclxZyx1zCx6IQncFxldByfUstKzTRZyE3RbEoJKL7EDg6sckKbs3w R3IkKCZZ5yuPmHSKNnQVvstPMutsnO8qdCtzpjms/PfDaO9cxLpXbi5hkV0tCIGT4cwE0mkOYoY Vxvi2Db76C+ZPPxNOtupYlrVlMSS3B/VcI6UuCtYNJEBYgi5JCexDoOgArMh+NUbW1XoNFvTvvW 1Yc+l7pcCJH8QDEvRO6a+uyV21SgwpOQ7F9hUHacB13vtiJJhKKy7GmrDTteNv983xSL33TS3ka gAhjUxF9ON5s2kq/ntBnmQ4+wNhl+kuDNArDbfaOqVyzY2qCq3QEx2pBXjXBcyt+36VxoU1hBlS KBqC9WQKv9wK7TcyFIDeGJOdLKgBIw06Uf3PgGTPQauJsc06sDrRGc7WlJw== X-Google-Smtp-Source: AGHT+IFZkJgo4vtW2ZyBQXu3V3IJy9uSTZe8c0oCJQTckSb7/eQ3Td1Zhf42l0ASPX3nOpmjL9JMdQ== X-Received: by 2002:a05:600c:4fc6:b0:439:9274:81db with SMTP id 5b1f17b1804b1-43ab8fd1d84mr23394835e9.5.1740563332636; Wed, 26 Feb 2025 01:48:52 -0800 (PST) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-181-182.eu-west-3.compute.amazonaws.com. [15.237.181.182]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43aba52b9dcsm15420825e9.6.2025.02.26.01.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Feb 2025 01:48:51 -0800 (PST) Date: Wed, 26 Feb 2025 09:48:50 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: pgsql-hackers@lists.postgresql.org Subject: Re: [BUG]: the walsender does not update its IO statistics until it exits Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="AOn2IxnzNvFdITAb" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --AOn2IxnzNvFdITAb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Wed, Feb 26, 2025 at 03:37:10PM +0900, Michael Paquier wrote: > On Tue, Feb 25, 2025 at 01:42:08PM +0000, Bertrand Drouvot wrote: > > Now we can see that the numbers increased for the relation object and that we > > get non zeros numbers for the wal object too (which makes fully sense). > > > > With the attached patch applied, we would get the same numbers already in > > step 4. (means the stats are flushed without the need to wait for the walsender > > to exit). > > @@ -2793,6 +2794,12 @@ WalSndLoop(WalSndSendDataCallback send_data) > if (pq_flush_if_writable() != 0) > WalSndShutdown(); > > + /* > + * Report IO statistics > + */ > + pgstat_flush_io(false); > + (void) pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO); > + > /* If nothing remains to be sent right now ... */ > if (WalSndCaughtUp && !pq_is_send_pending()) > { > > That's bad, worse for a logical WAL sender, because it means that we > have no idea what kind of I/O happens in this process until it exits, > and logical WAL senders could loop forever, since v16 where we've > begun tracking I/O. Yeah... And while the example shared up-thread is related to logical walsender, the same issue exists for a physical walsender. OTOH, It's also great to see that the new stats that have been added (the WAL ones) helped to spot the issue. > A non-forced periodic flush like you are proposing here sounds OK to > me, Thanks for looking at it! > but the position of the flush could be positioned better in the > loop. If there is a SIGUSR2 (aka got_SIGUSR2 is true), WAL senders > would shut down, That's true for a physical walsender but I'm not sure it is for a logical walsender (due to the "sentPtr == replicatedPtr" check in WalSndDone()). > so it seems rather pointless to do a flush just > before exiting the process in WalSndDone(), no? I'd suggest to move > the flush attempt closer to where we wait for some activity, just > after WalSndKeepaliveIfNecessary(). Yeah I think that makes sense, done that way in the attached. Speaking about physical walsender, I moved the test to 001_stream_rep.pl instead (would also fail without the fix). Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --AOn2IxnzNvFdITAb Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Flush-the-IO-statistics-of-active-walsenders.patch" From 0b610eaea0f71c3f61e1cded137dc59db596322e Mon Sep 17 00:00:00 2001 From: Bertrand Drouvot Date: Tue, 25 Feb 2025 10:18:05 +0000 Subject: [PATCH v2] Flush the IO statistics of active walsenders The walsender does not flush its IO statistics until it exits. The issue is there since pg_stat_io has been introduced in a9c70b46dbe. This commits: 1. ensures it does not wait to exit to flush its IO statistics 2. adds a test for a physical walsender (a logical walsender had the same issue but the fix is in the same code path) --- src/backend/replication/walsender.c | 7 +++++++ src/test/recovery/t/001_stream_rep.pl | 14 ++++++++++++++ 2 files changed, 21 insertions(+) 26.8% src/backend/replication/ 73.1% src/test/recovery/t/ diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 446d10c1a7d..9ddf111af25 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -90,6 +90,7 @@ #include "utils/guc.h" #include "utils/memutils.h" #include "utils/pg_lsn.h" +#include "utils/pgstat_internal.h" #include "utils/ps_status.h" #include "utils/timeout.h" #include "utils/timestamp.h" @@ -2829,6 +2830,12 @@ WalSndLoop(WalSndSendDataCallback send_data) /* Send keepalive if the time has come */ WalSndKeepaliveIfNecessary(); + /* + * Report IO statistics + */ + pgstat_flush_io(false); + (void) pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO); + /* * Block if we have unsent data. XXX For logical replication, let * WalSndWaitForWal() handle any other blocking; idle receivers need diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index ee57d234c86..aea32f68b79 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -42,6 +42,9 @@ $node_standby_2->init_from_backup($node_standby_1, $backup_name, has_streaming => 1); $node_standby_2->start; +# To check that an active walsender updates its IO statistics below. +$node_primary->safe_psql('postgres', "SELECT pg_stat_reset_shared('io')"); + # Create some content on primary and check its presence in standby nodes $node_primary->safe_psql('postgres', "CREATE TABLE tab_int AS SELECT generate_series(1,1002) AS a"); @@ -69,6 +72,17 @@ ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS; $node_primary->wait_for_replay_catchup($node_standby_1); $node_standby_1->wait_for_replay_catchup($node_standby_2, $node_primary); +# Ensure an active walsender updates its IO statistics. +is( $node_primary->safe_psql( + 'postgres', + qq(SELECT sum(reads) > 0 + FROM pg_catalog.pg_stat_io + WHERE backend_type = 'walsender' + AND object = 'wal') + ), + qq(t), + "Check that the walsender updates its IO statistics"); + my $result = $node_standby_1->safe_psql('postgres', "SELECT count(*) FROM tab_int"); print "standby 1: $result\n"; -- 2.34.1 --AOn2IxnzNvFdITAb--