选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 22 数据异常

22P06 nonstandard_use_of_escape_character

ERROR 源码确认 参考 未实测

类别
Class 22 数据异常
严重等级
ERROR
条件名
nonstandard_use_of_escape_character
宏名称
ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER
启用版本
8.1
状态
活跃

版本覆盖

速览

SQL 扫描器发现反斜杠转义形式在当前字符串字面量规则下非标准或不安全。PostgreSQL 18.6 有一个不安全引号转义的 ERROR 分支,以及反斜杠-引号、反斜杠-反斜杠和其他转义用法的三个 WARNING 分支。

含义

扫描器把 backslash_quoteescape_string_warningstandard_conforming_strings 复制到自身状态。普通带引号字符串只有在 standard_conforming_strings 为 off 时才进入可处理转义的 xe 规则;显式 E'...' 直接进入该规则。显式 E 字符串在进入 xe 时把 warn_on_first_escape 初始化为 false,因此不会触发这组旧式的首次转义 WARNING;但它仍受 backslash_quoteERROR 检查条件约束,E 前缀不是绕过该条件的通用办法。反斜杠后是单引号时,如果 backslash_quote = off,或安全编码模式遇到仅客户端支持的编码(client-only),就以 ERROR 发出主消息 unsafe use of \' in a string literal,并提示使用成对单引号。

escape_string_warning 和每个字符串只警告第一次转义的条件打开时,词法器会分别为 \'\\ 或其他转义每个字符串发出一次 WARNING。精确主消息是 nonstandard use of \' in a string literalnonstandard use of \\ in a string literalnonstandard use of escape in a string literal。这些是警告诊断,不是 NOTICE,本身不会中止语句。

报文

  • ERROR 主消息:unsafe use of \' in a string literal;HINT:Use '' to write quotes in strings. \' is insecure in client-only encodings.
  • WARNING 主消息:nonstandard use of \' in a string literal;HINT:Use '' to write quotes in strings, or use the escape string syntax (E'...').
  • WARNING 主消息:nonstandard use of \\ in a string literal;HINT:Use the escape string syntax for backslashes, e.g., E'\\'.
  • WARNING 主消息:nonstandard use of escape in a string literal;HINT:Use the escape string syntax for escapes, e.g., E'\r\n'.

报文模板

源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。

主消息 unsafe use of \' in a string literal
HINT Use '' to write quotes in strings. \' is insecure in client-only encodings.

来源:src/backend/parser/scan.l @ REL_18_6

主消息 nonstandard use of \' in a string literal
HINT Use '' to write quotes in strings, or use the escape string syntax (E'...').

来源:src/backend/parser/scan.l @ REL_18_6

主消息 nonstandard use of \\ in a string literal
HINT Use the escape string syntax for backslashes, e.g., E'\\'.

来源:src/backend/parser/scan.l @ REL_18_6

主消息 nonstandard use of escape in a string literal
HINT Use the escape string syntax for escapes, e.g., E'\r\n'.

来源:src/backend/parser/scan.l @ REL_18_6

诊断

保留字面量写法和字符位置、是否有 E 前缀、standard_conforming_stringsbackslash_quoteescape_string_warning、客户端编码及消息严重性。ERROR 分支会拒绝当前语句,警告分支可能允许语句继续,因此不能只根据“反斜杠”三个字判断结果。

处理

嵌入单引号时写成连续两个单引号;确实需要反斜杠时使用明确的 E'...' 语法并固定预期转义语义。如果是 ERROR,显式事务已中止,重试前执行 ROLLBACKROLLBACK TO SAVEPOINT;自动提交模式下先修正字面量。WARNING 不需要事务恢复,但应优先修正字面量,不要靠隐藏警告或全局改策略来掩盖问题。引用路径不要求重置连接。

版本

锁定目录从 PostgreSQL 8.1.0 记录此条件。引用的扫描器检查条件和精确消息来自 PostgreSQL 18.6 REL_18_6;本次没有运行区域设置或 GUC 案例。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

  • 22P06 is nonstandard_use_of_escape_character in SQLSTATE Class 22.

    核实方式Read fixed definition.

    来源src/backend/utils/errcodes.txt

  • 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.

    核实方式Read the complete xe escape rule and scanner setting copy.

    来源src/backend/parser/scan.l · src/backend/parser/scan.l

  • 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.

    核实方式Read ordinary and explicit-E literal-mode selection and all three warning branches.

    来源src/backend/parser/scan.l · src/backend/parser/scan.l · src/backend/parser/scan.l

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

    核实方式Read ereport severity at each branch.

    不覆盖No locale runtime was executed.

    来源src/backend/parser/scan.l · src/backend/parser/scan.l · src/backend/parser/scan.l

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

    核实方式Use catalogue boundary.

    来源src/backend/utils/errcodes.txt

同类错误代码

Class 22 数据异常 下的其他成员。