Incident brief
LIMIT must not be negative
A query passed a negative value to LIMIT. PostgreSQL requires LIMIT to be non-negative; use LIMIT ALL or NULL to mean “no limit”.
What lands in your log
ERROR: LIMIT must not be negative
In 10 seconds
- What triggers it
- Run a query whose LIMIT clause evaluates to a negative number.
- Fix
- Use LIMIT ALL, or a NULL LIMIT, when you mean “return everything”.
- Proof
- Reproduced on PostgreSQL 16.14 → A query with LIMIT -1 was rejected with SQLSTATE 2201W. Replacing it with a NULL limit (via NULLIF) returned all five rows.
Fix
What to do right now
Application-level steps for this error.
- Use LIMIT ALL, or a NULL LIMIT, when you mean “return everything”.
- Clamp a computed limit with GREATEST(n, 0) so pagination math can never send a negative.
- Validate page-size input before it reaches the query; reject or floor negatives.
-- LIMIT ALL / NULL means "no limit"; never pass a negative
SELECT g FROM generate_series(1, 5) AS g ORDER BY g LIMIT NULLIF(-1, -1);For this error
See this error live on the server
Run these against the affected instance to confirm the diagnosis before you act.
LIMIT must not be negative (2201W) is statement-local: PostgreSQL does not retain the rejected value after the statement ends. Capture the exact bound value and statement position in application/server logs, then run this targeted validation before retrying.
Validate the bound LIMIT
Validate the specific value/shape that can raise this SQLSTATE.
SELECT requested_limit,
requested_limit < 0 AS would_raise_2201W,
greatest(requested_limit, 0) AS guarded_limit
FROM (VALUES (-1)) v(requested_limit);Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual, not paraphrased.
PostgreSQL 16 Documentation, §7.6 LIMIT and OFFSET
If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the query itself yields fewer rows). LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT with a NULL argument.Read the full section on postgresql.org →
The negative LIMIT
LIMIT counts rows to return, so a negative value is meaningless. PostgreSQL raises SQLSTATE 2201W rather than guessing what -1 should mean.The session is unaffected by the error
The rejected query never ran; the source still has all five rows and the session is unaffected.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab. The commands below are exactly what was executed.
- 1Run a query whose LIMIT clause evaluates to a negative number.
- 2PostgreSQL raises SQLSTATE 2201W and returns no rows.
- 3Outside a transaction, the session is unaffected, the next statement runs normally.
One client, no schema: generate_series feeds five rows to a LIMIT clause that evaluates to -1.
-- LIMIT parsing is self-contained; generate_series supplies the rows.
SELECT 'no schema needed' AS setup_note;SELECT g FROM generate_series(1, 5) AS g ORDER BY g LIMIT -1;SELECT count(*) AS rows_available FROM generate_series(1, 5) AS g;What PostgreSQL actually returned
setup_note
------------------
no schema needed
(1 row)ERROR: LIMIT must not be negative rows_available
----------------
5
(1 row)Replacing the negative LIMIT with a NULL limit (LIMIT ALL semantics) was tested and returned every row, matching the manual's statement that a NULL LIMIT means no limit.
Without this
Above: LIMIT -1 raises SQLSTATE 2201W.
With this, tested
Below: a NULL LIMIT returns all rows.
- A second operational test: exact SQL, raw output, measured result, and engineer notes
Card required. Cancel before day 7 and you are not charged.
Connected
Everything this error touches
Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.
Understand the concept
Related errors
Verification
- Last verified
- 2026-07-19 (isolated lab, PostgreSQL 16.14)
- Verification scope
- Verified against PostgreSQL 16.14 in an isolated lab environment
- Audit status
- reviewed
Went further?
Pro unlocks the second lab proof
Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.