Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing

Поиск
Список
Период
Сортировка
Искать

Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
Hi Haibo,

Thanks for picking this up and sorry for the late reply.

On Sat, Sep 12, 2026 at 5:16 AM Haibo Yan  wrote:
>
> Hi hackers,
>
> While looking at the WAIT FOR timeout parsing issue, I noticed that the
> underlying problem is not specific to WAIT FOR.  parse_int() and parse_real()
> can lose information about the original value during unit conversion and
> rounding, before the caller gets a chance to validate it.
> For example, these are currently accepted:
>
> SET statement_timeout = '-0.5ms';
> CREATE TABLE t (i int) WITH (parallel_workers = -0.4);
> VACUUM (BUFFER_USAGE_LIMIT '-512B') t;
>
> The first is stored as 0, the reloption is accepted after -0.4 rounds to 0, and
> the VACUUM option is interpreted as the special value 0 (“no ring
> buffer limit”).
> WAIT FOR has the same issue: a timeout of ‘-0.4ms’ passes the negative check
> after rounding and becomes an indefinite wait.

I agree the problem is not specific to WAIT. But I kinda think the
lost sign is one symptom of the rounding issue during the parsing
stage. What we need to do is to read the time value exactly as it is.
I'll reply later after thinking more.

CCed Sawada-san since he suggested dealing with this separately.


--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.


Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
"This series deliberately does not change the existing behavior for positive GUC
values that round to zero.  For example, statement_timeout = ‘0.4ms’ still
becomes 0.  It also does not address the separate parse_real() unit-conversion
precision issue."

I am curious why you chose to preserve the existing behavior for
positive GUC values to zero?
Another more concerning drawback for this conversion is that it does
not respect the original value.

it could give wrong values for these:

    statement_timeout = '0.144min'          -> 9s     (8.64 s)
    statement_timeout = '0.021d'            -> 1h     (30.24 min)

I think this results from 1a83a80a2fe (PG12), convert_to_base_unit()
rounds a fractional value to the next smaller unit than the input one.
It was added for the sake of showing memory settings nicely, but
without taking time value into proper consideration [2].

    cvalue = rint(cvalue / table[i + 1].multiplier) *
        table[i + 1].multiplier;

Converting time exactly without next-unit rounding seems able to cover
both the truncation and zeroing well. I understand that this is a
broader behavior change. But it seems worthwhile to trade a bit of
readability for accuracy.

    '0.144min'                  -> 8640ms
    '0.020d'                    -> 1728s
    '0.021d'                    -> 1814400ms
   '-0.4ms', '0.4ms'           -> rejected by rule 2

Does this seem sensible to you?

[1] https://postgr.es/m/20054.1552251519@sss.pgh.pa.us
[2] https://postgr.es/m/20190312.124559.221664593.horiguchi.kyotaro@lab.ntt.co.jp

-- 
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.


Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 3:57 AM Xuneng Zhou  wrote:
>
> "This series deliberately does not change the existing behavior for positive GUC
> values that round to zero.  For example, statement_timeout = ‘0.4ms’ still
> becomes 0.  It also does not address the separate parse_real() unit-conversion
> precision issue."
>
> I am curious why you chose to preserve the existing behavior for
> positive GUC values to zero?
> Another more concerning drawback for this conversion is that it does
> not respect the original value.
>
> it could give wrong values for these:
>
>     statement_timeout = '0.144min'          -> 9s     (8.64 s)
>     statement_timeout = '0.021d'            -> 1h     (30.24 min)
>
> I think this results from 1a83a80a2fe (PG12), convert_to_base_unit()
> rounds a fractional value to the next smaller unit than the input one.
> It was added for the sake of showing memory settings nicely, but
> without taking time value into proper consideration [2].
>
>     cvalue = rint(cvalue / table[i + 1].multiplier) *
>         table[i + 1].multiplier;
>
> Converting time exactly without next-unit rounding seems able to cover
> both the truncation and zeroing well. I understand that this is a
> broader behavior change. But it seems worthwhile to trade a bit of
> readability for accuracy.
>
>     '0.144min'                  -> 8640ms
>     '0.020d'                    -> 1728s
>     '0.021d'                    -> 1814400ms
>    '-0.4ms', '0.4ms'           -> rejected by rule 2
>
> Does this seem sensible to you?

Hi Xuneng,

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.

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.

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.

Before proposing another patch, I think it would be useful to agree
on the intended semantics:

- 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?
- How much compatibility impact from changing previously accepted
  fractional values would be acceptable?

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.

>
> [1] https://postgr.es/m/20054.1552251519@sss.pgh.pa.us
> [2] https://postgr.es/m/20190312.124559.221664593.horiguchi.kyotaro@lab.ntt.co.jp
>
> --
> Regards,
> Xuneng Zhou
> HighGo Software Co., Ltd.

Thanks,
Haibo


Re: [PATCH v1 0/2] Preserve input sign across numeric parameter parsing

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
>
> 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.
FAQ