Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which PostgreSQL column type should be used to store a Java BigDecimal?

People also ask

What is BigDecimal data type?

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.

Is Java BigDecimal immutable?

Since BigDecimal is immutable, these operations do not modify the existing objects. Rather, they return new objects.


See PostgreSQL datatypes - perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension).

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

I am not entirely sure what "implementation limit on precision is", though. Have never tried really large numbers. If the limit is reached, then a fallback to text is possible. But, I suspect there will be significant other issues before then ;-) If you wish to store less, then specify a precision and/or scale to numeric.

Edit as sjr pointed out, the restriction is 1000 decimal digits of precision (from the same link):

The type numeric can store numbers with up to 1000 digits of precision [in currentl implementations] and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required...

If more precision is needed - despite having a much bigger problem at hand - then a numeric column will not be suitable (by itself). But this is really more of a very extreme "what if" and likely does not play a limiting role.


Just use the Java mappings for the common SQL data types. In this case you can use a NUMERIC or DECIMAL.