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.96) (envelope-from ) id 1vnfbK-00A3E9-1C for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 16:19:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnfbJ-00CTPH-13 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 16:19:33 +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.96) (envelope-from ) id 1vnfbI-00CTOn-2t for pgsql-hackers@lists.postgresql.org; Wed, 04 Feb 2026 16:19:32 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnfbB-00000000Y5g-36YY for pgsql-hackers@lists.postgresql.org; Wed, 04 Feb 2026 16:19:26 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-b8869cd7bb1so1218494366b.1 for ; Wed, 04 Feb 2026 08:19:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770221959; cv=none; d=google.com; s=arc-20240605; b=V7RRDlv3baN8tb4aTmx6FQZfJgOUmV+CxObwf1RW7FKGof7sWerIGjgtApARB5Eszv tINSr2LT5Gd6Kn4yRVIpKj9uwhpr0+oXBC0qrbSv5zsvVn6BSAKcukLfdCjz1G0RKbn+ aIu45ZdL+8NkcjFUPB+Ns1hc3BsVeSE8+Dw/6N0buwwF79zD7N5iZX/Ry9TU1dXKol6E 6aHbd+1LoCruBlXbj50paYJbVG2Ym5CZOLJRt9E3JIiol0RbNudiD/pTeZk+kx8/ZoY3 +07yl6KNfRI23B4fjBun6KhggYLJLUjs9TV/QGLuI1qwrE1PWKeWTixU/D6nvgzYinXJ a5bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=1ZeWxqeOFMhvF0DRCtFKfpMTgvEOsieRQIIamujFsZo=; fh=PVvXIbcblMwAu6jmsNTVsSEleOxZy2oky7Jbsamtemo=; b=e00g63Ghs1Dtb0YXOpXJpK5Lb/aVzbatOIDFa6wDy8hE5JDUp8/GfPMRMheyWJ9qgF oM7RrxM8sl9iCUdOfxsguSFhT85nbOtnY7Za4BZlkLItHAKwEe0m2jltIjHAZ+67W6Ig eZRA11SIRzRrE4//TNg43v51j97J6+1QAULFNRhV15f0T6tVQrgWEZpXqejhPvb3Fq3h 5Hnx6/bkSaVruqHcqKa7SZ/UXE8cncmV3cUEyykMXZpayBIXAQ5dgdfbgT/p21twsqeO 4m+bWud+1TjX5HTb6nF5YFFT0vphj5/XwWQNa0e7mOdWvCMG/XnFeTROuDANeXeWJCUB O+DA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770221959; x=1770826759; 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=1ZeWxqeOFMhvF0DRCtFKfpMTgvEOsieRQIIamujFsZo=; b=aFYxgb7UFT9u/yiFxJaxuQmPOIyquXs8pWsL0eTeualPax4CdDDCJfK9n/BdY1SpGN M3DP9kQUvNjaUjdAzYifU4cdMaX40wSZQsAlsjupY+flT7EneNVnh5YoBHxzzGNbcK4w zGM5q+3zGvhtonxgrBE0CZWX7Lu5xuIdl2Mg8inEHL4NU4ldnH0hfHt6ntErqcZMaZOg qLIApdxqvlJe89yjUzTjR5YR1GvEVZ7yCPUNyHDnjhA1yUOcsJssIO73XjBc7sba5+qI 0PRFsG5ENVeeD6Bpyjvd4pjpnWJ4bXOmrmbZWHoCjqgrzECnJUg/XFdR4R0zh28B4Z17 OqPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770221959; x=1770826759; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1ZeWxqeOFMhvF0DRCtFKfpMTgvEOsieRQIIamujFsZo=; b=trDJgl3Ju3na/hEc6w7MqiYZb3AAITwoR81YqXh8kd1Bez3eyozTlY9JapzbyJcqXJ mHgY68Vs5JktLiXykP7C0VjYBxE+dU+FUjujx8z5IE2EinusTRQ09LJ7Dce8XjnXyCUt yTnABQizTGR8ITEvudfgS904w48paXIQAiwRD4p4sJSwJGSV/es2DqzsAfgegQ7qXgVR D+OiN1oOGC15Zh4srehXu1sdMpkxvX2qwn13qq3f0tNR3cPuSNeOhNKNABF7Esc4f/39 Oq15P7jrw1gQ9nvlhKg5dJrGf7sjXzUuCPSLUIMAG0cDPJJ65NkQaT+cPLCeKHnRY2xT jEcQ== X-Forwarded-Encrypted: i=1; AJvYcCWUGFyJ9TGm+su7zIxYDIhWtqGbx3CF+IHOk2CreOGUb1UiSPGxFEf3u3oqol0VyFDydZSGvVzzR2U8x1Mp@lists.postgresql.org X-Gm-Message-State: AOJu0YxqnPbPCqtSwG60tebJMNaRNwGNnUJd196uv9WOI+pupZb/Ht35 A4eSvKh/V3NBKGc9lP5quHSqcO9aPjd0fbRNSA2JtT4NnkPjH76ijjupqov2JfF6a7go5CyaRL6 V/eWUE00q0ZMC/I7c9xa6ojNh7X2Rn5s= X-Gm-Gg: AZuq6aJ8McEueuw20iI8PwW7aMhn7s6aVAhQQ2wh7qLM5uLBilqUz4trnyxVJLHHXik U1T3yX5FmjvuKfSiJkDLgM1+hli+MzvMd4pZMA+hui6FqhdTi8XZYAnv3m+7QYtHR3LZ7fgORdN mt1MBc5ZAo7ZoJEgVe3AinuANsfNIYLLT+cfTTJijWnK5fnXf8q5W9Rxwy92f7+t0DcSymgDyzN hsscb+0AU/Vt3aCnLE2uGiM1IKrRkJZc34OCDDI1VdVJrls/2qfCbh6Nq0UtiTX3EhHjrH7j4ZS 4GfzWQ== X-Received: by 2002:a17:907:5cd:b0:b88:60d2:11a2 with SMTP id a640c23a62f3a-b8e9f17345amr240193766b.34.1770221954618; Wed, 04 Feb 2026 08:19:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Wed, 4 Feb 2026 10:19:03 -0600 X-Gm-Features: AZwV_QiTpSJ4VZ8GOJ2eFCEPLR58U0Nor7qSA04Hzz3ldbzzvR4UFu1HSzQ-JPU Message-ID: Subject: Re: Flush some statistics within running transactions To: Bertrand Drouvot Cc: Michael Paquier , Fujii Masao , pgsql-hackers@lists.postgresql.org, Zsolt Parragi Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > In v5 attached I changed the design so that we don't re-enable the timeout Thanks! > I do think we still need it. Indeed in 0004 that helps distinguish between > anytime flush or mixed flush (with the help of the new pgstat_report_mixed_anytime > global variable) in pgstat_prep_pending_entry(). Will address below in the comments. v5-0001: 1/ ``` .write_to_file = true, + .flush_mode = FLUSH_ANYTIME, .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver), ``` FLUSH_ANYTIME does not have any impact on Archiver. It does not have a .flush_static_cb registered and entries are flushed directly to shared memory via `pgstat_report_archiver` inside the `pgarch_ArchiverCopyLoop` loop. FLUSH_ANYTIME needs to be described a bit more that it only applies to kinds that have flush callbacks. 2/ I suggest simplifying the code comment to this: ``` /* - * Some stats have to be updated only at transaction boundaries (such as - * tuples_inserted updated, deleted), so it's very important to set the - * right flush mode (FLUSH_AT_TXN_BOUNDARY being the default). + * The mode of when to flush stats. See PgStat_FlushMode for more details. */ PgStat_FlushMode flush_mode; ``` PgStat_FlushMode has sufficient description. 3/ we have this pattern: ``` /* Schedule next anytime stats update timeout */ if (IsUnderPostmaster && !get_timeout_active(ANYTIME_STATS_UPDATE_TIMEOUT)) enable_timeout_after(ANYTIME_STATS_UPDATE_TIMEOUT, pgstat_flush_interval); /* Required for the flush of pending stats WAL data */ pgstat_report_fixed = true; ``` to report fixed stats. I think it will be good to turn this into a public routine, i.e. pgstat_report_fixed_entry, that can also be used by extensions that register custom kinds with ANYTIME flush. extensions using variable-numbered will get the timeout enabled via pgstat_prep_pending_entry, per v5-0004. We may want to add a test in test_custom_stats to ensure this ANYTIME mechanism can work with custom kinds. What do you think? v5-0002: Overall, this GUC seems like a good idea. 10 second default and 1 second minimum are fine. The patch overall looks solid, and I could not find any issues. I do have a suggestion about the documentation: ``` - Sets the interval at which non-transactional statistics are made visible - during running transactions. Non-transactional statistics include, for - example, WAL activity and I/O operations. - They become visible at that interval in monitoring views such as - pg_stat_io - and pg_stat_wal - during running transactions. - If this value is specified without units, it is taken as milliseconds. - The default is 10 seconds (10s), which is probably - about the smallest value you would want in practice for long running - transactions. + Sets the interval at which statistics that can be updated while a transaction is still running + are made visible. These include, for example, WAL activity and I/O operations. + Such statistics are refreshed at the specified interval and can be observed during active + transactions in monitoring views such as + pg_stat_io and + pg_stat_wal. + Other statistics are only made visible at transaction end and are not affected by this setting. + If the value is specified without a unit, milliseconds are assumed. + The default is 10 seconds (10s), which is generally the smallest practical + value for long-running transactions. - - This parameter does not affect transactional statistics such as - pg_stat_all_tables columns (like - n_tup_ins, n_tup_upd, - n_tup_del), which are always flushed at transaction - boundaries to maintain consistency. - + This parameter does not affect statistics that are only reported at transaction end, + such as the columns of pg_stat_all_tables + (for example, n_tup_ins, n_tup_upd, + and n_tup_del). These statistics are always flushed at the end of + a transaction. ``` specifically, I want to avoid using "non-transactional", "transaction boundary" terms, as they may be confusing. v5-0003: looks straightforward. I have no comments. v5-0004: /1 + FLUSH_MIXED, /* Mix of fields that can be flushed anytime + * or only at transaction boundary */ I still don't think this is needed. I fail to see what value it adds. When we set pgstat_report_mixed_anytime inside the pgstat_count functions, which make it clear which of the fields are anytime fields. 2/ This is an oversight comment, it seems. ``` extern PGDLLIMPORT bool pgstat_report_fixed; +/* Track if mixed anytime stats need to be flushed */ + /* Backend-local stats state */ extern PGDLLIMPORT PgStat_LocalState pgStatLocal; ``` 3/ ``` + tabentry->numscans += lstats->counts.numscans; + if (lstats->counts.numscans) + { + TimestampTz t = GetCurrentTimestamp(); ``` Considering the 10 second default, GetCurrentTimestamp() should not be an issue here. The doc does need to be updated. From: "The time of the last sequential scan on this table, based on the most recent transaction stop time" To this? "The approximate time of the last sequential scan on this table, updated at least every STATS_FLUSH_INTERVAL" same for `last_index_scan` 4/ nit: /non partial flush/non-partial flush/ -- Sami Imseih Amazon Web Services (AWS)