PG
PRO
22025ERRORTier 2 — Caution✅ HIGH confidence

invalid escape sequence

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 22025 is raised when a LIKE or SIMILAR TO pattern contains a backslash escape sequence at the end of the string (incomplete escape), or when the escape character is followed by something that does not form a valid escape.

Why it happens

  1. 1A LIKE pattern ends with the escape character, leaving it without a following character to escape
  2. 2An escape sequence in a SIMILAR TO pattern is incomplete

How to reproduce

LIKE pattern ending in an unfinished escape sequence.

trigger — this will ERROR
SELECT * FROM files WHERE name LIKE 'report\'; -- escape at end
ERROR: invalid escape sequence

Fix 1: Ensure the escape character is followed by a valid character to escape

When building LIKE patterns programmatically.

fix
SELECT * FROM files WHERE name LIKE 'report\\'; -- escaped backslash

Why this works

Double the escape character to match a literal backslash, or ensure the pattern always has a character after the escape.

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 LIKE/SIMILAR TO escape validation. Stable across versions.

See also

📄 Reference pages

LIKESIMILAR TOPattern Matching
⚙️ 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 →