0
0
PostgreSQLquery~10 mins

Numeric and decimal precision in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Numeric and decimal precision
Define numeric/decimal column with precision
Insert value into column
Check if value fits precision and scale
Store value
Retrieve value with precision
This flow shows how PostgreSQL handles numeric/decimal values with defined precision and scale during insert and retrieval.
Execution Sample
PostgreSQL
CREATE TABLE prices (
  price NUMERIC(5,2)
);

INSERT INTO prices VALUES (123.456);
SELECT price FROM prices;
Create a table with a numeric column of precision 5 and scale 2, insert a value with more decimals, then select it.
Execution Table
StepActionInput ValuePrecision CheckStored ValueNotes
1Create tableN/AN/ATable with price NUMERIC(5,2)Defines max 5 digits total, 2 after decimal
2Insert value123.456123.456 fits? Max digits=5, scale=2123.46Value rounded to 2 decimal places
3Select valueN/AN/A123.46Retrieved value shows rounded number
4Insert large value12345.67Digits=7 > 5ErrorValue too large for defined precision
💡 Execution stops after error on inserting value exceeding precision
Variable Tracker
VariableStartAfter Insert 1After Insert 2Final
priceNULL123.46Error (no change)123.46
Key Moments - 2 Insights
Why does 123.456 become 123.46 when inserted?
Because the column is defined with scale 2, PostgreSQL rounds the value to 2 decimal places as shown in execution_table row 2.
What happens if the number has more digits than precision allows?
PostgreSQL raises an error and does not store the value, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the stored value after inserting 123.456?
A123.456
B123.46
C123.45
DError
💡 Hint
Check execution_table row 2 under Stored Value column
At which step does an error occur due to precision overflow?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
See execution_table row 4 Notes column
If the scale was changed to 3, how would the stored value for 123.456 change?
AIt would store 123.456 exactly
BIt would round to 123.46
CIt would cause an error
DIt would store 123.45
💡 Hint
Scale controls decimal places, see concept_flow and execution_table row 2
Concept Snapshot
NUMERIC(p,s) or DECIMAL(p,s) defines a number with p total digits and s digits after decimal.
Values inserted are rounded to scale s.
If value exceeds precision p, an error occurs.
Use for exact decimal storage like money.
Example: NUMERIC(5,2) stores up to 999.99.
Full Transcript
This visual execution trace shows how PostgreSQL handles numeric and decimal precision. First, a table is created with a numeric column defined as NUMERIC(5,2), meaning it can store numbers with up to 5 digits total and 2 digits after the decimal point. When inserting the value 123.456, PostgreSQL rounds it to 123.46 to fit the scale of 2 decimal places. Selecting the value returns 123.46. If a value like 12345.67 is inserted, which has 7 digits, it exceeds the precision and causes an error, so it is not stored. The variable tracker shows the price value changing from NULL to 123.46 after the first insert, and no change after the failed insert. Key moments clarify why rounding happens and what causes errors. The quiz questions test understanding of rounding, error steps, and scale effects. The snapshot summarizes the key rules for numeric precision and scale in PostgreSQL.