The PostgreSQL REAL type (also known as FLOAT4) stores single-precision floating-point numbers using 4 bytes. It is an inexact type — like DOUBLE PRECISION — but trades precision for storage efficiency. Use it for sensor readings, physical measurements, and high-volume time-series data where approximately 6 significant digits is sufficient.
Syntax
column_name REAL
column_name FLOAT4 -- alias, identical behavior
Key characteristics:
| Property | Value |
|---|---|
| Storage | 4 bytes |
| Range | ~-3.40282347E+38 to 3.40282347E+38 |
| Precision | ~6 significant decimal digits |
| Standard | IEEE 754 single precision |
Practical Example
Store product weight measurements from a factory scale that only reports 5-digit precision:
CREATE TABLE shipments (
id BIGSERIAL PRIMARY KEY,
order_id INTEGER NOT NULL,
dispatched_at TIMESTAMPTZ NOT NULL DEFAULT now(),
weight_kg REAL NOT NULL,
volume_liters REAL NOT NULL
);
INSERT INTO shipments (order_id, weight_kg, volume_liters)
VALUES
(1001, 1.25, 3.50),
(1002, 0.87, 2.10),
(1003, 4.62, 8.00),
(1004, 2.30, 5.25);
SELECT
order_id,
weight_kg,
volume_liters,
weight_kg / volume_liters AS density_kg_per_liter
FROM shipments
ORDER BY order_id;
Observe the characteristic single-precision rounding in the output:
order_id | weight_kg | volume_liters | density_kg_per_liter
----------+-----------+---------------+----------------------
1001 | 1.25 | 3.5 | 0.357142865657806
1002 | 0.87 | 2.1 | 0.414285719394684
1003 | 4.62 | 8.0 | 0.577500
1004 | 2.30 | 5.25 | 0.438095241785049
The trailing digits (e.g. 0.357142865657806) are the visible manifestation of single-precision arithmetic.
REAL vs. DOUBLE PRECISION vs. NUMERIC
| Type | Storage | Precision | Use case |
|---|---|---|---|
REAL | 4 bytes | ~6 digits | High-volume sensors, telemetry |
DOUBLE PRECISION | 8 bytes | ~15 digits | Scientific calculations |
NUMERIC | Variable | Exact | Financial values, billing |
Avoiding Equality Comparisons
Single-precision arithmetic makes equality comparisons unreliable. Use range queries:
-- Avoid: may never match due to rounding
WHERE weight_kg = 1.25
-- Prefer: tolerant range
WHERE weight_kg BETWEEN 1.249 AND 1.251
Testing with Vela
When aggregating large volumes of REAL measurements (cumulative weight totals, average densities), accumulated rounding errors can become visible at scale. Before deploying a reporting query to production, test it on a database branch loaded with production-scale data to quantify the precision loss and decide whether to promote the column to DOUBLE PRECISION.
Production Tips
- Use
REALwhen storing large time-series datasets where storage reduction is meaningful and 6 digits of precision is sufficient. - Avoid
REALfor any financial, billing, or precise scientific computation — useNUMERICfor exact values. - Avoid equality comparisons on
REALcolumns — always use range comparisons to account for floating-point imprecision. - Consider
DOUBLE PRECISIONif you need to aggregate (SUM,AVG)REALvalues frequently — accumulated rounding errors are more pronounced in single precision. - Inspect the column definition after creation with
\d+ table_nameto confirm the type is stored asrealand not silently promoted.