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 1viHOd-002EKX-0v for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Jan 2026 19:28:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viHOc-002nDw-14 for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Jan 2026 19:28:10 +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 1viHOb-002nDo-35 for pgsql-hackers@lists.postgresql.org; Tue, 20 Jan 2026 19:28:10 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1viHOZ-001ZvF-30 for pgsql-hackers@lists.postgresql.org; Tue, 20 Jan 2026 19:28:10 +0000 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-65808d08423so1133884a12.1 for ; Tue, 20 Jan 2026 11:28:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1768937287; cv=none; d=google.com; s=arc-20240605; b=ZYJDrmmPyP5XOw8huwzUsoYfTQtHTvLOQg1a0l2YmQk5wYABM0PW4pGfujqNZKK+sy AYAXXYZcVHxcdPs42yH32zRCAWlNxgDwLs7x3WgfMvl9plX6AHFPqS3R+6lLs1Sepnit 88aTXugWeI8m637proxM7C1B4gUfkJUZEtvSB/sHFZoH26AWteoVz/UxES7/3+ENWNll A/uSA/u4swsC2i9EhdgLOm9rfvP3UDzJqAHN7XAoA5wiNIOZH9J9YdzgqqY37Nq4m8uc NhZiJIrTaDrr2KvPOvj/ynqAaMOEh7s+TgB3cxzLV+BOzMSIxnsowEyUQnKQyI2LsKRX zIMQ== 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=IX/TccJwz/UTO00v//xtpL1J4SlmvFVmdImiXICKngs=; fh=PyrCF+qsNaqndkAt6nzpU6jTKSl/iqI3Radtv0tyAIM=; b=KFXtr04ki8qyMTnWLcsVeLuJoYqLK9DkpGzHb0rmAa21vq0t+7o/mKKo006WN9gbzW mhX9HovCFHU8rvrArMoXTvX35KCu7VFWdN/JZAG/nOSmTJfOimcQdTuyri6/yrPwAWHe KZpca3geyMaEkcVO4ikRq08IZhhpLPgCl/eRuuOsMcuyp1+V1PEiNrx2fHiaFXCDbn1s htaKvwX8JtGqUAYFLCMJ0DM77oPHUxkTp5Ysh+5qBXdNJEs8ZbPlc+4CIKVNRLXEf+8E vgxyV8/d05BXvvcDa/6rEzjCeCyXO/BNZ8bv7pVvrummB8UCk75/Ez8YRBzAr9D85gjH jTsA==; 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=1768937287; x=1769542087; 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=IX/TccJwz/UTO00v//xtpL1J4SlmvFVmdImiXICKngs=; b=ZIib8vQXiFRWzSPXRqVZ/pe+AC1eQrzi2dlLUKupwcOjH9kXGvrC4G2IW/Ky+0eFY0 RbuP4ZgcMTjLr24uaEVOCDC8Qm7/FM3en1QuThDafFukdloQZoAMItPwwPrbvq4itNl+ hYVi5izuRMO1IkbYC36ycoSmAm2rBQusXf7ipS3gBjmfyHqGnTAvwDs8pWrPP+MZivTn vHfasYtAb2VkV6+zCuq5YMtXp2fB8oMoM75dwgqMfgyNd97lSqJGrK7xkxunvBKjxSPm BYSuISir3nniZxRFY5Pn3Epm0DoIlx2fxG0hZnSZiYB/LKHtbfJ8GUwxMMix4bWD7WL+ s5Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768937287; x=1769542087; 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=IX/TccJwz/UTO00v//xtpL1J4SlmvFVmdImiXICKngs=; b=vG+Fj3769Xdsu6OpNWFj2TmNHrHtwyefj5+tMWLrojvA38O3N3txdLENhd0fNI0OAN TV3M6QLpNB2D5VQ/73GJlxI3WfBOiMHAf60YKHFMhlt4XnzQpH+qghv40egQe3R5yiZP oJUE2Lt3uX+x8UKEzXETTrHxUd2uvK3kgXMnbmLAdXmD+8y+lKAHY9YU3u8Rn2Lokbsg +KIVhN5AzIonmHDb6M4ayxS5P5Gtx2isk0wWRLf+ZclTCUJkHqeLTwW+DJamvQlkZ98K QTrHBR3vtOrONuAsnzHSlAm4Abr6gytnz4EFi01p5lS0NbDpwqNHGYvXWlyGSuWrDWGi 9qRg== X-Gm-Message-State: AOJu0YxVLJcBYFff3IkMAD8INT4XSnVodlOYbC3YYNgMA/GIVbYqn3ki J2e12b6/QlAS5VQx6zj90oDyNEvMhYlVXT7KRRReiARtovYEmhTSPQr6h9/5Fhgn3rbZiOTx8IX HBpk3P6SYCfgkPR31idxxsZUMWV0XzjtsHZs+ X-Gm-Gg: AZuq6aKjoBwEodpRkMgeULFGsdZwpXdAVG3eGL1s3GX1huBFnU6/EZc8NmlS+42rWcU zRhJ5I1NEybR7cYbw3hxPmaeTqrxvQXpnoP4yweSJUgQxVS5ipOUcc9HHUHvNfmJUlZN0CAw9T1 IE34bRpCq3B4o3kzBhgOgj1zrACS8MedJChamrIQxsUzD4Jr7IyWch1A2etNDoU5qBc6NguoZ+L JNe6yEklhhQF+JP7V5UvYfT0KCOAdqeGiV4/DxEUlsi3Ey0aGnqlWTaS6XonsWas3grxQ== X-Received: by 2002:a05:6402:4596:b0:650:5d7e:8a29 with SMTP id 4fb4d7f45d1cf-657ff4feb5emr2529819a12.32.1768937286733; Tue, 20 Jan 2026 11:28:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Tue, 20 Jan 2026 13:27:55 -0600 X-Gm-Features: AZwV_Qhxth5-fskZ-pqXYbNsI0ImDmq8nuP3IU-OAm2QzX2NdROWqrB0gGay4eA Message-ID: Subject: Re: Flush some statistics within running transactions To: Bertrand Drouvot Cc: 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 Thanks for the updates! > > > I don't think this feature could add a noticeable performance impact, so the tests > > > have been that simple. Do you think we should worry more? > > > > One observation is there's no coordination between ANYTIME and > > TXN_BOUNDARY flushes. While PGSTAT_MIN_INTERVAL > > prevents a backend from flushing more than once per second, a backend can > > still perform both an ANYTIME flush and a TXN_BOUNDARY flush within > > the same 1-second window. Not saying this will be a real problem in > > the real-world, > > but we definitely took measures in the current implementation to avoid > > this scenario. > > Right. I think that the PGSTAT_MIN_INTERVAL throttling was put in place to prevent > flushing too frequently when the backend has a high commit rate. But here, while > it's true that we don't follow that rule (means a backend could flush more than one > time per second), that would be a maximum of 2 times (given that ANYTIME is > flushing every second). So, I'm not sure that this single extra flush is worth > worrying about. Plus we'd certainly need an extra GetCurrentTimestamp() call, so > I'm not sure it's worth it. Yeah, all PGSTAT_MIN_INTERVAL does is throttle pgstat_flush_pending_entries. Even in the current state, it does not limit how many kinds are flushed, etc. I consider the ANYTIME flushes the same as just adding another stats kind. So, I am not really worried about either. I have some more comments: -- v2-0001 #1. +/* When to call pgstat_report_anytime_stat() again */ +#define PGSTAT_ANYTIME_FLUSH_INTERVAL 1000 + We should just use PGSTAT_MIN_INTERVAL. #2. instead of ".flush_behavior", maybe ".flush_mode"? "mode" in the name is better for configuration fields. #3. +/* + * Flush behavior for statistics kinds. + */ +typedef enum PgStat_FlushBehavior +{ + FLUSH_ANYTIME, /* All fields can be flushed anytime, + * including within transactions */ + FLUSH_AT_TXN_BOUNDARY, /* All fields can only be flushed at + * transaction boundary */ +} PgStat_FlushBehavior; FLUSH_AT_TXN_BOUNDARY should be the first value in PgStat_FlushBehavior. Otherwise kinds ( built-in or custom ) that do not specify a flush_behavior will default to FLUSH_ANYTIME. I don't think this is what we want. FLUSH_AT_TXN_BOUNDARY should be the default. #4. Can we add a test here? Maybe generate some wal inside a long running transaction and make sure the stats are updated after > 1 second -- v2-0002 No comments for this one. With ANYTIME, indeed those flushes are not needed. -- v2-0003 #1. Should we maybe make this a bit longer? maybe 2 or 3 seconds? May make the tests slightly longer, but maybe better for test stability. ``` +step s1_sleep: SELECT pg_sleep(1.5); +pg_sleep +-------- ``` #2. + /* + * 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) + has_nontxn_stats = true; + + if (!has_nontxn_stats) + return true; Can we just do this without a has_nontxn_stats? This is also the same patter as a regular flush, although in the case `pg_memory_is_all_zeros` is used. ``` 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; ``` #3. + are updated while the transactions are in progress. This means that we can see + those statistics being updated without having to wait until the transaction + finishes. + The "This means ...... " line used several times does not add value, IMO. "are updated while the transactions are in progress." is sufficient. #4. + + + All the statistics are updated while the transactions are in progress, except + for xact_commit, xact_rollback, + tup_inserted, tup_updated and + tup_deleted that are updated only when the transactions + finish. + + Only these 5 fields from pgstat_relation_flush_anytime_cb, so only the below are "All the statistics are updated while the transactions are in progress", right? numscans tuples_returned tuples_fetched blocks_fetched blocks_hit -- Sami Imseih Amazon Web Services (AWS)