0
0
PostgresqlComparisonBeginner · 3 min read

Real vs Double Precision in PostgreSQL: Key Differences and Usage

In PostgreSQL, real is a 4-byte floating-point type with about 6 decimal digits precision, while double precision is an 8-byte type offering about 15 decimal digits precision. Use real for less precise, smaller storage needs and double precision when higher accuracy is required.
⚖️

Quick Comparison

This table summarizes the main differences between real and double precision types in PostgreSQL.

Featurerealdouble precision
Storage Size4 bytes8 bytes
PrecisionApprox. 6 decimal digitsApprox. 15 decimal digits
Range±1.18e-38 to ±3.4e+38±2.23e-308 to ±1.79e+308
Use CaseLess precise calculations, saves spaceHigh precision calculations
PerformanceFaster due to smaller sizeSlightly slower but more accurate
SQL Aliasfloat4float8
⚖️

Key Differences

The real type in PostgreSQL is a single precision floating-point number that uses 4 bytes of storage. It provides about 6 digits of decimal precision, which is enough for many applications where exact precision is not critical. Because it uses less storage, it can be faster for some operations and saves disk space.

On the other hand, double precision uses 8 bytes and offers roughly 15 digits of decimal precision. This makes it suitable for scientific calculations, financial data, or any use case where accuracy is important. The trade-off is that it requires more storage and can be slightly slower to process.

Both types follow the IEEE 754 standard for floating-point arithmetic, but double precision reduces rounding errors and increases the range of representable numbers compared to real. Choosing between them depends on your need for precision versus storage and speed.

⚖️

Code Comparison

Here is how you declare and insert values using the real type in PostgreSQL.

sql
CREATE TABLE real_example (
  value real
);

INSERT INTO real_example (value) VALUES (123.456789);

SELECT value, value::text FROM real_example;
Output
value | text --------+--------- 123.457 | 123.457 (1 row)
↔️

Double Precision Equivalent

This example shows the same operation using double precision for higher accuracy.

sql
CREATE TABLE double_example (
  value double precision
);

INSERT INTO double_example (value) VALUES (123.4567890123456789);

SELECT value, value::text FROM double_example;
Output
value | text ------------------------+-------------------------- 123.45678901234568 | 123.45678901234568 (1 row)
🎯

When to Use Which

Choose real when you need to save storage space and your application can tolerate some rounding errors, such as in graphics or simple measurements. It is also useful when performance is a priority and precision is less critical.

Choose double precision when your calculations require high accuracy, such as in scientific computations, financial data, or when working with very large or very small numbers. It ensures less rounding error and a wider range of values.

Key Takeaways

real uses 4 bytes and offers about 6 digits precision, suitable for less precise needs.
double precision uses 8 bytes and offers about 15 digits precision for accurate calculations.
Use real to save space and improve speed when precision is not critical.
Use double precision for scientific, financial, or high-accuracy applications.
Both types follow IEEE 754 but differ in storage size, precision, and range.