↑↓ select↵ open⌫ change scopeOpen full search

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

Wiki / SQLSTATE

22P06 — nonstandard_use_of_escape_character

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.

English manual coverage begins with PostgreSQL 8.1. Source history and runtime observations have their own recorded version boundaries.

Reading PG 18·Select a recorded version

aliases
Unknown
class name
Data Exception
condition name
nonstandard_use_of_escape_character
lang
en
sqlstate
22P06

Definition PG 18

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.

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, 42601

Sources {#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.

Compare versions

SQLSTATE evidence and recorded runtime versions

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read fixed definition.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "22P06 is nonstandard_use_of_escape_character in SQLSTATE Class 22."
}
claims · error-guard
{
  "claim_id": "error-guard",
  "limits": "",
  "method": "Read the complete xe escape rule and scanner setting copy.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.error-guard",
    "src.path.scanner-copy"
  ],
  "statement": "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."
}
claims · warning-guards
{
  "claim_id": "warning-guards",
  "limits": "",
  "method": "Read ordinary and explicit-E literal-mode selection and all three warning branches.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.path.literal-mode",
    "src.path.warning",
    "src.path.escape-warning"
  ],
  "statement": "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."
}
claims · severity-boundary
{
  "claim_id": "severity-boundary",
  "limits": "No locale runtime was executed.",
  "method": "Read ereport severity at each branch.",
  "position": 3,
  "runtime": [],
  "sources": [
    "src.path.error-guard",
    "src.path.warning",
    "src.path.escape-warning"
  ],
  "statement": "The ERROR rejects the current statement; WARNING is a warning diagnostic and does not by itself abort the statement or explicit transaction."
}
claims · history
{
  "claim_id": "history",
  "limits": "",
  "method": "Use catalogue boundary.",
  "position": 4,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked catalogue records 22P06 from 8.1.0; that boundary does not prove exact implementation introduction."
}
messages · message.error
{
  "limits": "",
  "message_id": "message.error",
  "path": "",
  "position": 0,
  "raw": {
    "hint_template": "Use '' to write quotes in strings. \\' is insecure in client-only encodings.",
    "id": "message.error",
    "primary_template": "unsafe use of \\' in a string literal",
    "severity_source": "ERROR",
    "sources": [
      "src.path.error-guard"
    ],
    "sqlstate": "22P06"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.error-guard"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "unsafe use of \\' in a string literal",
      "position": 0,
      "role": "",
      "template": "unsafe use of \\' in a string literal"
    },
    {
      "kind": "hint",
      "literal": "Use '' to write quotes in strings. \\' is insecure in client-only encodings.",
      "position": 1,
      "role": "",
      "template": "Use '' to write quotes in strings. \\' is insecure in client-only encodings."
    }
  ]
}
messages · message.quote-warning
{
  "limits": "",
  "message_id": "message.quote-warning",
  "path": "",
  "position": 1,
  "raw": {
    "hint_template": "Use '' to write quotes in strings, or use the escape string syntax (E'...').",
    "id": "message.quote-warning",
    "primary_template": "nonstandard use of \\' in a string literal",
    "severity_source": "WARNING",
    "sources": [
      "src.path.warning"
    ],
    "sqlstate": "22P06"
  },
  "severity": "WARNING",
  "sources": [
    "src.path.warning"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "nonstandard use of \\' in a string literal",
      "position": 0,
      "role": "",
      "template": "nonstandard use of \\' in a string literal"
    },
    {
      "kind": "hint",
      "literal": "Use '' to write quotes in strings, or use the escape string syntax (E'...').",
      "position": 1,
      "role": "",
      "template": "Use '' to write quotes in strings, or use the escape string syntax (E'...')."
    }
  ]
}
messages · message.backslash-warning
{
  "limits": "",
  "message_id": "message.backslash-warning",
  "path": "",
  "position": 2,
  "raw": {
    "hint_template": "Use the escape string syntax for backslashes, e.g., E'\\\\'.",
    "id": "message.backslash-warning",
    "primary_template": "nonstandard use of \\\\ in a string literal",
    "severity_source": "WARNING",
    "sources": [
      "src.path.warning"
    ],
    "sqlstate": "22P06"
  },
  "severity": "WARNING",
  "sources": [
    "src.path.warning"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "nonstandard use of \\\\ in a string literal",
      "position": 0,
      "role": "",
      "template": "nonstandard use of \\\\ in a string literal"
    },
    {
      "kind": "hint",
      "literal": "Use the escape string syntax for backslashes, e.g., E'\\\\'.",
      "position": 1,
      "role": "",
      "template": "Use the escape string syntax for backslashes, e.g., E'\\\\'."
    }
  ]
}
messages · message.escape-warning
{
  "limits": "",
  "message_id": "message.escape-warning",
  "path": "",
  "position": 3,
  "raw": {
    "hint_template": "Use the escape string syntax for escapes, e.g., E'\\r\\n'.",
    "id": "message.escape-warning",
    "primary_template": "nonstandard use of escape in a string literal",
    "severity_source": "WARNING",
    "sources": [
      "src.path.escape-warning"
    ],
    "sqlstate": "22P06"
  },
  "severity": "WARNING",
  "sources": [
    "src.path.escape-warning"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "nonstandard use of escape in a string literal",
      "position": 0,
      "role": "",
      "template": "nonstandard use of escape in a string literal"
    },
    {
      "kind": "hint",
      "literal": "Use the escape string syntax for escapes, e.g., E'\\r\\n'.",
      "position": 1,
      "role": "",
      "template": "Use the escape string syntax for escapes, e.g., E'\\r\\n'."
    }
  ]
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L183"
}
sources · src.path.guc
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 1,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.guc",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L62"
}
sources · src.path.literal-mode
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 2,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.literal-mode",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L545-L560"
}
sources · src.path.error-guard
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 3,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.error-guard",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L706"
}
sources · src.path.warning
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 4,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.warning",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L1423"
}
sources · src.path.escape-warning
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 5,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.escape-warning",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L1450"
}
sources · src.path.scanner-copy
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/scan.l",
  "position": 6,
  "sha256": "bf453f1ae3c22b84fea6f8e8bed4fda476b7cfa49b234efbf92ca228234d0293",
  "source_id": "src.path.scanner-copy",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/scan.l#L1262"
}

Source references

Full definition and evidence JSON

Definition sources

center · PostgreSQL 18 · english-manuals:31426318850b232cfa7dbfcd4344a56decc442f09dc4ea741b7dc72bb61279ac

Text language: en · english-manuals:31426318850b232cfa7dbfcd4344a56decc442f09dc4ea741b7dc72bb61279ac