| From: | Dave Sharpe <davesharpe13(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | Peter Eisentraut <peter(at)eisentraut(dot)org> |
| Subject: | [PATCH] Round fractional Julian days to the nearest microsecond |
| Date: | 2026-12-06 15:46:26 |
| Message-ID: | CAKFmFJ-7HVBEKraC6KvErpVrrJ54rth9nGZ-yROiAu8RaDXREg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Julian-day input with a fractional part truncates the day fraction to
microseconds instead of rounding it, so the result can be one
microsecond low:
SELECT 'J2451187.7'::timestamp; -- 1999-01-08 16:47:59.999999
SELECT '0.7 day'::interval; -- 16:48:00
0.7 * USECS_PER_DAY is 60479999999.99999 as a double. The interval
code rounds that (AdjustFractMicroseconds), as does
ParseFractionalSecond(), but the Julian-day cases in DecodeDateTime()
and DecodeTimeOnly() pass the double to dt2time(), whose Timestamp
parameter truncates it. This is not so rare: ~ 6% of all fractions
of one to six digits come out one microsecond low (1 of 10 one-digit
fractions, 4 of 100 two-digit ones: .35, .57, .69, .7). It affects
timestamp, timestamptz, time, and timetz input.
I mentioned this in Peter's "several cases of float and integer mixup"
thread [1], where his 0005 made the truncation explicit in ecpg
without changing behaviour. Given the explicit cast, I noticed it would
truncate, and Peter agreed it could be improved separately (here).
The attached patches round with rint() before the conversion:
0001 fixes the two backend sites in datetime.c and adds regression
tests to horology.sql.
0002 fixes the copy in ecpg's pgtypeslib (dt_common.c) and adds a test
to ecpg's dt_test2.pgc.
Behaviour change: a fraction within half a microsecond of a whole day
now rounds up to the full day. For example, with
'J2451187.999999999999':
before after
timestamp 1999-01-08 23:59:59.999999 1999-01-09 00:00:00
timestamptz 1999-01-08 23:59:59.999999+00 1999-01-09 00:00:00+00
timetz 23:59:59.999999+00 24:00:00+00
This is not new territory: a fraction close enough to 1 that it is
already 1.0 as a double ('J2451187.99999999999999999') gives exactly
the "after" results on master today, and '24:00:00' is accepted time
input anyway. Rounding can never exceed a full day, so 24:00:00 is
the maximum. I added no clamping, since this matches what master
already does for inputs that reach a full day; happy to change that if
people would rather see 23:59:59.999999.
The tests cover .7, .35 and the rounding-up edge for timestamp,
timestamptz and timetz, and for ecpg's PGTYPEStimestamp_from_asc().
All of the new tests fail without the code change.
Interaction with Peter's series: my 0002 touches the same line as his
0005 ("ecpg: Fix type of dt2time() argument"), so whichever goes in
second has a one-line textual conflict. 0002 is written to be correct
with either dt2time() signature (it rounds into the double, then passes
(timestamp) time), so the resolution is to keep the 0002 version of
that line. 0001 does not overlap with his series.
While looking at this, I noticed that pgtypeslib's copy has other
truncation of fractional seconds (the DTK_SECOND case in
DecodeDateTime(), DecodeNumber() assigning strtod() to fsec, and the
existing XXX comment about truncating the seventh digit). Those are not
addressed here; I can look at them in a follow-up if there is
interest.
The patches are against master (8d90516d76d) and intended for
commit. They build without warnings and pass the full meson test
suite. No catversion bump is needed. I have not benchmarked it; the
only added cost is one rint() call per fractional Julian-day input.
[1] https://postgr.es/m/35169b31-3c1a-4042-8e4f-119e6bb813b6@eisentraut.org
Regards,
Dave Sharpe
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0002-ecpg-Round-fractional-Julian-days-to-the-nearest-.patch | application/octet-stream | 4.4 KB |
| v1-0001-Round-fractional-Julian-days-to-the-nearest-micro.patch | application/octet-stream | 6.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zhijie Hou | 2026-12-06 16:02:22 | pg_combinebackup copies stale tablespace_map, overriding -T on recovery |
| Previous Message | Tomas Vondra | 2026-12-06 15:21:49 | Re: BRIN re-summarization is not properly logging snapshotConflictHorizon / wrong results on hot standby |