pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Improve TimestampDifferenceMilliseconds to cope with overflow sa
Date: Thu, 26 Jan 2023 22:09:21 +0000
Message-ID: <E1pLAQq-00057N-Cg@gemulon.postgresql.org> (raw)

Improve TimestampDifferenceMilliseconds to cope with overflow sanely.

We'd like to use TimestampDifferenceMilliseconds with the stop_time
possibly being TIMESTAMP_INFINITY, but up to now it's disclaimed
responsibility for overflow cases.  Define it to clamp its output to
the range [0, INT_MAX], handling overflow correctly.  (INT_MAX rather
than LONG_MAX seems appropriate, because the function is already
described as being intended for calculating wait times for WaitLatch
et al, and that infrastructure only handles waits up to INT_MAX.
Also, this choice gets rid of cross-platform behavioral differences.)

Having done that, we can replace some ad-hoc code in walreceiver.c
with a simple call to TimestampDifferenceMilliseconds.

While at it, fix some buglets in existing callers of
TimestampDifferenceMilliseconds: basebackup_copy.c had not read the
memo about TimestampDifferenceMilliseconds never returning a negative
value, and postmaster.c had not read the memo about Min() and Max()
being macros with multiple-evaluation hazards.  Neither of these
quite seem worth back-patching.

Patch by me; thanks to Nathan Bossart for review.

Discussion: https://postgr.es/m/3126727.1674759248@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3a28d78089289794fda86cdbd275fc4756c6c6aa

Modified Files
--------------
src/backend/backup/basebackup_copy.c  |  3 ++-
src/backend/postmaster/postmaster.c   | 11 ++++++-----
src/backend/replication/walreceiver.c | 12 +++---------
src/backend/utils/adt/timestamp.c     | 21 +++++++++++++--------
4 files changed, 24 insertions(+), 23 deletions(-)



Message-ID: <E1pLAQq-00057N-Cg@gemulon.postgresql.org>
Permalink:  ../E1pLAQq-00057N-Cg@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1pLAQq-00057N-Cg@gemulon.postgresql.org

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Improve TimestampDifferenceMilliseconds to cope with overflow sa
  In-Reply-To: <E1pLAQq-00057N-Cg@gemulon.postgresql.org>
  References: <E1pLAQq-00057N-Cg@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox