SQLSTATE 2201WSeverity lowLab verified

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”.

Reproduced on PostgreSQL 16.14Verified 2026-07-19 (isolated lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
LIMIT must not be negative
What triggers it
Run a query whose LIMIT clause evaluates to a negative number.
The 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.

The fix

What to do right now

The immediate, application-level response to 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.
Fix — SQL
-- 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);

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-19 (isolated lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed