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 1vlzsP-0067Ep-0O for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 01:34:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vlzsM-006v00-2Y for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 01:34:15 +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 1vlzsM-006uzr-11 for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 01:34:15 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vlzsK-000FgI-31 for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 01:34:14 +0000 Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-65808bb859cso4133503a12.2 for ; Fri, 30 Jan 2026 17:34:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769823251; cv=none; d=google.com; s=arc-20240605; b=ahOpx/I34Vlr86ScOKrtxVb6wkTqcoNAG6SwtYoYPCER1ATsqBx2xtNuT16p+oiEZh QvOxQMJaHg8oIpnLynRGE6Tg605y0ZrYtub8JTu4Z5SdYn4f2RuppORn5QPxzp38tUki sgFRWvaQ6dhnM28UaYN/j35tbtIjJpRWEXN1pfzVj9B/hj3VZQnHLwl4n5ivMibBngmd gnVexjIVLXzLdYO1J7ays4GSGmJzz/EkKMG9vtfOHTefLdyo8NhOx2cH0ZFltNKdydj5 Ic1GTf9OaZI4pVT6DLb2GFgOlIMCmqCxhg2Ks4GbEZjyfWrAwVe0wL43k+qjxxv3wth3 VF6w== 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=dA8oH5WQXFRnTZ+fC1r+nfSRycllT+T701jCCYAz60U=; fh=BpvoqpMW2LPPdIh5+nPCgPvUrCg5wIUe70qCRBB6+WM=; b=bKzTzDcMjOCk5MLpusp47NL8T1kg7H44Ae0LVGqaquOOWdy8JceEZTtdUsAkl7JVxo yo6iT9dADf+ZkNU9oY6AkeD47nAf9wkljPhheglDH60A/clLNw9dSE70pU/9TkSetT3U GBftdPqd/s0oqLZKjgjRJMyrxwZz/p7K30WLLrRS8kliOAAJaiOyVY7gRxvISgf12T6v Z3RccsNQb4SszQMv4vMMdSc82km6mid2yxDELOUtqTVQY7bsL8yzx0ImXoR8qBwx/+lT T0dh8Qwb+Lfb0MbF55C+p2ab8BNpBy8Ag3xu5/2Buf3qH5r+GDSvb0XSVaSjA6yS1uI4 BB1g==; 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=1769823251; x=1770428051; 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=dA8oH5WQXFRnTZ+fC1r+nfSRycllT+T701jCCYAz60U=; b=LHIMdRp8s1aTtZu7MDHJQxatbvUX+tBb/9TXPCvBiFzSCMh/CNzquKis9MDr5H2hSN pdgYYsMHrvC+ER65ynEHNkXSnEX3uYFJ0lqoloQfCjDoRd1sFGeh5ttZ529vMGacx63H guxbscM2hWMVF96z8LJ6OO5hNRIOAlVGwHZOaaEo5054tXbSOVG5b1Eb5a/qzbKEKnJh sKlCVPGDKdMpvQHjcMXsydXlgfSbMNvtguWizeJbo+iBMHUYAmYkFVLoKEk8ciFEJfiO V805pDAuD4pOoqMxyBXBX7fAMaWU52QWu0ekN0vpfxqSe66MNEz22KQElfTS1QVpoFI0 BD9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769823251; x=1770428051; 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=dA8oH5WQXFRnTZ+fC1r+nfSRycllT+T701jCCYAz60U=; b=UjlRC0K50F/Qgufr9GjQ85dfWvLTPZ/AfH9fp1U3VD4c8cF1/DQpGr58PORtfwdxHs 1kpEF1gZ3uqURzA1d5ZkfdDnyHwZCzVlac0Q/V/PaG/jY0tf87OPefE7CWyrDoffoboJ DFnKwc+MIFyFWB1vpkbfkmA77dv+8UuK9p8Yek8WHWsdoHyY5TH5hB547BF4S8QGJtNt 7aDf75gT3pTClcSNMv9to3nmpIx4FeG9tBhWU3CyWMjYrTkk+r6EiZnvSxG2/wS5bY9P GfG8LXU9nQ5kbgwSFXYQpUtb+4aqcpZYhxGxwklaBqDZS6Tf6w0E0nYrtL1izhET6FFZ ECfg== X-Forwarded-Encrypted: i=1; AJvYcCVvCFfO/jbbawi9ItrenHOsKB+4bn565tKAvy+B4XR8NXmCtjRCpmr8pKz8ej9YZTN9Gxi+DCKZrI2nr25x@lists.postgresql.org X-Gm-Message-State: AOJu0YxdWIIl4x5Xt5WM0hkiuayvB1iGvoCnQZawDow9UuIzH2QTz10t RVT+3XxW1nF1f9RkzMUhW4DTxjccwh6UX/nHem1Wsze3mpU2NE6B64WoI51yST4jbsCRsu1HJRz gAVTiCsoEuZI6cAy+3ax6RPqP/+nZXQw= X-Gm-Gg: AZuq6aK8mKfyXJXnViStTlaOzvwcuDn4i5ERFBowrvSnkIVzCgDG/oNyrM9eW2fiCQT i6vIf6BrL82U/UCdAmbYYmm6ExTonLHBNNZHpcRspUGku2/qdrX3YGLcstw/kakTGI3uqqMSX8x TYfLCpBegETnhzEK0uMPs9vHFrjoTNP1H4q08B2cYv+NaWoBthZvjhBsHtpQ6kq+pslQhHceS2v T2xWosS8GXm7KlzL9Hm4bQB5yloR+fVTkgSsLEkeIX9wWQ9XZ/K7JNOnAvWdScNRwPVOpM= X-Received: by 2002:a05:6402:2683:b0:649:5bb4:59e5 with SMTP id 4fb4d7f45d1cf-658de5b2e12mr3054891a12.30.1769823250735; Fri, 30 Jan 2026 17:34:10 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Fri, 30 Jan 2026 19:33:59 -0600 X-Gm-Features: AZwV_QjVdeMajUoOPU7bIxgh8_giYMVD8ZerT9Kfne3zrcHS-Gomv-xnC_VTIgk 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 > Also the attached is now split in 4 sub-patches with 0002 introducing a new > GUC to control the flush interval (default is 10s). Note that 0001 to 0003 could > be merged as one patch but I did it that way to ease the review. Thanks for the patches! I do like the GUC introduced in 0002 to control the ANYTIME stats flush interval. But, as I was looking at this a bit more today and testing it, I am not quite sure I like what is happening here: +void +pgstat_report_anytime_stat(bool force) +{ + bool nowait = !force; + + pgstat_assert_is_up(); + + /* + * Exit if no pending stats at all. This avoids unnecessary work when + * backends are idle or in sessions without stats accumulation. + * + * Note: This check isn't precise as there might be only transactional + * stats pending, which we'll skip during the flush. However, maintaining + * precise tracking would add complexity that does not seem worth it from + * a performance point of view (no noticeable performance regression has + * been observed with the current implementation). + */ + if (dlist_is_empty(&pgStatPending) && !pgstat_report_fixed) + return; + + /* Flush stats outside of transaction boundary */ + pgstat_flush_pending_entries(nowait, true); + pgstat_flush_fixed_stats(nowait, true); +} There is a check if pgStatPending is empty so we can return early. However, with FLUSH_MIXED, the scenario is we will more than likely always have TXN_BOUNDARY stats that can only be flushed at the end of a transaction. This means that most of the time we will call pgstat_flush_pending_entries and then leave it up to the anytime_cb to check for pending ANYTIME stats to flush (before taking the lock ). +bool +pgstat_relation_flush_anytime_cb(PgStat_EntryRef *entry_ref, bool nowait) +{ + Oid dboid; + PgStat_TableStatus *lstats; /* pending stats entry */ + PgStatShared_Relation *shtabstats; + PgStat_StatTabEntry *tabentry; /* table entry of shared stats */ + PgStat_StatDBEntry *dbentry; /* pending database entry */ + + dboid = entry_ref->shared_entry->key.dboid; + lstats = (PgStat_TableStatus *) entry_ref->pending; + shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats; + + /* + * Check if there are any non-transactional stats to flush. Avoid + * unnecessarily locking the entry if nothing accumulated. + */ + if (!(lstats->counts.numscans > 0 || + lstats->counts.tuples_returned > 0 || + lstats->counts.tuples_fetched > 0 || + lstats->counts.blocks_fetched > 0 || + lstats->counts.blocks_hit > 0)) + return true; This makes things confusing because instead of just relying on dlist_is_empty(&pgStatPending) to check if we need to flush anything, the responsibility now moves to the callback, which now also has to account for all the ANYTIME fields. The way I can think about making this better is to somehow track if we have ANYTIME data that needs to be flushed a different way ( maybe a second pgStatPending list for anytime stats ?? ) What do you think? -- Sami Imseih Amazon Web Services (AWS)