select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 22 · DATA EXCEPTION

nonstandard_use_of_escape_character

SQLSTATE
22P06
Condition name
nonstandard_use_of_escape_character
Class
Data Exception
Source macro
ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER
Evidence
Source path confirmed
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>22P06</h1>

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.

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.

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.

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

22P06 is nonstandard_use_of_escape_character in SQLSTATE Class 22.

Method: Read fixed definition.

src.errcodes.18.6

For an escape followed by a single quote, the scanner emits ERROR 22P06 when backslash_quote is off, or when safe-encoding mode is used with a client-only encoding. The primary and hint advise doubled quotes.

Method: Read the complete xe escape rule and scanner setting copy.

src.path.error-guard src.path.scanner-copy

For ordinary non-E strings the scanner enters xe only when standard_conforming_strings is off and initializes warn_on_first_escape for warning checks. Explicit E strings enter xe with warn_on_first_escape false, so they avoid this legacy first-escape WARNING while remaining subject to the backslash_quote ERROR guard. With escape_string_warning enabled on the warning path, the scanner emits one WARNING per string for backslash-quote, backslash-backslash, or another escape.

Method: Read ordinary and explicit-E literal-mode selection and all three warning branches.

src.path.literal-mode src.path.warning src.path.escape-warning

The ERROR rejects the current statement; WARNING is a warning diagnostic and does not by itself abort the statement or explicit transaction.

Method: Read ereport severity at each branch.

Limits: No locale runtime was executed.

src.path.error-guard src.path.warning src.path.escape-warning

The locked catalogue records 22P06 from 8.1.0; that boundary does not prove exact implementation introduction.

Method: Use catalogue boundary.

src.errcodes.18.6

Message templates

ERROR · message.error

Primary

unsafe use of \' in a string literal

Hint

Use '' to write quotes in strings. \' is insecure in client-only encodings.
WARNING · message.quote-warning

Primary

nonstandard use of \' in a string literal

Hint

Use '' to write quotes in strings, or use the escape string syntax (E'...').
WARNING · message.backslash-warning

Primary

nonstandard use of \\ in a string literal

Hint

Use the escape string syntax for backslashes, e.g., E'\\'.
WARNING · message.escape-warning

Primary

nonstandard use of escape in a string literal

Hint

Use the escape string syntax for escapes, e.g., E'\r\n'.

Reproduction & repair cases

No reproduction case is attached to this condition.

Recorded runtime evidence

No runtime observation is attached to this entry.

Definition snapshot: english-manuals:85ef690bd3bf966029a8889e159… · English manual source