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

From: Haibo Yan <tristan(dot)yim(at)gmail(dot)com>
To: Xuneng Zhou <xunengzhou(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-12-01 00:41:57
Message-ID: CABXr29Fk+J8ODdxMCYmHsKeBqzGqpMBoFGrHT0QCPSV1ph1sfQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Xuneng,

On Sun, Nov 22, 2026 at 6:18 PM Xuneng Zhou <xunengzhou(at)gmail(dot)com> wrote:
>
> >
> > 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);

I tested this approach, both with and without the time-conversion
change. It certainly simplifies the integer validation path, but
I think it also raises a broader question about the intended
semantics of numeric parsing in PostgreSQL.

The current series deliberately preserves the existing rounding
behavior wherever possible. Your proposal would establish a new
invariant: a nonzero input must never round to integer zero,
regardless of whether zero has special meaning to the caller.

There is also a remaining issue with parse_real(). Removing
intermediate rounding for time units resolves the known real-valued
time GUC cases, but not necessarily those using memory units.

I reproduced this with a test extension defining a real-valued
memory GUC with kB as its base unit and zero as its minimum:

'-0.0004MB' -> -0.4096kB -> -0.0kB

Since we intend to retain intermediate rounding for memory units,
a check in parse_int() alone would not cover this case.
>
> 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.

I agree. Silently turning a finite timeout into an unlimited wait,
for example, is unlikely to be what the user intended.

The question is whether we want to extend this principle to all
integer parameters, including those where zero is simply an ordinary
numeric value rather than a special sentinel.

>
> 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.

My testing confirmed that the compatibility impact extends beyond
time-related GUCs. Some existing regression and TAP tests rely on
nonzero values rounding to zero, for example:

min_parallel_table_scan_size = '1kB'
max_slot_wal_keep_size = '64kB'

However, I suspect the practical impact of these memory-related
changes would be quite small, since such values are unlikely to be
used in production.

The implications for time settings and persisted options may be
more relevant. For example, an existing subscription with
wal_receiver_timeout = '0.4' can encounter repeated apply-worker
failures when the stored value is reparsed under the new rule.

That does not necessarily mean we should preserve the existing
behavior indefinitely, but it seems worth considering as part of
the compatibility decision.
>
> 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.

Agreed. I think retaining intermediate rounding for memory units
while removing it for time units is a reasonable direction. This
change can be implemented independently of the zero-value policy.

> 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.

I think there are three questions worth settling before deciding on
the implementation:

1. Should we establish a general rule that nonzero numeric inputs
must never round to integer zero, regardless of the caller's
interpretation of zero?

2. If so, are we willing to accept the resulting compatibility
changes in a major release?

3. If we adopt this rule, what is the minimal additional change
needed to address the remaining sign-loss cases in parse_real()?

I am open to replacing the current sign-preservation approach with
a simpler implementation if we agree on the broader semantics and
can retain the required correctness guarantees.

>
> 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.

It would be good to hear others' thoughts on these questions before
we decide which implementation to pursue.

Thanks,
Haibo

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Manu 2026-12-01 00:49:42 Re: Reject columns of permanent relations that depend on temporary objects
Previous Message wenhui qiu 2026-12-01 00:41:12 Re: Simplify the uniqueness proof for self-join elimination