# 22P06 — nonstandard_use_of_escape_character

> Source-backed reference for PostgreSQL SQLSTATE 22P06.
---

# 22P06

## At a glance {#at-a-glance}
The SQL scanner found a backslash escape form that is nonstandard or unsafe under the current string-literal settings. PostgreSQL 18.6 has one `ERROR` branch for unsafe quote escaping and three `WARNING` branches for backslash-quote, backslash-backslash, and other escape use.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| Field | Value |
| --- | --- |
| SQLSTATE | `22P06` |
| Condition | `nonstandard_use_of_escape_character` |
| Status | `active` |
| Known present by | `8.1.0` |
| Locked snapshots | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| Macros | `ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER` |
| Aliases | `—` |

<!-- source facts: data/errcodes/22P06.json -->
<!-- END SQLSTATE FACTS -->

## Meaning {#meaning}
The scanner copies `backslash_quote`, `escape_string_warning`, and `standard_conforming_strings` into its state. Ordinary quoted strings enter the escape-aware `xe` rules only when `standard_conforming_strings` is off; explicit `E'...'` strings enter them directly. The explicit-`E` start also initializes `warn_on_first_escape` to false, so it avoids this legacy first-escape `WARNING`; it still enters `xe` and remains subject to the `backslash_quote` `ERROR` guard. An `E` prefix is not a general way to bypass that guard. For an escaped single quote, `backslash_quote = off`, or safe-encoding mode combined with a client-only encoding, raises `ERROR` with primary `unsafe use of \' in a string literal` and a quote-doubling hint.

When `escape_string_warning` and the scanner's first-escape guard are enabled, the lexer emits one `WARNING` per string for `\'`, `\\`, or another escape. The exact primaries are `nonstandard use of \' in a string literal`, `nonstandard use of \\ in a string literal`, and `nonstandard use of escape in a string literal`. These warnings are warning diagnostics, not `NOTICE`, and do not by themselves abort a statement.

## Messages {#messages}
- `ERROR` primary: `unsafe use of \' in a string literal`; HINT: `Use '' to write quotes in strings. \' is insecure in client-only encodings.`
- `WARNING` primary: `nonstandard use of \' in a string literal`; HINT: `Use '' to write quotes in strings, or use the escape string syntax (E'...').`
- `WARNING` primary: `nonstandard use of \\ in a string literal`; HINT: `Use the escape string syntax for backslashes, e.g., E'\\'.`
- `WARNING` primary: `nonstandard use of escape in a string literal`; HINT: `Use the escape string syntax for escapes, e.g., E'\r\n'.`

## Diagnosis {#diagnosis}
Preserve the literal spelling and character position, the presence or absence of the `E` prefix, `standard_conforming_strings`, `backslash_quote`, `escape_string_warning`, client encoding, and message severity. A backslash-quote under the ERROR guard is a rejected statement; a warning branch may allow the statement to proceed. The same text can therefore have different outcomes when the scanner settings or client encoding change.

## Response {#response}
Write embedded quotes as doubled single quotes. Use explicit `E'...'` syntax for intended backslash escapes and make the desired escape semantics clear. If the branch was `ERROR`, an explicit transaction is aborted and needs `ROLLBACK` or `ROLLBACK TO SAVEPOINT` before retrying; in autocommit, correct the literal first. A `WARNING` does not require transaction recovery, although correcting the literal is preferable to hiding the warning or globally changing policy. The cited paths do not imply a connection reset.

## Versions {#versions}
The locked catalogue records this condition from PostgreSQL `8.1.0`. The scanner guards and exact messages cited here are from PostgreSQL 18.6 `REL_18_6`; no locale or GUC runtime was executed.

## Related {#related}
[`22019`](../22019/), [`42601`](../42601/)

## Sources {#sources}
[`src/backend/parser/scan.l#L545-L560`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L545)

[`src/backend/parser/scan.l#L706-L720`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L706)

[`src/backend/parser/scan.l#L1423-L1459`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L1423)

The structured [evidence record](../data/evidence/22p06.json) records all four exact primaries, hints, guards, severity, and the source/runtime boundary.
