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 1vnjHT-00Aqyn-2Y for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 20:15:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnjHS-00DM3q-2r for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 20:15:18 +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.96) (envelope-from ) id 1vnjHS-00DM3i-1b for pgsql-hackers@lists.postgresql.org; Wed, 04 Feb 2026 20:15:18 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnjHP-0000000159k-2JaZ for pgsql-hackers@lists.postgresql.org; Wed, 04 Feb 2026 20:15:17 +0000 Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-658034ce0e3so326907a12.3 for ; Wed, 04 Feb 2026 12:15:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770236114; cv=none; d=google.com; s=arc-20240605; b=BvCvMP+7tDaiXAWhX3RqoGvQpQOlR/Ke+g7L04GIlEzCW6RS93JMlHXZT5CgWAnMJ4 t66v0QNwlBAS+Ju5Lral7DSog9eubaBP8QKPGnkcIRv5KwBmvUgU/esJftpQNK9nQmD0 cnDIBm7WaHw1pci8cqRYb6F4q7N5IiYHrS5miW+l5GtyJExJMvz9C+Oxe5G4F0OB7eOL b8Nyyo5YQp8SsIZmbfH05QcCrTWxpcYuqJCM73wxqtj4w2A+zCJW3PIimPBfnJbBUw+c O6OxYbCqLC91r95ON4zZwrVCkNY1P0bjaJn4y1abp5NigXPSH94++ZC6LLJgGgo+PctM L6GA== 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=I368AGBxsnJp99RInqhSl4myhEkDM8lhaluAFn/wLtc=; fh=C/OU78t96EcPqcqBSxacwrmHiit4dfcZv+r13WOsdEI=; b=AXc8Qj3CQePuKuNTfQpq1V79QvDrR3+8fP/bSvdBVJM5BsUFhPZq6pL0O1qiBuzU92 T/uPPJ+6FANAGbvZdSq1jYH8dm5xiNZBmZ4HHzenTb3yHGazWpAL6YcaU7t9BbGqFTiy uqftqeyJNkC707A6d3zkaiZem2YesotiYJBND88G3lrxFeQrLoN63bZ95o+ZN7BPFg8H ekFWRimKmdfBw2KHuGzVtUYeUm0BYxZsIbMrMujfy11+SWamlMqrTU0ofSJn41Ve64a7 S62Mca9MeBfPKyNpf/WVEgESf7wxf7Mj5Wb7Ts6oJ/3z6CD6rWALuDCquY6CD6+DaYxL qlwA==; 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=1770236114; x=1770840914; 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=I368AGBxsnJp99RInqhSl4myhEkDM8lhaluAFn/wLtc=; b=IXfRxVNQlDoT/XkF8NCmXzZgdwm4ful2K5eCAUWbHb8Quwd4TLXdsdUUZUtfBwssuC 2acHqvg0Bq3vZzChXK954Xcjv947NrebLbyDo4uM70W91Gg4/S2AWzo9EsOoQhvFjwzK as7OwqD6buWUqKu4D0tZyEx7iCrqxz+as/kzGdsU+4kHrP2yNsPhXHWWUtuvfM5XgTpr O2YAR3/EtFrAxXvaK2KX1HD4RPgtIzBZzzcKVj3/FhhDY2vQWLuuoXbOMX6QHYdlXkav VGCRqAXQvtbU8XC5Z8OJS4+sapynsYlR0AhsImqzs7jb4zE9EqSwFQqIaNxdmNIiXvkT HJ1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770236114; x=1770840914; 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=I368AGBxsnJp99RInqhSl4myhEkDM8lhaluAFn/wLtc=; b=n4+psuLb8evFRDXrxRYg5u1VwaUPQC8pvourpmEtn8uz94L+TeLD3iJaqAO/IqlhfX 48OXCrqbIVThOXVvCS7jeVJDts9XD+4qaINNeCsu2Syh5KJOtCUAtayUs0ewlfF6JlRw kSgMBeAwzmb+E/gnOlFHaGHwXb2fhTXF2ShWufvYunslHOxvVXwtC4T0H5SluR1rCTEK NJzo00UYq1lPWCpd5EpzYfoJobjjt8K2PiLPZQQIemZT//pjoAjUhR+QVlcFqVvMGT3a UEwFhBTjeeU+lwiREAUhljoLCM6XgLfOWfdeOuA+00hPXK74WOjCiVoMpguc7RXbxG/w B+rg== X-Forwarded-Encrypted: i=1; AJvYcCU4Rx3quOwT8bZ2ihSBJMfHIH5nlUS1WmYnRwKeG/fn9TAnKwJyVw9XW47u+iA33NxbKrJc5s/a6IUB4zAt@lists.postgresql.org X-Gm-Message-State: AOJu0YxKbV0kN1nEAWW47xPiJ6ui/qYJ5rmRZCZuoWVOK4PSPS02rb5v 0mtiPm20DCq+dn5aSDlADP8g2tm4fArmx53mqqaVHgkNuuawVPMl1PVWFY/tzd2fix04Nx2lapY ICnE1f3qrU6L7DxE4KYaiQ3L1AW9asIY= X-Gm-Gg: AZuq6aKwwgHmSwCEwBrBUENidknzbDCFijLYCfDd5hNREqM+ZHZD6/dtL3P8TviPwP2 FNnZ5Xn4BN+Z3kMWJZoVxTLK713lSLByVBp11FULYmxLS1jmx5MeuXUrBmfifnyK5gfo3dxLigm q3FgdpYqJKtW2IQ3jPuyg6pv2oC22L7uYJjkM1bAKo6ldrn3Kt5tRsOy3V6ft7wDvuru6N5tOQy 9mPgzDaqCYUus1gHliaRcRPLBHUcaoGvAZ2O9L6Bxgxw2fFt//f6kyD4vxcrnX5t2lPAkx/Xn6Z 7TLskg== X-Received: by 2002:a05:6402:2787:b0:650:8563:fdee with SMTP id 4fb4d7f45d1cf-65949dc9609mr2492499a12.25.1770236114122; Wed, 04 Feb 2026 12:15:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Wed, 4 Feb 2026 14:15:02 -0600 X-Gm-Features: AZwV_QgHWW53V19kzyv-DJpm-rlPdgYhrtoBr40NiZi8c0TLIrUuTbtZl-PTmOk Message-ID: Subject: Re: Flush some statistics within running transactions To: Zsolt Parragi Cc: Bertrand Drouvot , Michael Paquier , Fujii Masao , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > do { \ > + pgstat_report_mixed_anytime = true; \ > if (pgstat_should_count_relation(rel)) \ > (rel)->pgstat_info->counts.numscans++; \ > > Shouldn't these pgstat_report_mixed_anytime changes go inside the if > statement in all macros? I think you are correct here, and there is an even more fundamental issue. since `` #define pgstat_should_count_relation(rel) \ (likely((rel)->pgstat_info != NULL) ? true : \ ``` could return if there is already a pgstat_info, we may never actually enable the timeout. so, I think we should: 1/ remove the scheduling of the timeout from pgstat_prep_pending_entry @@ -1308,11 +1308,6 @@ pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *creat entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize); dlist_push_tail(&pgStatPending, &entry_ref->pending_node); - - /* Schedule next anytime stats update timeout */ - if ((kind_info->flush_mode == FLUSH_ANYTIME || pgstat_report_mixed_anytime) && - IsUnderPostmaster && !get_timeout_active(ANYTIME_STATS_UPDATE_TIMEOUT)) - enable_timeout_after(ANYTIME_STATS_UPDATE_TIMEOUT, pgstat_flush_interval); } 2/ Create a routine to schedule the timeout: +void +ScheduleAnyTimeUpdate(void) +{ + /* 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); + + pgstat_report_mixed_anytime = true; +} This will also set pgstat_report_mixed_anytime to true. In my earlier comments, I mentioned having this as a public routine will be needed for extensions that register a custom kind as well. 3/ All the count routines that wish to schedule any ANYTIME update because their kind allows it can do so with ScheduleAnyTimeUpdate(). In the relation stats, this can happen after it is checked that the relation should track counts. +#define pgstat_count_heap_scan(rel) \ + do { \ + if (pgstat_should_count_relation(rel)) \ + { \ + (rel)->pgstat_info->counts.numscans++; \ + ScheduleAnyTimeUpdate(); \ + } \ + } while (0) Also, it would be good to check if we have anytime flushes of either a mixed or a fixed kind. Not strictly necessary, but I think it's better to avoid needlessly entering the flush routines. @@ -2220,11 +2215,27 @@ pgstat_report_anytime_stat(bool force) pgstat_assert_is_up(); /* Flush stats outside of transaction boundary */ - pgstat_flush_pending_entries(nowait, true); - pgstat_flush_fixed_stats(nowait, true); + if (pgstat_report_mixed_anytime) + pgstat_flush_pending_entries(nowait, true); + + if (pgstat_report_mixed_anytime) + pgstat_flush_fixed_stats(nowait, true); pgstat_report_mixed_anytime = false; + pgstat_report_fixed = false; } I think we could benefit from a test that st track_counts to OFF inside a transaction and re-enables it, to make sure the pgstat_should_count_relation work is done correctly. -- Sami Imseih Amazon Web Services (AWS)