PostgreSQL adds trailing zeros to numeric

后端 未结 3 1403
误落风尘
误落风尘 2021-01-12 07:23

Recently I migrated a DB to PostgreSQL that has some columns defined as numeric(9,3) and numeric(9,4). In testing the app I have found that when da

相关标签:
3条回答
  • 2021-01-12 07:53

    If you specify a precision and scale, Pg pads to that precision and scale.

    regress=> SELECT '0'::NUMERIC(8,4);
     numeric 
    ---------
      0.0000
    (1 row)
    

    There's no way to turn that off. It's still the same number, and the precision is defined by the type, not the value.

    If you want to have the precision defined by the value you have to use unconstrained numeric:

    regress=> SELECT '0'::NUMERIC, '0.0'::NUMERIC;
     numeric | numeric 
    ---------+---------
           0 |     0.0                                                                                                                                                             
    (1 row)                                                                                                                                                                        
    
    0 讨论(0)
  • 2021-01-12 07:55

    I think this is it, if I am understanding "coerce" correctly in this context. This is from the PostgreSQL docs:

    Both the maximum precision and the maximum scale of a numeric column can be configured. To declare a column of type numeric use the syntax:

    NUMERIC(precision, scale)
    

    The precision must be positive, the scale zero or positive. Alternatively:

    NUMERIC(precision)
    

    selects a scale of 0. Specifying:

    NUMERIC
    

    without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. A column of this kind will not coerce input values to any particular scale, whereas numeric columns with a declared scale will coerce input values to that scale.

    Bold emphasis mine.

    So it is misleading later in the same section:

    Numeric values are physically stored without any extra leading or trailing zeroes. Thus, the declared precision and scale of a column are maximums, not fixed allocations.

    Bold emphasis mine again.

    This may be true of the precision part, but since the scale is being coerced when it is defined, trailing zeros are being added to the input values to meet the scale definition (and I would assume truncated if too large).

    I am using precision,scale definitions for constraint enforcement. It is during the DB insert that the trailing zeros are being added to the numeric scale, which seems to support the coercion and conflicts with the statement of no trailing zeros being added.

    Correct or not, I had to handle the problem in code after the select is made. Lucky for me the impacted attributes are BigDecimal so stripping trailing zeros was easy (albeit not graceful). If someone out there has a better suggestion for not having PostgreSQL add trailing zeros to the numeric scale on insert, I am open to them.

    0 讨论(0)
  • 2021-01-12 07:57

    You can strip training zeros with the trim_scale function from PostgreSQL v13 on. That will reduce the storage size of the number.

    0 讨论(0)
提交回复
热议问题