agora inbox for pgsql-hackers@postgresql.org
help / color / mirror / Atom feedFrom: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Subject: Fix floating-point noise in pg_stat_us_to_ms()
Date: Mon, 29 Jun 2026 07:02:40 +0000
Message-ID: <akIYkMK4bHe9qX/N@bdtpg> (raw)
Hi hackers,
while reviewing [1], I noticed that the IO timings displayed in pg_stat_io can
produce floating-point noise like:
postgres=# select read_time from pg_stat_io where read_time > 0;
read_time
---------------------
2.2640000000000002
0.08700000000000001
That's because 0.001 cannot be represented exactly in binary floating
point. I think this output looks weird, even if understandable. Note that with
extra_float_digits set to 0 you don't see the noise (but 1 is the default).
The attached patch changes pg_stat_us_to_ms() so that it uses a division
by 1000.0 instead as it's correctly rounded, see for example:
postgres=# SELECT (9 * 0.001::float8)::text;
text
----------------------
0.009000000000000001
(1 row)
postgres=# SELECT (9::float8 / 1000.0)::text;
text
-------
0.009
(1 row)
Given that / 1000.0 is the most common way to do this kind of computation in the
code tree, I think that it makes sense to update pg_stat_us_to_ms() to do so.
Thoughts?
[1]: https://www.postgresql.org/message-id/akH0SxXlXPNjD%2BR5%40bdtpg
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Attachments:
[text/x-diff] v1-0001-Fix-floating-point-noise-in-pg_stat_us_to_ms.patch (1.2K, ../akIYkMK4bHe9qX%2FN@bdtpg/2-v1-0001-Fix-floating-point-noise-in-pg_stat_us_to_ms.patch)
download | inline diff:
From 6e3ee0d38af6cf649355daf8a9afe27355e93fef Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Mon, 29 Jun 2026 06:31:58 +0000
Subject: [PATCH v1] Fix floating-point noise in pg_stat_us_to_ms()
Multiplying by 0.001 can produce trailing-digit noise in displayed
values (for example 0.009000000000000001 instead of 0.009, should extra_float_digits
not being set to 0) because 0.001 cannot be represented exactly in binary floating
point.
Use division by 1000.0 instead as it is the most common way to deal with such
computation in the code tree.
Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
---
src/backend/utils/adt/pgstatfuncs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
100.0% src/backend/utils/adt/
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 6f9c9c72de5..1e5f8293b28 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1457,7 +1457,7 @@ pgstat_get_io_time_index(IOOp io_op)
static inline double
pg_stat_us_to_ms(PgStat_Counter val_ms)
{
- return val_ms * (double) 0.001;
+ return (double) val_ms / 1000.0;
}
/*
--
2.34.1
view thread (4+ messages) latest in thread
Message-ID: <akIYkMK4bHe9qX/N@bdtpg>
Permalink: ../akIYkMK4bHe9qX%2FN@bdtpg/
Also on: postgresql.org/message-id/akIYkMK4bHe9qX/N@bdtpg
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: bertranddrouvot.pg@gmail.com, pgsql-hackers@lists.postgresql.org
Subject: Re: Fix floating-point noise in pg_stat_us_to_ms()
In-Reply-To: <akIYkMK4bHe9qX/N@bdtpg>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox