PG
PRO
2202EERRORTier 2 — Caution✅ HIGH confidence

array subscript error

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 2202E is raised when an array subscript (index) is used that falls outside the valid bounds of the array. Unlike some languages, Postgres does not raise this for simple out-of-bounds reads — it returns NULL — but it does raise it for invalid subscript expressions used in array slicing or construction.

Why it happens

  1. 1Array subscript expression evaluates to a non-integer or NULL in a context that requires a valid integer index
  2. 2Using a subscript that violates array dimension constraints

How to reproduce

Constructing an array with an invalid subscript expression.

trigger — this will ERROR
SELECT ARRAY[1,2,3]['a']; -- non-integer subscript
ERROR: array subscript must have integer type

Fix 1: Ensure array subscripts are integer expressions

When subscripting arrays in SQL or PL/pgSQL.

fix
SELECT ARRAY[1,2,3][2]; -- valid integer subscript

Why this works

Postgres arrays require integer subscripts. Cast or convert subscript expressions to INTEGER if necessary.

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 array subscript errors. Stable across versions.

See also

📄 Reference pages

ArraysArray Subscripts
⚙️ 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 →