Seaquel
PostgreSQL error code

PostgreSQL error 2201X

2201X is the SQLSTATE PostgreSQL reports for invalid row count in result offset clause, in class 22, Data Exception.

SQLSTATE2201X
Condition nameinvalid_row_count_in_result_offset_clause
Class22 — Data Exception

Reproduce it, then fix it

The offset came out negative, which usually means page arithmetic like (page - 1) * size ran with page 0. Clamp it with greatest(..., 0). Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.

SELECT id, name FROM products
ORDER BY id
LIMIT 5 OFFSET (0 - 1) * 5;
SELECT id, name FROM products
ORDER BY id
LIMIT 5 OFFSET greatest(0 - 1, 0) * 5;

Source: PostgreSQL documentation, Appendix A