array subscript out of range

SQLSTATE 2202E condition array_subscript_error class 22 — Data Exception severity ERROR
Reproduced & verified on PostgreSQL 14.23, 15.18, 16.14, 17.10 and 18.4 — identical message on every version.
Last reviewed 30 May 2025 · Reproduced live with the SQL on this page.

Symptoms

An array assignment used a subscript outside the allowed bounds, or an array grew beyond the maximum number of dimensions/size. PostgreSQL raises SQLSTATE 2202E (array_subscript_error).

What the server log shows

ERROR:  array subscript out of range

Why PostgreSQL raises this — what the manual says

As Section 8.15 Arrays explains:

An array assignment or fixed-length context used a subscript outside the array’s current bounds; while reading an out-of-range subscript yields null, assigning beyond the allowed range raises this error.

PostgreSQL arrays allow sparse growth via assignment, but subscripts must remain within the supported integer range and dimension limit. A subscript beyond those limits is invalid and raises 2202E.

Common causes

How to fix it

  1. Use sequential or bounded subscripts when extending arrays.
  2. Build arrays with array_append()/array_agg() instead of arbitrary indexes.
  3. Validate computed subscripts before assignment.

Related & next steps

Reference: PostgreSQL 18 Section 8.15 “Arrays”.