PG
PRO
2201XERRORTier 2 — Caution✅ HIGH confidence

invalid row count in result offset clause

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 2201X is raised when an OFFSET clause specifies a negative row count. OFFSET must be zero or a positive integer.

Why it happens

  1. 1Using a negative integer or computed negative expression as the OFFSET argument

How to reproduce

Query with a negative OFFSET.

trigger — this will ERROR
SELECT * FROM orders LIMIT 10 OFFSET -5;
ERROR: OFFSET must not be negative

Fix 1: Clamp OFFSET to zero or above

When the OFFSET value is computed from pagination logic.

fix
SELECT * FROM orders LIMIT 10 OFFSET GREATEST(page * 10, 0);

Why this works

GREATEST ensures the offset is never negative, which can happen when page arithmetic results in a negative number.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for OFFSET validation. Stable across versions.

See also

📄 Reference pages

SELECTOFFSET ClausePagination
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →