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

What lands in your log

ERROR: LIMIT must not be negative

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

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.
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);

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.

Open in the interactive map →

Verification

PG 16.14
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
ShareLinkedInX

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.

FollowSubstackLinkedInnew errors · lab notes · hiring loops