Skip to content

22P06 — nonstandard_use_of_escape_character

Source-backed reference for PostgreSQL SQLSTATE 22P06.

22P06

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.

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

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

  • 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

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

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

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.

22019, 42601

Sources

src/backend/parser/scan.l#L545-L560

src/backend/parser/scan.l#L706-L720

src/backend/parser/scan.l#L1423-L1459

The structured evidence record records all four exact primaries, hints, guards, severity, and the source/runtime boundary.