diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2020-11-10 22:51:19 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2020-11-10 22:51:56 -0500 |
commit | 3a89ea0eb64a1b50b27de1965fdb2173a04d5e11 (patch) | |
tree | 37416b36a84dfedc269eb48d49e839219af80d5a /contrib/pg_prewarm/autoprewarm.c | |
parent | 9fed2b5b2eddba03bda7d62376186b141630ce38 (diff) | |
download | postgresql-3a89ea0eb64a1b50b27de1965fdb2173a04d5e11.tar.gz postgresql-3a89ea0eb64a1b50b27de1965fdb2173a04d5e11.zip |
Fix and simplify some usages of TimestampDifference().
Introduce TimestampDifferenceMilliseconds() to simplify callers
that would rather have the difference in milliseconds, instead of
the select()-oriented seconds-and-microseconds format. This gets
rid of at least one integer division per call, and it eliminates
some apparently-easy-to-mess-up arithmetic.
Two of these call sites were in fact wrong:
* pg_prewarm's autoprewarm_main() forgot to multiply the seconds
by 1000, thus ending up with a delay 1000X shorter than intended.
That doesn't quite make it a busy-wait, but close.
* postgres_fdw's pgfdw_get_cleanup_result() thought it needed to compute
microseconds not milliseconds, thus ending up with a delay 1000X longer
than intended. Somebody along the way had noticed this problem but
misdiagnosed the cause, and imposed an ad-hoc 60-second limit rather
than fixing the units. This was relatively harmless in context, because
we don't care that much about exactly how long this delay is; still,
it's wrong.
There are a few more callers of TimestampDifference() that don't
have a direct need for seconds-and-microseconds, but can't use
TimestampDifferenceMilliseconds() either because they do need
microsecond precision or because they might possibly deal with
intervals long enough to overflow 32-bit milliseconds. It might be
worth inventing another API to improve that, but that seems outside
the scope of this patch; so those callers are untouched here.
Given the fact that we are fixing some bugs, and the likelihood
that future patches might want to back-patch code that uses this
new API, back-patch to all supported branches.
Alexey Kondratov and Tom Lane
Discussion: https://postgr.es/m/3b1c053a21c07c1ed5e00be3b2b855ef@postgrespro.ru
Diffstat (limited to 'contrib/pg_prewarm/autoprewarm.c')
-rw-r--r-- | contrib/pg_prewarm/autoprewarm.c | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c index 3bd0010bf8b..fcbdbdd0279 100644 --- a/contrib/pg_prewarm/autoprewarm.c +++ b/contrib/pg_prewarm/autoprewarm.c @@ -226,18 +226,16 @@ autoprewarm_main(Datum main_arg) } else { - long delay_in_ms = 0; - TimestampTz next_dump_time = 0; - long secs = 0; - int usecs = 0; + TimestampTz next_dump_time; + long delay_in_ms; /* Compute the next dump time. */ next_dump_time = TimestampTzPlusMilliseconds(last_dump_time, autoprewarm_interval * 1000); - TimestampDifference(GetCurrentTimestamp(), next_dump_time, - &secs, &usecs); - delay_in_ms = secs + (usecs / 1000); + delay_in_ms = + TimestampDifferenceMilliseconds(GetCurrentTimestamp(), + next_dump_time); /* Perform a dump if it's time. */ if (delay_in_ms <= 0) |