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

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

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

22P02 invalid_text_representation

文本表示无效

ERROR 已实测 详解 实测通过

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

版本覆盖

速览

22P02 表示文本输入例程无法把值解释为目标类型。本页选择普通整数输入路径,同时标出 COPY/文本边界;COPY、枚举、扩展和 contrib 调用可能使用不同主报文。

含义

cast、赋值或文本 COPY 字段由目标类型的输入例程判断文本是否合法。PostgreSQL 18.6 使用 invalid input syntax for type integer: "%s",锁定的 PostgreSQL 10.23 源码使用 invalid input syntax for integer: "%s";两者都是动态模板,也不是统一的本地化字符串。其他固定调用者还包括 COPY reject-limit、枚举输入和扩展校验,因此 primary 文本和上下文可能不同。

诊断

记录目标类型、客户端编码和参数绑定后的原始值,并确认操作是 cast、赋值、文本 COPY 字段、枚举输入还是其他类型包装。把坏文本(22P02)与数值越界(22003)、日期时间语法错误(22007)、二进制表示无效(22P03)以及 COPY 文件/头部或 framing 错误(22P04)区分开。二进制 COPY payload 不能只按文本输入报文诊断。

处理

在输入边界校验并保持预期目标类型。只有修正值后才重试;除非业务规则明确要求,不要静默截断或把值变成 NULL。显式事务中的输入 ERROR 会使事务进入中止状态(25P02);继续前执行 ROLLBACK,或使用 ROLLBACK TO SAVEPOINT convert_input。COPY 出错后也必须先恢复事务,再执行下一条命令。

可复现案例

在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。

invalid_integer_text PG 10 / 18 有 SQL

前置条件

  • A runner-owned disposable target is provisioned.

触发

Cast non-numeric text to integer, then cast a valid integer text on the same connection.

断言

  • SQLSTATE is 22P02 with ERROR severity
  • The primary diagnostic identifies invalid integer input and the supplied text
  • The failed autocommit session remains IDLE
  • A valid integer text returns 42 afterward

处置

Validate or reject input before casting; if the input is valid, cast it to the intended type and keep the business validation separate from syntax recovery.

清理

Close all runner connections and drop the case schema with an owner connection.

实测诊断

普通整数路径固定为 ERROR,并有上述版本差异的动态主报文。选定实测记录了精确值:"not-an-integer" 产生 22P02,同一自动提交会话随后把合法 "42" 转换为 42。

报文模板

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

主消息 skipped more than REJECT_LIMIT (%" PRId64 ") rows due to data type incompatibility

来源:src/backend/commands/copyfrom.c @ REL_18_6 · contrib/file_fdw/file_fdw.c @ REL_18_6

主消息 cannot cast EAN13(%s) to %s for number: "%s"

来源:contrib/isn/isn.c @ REL_18_6

主消息 invalid character in extension owner: must not contain any of "%s"

来源:src/backend/commands/extension.c @ REL_18_6

主消息 invalid input syntax for type integer: "%s"

来源:src/backend/utils/adt/numutils.c @ REL_18_6

主消息 invalid input syntax for integer: "%s"

来源:src/backend/utils/adt/numutils.c @ REL_10_23

代表案例

共享注册表在自动提交下先发送一次无效文本 cast,检查真实诊断和 IDLE 状态,再在同一连接发送合法整数文本。这个实测恢复不同于显式事务;后者必须先回滚或回滚到保存点。页面 SQL 与运行器来自同一注册表,没有隐藏的第二定义。

SELECT 'not-an-integer'::integer;
SELECT '42'::integer

选定的 PostgreSQL 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态或断开恢复、修复、清理和一次性实例停止断言。详见 案例 JSON作者证据

版本

锁定目录从 7.4 起记录 22P02。18.6 与 10.21 的有界运行均通过,但主报文措辞如上有所不同;这不能推广到所有 22P02 调用方或所有中间版本。

来源

证据

断言

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

  • 22P02 is invalid_text_representation in SQLSTATE Class 22.

    核实方式Read fixed definition.

    来源src/backend/utils/errcodes.txt

  • The cited PostgreSQL 18.6 source paths support the representative mechanisms and message boundaries described.

    核实方式Read complete fixed source contexts.

    不覆盖Source confirmation is not natural runtime.

    来源src/backend/commands/copyfrom.c · contrib/file_fdw/file_fdw.c · contrib/isn/isn.c · src/backend/commands/extension.c

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

    核实方式Use catalogue boundary.

    来源src/backend/utils/errcodes.txt

  • The selected ordinary cast path reports invalid integer text through the numeric input routine; PostgreSQL 18 and 10 use different fixed wording around the type name.

    核实方式Read fixed numutils.c source-call records and compare selected runtime diagnostics.

    不覆盖This is one integer-input path; COPY, enum, extension, and contrib callers can use different 22P02 messages.

    来源src/backend/utils/adt/numutils.c · src/backend/utils/adt/numutils.c

  • The invalid_integer_text case passed on isolated PostgreSQL 18.6 and 10.21 targets, with IDLE recovery and a same-session valid value of 42.

    核实方式Execute the shared registry and inspect SQLSTATE, severity, IDLE state, repair, cleanup, and target stop.

    不覆盖Scope is limited to the integer cast statements, psycopg, and the two selected versions.

    来源verify/cases/22P02/cases.json · verify/cases/22P02/snippets.json

运行记录

目标服务器版本结果覆盖案例
latest 18.6 (Homebrew) passed invalid_integer_text
pg10 10.21 (Debian 10.21-1.pgdg90+1) passed invalid_integer_text

同类错误代码

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