文档 / SQL 状态码 / Class 22 数据异常
22P02 invalid_text_representation
文本表示无效
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"
invalid character in extension owner: must not contain any of "%s"
invalid input syntax for type integer: "%s"
invalid input syntax for integer: "%s"
代表案例
共享注册表在自动提交下先发送一次无效文本 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 调用方或所有中间版本。
来源
- 上游源码 src/backend/utils/errcodes.txt 第 206 行
- 上游源码 src/backend/utils/adt/numutils.c 第 59–62 行
- 上游源码 src/backend/utils/adt/numutils.c 第 614–618 行
- 上游源码 src/backend/commands/copyfrom.c 第 1169 行
- 上游源码 contrib/file_fdw/file_fdw.c 第 799 行
- 上游源码 contrib/isn/isn.c 第 409 行
- 上游源码 src/backend/commands/extension.c 第 1366 行
- 核验材料 verify/cases/22P02/cases.json
- 核验材料 verify/cases/22P02/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
22P02 is invalid_text_representation in SQLSTATE Class 22.
-
The cited PostgreSQL 18.6 source paths support the representative mechanisms and message boundaries described.
-
The locked catalogue records 22P02 from 7.4; that boundary does not prove exact implementation introduction.
-
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.
-
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.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | invalid_integer_text |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed | invalid_integer_text |
同类 SQL 状态码
| 状态码 | 条件名 | 宏名称 | 严重等级 | 版本 |
|---|---|---|---|---|
| 22000 | data_exception | ERRCODE_DATA_EXCEPTION |
ERROR | 7.4 |
| 数据异常的通用类别,具体原因见成员码。 | 活跃 | |||
| 22001 | string_data_right_truncation | ERRCODE_STRING_DATA_RIGHT_TRUNCATION |
ERROR | 7.4 |
| 值超过目标类型长度而被右截断。 | 活跃 | |||
| 22002 | null_value_no_indicator_parameter | ERRCODE_NULL_VALUE_NO_INDICATOR_PARAMETER |
ERROR | 7.4 |
| 返回 NULL 但没有提供指示变量,见于 ECPG。 | 活跃 | |||
| 22003 | numeric_value_out_of_range | ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE |
ERROR | 7.4 |
| 数值超出目标类型范围,如整数溢出。 | 活跃 | |||
| 22004 | null_value_not_allowed | ERRCODE_NULL_VALUE_NOT_ALLOWED |
ERROR | 7.4 |
| 参数或值不允许为 NULL。 | 活跃 | |||
| 22005 | error_in_assignment | ERRCODE_ERROR_IN_ASSIGNMENT |
ERROR | 7.4 |
| 赋值违反目标变量契约,如变量声明为 CONSTANT。 | 活跃 | |||
| 22007 | invalid_datetime_format | ERRCODE_INVALID_DATETIME_FORMAT |
ERROR | 7.4 |
| 日期时间格式串无效,如 interval 格式错误。 | 活跃 | |||
| 22008 | datetime_field_overflow | ERRCODE_DATETIME_FIELD_OVERFLOW |
ERROR | 7.4 |
| 日期或时间字段取值越界。 | 活跃 | |||
| 22009 | invalid_time_zone_displacement_value | ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE |
ERROR | 7.4 |
| 时区偏移量超出支持范围。 | 活跃 | |||
| 2200B | escape_character_conflict | ERRCODE_ESCAPE_CHARACTER_CONFLICT |
ERROR | 7.4 |
| 转义字符与其他字符冲突。 | 活跃 | |||
| 2200C | invalid_use_of_escape_character | ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER |
ERROR | 7.4 |
| 转义字符用法无效,如 SIMILAR TO 模式错误。 | 活跃 | |||
| 2200D | invalid_escape_octet | ERRCODE_INVALID_ESCAPE_OCTET |
ERROR | 7.4 |
| 转义八位组无效的数据异常条件。 | 活跃 | |||
| 2200F | zero_length_character_string | ERRCODE_ZERO_LENGTH_CHARACTER_STRING |
ERROR | 7.4 |
| 操作拒绝了零长度字符串。 | 活跃 | |||
| 2200G | most_specific_type_mismatch | ERRCODE_MOST_SPECIFIC_TYPE_MISMATCH |
ERROR | 7.4 |
| 无法确定值的最具体类型,类型不匹配。 | 活跃 | |||
| 2200H | sequence_generator_limit_exceeded | ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED |
ERROR | 10 |
| nextval 触及序列的最大值或最小值。 | 活跃 | |||
| 2200L | not_an_xml_document | ERRCODE_NOT_AN_XML_DOCUMENT |
ERROR | 8.3 |
| 输入不满足 XML DOCUMENT 形式要求。 | 活跃 | |||
| 2200M | invalid_xml_document | ERRCODE_INVALID_XML_DOCUMENT |
ERROR | 8.3 |
| XML 文档解析失败,文档无效。 | 活跃 | |||
| 2200N | invalid_xml_content | ERRCODE_INVALID_XML_CONTENT |
ERROR | 8.3 |
| XML 内容无效,未通过 CONTENT 模式解析。 | 活跃 | |||
| 2200S | invalid_xml_comment | ERRCODE_INVALID_XML_COMMENT |
ERROR | 8.3 |
| XML 注释无效,含 -- 或以 - 结尾。 | 活跃 | |||
| 2200T | invalid_xml_processing_instruction | ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION |
ERROR | 8.3 |
| XML 处理指令无效,目标名或内容非法。 | 活跃 | |||
| 22010 | invalid_indicator_parameter_value | ERRCODE_INVALID_INDICATOR_PARAMETER_VALUE |
ERROR | 7.4 |
| 指示参数取值不符合规则。 | 活跃 | |||
| 22011 | substring_error | ERRCODE_SUBSTRING_ERROR |
ERROR | 7.4 |
| substring 长度参数为负等取子串错误。 | 活跃 | |||
| 22012 | division_by_zero | ERRCODE_DIVISION_BY_ZERO |
ERROR | 7.4 |
| 除法或取模运算遇到零除数。 | 活跃 | |||
| 22013 | invalid_preceding_or_following_size | ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE |
ERROR | 11 |
| 窗口帧的 PRECEDING/FOLLOWING 偏移为负。 | 活跃 | |||
| 22014 | invalid_argument_for_ntile_function | ERRCODE_INVALID_ARGUMENT_FOR_NTILE |
ERROR | 8.4 |
| ntile 的桶数量必须大于零。 | 活跃 | |||
| 22015 | interval_field_overflow | ERRCODE_INTERVAL_FIELD_OVERFLOW |
ERROR | 7.4 |
| interval 输入字段超出可接受范围。 | 活跃 | |||
| 22016 | invalid_argument_for_nth_value_function | ERRCODE_INVALID_ARGUMENT_FOR_NTH_VALUE |
ERROR | 8.4 |
| nth_value 的序号必须大于零。 | 活跃 | |||
| 22018 | invalid_character_value_for_cast | ERRCODE_INVALID_CHARACTER_VALUE_FOR_CAST |
ERROR | 7.4 |
| 字符值无法通过所要求的类型转换。 | 活跃 | |||
| 22019 | invalid_escape_character | ERRCODE_INVALID_ESCAPE_CHARACTER |
ERROR | 7.4 |
| 操作拒绝了给定的转义字符。 | 活跃 | |||
| 2201B | invalid_regular_expression | ERRCODE_INVALID_REGULAR_EXPRESSION |
ERROR | 7.4 |
| 正则表达式无效,编译或执行阶段被拒绝。 | 活跃 | |||
| 2201E | invalid_argument_for_logarithm | ERRCODE_INVALID_ARGUMENT_FOR_LOG |
ERROR | 8.0 |
| 对数函数参数为零或负数。 | 活跃 | |||
| 2201F | invalid_argument_for_power_function | ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION |
ERROR | 8.0 |
| 幂运算参数使实数结果无定义。 | 活跃 | |||
| 2201G | invalid_argument_for_width_bucket_function | ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION |
ERROR | 8.0 |
| width_bucket 参数无效,如桶数非正。 | 活跃 | |||
| 2201W | invalid_row_count_in_limit_clause | ERRCODE_INVALID_ROW_COUNT_IN_LIMIT_CLAUSE |
ERROR | 8.4 |
| LIMIT 行数无效,不能为负。 | 活跃 | |||
| 2201X | invalid_row_count_in_result_offset_clause | ERRCODE_INVALID_ROW_COUNT_IN_RESULT_OFFSET_CLAUSE |
ERROR | 8.4 |
| OFFSET 行数无效,不能为负。 | 活跃 | |||
| 22021 | character_not_in_repertoire | ERRCODE_CHARACTER_NOT_IN_REPERTOIRE |
ERROR | 7.4 |
| 字节序列对目标编码无效。 | 活跃 | |||
| 22022 | indicator_overflow | ERRCODE_INDICATOR_OVERFLOW |
ERROR | 7.4 |
| 指示变量容纳不下接口返回的结果。 | 活跃 | |||
| 22023 | invalid_parameter_value | ERRCODE_INVALID_PARAMETER_VALUE |
ERROR | 7.4 |
| 参数或选项取值超出允许范围。 | 活跃 | |||
| 22024 | unterminated_c_string | ERRCODE_UNTERMINATED_C_STRING |
ERROR | 7.4 |
| C 风格字符串缺少终止符。 | 活跃 | |||
| 22025 | invalid_escape_sequence | ERRCODE_INVALID_ESCAPE_SEQUENCE |
ERROR | 7.4 |
| 转义序列无效,如 ESCAPE 参数非单字符。 | 活跃 | |||
| 22026 | string_data_length_mismatch | ERRCODE_STRING_DATA_LENGTH_MISMATCH |
ERROR | 7.4 |
| 位串长度与声明的类型长度不一致。 | 活跃 | |||
| 22027 | trim_error | ERRCODE_TRIM_ERROR |
ERROR | 7.4 |
| trim 操作的参数或裁剪规格无效。 | 活跃 | |||
| 2202E | array_subscript_error | ERRCODE_ARRAY_SUBSCRIPT_ERROR |
ERROR | 7.4 |
| 数组下标或维度错误,如拼接维度不兼容。 | 活跃 | |||
| 2202G | invalid_tablesample_repeat | ERRCODE_INVALID_TABLESAMPLE_REPEAT |
ERROR | 9.5 |
| TABLESAMPLE 的 REPEATABLE 种子不能为空。 | 活跃 | |||
| 2202H | invalid_tablesample_argument | ERRCODE_INVALID_TABLESAMPLE_ARGUMENT |
ERROR | 9.5 |
| TABLESAMPLE 方法收到越界的采样参数。 | 活跃 | |||
| 22030 | duplicate_json_object_key_value | ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE |
ERROR | 12 |
| 构造 JSON 对象时出现重复键。 | 活跃 | |||
| 22031 | invalid_argument_for_sql_json_datetime_function | ERRCODE_INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION |
ERROR | 13 |
| SQL/JSON datetime 方法的参数或格式无效。 | 活跃 | |||
| 22032 | invalid_json_text | ERRCODE_INVALID_JSON_TEXT |
ERROR | 12 |
| JSON 文本无效,无法被消费方解析。 | 活跃 | |||
| 22033 | invalid_sql_json_subscript | ERRCODE_INVALID_SQL_JSON_SUBSCRIPT |
ERROR | 12 |
| SQL/JSON 数组下标无效,如越界或非数值。 | 活跃 | |||
| 22034 | more_than_one_sql_json_item | ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM |
ERROR | 12 |
| SQL/JSON 结果多于一项,需要单项。 | 活跃 | |||
| 22035 | no_sql_json_item | ERRCODE_NO_SQL_JSON_ITEM |
ERROR | 12 |
| 指定 SQL/JSON path 没有找到任何项。 | 活跃 | |||
| 22036 | non_numeric_sql_json_item | ERRCODE_NON_NUMERIC_SQL_JSON_ITEM |
ERROR | 12 |
| JSON 路径项方法收到非数值的项。 | 活跃 | |||
| 22037 | non_unique_keys_in_a_json_object | ERRCODE_NON_UNIQUE_KEYS_IN_A_JSON_OBJECT |
ERROR | 12 |
| JSON 对象中的键不唯一。 | 活跃 | |||
| 22038 | singleton_sql_json_item_required | ERRCODE_SINGLETON_SQL_JSON_ITEM_REQUIRED |
ERROR | 12 |
| SQL/JSON 路径要求单一指定类型的结果。 | 活跃 | |||
| 22039 | sql_json_array_not_found | ERRCODE_SQL_JSON_ARRAY_NOT_FOUND |
ERROR | 12 |
| JSON 路径访问器要求数组却收到非数组项。 | 活跃 | |||
| 2203A | sql_json_member_not_found | ERRCODE_SQL_JSON_MEMBER_NOT_FOUND |
ERROR | 12 |
| JSON 对象成员不存在或当前项不是对象。 | 活跃 | |||
| 2203B | sql_json_number_not_found | ERRCODE_SQL_JSON_NUMBER_NOT_FOUND |
ERROR | 12 |
| jsonpath 一元运算收到非数值的项。 | 活跃 | |||
| 2203C | sql_json_object_not_found | ERRCODE_SQL_JSON_OBJECT_NOT_FOUND |
ERROR | 12 |
| JSON 路径访问器要求对象却收到非对象项。 | 活跃 | |||
| 2203D | too_many_json_array_elements | ERRCODE_TOO_MANY_JSON_ARRAY_ELEMENTS |
ERROR | 12 |
| JSON 数组元素数量过多。 | 活跃 | |||
| 2203E | too_many_json_object_members | ERRCODE_TOO_MANY_JSON_OBJECT_MEMBERS |
ERROR | 12 |
| JSON 对象成员数量过多。 | 活跃 | |||
| 2203F | sql_json_scalar_required | ERRCODE_SQL_JSON_SCALAR_REQUIRED |
ERROR | 12 |
| JSON_VALUE 要求标量,收到的项不是标量。 | 活跃 | |||
| 2203G | sql_json_item_cannot_be_cast_to_target_type | ERRCODE_SQL_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE |
ERROR | 15 |
| SQL/JSON 项无法转换为目标类型。 | 活跃 | |||
| 22P01 | floating_point_exception | ERRCODE_FLOATING_POINT_EXCEPTION |
ERROR | 7.4 |
| 浮点异常,进程收到 SIGFPE 信号。 | 活跃 | |||
| 22P02 | invalid_text_representation | ERRCODE_INVALID_TEXT_REPRESENTATION |
ERROR | 7.4 |
| 文本无法解析为目标类型,如整数输入非法。 | 活跃 | |||
| 22P03 | invalid_binary_representation | ERRCODE_INVALID_BINARY_REPRESENTATION |
ERROR | 7.4 |
| 二进制数据格式无效,如 Bind 参数或二进制 COPY。 | 活跃 | |||
| 22P04 | bad_copy_file_format | ERRCODE_BAD_COPY_FILE_FORMAT |
ERROR | 7.4 |
| COPY 文件结构错误,如字段数或头部不符。 | 活跃 | |||
| 22P05 | untranslatable_character | ERRCODE_UNTRANSLATABLE_CHARACTER |
ERROR | 7.4 |
| 字符在目标编码中没有对应表示。 | 活跃 | |||
| 22P06 | nonstandard_use_of_escape_character | ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER |
ERROR | 8.1 |
| 字符串字面量中反斜杠转义用法非标准。 | 活跃 | |||