Re: Avoiding roundoff error in pg_sleep()

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Nathan Bossart <nathandbossart@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2025-09-25T21:05:45Z
Lists: pgsql-hackers
Nathan Bossart <nathandbossart@gmail.com> writes:
> Agreed, I'm not too worried about the system calls in this case.  I think I
> was more interested in seeing whether we could avoid the complicated float
> handling.  Something else that seems to work is moving the initial endtime
> calculation to within the loop, like so:

> -        delay = endtime - GetNowFloat();
> +        if (first)
> +        {
> +            endtime = GetNowFloat() + secs;
> +            delay = secs;
> +            first = false;
> +        }
> +        else
> +            delay = endtime - GetNowFloat();

Hmm ... I'm not sure I'd trust that to give the right answer
if WaitLatch is interrupted so that we have to recalculate
the delay.

> But, in any case, your patch still LGTM.

Pushed it.  Again, thanks for reviewing!

			regards, tom lane



Commits

  1. Try to avoid floating-point roundoff error in pg_sleep().