PostgreSQL Error 2201W: Invalid Row Count in LIMIT
In this article
The error 2201W means PostgreSQL rejected the value you gave to LIMIT (or OFFSET) because it is not a valid row count. In practice, the fix is almost always one of three things: pass a non-negative integer, remove quotes around a numeric literal, or stop using a placeholder that arrives as an empty string or NULL. PostgreSQL requires LIMIT to be an integer constant or a parameter that evaluates to a non-negative integer; anything else, including a negative number, a decimal, a text value, or NULL, raises this error. The same rule applies to OFFSET and to the SQL-standard form FETCH FIRST n ROWS ONLY. This guide walks through the causes, the exact steps to correct them, and how to keep the problem from coming back in application code.
What You'll Need
- Access to the PostgreSQL server, either through
psqlor a client such as pgAdmin or DBeaver. - The failing query text, ideally with the parameter values that were bound at the time.
- Permission to read the table involved so you can test corrected queries.
- For application-side fixes, access to the code that builds the query and the driver or ORM in use (for example psycopg, JDBC, SQLAlchemy, or Prisma).
Step-by-Step
- Reproduce the error and capture the exact SQL. Run the failing statement in
psqlor your client. If the error comes from an application, enable statement logging or check the driver's error output so you can see the literal value that was sent. The message usually readsERROR: LIMIT must not be negativeorinvalid row count in limit clause, and the position marker points at the offending value. - Check the value type. A valid
LIMITmust be an integer. RunSELECT pg_typeof(10);and compare with what your query supplies. If the value is a string such as'10', PostgreSQL will normally cast it, but a string like'ten',''(empty), or'10.5'will fail. Cast explicitly with::intor::bigintonly when you are sure the text is numeric. - Check for negative numbers.
LIMIT -1is invalid in PostgreSQL. Some other databases treat a negative limit as "no limit"; PostgreSQL does not. If your code uses-1or0as a sentinel for "return everything," replace it with a large positive integer or omit theLIMITclause entirely. - Check for NULL and empty strings from parameters. This is the most common cause in application code. A bound parameter that is
NULLor an empty string produces this error. Inspect the value just before the query runs, or log it, and default it to a real integer in the calling code rather than in SQL. - Check for decimals and floats.
LIMIT 10.0is rejected because the type is numeric, not integer. Round or truncate in the application, then cast:LIMIT 10orLIMIT (10.0)::int. - Check for expressions that are not constant.
LIMITaccepts a constant or a parameter, not an arbitrary column reference or subquery in older versions. If you need a dynamic limit, compute it in the application and bind it as an integer parameter, or use a server-side function that returns an integer. - Correct the query and re-run it. For example, change
SELECT * FROM orders LIMIT '50';toSELECT * FROM orders LIMIT 50;, or changeLIMIT $1with aNULLbinding to a real integer. Confirm the row count returned matches what you expect. - Fix the application layer. In your driver, bind the limit as an integer type, not as text. In psycopg, pass a Python
int. In JDBC, usesetInt. In ORMs, use the ORM's limit method with an integer argument rather than string concatenation. Add a guard such aslimit = max(1, int(user_value))before binding, with a sensible upper bound. - Add validation and tests. Write a test that sends
NULL,'',-1, and10.5as the limit and confirm each is rejected or normalised before reaching the database. This prevents regressions when the query builder changes.
Common Mistakes
- Assuming negative means unlimited. In PostgreSQL, omit
LIMITor use a very large integer instead. - Quoting the number.
LIMIT '10'works only because PostgreSQL casts the literal;LIMIT '10.5'orLIMIT ''does not. Prefer unquoted integers. - Passing user input straight through. A form field that is empty or non-numeric becomes an invalid limit. Validate and coerce before binding.
- Confusing
LIMITwithFETCH FIRST. Both accept integers, but mixing the syntaxes in one query is invalid. - Forgetting
OFFSET.OFFSETfollows the same rules; a negative offset raises a related error. - Using a float column or expression.
LIMIT (total * 0.1)fails because the result is numeric. Cast tointfirst.
Tips
- Use
LIMIT ALLwhen you want no limit but still need the clause for a query builder. - In
psql, run\errverboseafter the error to see the full message, SQLSTATE, and position. - Keep limits as integers end to end: validate in the UI, coerce in the service layer, bind as integer in the driver.
- Set a maximum page size in your API so a huge or malformed limit cannot reach the database.
- If you use an ORM, check that its
limit()method receives an integer, not a string from a query parameter.
Troubleshooting
If the query still fails after correcting the literal, log the bound parameters at the driver level and compare them with what you expect. A common surprise is an ORM passing None or an empty string when a filter is absent. If the error appears only in production, check whether a different code path or an older client version builds the query differently. If you cannot change the application immediately, a temporary workaround is to wrap the parameter in a server-side function that returns a safe default, but fix the caller as soon as possible. If the error mentions OFFSET rather than LIMIT, apply the same checks to the offset value. If you are on a hosted PostgreSQL service, confirm the server version in the dashboard, because behaviour around parameter types has been consistent but client libraries vary. When all else fails, reduce the query to SELECT 1 LIMIT $1; and test the parameter in isolation to prove where the bad value originates.
Frequently Asked Questions
What does SQLSTATE 2201W mean exactly?
It is the class 22 (data exception) code for an invalid row count in a LIMIT clause. PostgreSQL raises it when the limit is negative, non-integer, or not a valid numeric value.
Can I use a negative number to mean "no limit"?
No. Omit the LIMIT clause or use LIMIT ALL instead.
Why does it work in psql but fail in my app?
The app is likely binding the value as text or sending NULL or an empty string. Check the parameter type and value at the driver level.
Does OFFSET have the same restriction?
Yes. OFFSET must also be a non-negative integer, and a bad offset raises a similar data exception.
Can I use a subquery or column in LIMIT?
Only where the expression evaluates to a valid integer constant or parameter. Compute dynamic limits in the application and bind an integer.
Is LIMIT 0 valid?
Yes. It returns zero rows and is a valid integer row count.