How to Fix Postgres Error 22011 Substring Error
The postgres error 22011 substring error happens when a query passes a negative length to a substring function. You fix it by checking that your length math always makes a number of zero or more. PostgreSQL tracks this failure using SQLSTATE 22011 and the internal macro ERRCODE_SUBSTRING_ERROR. This data exception stops the active query right away to prevent bad text cuts. Your app will see a failed run until you fix the length math. What causes the postgres error 22011 substring error Datab
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
The postgres error 22011 substring error happens when a query passes a negative length to a substring function. You fix it by checking that your length math always makes a number of zero or more.
PostgreSQL tracks this failure using SQLSTATE 22011 and the internal macro ERRCODE_SUBSTRING_ERROR. This data exception stops the active query right away to prevent bad text cuts. Your app will see a failed run until you fix the length math.
What causes the postgres error 22011 substring error
Database engines need clear rules when they slice text or byte values. In PostgreSQL, a substring call takes a source string, a start spot, and a length. The start spot can be negative or zero without breaking the query. Still, the length value must never drop below zero.
Most teams do not write a negative length by hand. Instead, this bug shows up when queries calculate the cut length on the fly. A common pattern finds a separator symbol and subtracts the start index from that spot. If the symbol is missing from a row, the search returns zero. Subtracting a high start index from zero yields a negative number. That single negative result triggers SQLSTATE 22011 and stops the whole read or write job.
Dirty data in production tables makes this failure hard to spot in test setups. One row with a missing dash or space in a phone number can break a report. Byte slice calls on binary columns follow the exact same rule. If your math produces a negative byte count, the engine raises ERRCODE_SUBSTRING_ERROR right away.
How to diagnose and fix substring failures in production
To find the broken row, you must inspect the data without exposing private fields. You do not need full superuser rights to debug a text slice bug. PostgreSQL lets an admin grant read rights on specific columns of a table or view. An admin can set up a group role for support staff and grant column read access. If the broken column holds emails or card numbers, masking rewrites matched fields in results.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Start by running a check query that calculates your length math by itself. Filter for rows where that math gives a number below zero. This simple check shows which records lack the expected separator or text format. You can inspect the row keys and the bad length numbers safely.
Once you find the edge cases, update your query logic so it handles messy rows. Wrap the length math in a guard that picks the larger value between zero and your formula. You can also use a conditional check that skips the slice when a separator is missing. If you use a custom function to parse text, review its access rules too. By default, the special PUBLIC role gets run rights on new functions and procedures. The function owner can revoke those default rights so only approved app roles run the parser.
Preventing future data exceptions in text parsing
Fixing the query stops the immediate error, but clean table rules prevent repeat outages. Check your app forms and import scripts to make sure text fields match a standard shape. You can add table check rules that reject rows with missing separators before they save. In PostgreSQL 15 and later, regular users cannot create new objects in the public schema by default. Keep schema changes locked down so only reviewed migrations update your table rules and parsing views.
Add query and result controls beside Postgres
Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your Postgres. For every plain or prepared query an agent or human runs against production, hoop.dev decides to pass, mask, block, or escalate it for review.
When a tool or agent sends SQL, hoop.dev Postgres controls block banned statements before execution or hold risky ones for human approval via an AI Analyzer you configure. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.
Frequently asked questions
What does SQLSTATE 22011 mean in PostgreSQL?
SQLSTATE 22011 is the standard code for a substring error, mapped to ERRCODE_SUBSTRING_ERROR. It means a text or byte slice function received a negative length value during a query.
Can a negative start position trigger error 22011?
No, a negative start position does not trigger this specific error in PostgreSQL. Only a negative length parameter causes the database engine to raise SQLSTATE 22011.
How do I safely debug rows that cause substring errors?
Grant your debug role read rights on only the needed columns and filter for negative length math. This finds the broken rows without needing broad table privileges or changing production data.
Related guides
In this post