Diagnostic Queries
Symptoms
A LIKE/ILIKE pattern ends with the escape character (default backslash) with nothing to escape. PostgreSQL raises SQLSTATE 22025 (invalid_escape_sequence).
- Usually from a trailing backslash in a user-supplied search term.
- The escape char must always precede a character to escape.
- Common when wildcards are built from raw input.
What the server log shows
ERROR: LIKE pattern must not end with escape character
Why PostgreSQL raises this — what the manual says
Section 9.7.1 LIKE:
“To match the escape character itself, write two escape characters.”
In LIKE, the escape character makes the next character literal. If the pattern ends with the escape character there is nothing to escape, which is invalid and reported as 22025.
Common causes
- A trailing backslash in the search pattern.
- Concatenating user input that ends with the escape character.
- Double-escaping gone wrong in application code.
How to fix it
- Escape user input properly before building the LIKE pattern (double the backslash, or escape
%/_). - Choose a different escape character:
LIKE pattern ESCAPE '!'. - Disable escaping when not needed:
LIKE pattern ESCAPE ''.
Related & next steps
Reference: PostgreSQL 18 Section 9.7 “Pattern Matching”.
Thanks — noted. This helps keep the database accurate.