| From: | Xuneng Zhou <xunengzhou(at)gmail(dot)com> |
|---|---|
| To: | Haibo Yan <tristan(dot)yim(at)gmail(dot)com> |
| Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> |
| Subject: | Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing |
| Date: | 2026-11-23 02:18:28 |
| Message-ID: | CABPTF7XUX54pcRnj4Fc19AX1K+gRnAovTwLBzGzOvOqi4JDXGg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
>
> Thanks for pointing this out. I agree that the intermediate rounding
> in convert_to_base_unit() can cause much more significant precision
> loss than the examples in my original patch.
>
> I investigated this further, including testing a version that skips
> intermediate rounding for time units while preserving the existing
> behavior for memory units.
>
> The results confirm your observation. For example:
>
> statement_timeout = '0.144min'
> current: 9000ms
> proposed: 8640ms
>
> statement_timeout = '0.020d'
> current: 0 (timeout disabled)
> proposed: 1728000ms
>
> statement_timeout = '0.021d'
> current: 3600000ms
> proposed: 1814400ms
>
> The second example is particularly concerning, since a positive
> timeout of 28.8 minutes currently becomes zero, disabling the timeout
> entirely.
Thanks for confirming!
> I think this is a separate issue from the sign-preservation patch.
> The current series addresses the loss of the original input sign
> during lossy conversion and rounding, which can allow negative values
> to bypass validation. Even without intermediate rounding, a value
> such as -0.4ms can still round to zero when stored as an integer
> number of milliseconds. So improving conversion precision would not
> eliminate the need for the current patch.
Yeah, this is an issue. I wonder whether we could add a check in
parse_int() instead of placing that in caller side.
written_nonzero = (val != 0); /* remember before conversion */
...unit conversion...
/* A nonzero value must not round to 0; zero has to be written as 0. */
if (written_nonzero && rint(val) == 0)
{
if (hintmsg)
*hintmsg = gettext_noop("Use 0 explicitly, or a value of at
least one unit.");
return false;
}
val = rint(val);
With this, '-0.4ms' is rejected, and so are the positive cases such as
wal_receiver_status_interval = '400ms' -> 0 (disabled) today
After this, there're some cases of less-than-unit sized values being
rounded to zero. With this check, some cases would raise errors.
parameter/value HEAD
V1 parse_int check
statement_timeout = '0.4ms' 0 (disabled)
0 (disabled) error
wal_receiver_status_interval = '400ms' 0 (disabled)
0 (disabled) error
I kinda think that erroring out is slightly better than rounding a finite
input into disabling the parameter.
log_min_duration_statement = '0.4ms' 0
0 error
parallel_workers = 0.4 0
0 error
Tom suggested this in 2014 [1]: a value below one unit is likely a
mistake, so reject it; 0 must be written as 0.
However, with this approach, the upgrade compatibility problem could sneak
in..
[1] https://postgr.es/m/8651.1411755440@sss.pgh.pa.us
> There is also a compatibility question with changing the rounding
> behavior. The existing next-smaller-unit rule was introduced primarily
> for memory settings, and is documented for unit conversion generally.
> Removing it for time units produces more accurate values, but can also
> change the interpretation of existing configuration files. For example:
>
> checkpoint_timeout = '1.0001d'
> current: 86400s (accepted)
> proposed: 86409s (rejected)
>
> A previously valid configuration could therefore prevent a server
> from starting after an upgrade. Other values, such as '0.020d', would
> silently acquire a nonzero timeout where they previously disabled it.
I think both show the same problem: the range check sees the rounded
value, not the one written. It seems good to restore the intended
behavior if we state the change clearly.
> Before proposing another patch, I think it would be useful to agree
> on the intended semantics:
Agree. These are well-considered questions.
> - Should time values be converted to the parameter's base unit and
> rounded only to that unit's resolution, while memory values retain
> the existing next-smaller-unit rounding?
> - Or should time and memory conversions follow the same rounding rule?
The next-unit rounding exists only so that SHOW prints a short and
readable value. I think memory needs it, but time doesn't.
Memory units are powers of 2 (1GB = 2^20kB = 1,048,576kB), so a
decimal fraction of them is rarely whole: '0.1GB' is 104857.6kB, and
SHOW would print 104858kB. Rounding to whole MB gives 102MB, with a
loss of at most half a MB. So keeping it is sensible.
Time units are powers of 10 (1d = 864 * 10^5ms = 86,400,000ms), so a
short decimal fraction is already whole: '0.1d' is exactly
8,640,000ms, so SHOW prints 144min, which is already nice. Therefore
rounding here only adds greater loss.
> - How much compatibility impact from changing previously accepted
> fractional values would be acceptable?
That's a concern here, but I expect the impact to be small and
manageable. Only misread values today would change, and the ones that
error out would fail with a clear message and be easy to fix. Besides
that, users are expected to rehearse and run a checklist before a
major upgrade. It would be good to hear others' thoughts on this.
> My preference is to separate unit conversion from the final rounding
> policy conceptually, while keeping any implementation change as small
> as possible. The time-only approach looks promising, but I would
> rather settle the intended behavior first.
>
> Once there is agreement, I can prepare a separate patch for the
> time-conversion behavior. I have also tested it together with the
> current sign-preservation series, and found no additional functional
> regressions in the tested cases, apart from an expected error-message
> difference.
Make sense to me.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Peter Smith | 2026-11-23 02:48:22 | Re: Initial table sync copies the whole table when it is no longer published |
| Previous Message | Manu | 2026-11-23 00:44:20 | Re: Use an index scan in RemoveSubscriptionRel() |