Numeric and decimal precision in PostgreSQL - Time & Space Complexity
When working with numbers in a database, the precision of numeric and decimal types affects how calculations are done.
We want to understand how the time to process these numbers changes as their size or precision grows.
Analyze the time complexity of the following SQL snippet that sums decimal numbers with precision.
SELECT SUM(price)
FROM products
WHERE price > 0;
This query adds up all positive prices stored as numeric or decimal values.
Look for repeated actions that affect time.
- Primary operation: Adding each price value one by one.
- How many times: Once for every row with a positive price.
As the number of rows increases, the total additions increase too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to sum prices grows in a straight line as the number of prices increases.
[X] Wrong: "Adding numbers with more decimal places takes longer time per addition."
[OK] Correct: While precision affects storage, the addition operation time is mostly affected by how many numbers are added, not their decimal length.
Understanding how numeric precision impacts query time helps you explain performance in real projects where money or measurements matter.
"What if we changed the query to multiply prices instead of summing? How would the time complexity change?"
Practice
scale represent in a NUMERIC(10, 3) data type in PostgreSQL?Solution
Step 1: Understand NUMERIC precision and scale
InNUMERIC(precision, scale), precision is total digits, scale is digits after decimal.Step 2: Apply to NUMERIC(10, 3)
Here, scale = 3 means 3 digits after decimal point.Final Answer:
The number of digits after the decimal point -> Option AQuick Check:
Scale = digits after decimal [OK]
- Confusing scale with precision
- Thinking scale is digits before decimal
- Assuming scale limits total digits
price with 7 digits total and 2 digits after the decimal in PostgreSQL?Solution
Step 1: Recall NUMERIC/DECIMAL syntax
Syntax isNUMERIC(precision, scale)where precision >= scale.Step 2: Match precision=7 and scale=2
price NUMERIC(7,2)usesNUMERIC(7,2)which means 7 total digits, 2 after decimal.Final Answer:
price NUMERIC(7,2) -> Option DQuick Check:
NUMERIC(precision, scale) = NUMERIC(7,2) [OK]
- Swapping precision and scale
- Using scale larger than precision
- Omitting scale when needed
products(price NUMERIC(5,2)), what will be the stored value after inserting INSERT INTO products(price) VALUES (123.4567);?Solution
Step 1: Understand NUMERIC(5,2) limits
Precision 5 means total 5 digits, scale 2 means 2 digits after decimal.Step 2: Round input 123.4567 to 2 decimal places
123.4567 rounds to 123.46 to fit scale 2.Final Answer:
123.46 -> Option BQuick Check:
Rounded to 2 decimals = 123.46 [OK]
- Expecting truncation instead of rounding
- Assuming error on extra decimals
- Ignoring precision limits
amount NUMERIC(4,2). You try to insert 999.99 but get an error. What is the problem?Solution
Step 1: Calculate digits in 999.99
999.99 has 5 digits total (3 before decimal + 2 after).Step 2: Compare with NUMERIC(4,2)
NUMERIC(4,2) allows only 4 digits total, so 999.99 is too large.Final Answer:
The value exceeds the total precision of 4 digits -> Option CQuick Check:
Value digits > precision causes error [OK]
- Thinking scale limits integer part
- Believing NUMERIC disallows decimals
- Ignoring precision limits
Solution
Step 1: Determine needed precision and scale
Value 999999.999 has 6 digits before decimal, 3 after, total 9 digits.Step 2: Choose NUMERIC with precision 9 and scale 3
NUMERIC(9,3) stores exactly 9 digits total with 3 decimals, matching requirement.Step 3: Avoid FLOAT for exact money values
FLOAT is approximate, may cause rounding errors.Final Answer:
amount NUMERIC(9,3) -> Option AQuick Check:
Exact money storage needs NUMERIC(9,3) [OK]
- Using FLOAT for money causing rounding errors
- Choosing too small precision or scale
- Confusing DECIMAL scale with precision
Ready to prove it?
Pass the test and this pattern moves unknown
