AnalyticDB for PostgreSQL supports the FIXNUMERIC data type, which combines fixed precision with integer-based storage. In compute-intensive scenarios, the FIXNUMERIC type significantly outperforms native NUMERIC or DECIMAL types, delivering better performance for aggregation and basic arithmetic operations while maintaining lossless precision within specific ranges.
Introduction
FIXNUMERIC is a fixed-precision decimal data type in AnalyticDB for PostgreSQL, designed to provide better performance and more efficient storage than native NUMERIC or DECIMAL types. As a high-performance subset of NUMERIC or DECIMAL types, FIXNUMERIC maps decimals to integers by using integer-based storage mechanisms (INT 64 and INT 128) and a preset scale (the number of decimal places). The FIXNUMERIC type prevents the overheads of simulating decimal calculations for traditional NUMERIC or DECIMAL types and the precision loss caused by binary representation errors in floating-point calculations. The FIXNUMERIC type is suitable for scenarios that require high-precision calculations, controllable data ranges, high performance, and large disk space.
The FIXNUMERIC type has two modes:
FIXNUMERIC: implemented based on 64-bit integers with a fixed precision of(19,6). It supports up to 19 digits that include 6 decimal places, equivalent toNUMERIC(19,6).FIXNUMERICis suitable for small and medium-scale data calculation scenarios to balance performance and storage efficiency.FIXNUMERIC128: implemented based on 128-bit integers with a fixed precision of(38,10). It supports up to 38 digits that include 10 decimal places, equivalent toNUMERIC(38,10).FIXNUMERIC128is suitable for scenarios that require large-range and high-precision data calculations and excels particularly in large-scale data analysis scenarios.
Comparison with the NUMERIC type
Feature | FIXNUMERIC or FIXNUMERIC128 | NUMERIC |
Precision | Fixed (6 or 10 decimal places by default) | Variable |
Range | Limited (based on 64-bit or 128-bit integers) | Almost unlimited |
Performance | High (through operations based on native CPU instruction sets) | Low |
Rounding method | Round toward zero | Round to nearest |
NaN support | Not supported | Supported |
Limits
Only AnalyticDB for PostgreSQL V7.0 instances of V7.2.1.1 or later support the fixnumeric extension.
NoteFor information about how to view the minor version of an AnalyticDB for PostgreSQL instance, see View the minor version of an instance. If your instance does not meet the preceding requirements, update the minor version of the instance. For more information, see UpgradeDBVersion.
AnalyticDB for PostgreSQL instances in Serverless mode do not support the fixnumeric extension.
FIXNUMERICandFIXNUMERIC128types have fixed value ranges. When these value ranges are exceeded, an overflow error occurs.Unlike native
DECIMALorNUMERICtypes, theFIXNUMERICtype has a fixed scale when columns are created. You do not need to explicitly define the scale when you create a table because the scale is preset at the underlying level.
Installation and uninstallation
The fixnumeric extension does not support GUI-based installation. To install the fixnumeric extension, submit a ticket. After the extension is installed, you must restart your AnalyticDB for PostgreSQL instance. To uninstall the fixnumeric extension, submit a ticket.
Examples
The FIXNUMERIC type can be used like other data types in AnalyticDB for PostgreSQL and can seamlessly replace floating-point types, such as FLOAT and DOUBLE PRECISION.
Step 1: Create a table and write data to the table
Create a table named test_t with the FIXNUMERIC data type and write test data to the table.
CREATE TABLE test_t(a float, b numeric(14,6), c fixnumeric, d fixnumeric128);
INSERT INTO test_t SELECT v+0.1,v+0.1,v+0.1,v+0.1 FROM generate_series(1,1000) v;Step 2: Create indexes
The FIXNUMERIC type supports indexes. Create indexes for the test_t table.
CREATE INDEX ON test_t (c);
CREATE INDEX ON test_t (d);Step 3: Query data
Query the
SUMvalues of the corresponding columns.SELECT SUM(a), SUM(b), SUM(c), SUM(d) FROM test_t ;The
FLOATtype returns a less accurate approximate value. TheNUMERIC,FIXNUMERIC, andFIXNUMERIC128types return exact values.sum | sum | sum | sum --------------------+---------------+-------------------+------------------- 500600.00000000175 | 500600.000000 | 500600.0000000000 | 500600.0000000000 (1 row)Query the
AVGvalues of the corresponding columns.SELECT AVG(a), AVG(b), AVG(c), AVG(d) FROM test_t;The
FLOATtype returns an approximate value. TheNUMERIC,FIXNUMERIC, andFIXNUMERIC128types return exact values.avg | avg | avg | avg -------------------+----------------------+------------+---------------- 500.6000000000017 | 500.6000000000000000 | 500.600000 | 500.6000000000 (1 row)
Supported functions
The FIXNUMERIC type provides high compatibility and scalability. It supports all aggregate functions, such as SUM, AVG, MIN, and MAX, and is compatible with most functions that support native DECIMAL or NUMERIC types. The following table describes the supported functions.
Function name | Description |
| Calculates the sum, average, minimum, or maximum value. |
| Checks whether a value falls within a range. |
| Returns an absolute value. |
| Returns the sign of a parameter value, such as -1, 0, or +1. |
| Rounds a number to the nearest integer with the |
| Truncates a number to a specific number of decimal places. |
| Rounds up a number to the nearest integer. |
| Rounds up a number to the nearest integer (alias for |
| Rounds down a number to the nearest integer. |
| Performs addition, subtraction, multiplication, or division. |
| Acts as a comparison operator, such as equal to, greater than, less than, less than or equal to, greater than or equal to, or not equal to. |
| Returns a remainder. |
| Returns a square root with the |
| Returns a natural exponent with the |
| Returns a natural logarithm with the |
| Performs a power operation with the |
| Performs a power operation with the |
| Returns the number of decimal places. |
| Performs a logarithmic operation with the |
| Performs a base-10 logarithmic operation with the |
| Converts a number to a string with the |