SQLSTATE 22016Severity lowLab verified

Incident brief

nth_value argument must be positive

nth_value(expr, n) returns the value from the n-th row of the window frame and requires n to be a positive integer; zero or a negative position raises this data exception.

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

In 10 seconds

What
nth_value argument must be positive
What triggers it
Use nth_value(expr, n) OVER (...) with n set to zero or a negative value.
The fix
Pass a positive row position to nth_value().
Proof
Reproduced on PostgreSQL 18.4 → A single windowed SELECT reproduces SQLSTATE 22016; nth_value rejects the non-positive position.

The fix

What to do right now

The immediate, application-level response to this error.

  • Pass a positive row position to nth_value().
  • Validate any dynamic position before the call so it is always 1 or greater.
Fix — SQL
-- nth_value positions are 1-based and must be positive.
SELECT nth_value(g, 1) OVER (ORDER BY g) FROM generate_series(1,3) g;

Related & next steps

Follow the thread

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

Verification

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