2201W
PostgreSQLERRORNotableData ExceptionHIGH confidence
invalid row count in LIMIT clause
What this means
SQLSTATE 2201W is raised when a LIMIT clause specifies a row count that is negative. A LIMIT of zero is valid (returns no rows), but negative values are not permitted.
Why it happens
- 1Using a negative integer or a computed negative expression as the LIMIT argument
How to reproduce
Query with a negative LIMIT.
trigger — this will error
trigger — this will error
SELECT * FROM orders LIMIT -1;
expected output
ERROR: LIMIT must not be negative
Fix
Clamp LIMIT to zero or above
WHEN When the LIMIT value is computed dynamically.
Clamp LIMIT to zero or above
SELECT * FROM orders LIMIT GREATEST(computed_limit, 0);
Why this works
GREATEST ensures the limit is never negative. Use LIMIT ALL or omit LIMIT to return all rows when no limit is intended.
Sources
Official documentation ↗
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev