Fix PostgreSQL Error 22022: Indicator Overflow in 5 Steps
In this article
PostgreSQL error 22022 — "indicator overflow" — happens when numeric values exceed the precision (scale) or range (min/max) specified in your column definitions. This isn't a corrupt database but a design/calculation error requiring either data adjustment or schema modification. The fix depends on whether the issue stems from numeric types (numeric/decimal) exceeding their limits or from variables/triggers producing overflow during query execution.
What You'll Need
- PostgreSQL client (psql) or IDE (DBeaver, pgAdmin) with access to the database
- Basic SQL knowledge for troubleshooting (ALTER TABLE, SELECT ... WITH OPTIONS)
- Backup of critical tables if making structural changes (use pg_dump)
- Knowledge of your application's data ranges for validation
- PostgreSQL version 9.0+ (error 22022 was introduced in v9.4)
Step-by-Step Resolution
- Identify the affected query
Use the following query to find queries triggering the error:SELECT query FROM pg_stat_statements WHERE query LIKE '%ERROR 22022%' OR query LIKE '%indicator overflow%' ORDER BY calls DESC LIMIT 10;If this shows no results, check recent application logs for similar error patterns.
- Examine the problematic column definitions
Run:SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_name = 'your_table' AND data_type IN ('numeric', 'decimal', 'integer');Look for columns with: -
numeric_scalegreater than 0 on numeric/decimal types (weird scaling) - Precision values wherenumeric_precision<numeric_scale+ 1 (invalid precision for scale) - Columns storing values clearly beyond their declared ranges - Case 1: Fix data type if precision/scale mismatch exists
Run this for each problematic column:ALTER TABLE your_table ALTER COLUMN column_name TYPE numeric(10,2); -- Use appropriate precision/scale like (10,2) for 10-digit max with 2 decimal placesIf you must keep existing data, first convert:
UPDATE your_table SET column_name = CAST(column_name AS numeric(10,2)); COMMIT; - Case 2: Fix problematic queries triggering overflow
For application-generated errors: 1) Locate the triggering code (query, stored procedure, or script) 2) Add explicit CAST where numeric operations occur:SELECT CAST(value AS numeric(20,0)) FROM your_table;3) Or modify the query to use appropriate types:SELECT numeric_column::numeric(10,2) FROM your_table; - Case 3: Schema redesign for fundamental overflows
If columns must store values beyond current precision:CREATE TABLE new_table AS SELECT column1, column2::numeric(20,0) AS numeric_column, column3::numeric(20,2) AS decimal_column FROM your_table;Then migrate data:
INSERT INTO new_table SELECT * FROM your_table;Finally drop the old table:
DROP TABLE your_table;
If That Did Not Work
Check these advanced scenarios in order:
- Variable-length numeric types
Run this to find any potential variable precision usage:SELECT oid FROM pg_type WHERE typname LIKE '%numeric%' AND pg_get_expr(dtyp->xmax, pg_get_expr(dtyp->xmin, pg_get_expr(dtyp->precision, dtyp->scale))) LIKE '%variable precision%';Add explicit precision if needed with ALTER TYPE
- Trigger functions causing overflow
Locate triggers on the table:SELECT t.tgname, t.tgprocedure, pg_get_triggerdef(t.oid) FROM pg_trigger t WHERE t.tgrelid = 'your_table'::regclass;Check each function for overflow potential and modify as needed
- Database replication issues
If this occurs during replication:SELECT * FROM pg_catalog.pg_stat_replication;Check replication lag and consider adjusting replication settings if needed
- Full schema audit
Run comprehensive schema validation:SELECT schema_name, table_name FROM information_schema.columns WHERE table_schema NOT LIKE 'pg_%' AND ( (numeric_scale > 0 AND data_type IN ('numeric', 'decimal')) OR (numeric_precision < numeric_scale + 1) );Address all issues found in this query
Common Mistakes
- Ignoring scale/precision mismatch
Many developers forget numeric_scale can exceed numeric_precision by 1, which causes this error. Always validatenumeric_precision >= numeric_scale + 1for decimal/numeric types - Applying fixes to wrong tables
Always verify the table name in your ALTER TABLE statements matches exactly what's in the database - Skipping data conversion validation
When converting data types, test withSELECT CAST(value AS new_type)on sample rows to ensure no truncation occurs - Overlooking application queries
This error can occur in application code, not just database queries - check all numeric operations - Modifying critical tables without backup
Always take a database backup before making structural changes to columns
Tips
- Use
pgBadgerto analyze query patterns showing this error over time - For large tables, consider adding new columns first with proper types, then migrate data
- Document all type changes in your database schema comments
- Implement automatic validation in your application code to catch numeric overflow early
- Monitor error 22022 frequency using pg_stat_statements or custom alerts
Troubleshooting
- Error persists after fix
Check if the error originated from application code (Java, Python, etc.) rather than the database. The error message might show the full stack trace including the application layer - Schema changes don't apply
Verify the table name matches exactly with:SELECT tablename FROM pg_tables WHERE tablename = 'your_table';Also check for table aliases in queries that might need updating
- Data conversion errors
When converting data, test with:SELECT EXCEPTION WHEN OTHERS THEN RETURN QUERY SELECT CAST(value AS target_type) FROM your_table LIMIT 1;This helps identify problematic rows
- Performance degradation after fix
Rebuild indexes after schema changes if performance is affected:REINDEX TABLE your_table; - Error only during replication
Check the replication lag and ensure the primary server's schema matches exactly with:SELECT * FROM information_schema.columns WHERE table_schema = 'your_schema' AND table_name = 'your_table' ORDER BY column_name; - No existing data matches new types
If your application expects older data formats, you may need to implement a hybrid approach with both old and new column types
What causes error 22022 in PostgreSQL 9.5?
Error 22022 occurs primarily because of numeric scale/precision mismatches in columns defined with numeric/decimal types where numeric_scale > numeric_precision or columns storing values beyond their declared numeric range. The error was introduced in PostgreSQL 9.4 as part of the numeric type improvements.
How do I prevent this error in future queries?
Always validate numeric column definitions in your schema with the rule: numeric_precision >= numeric_scale + 1. In your application code, implement explicit type casting for all numeric operations and validate data ranges before processing.
Can I use this error to identify data quality issues?
Absolutely. This error reveals both structural issues (column definitions) and data quality problems (actual values exceeding declared ranges). It's a great indicator of either poorly designed schemas or data ingestion issues where values were improperly normalized.
What's the difference between numeric and decimal types in PostgreSQL?
The key difference is that numeric types in PostgreSQL are variable precision (can store any number of digits), while decimal types are fixed precision (specified by precision/scale parameters). Error 22022 can occur with either type when values exceed their declared precision or when scale/precision parameters are incorrectly specified.
How do I handle large numbers in PostgreSQL columns?
For large numbers, use numeric types without scale (precision only) or decimal types with appropriate precision/scale. For example, to store up to 18-digit integers without decimal places: ALTER TABLE your_table ALTER COLUMN column_name TYPE numeric(18,0);
What happens if I don't fix this error?
Repeated errors will cause your application to fail silently or crash. Worse, they can lead to data corruption if not properly handled. This error also prevents your database from properly enforcing data integrity constraints, potentially allowing incorrect values to be stored.