2201X
PostgreSQLERRORNotableData ExceptionHIGH confidence
invalid row count in result offset clause
What this means
SQLSTATE 2201X is raised when an OFFSET clause specifies a negative row count. OFFSET must be zero or a positive integer.
Why it happens
- 1Using a negative integer or computed negative expression as the OFFSET argument
How to reproduce
Query with a negative OFFSET.
trigger — this will error
trigger — this will error
SELECT * FROM orders LIMIT 10 OFFSET -5;
expected output
ERROR: OFFSET must not be negative
Fix
Clamp OFFSET to zero or above
WHEN When the OFFSET value is computed from pagination logic.
Clamp OFFSET to zero or above
SELECT * FROM orders LIMIT 10 OFFSET GREATEST(page * 10, 0);
Why this works
GREATEST ensures the offset is never negative, which can happen when page arithmetic results in a negative number.
Sources
Official documentation ↗
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev