PostgreSQL’s DOUBLE PRECISION type (also called FLOAT8 or FLOAT) is an inexact, variable-precision numeric type implementing the IEEE 754 double-precision standard. It is best suited for scientific measurements, analytics, and calculations where approximate results are acceptable and query performance matters.
Syntax
column_name DOUBLE PRECISION
column_name FLOAT8 -- alias
column_name FLOAT -- alias (defaults to double precision)
Key characteristics:
| Property | Value |
|---|---|
| Storage | 8 bytes |
| Range | ~1E-307 to 1E+308 |
| Precision | at least 15 significant decimal digits |
| Standard | IEEE 754 double precision |
Practical Example
Store telemetry readings from IoT sensors in an events table:
CREATE TABLE events (
id BIGSERIAL PRIMARY KEY,
sensor_id INTEGER NOT NULL,
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
temperature DOUBLE PRECISION,
pressure_pa DOUBLE PRECISION,
humidity_pct DOUBLE PRECISION
);
INSERT INTO events (sensor_id, temperature, pressure_pa, humidity_pct)
VALUES
(101, 22.437891, 101325.0, 54.2),
(101, 22.501234, 101298.5, 53.8),
(102, 19.883412, 101400.1, 61.0),
(103, 25.112000, 100980.7, 47.5);
SELECT
sensor_id,
AVG(temperature) AS avg_temp,
MIN(pressure_pa) AS min_pressure,
AVG(humidity_pct) AS avg_humidity
FROM events
GROUP BY sensor_id;
The Inexact Nature of DOUBLE PRECISION
Because DOUBLE PRECISION uses binary floating-point, some decimal fractions cannot be represented exactly:
SELECT 0.1::double precision + 0.1::double precision + 0.1::double precision;
?column?
---------------------
0.30000000000000004
This is expected IEEE 754 behavior. For financial data, use NUMERIC instead. Avoid equality comparisons on DOUBLE PRECISION columns — use range comparisons:
-- Avoid:
WHERE temperature = 22.5
-- Prefer:
WHERE temperature BETWEEN 22.49 AND 22.51
Special Values
DOUBLE PRECISION supports IEEE 754 special values:
INSERT INTO events (sensor_id, temperature) VALUES (999, 'Infinity');
INSERT INTO events (sensor_id, temperature) VALUES (999, '-Infinity');
INSERT INTO events (sensor_id, temperature) VALUES (999, 'NaN');
-- NaN comparisons: NaN is not equal to itself (IEEE 754)
SELECT 'NaN'::double precision = 'NaN'::double precision; -- false
Testing with Vela
Before deploying an analytical schema that uses DOUBLE PRECISION columns, test aggregation queries on a database branch populated with production-scale data. Accumulated floating-point errors are only apparent at scale, and a branch lets you benchmark SUM, AVG, and STDDEV queries without risk to the live database.
Production Tips
- Never use
DOUBLE PRECISIONfor monetary values — useNUMERICto avoid floating-point rounding errors in financial calculations. - Avoid equality comparisons on
DOUBLE PRECISIONvalues — use range comparisons to account for floating-point imprecision. - Use
DOUBLE PRECISIONfor scientific measurements, GPS coordinates, and machine learning feature values where approximate precision is acceptable. - Arithmetic on
DOUBLE PRECISIONis significantly faster thanNUMERIC, making it appropriate for analytical workloads on large datasets. - Use
EXPLAIN ANALYZEto verify that indexes onDOUBLE PRECISIONcolumns are used by range queries as expected.