postgres.git / summary / log / commit / refs

commit    776df8a6d7c42282825d658ff631509f033e8f34
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Tue Oct 20 16:56:14 2026 +0000
First released in: 19.0

    Fix to_char() for number formats ending in a decimal point.
    
    When a number format has a decimal point with no digit positions
    after it (e.g. '9.' or 'FM999.PL'), NUM_numpart_to_char() copied the
    terminating '\0' of the number string into the output as if it were a
    digit, resulting in apparent truncation of the output; for example,
    a sign or literal text following the decimal point would disappear.
    Fix it by not copying the '\0'.
    
    This is clearly a bug, but given the lack of field complaints,
    people who have run into it may think it's expected behavior,
    and may even have written applications that depend on it.
    Therefore it seems unwise to change it in stable branches.
    I (tgl) think it's not too late to fix it in v19 though.
    
    Bug: #19771
    Author: Gowtham Reddy <gowthamdoma18@gmail.com>
    Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
    Discussion: https://postgr.es/m/19771-4f173b891525020c@postgresql.org
    Backpatch-through: 19


src/backend/utils/adt/formatting.c | 4 ++-- src/test/regress/expected/numeric.out | 6 ++++++ src/test/regress/sql/numeric.sql | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 900fa8f20e5..8b5f99e6a82 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -5659,13 +5659,13 @@ NUM_numpart_to_char(NUMProc *Np, int id) NUM_EMITC('0'); } } - else + else if (*Np->input_p) { NUM_EMITC(*Np->input_p); /* Write DIGIT */ Np->num_in = true; } } - /* do no exceed string length */ + /* do not advance past end of string */ if (*Np->input_p) ++Np->input_p; } diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out index fca2cb46c8c..6269d5bb397 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -2124,6 +2124,12 @@ SELECT to_char('100'::numeric, 'FM999.'); 100 (1 row) +SELECT to_char('100'::numeric, 'FM999.SG "items"'); + to_char +------------ + 100+ items +(1 row) + SELECT to_char('100'::numeric, 'FM999'); to_char --------- diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 5fd5c02f636..e63b21e4b6b 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -1035,6 +1035,7 @@ FROM v; SELECT to_char('100'::numeric, 'FM999.9'); SELECT to_char('100'::numeric, 'FM999.'); +SELECT to_char('100'::numeric, 'FM999.SG "items"'); SELECT to_char('100'::numeric, 'FM999'); SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000'); [parent: e75fcc9867fd]