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 1tY4Gg-002udD-A7 for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 14:21:14 +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 1tY4Ge-00CB2W-C6 for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 14:21:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tY4Gd-00CB2O-Us for pgsql-hackers@lists.postgresql.org; Wed, 15 Jan 2025 14:21:12 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tY4Gc-000Xqw-2d for pgsql-hackers@lists.postgresql.org; Wed, 15 Jan 2025 14:21:11 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21619108a6bso120808105ad.3 for ; Wed, 15 Jan 2025 06:21:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736950869; x=1737555669; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=2AY33CMt5OC/rKOXkPZCfoImZC9tIjaKJwhoNfXAoLg=; b=U473QPlyXQghKlKcdluCcXIse4et3+1X05Q9CG6OITxQDYMgLo6Uojqnr8tiZeFtQY b5w93z/NTnyXkjY3GAQnE+SVtynAQ9hZHrpct/pfpk7pGY/8X8BQ6dMUzyJTac0qVenm Ne0qmyER8zIwE/+7jWRl1l1WeDb4I7XpCn5MOzl0xYmY1QVv2XLw4Vhf/hklDtobRt3g vVLbW7YriHwxQGRCYGooi3ViHIfhosJ4gpqor8eC8X+j/erGTcqQZb5f8QnOTMBsdkvN Vh7HZzEPJjupzfb+ES/owxpzxujZ9/IpAcK5ooPSu7IE5BuWjvw5Srfw7ev//xGagrNd uyHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736950869; x=1737555669; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2AY33CMt5OC/rKOXkPZCfoImZC9tIjaKJwhoNfXAoLg=; b=PWLXw1M7PxqHye4HypqrHc3+LZDOlQC1SSYqwO7BbD6DcLPgYgBhhgg58Vb6D3vufe b+Vi8EgWSxVPAcrhVzm1+s+PQJrOVUSL6LL+9XlYawgBdgxNsVSpoA3VTPrBpe9nLHPD Z1FbChNgq4ojFgC8GVytF7Cz76n4AyDtamBcq9+AF9x9I4IHmhYZdzLtMPwE8Fj/9CRW SFbNA8JL7lK/pSMcRskD79j8VPghHbRTTLMvP4tLYs0zcalsyZ8zZ83hjgubAuFJh4xD TYP2XCfnvz0dphen0td/+Tih3+XSFOUqi3U41yIxmSKaDyTF6P12/sIggKNPqnLp3zYD xsMA== X-Forwarded-Encrypted: i=1; AJvYcCVALEdXe+WpObU3ur4GyjcsKucJulvITHLaszFKrgEB9z5pxqR0LRse5XRNS6YySiuHy4ltCtP4OS3eO4vX@lists.postgresql.org X-Gm-Message-State: AOJu0YyUAA40lWsu9Mc13L0KlfCTEE3pErBAGlPjzm0zS0vAfzyg8nCp F4P+CwuR2rF72nfLv9xkMmORcrD2r+ZvHrJb0GEBRPl5fWYHfHN3Wmr9mNtMfujIUxJdugFJ4ft 0nQ/TofB5WH2GGqRndG2NSPxGElU= X-Gm-Gg: ASbGncvwFdAlXWHcME7rcoa7eiu8zVpOJ3FtMF7p+c425ss+VCC/ZA+69MJmmzAlv9+ 7/b2kG18kJ51QWlb33Jd5Jvuk2YKQBplokp1pWg== X-Google-Smtp-Source: AGHT+IFJ119FtRdeS+pTvzQLB4CAVEX5A2ccLPIFyRk4G3kw4rGy3KXBZ8+UWyE+fAwqx6eWxF+ceOUiZ0JkS9GBOR8= X-Received: by 2002:a17:90b:540e:b0:2ee:a76a:830 with SMTP id 98e67ed59e1d1-2f548f1b2bfmr45564697a91.24.1736950869362; Wed, 15 Jan 2025 06:21:09 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Nazir Bilal Yavuz Date: Wed, 15 Jan 2025 17:20:57 +0300 X-Gm-Features: AbW1kvaZfp-t1QA1qa-sxeWmWzYYWzUXO81-WEDLa3Z29yGgC2hShBNBdDwIw14 Message-ID: Subject: Re: per backend I/O statistics To: Michael Paquier Cc: Bertrand Drouvot , Alvaro Herrera , Kyotaro Horiguchi , pgsql-hackers@lists.postgresql.org, Andres Freund Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Wed, 15 Jan 2025 at 12:27, Michael Paquier wrote: > > On Wed, Jan 15, 2025 at 08:30:20AM +0000, Bertrand Drouvot wrote: > > On Wed, Jan 15, 2025 at 11:03:54AM +0300, Nazir Bilal Yavuz wrote: > >> With this commit it may not be possible to count IOs in the critical > >> sections. I think the problem happens only if the local > >> PgStat_BackendPending entry is being created for the first time for > >> this backend in the critical section. > > > > Yeah, I encountered the exact same thing and mentioned it in [1] (see R1.). > > > > In [1] I did propose to use a new PendingBackendWalStats variable to "bypass" the > > pgstat_prep_backend_pending() usage. > > > > Michael mentioned in [2] that is not really consistent with the rest (what I > > agree with) and that "we should rethink a bit the way pending entries are > > retrieved". I did not think about it yet but that might be the way to > > go, thoughts? > > > > [1]: https://www.postgresql.org/message-id/Z3zqc4o09dM/Ezyz%40ip-10-97-1-34.eu-west-3.compute.internal > > [2]: https://www.postgresql.org/message-id/Z4dRlNuhSQ3hPPv2%40paquier.xyz Thanks! I must have missed these emails. > > My problem is that this is not only related to backend stats, but to > all variable-numbered stats kinds that require this behavior. One > other case where I've seen this as being an issue is injection point > stats, for example, while enabling these stats for all callbacks and > some of them are run in critical sections. > > A generic solution to the problem would be to allow > pgStatPendingContext to have MemoryContextAllowInCriticalSection() set > temporarily for the allocation of the pending data. > Perhaps not for all stats kinds, just for these where we're OK with > the potential memory footprint so we could have a flag in > PgStat_KindInfo. Giving to all stats kinds the responsibility to > allocate a pending entry beforehand outside a critical section is > another option, but that means going through different tweaks for all > stats kinds that require them. I think allowing only pgStatPendingContext to have MemoryContextAllowInCriticalSection() is not enough. We need to allow at least pgStatSharedRefContext as well to have MemoryContextAllowInCriticalSection() as it can be allocated too. ''' pgstat_prep_pending_entry() -> pgstat_get_entry_ref() -> pgstat_get_entry_ref_cached() -> MemoryContextAlloc(pgStatSharedRefContext, sizeof(PgStat_EntryRef)) ''' Also, I encountered another problem. I did not write it in the first email because I thought I fixed it but it seems I did not. While doing the initdb, we are restoring stats with the pgstat_restore_stats() and we do not expect any pending stats. The problem goes like that: ''' pgstat_restore_stats() -> pgstat_read_statsfile() -> pgstat_reset_after_failure() -> pgstat_drop_all_entries() -> pgstat_drop_entry_internal() -> We have an assertion there which checks if there is a pending stat entry: /* should already have released local reference */ if (pgStatEntryRefHash) Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key)); ''' And we have this at the same time: ''' BootstrapModeMain() -> InitPostgres() -> StartupXLOG() -> ReadCheckpointRecord() -> InitWalRecovery() -> ... -> XLogReadAhead() -> XLogDecodeNextRecord() -> ReadPageInternal() -> state->routine.page_read = XLogPageRead() then WAL read happens ''' So, this assert fails because we have pending stats for the PGSTAT_KIND_BACKEND. My fix was simply not restoring stats when the bootstrap is happening; but it did not work. Because, we reset all fixed-numbered stats 'pgstat_reset_after_failure() -> kind_info->reset_all_cb()' there, so if we skip it; we have some NULL stats, for example reset timestamps. Some of the tests do not expect them to be NULL at the start like below: SELECT stats_reset AS archiver_reset_ts FROM pg_stat_archiver \gset SELECT pg_stat_reset_shared('archiver'); SELECT stats_reset > :'archiver_reset_ts'::timestamptz FROM pg_stat_archiver; This test fails because archiver_reset_ts is NULL when the server is started. I was a bit surprised that Bertrand did not encounter the same problem while working on the 'per backend WAL statistics' patch. Then I found the reason, it is because this problem happens only for WAL read and WAL init IOs which are starting to be tracked in my patch. By saying that, I could not decide which thread to write about this problem, migrating WAL stats thread or this thread. Since this thread is active and other stats may cause the same problem, I decided to write here. Please warn me if you think I need to write this to the migrating WAL stats thread. [1] diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index bf3dbda901d..0538859cc7f 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5681,7 +5681,7 @@ StartupXLOG(void) */ if (didCrash) pgstat_discard_stats(); - else + else if (!IsBootstrapProcessingMode()) pgstat_restore_stats(checkPoint.redo); -- Regards, Nazir Bilal Yavuz Microsoft