百科 / 错误代码 / Class 22 数据异常
22007 invalid_datetime_format
ERROR 已实测 详解 实测通过
- 条件名
invalid_datetime_format- 宏名称
ERRCODE_INVALID_DATETIME_FORMAT- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
固定 interval 格式化路径报告 invalid format specification for an interval value,并说明 interval 不绑定到特定日历日期。
共享案例用无效的 ID mask 格式化 interval '1 day',再用 DD 作为修复后的 interval 格式。本案例应分开发送两条 SELECT;第一条预期失败,之后再执行修复格式。本案例只验证 interval 格式化路径;会话清理由运行器负责。
SELECT to_char(interval '1 day', 'ID');
SELECT to_char(interval '1 day', 'DD');
校准实测了 interval 的 DCH 格式化路径:无效格式报告 invalid format specification for an interval value,提示为 Intervals are not tied to specific calendar dates.;有效 DD 格式返回 01,运行器的两条自动提交会话均回到 IDLE。本次运行只覆盖 interval 格式化;一般日期解析和 DateStyle 仍属于源码/文档范围。
报文
INVALID_FOR_INTERVAL guard 以 ERROR 严重性报告 primary:invalid format specification for an interval value,HINT 为 Intervals are not tied to specific calendar dates.。引用分支没有独立 DETAIL。固定日期输入解析的 DTERR_BAD_FORMAT/default 分支把 22007 映射为通用 primary 模板 invalid input syntax for type %s: "%s";相邻的字段越界和月日越界分支使用 22008,后者才会增加 HINT Perhaps you need a different "DateStyle" setting.。其他日期时间格式解析器也可能使用 22007 并产生不同的 primary/detail/hint,因此应保留完整诊断。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
invalid format specification for an interval value
Intervals are not tied to specific calendar dates.
invalid input syntax for type %s: "%s"
含义
22007 表示所选日期时间转换的输入或格式说明无效。固定 18.6 interval 格式化路径报告 invalid format specification for an interval value,并说明 interval 不绑定到特定日历日期。在这个 DCH 路径中,ID 是带日历语义的星期几 token,interval 不支持;DD 则可用。固定日期输入路径会通过 ParseDateTime/DecodeDateTime 和 DateTimeParseError 处理文本:bad-format/default 分支使用 invalid input syntax for type %s: "%s";字段越界属于 22008,其月日歧义分支才可能提示调整 DateStyle。18.6 文档说明 DateStyle 选择含糊数字日期的解释顺序。DateTimeParseError 可以填充 ErrorSaveContext 而不直接抛错,因此 soft-validation 调用与正常 cast/input 传播 ERROR 的行为不同。
诊断
记录原始文本、目标类型、DateStyle、TimeZone、format mask,以及操作是 cast、输入函数、to_date/to_timestamp 还是 interval 格式化。在改变数据前,用部署会话设置重现同一文本。区分无效 token/分隔符与已解析但超出范围的月日字段;后者可能产生 22008。
处置
让输入无歧义,在适当场景使用显式格式或 ISO 形式,并在应用边界明确设置会话解析选项。interval 格式化应使用支持 interval 的 mask,而不是日历日期 mask。本次固定案例使用自动提交,失败格式化调用后会话仍为 IDLE;显式事务中应先回滚整个事务,或回滚到失败调用前已有的保存点,再继续执行。写入前拒绝或修正无效文本,不要用切换 DateStyle 的方式静默改写含义。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
invalid_interval_format PG 10 / 18 有 SQL
触发
Format an interval with the date-only field ID, which is not defined for intervals.
断言
- SQLSTATE is 22007
- The diagnostic and HINT identify the interval format restriction
- The failed autocommit session remains IDLE
- A valid interval format returns the expected value afterward
处置
Use an interval-compatible format such as DD, or convert a value whose semantics really require a calendar date before formatting.
清理
Drop the case schema with an owner connection.
版本
锁定目录从 7.4 记录该条件,并在列出的正式快照及 19beta3 中出现;固定源码覆盖为 PostgreSQL 18.6。
来源
- 上游源码 doc/src/sgml/datatype.sgml 第 1878–1896 行
- 上游源码 src/backend/utils/errcodes.txt 第 179 行
- 上游源码 src/backend/utils/adt/date.c 第 110–178 行
- 上游源码 src/backend/utils/adt/datetime.c 第 4195–4266 行
- 上游源码 src/backend/utils/adt/formatting.c 第 548–559 行
- 核验材料 verify/cases/22007/snippets.json
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
22007 is invalid_datetime_format in SQLSTATE Class 22.
-
The interval/time formatting guard rejects calendar-specific format tokens for interval values and supplies a fixed hint explaining the calendar boundary.
-
Fixed date input dispatches text through ParseDateTime and DecodeDateTime. DateTimeParseError maps the DTERR_BAD_FORMAT/default branch to 22007 with the common primary invalid input syntax for type template; field overflow and month/day overflow use 22008, with the latter adding a DateStyle hint. The PostgreSQL 18 documentation describes DateStyle as selecting the interpretation order for ambiguous numeric date input, and the parser can fill ErrorSaveContext instead of throwing for soft-validation callers.
-
The locked catalogue records 22007 from 7.4 without proving an exact implementation introduction date.
-
The invalid_interval_format case passed on isolated PostgreSQL 18.6 and 10.21: the interval mask ID raised 22007 with the fixed hint, left the autocommit session IDLE, and DD returned 01.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | invalid_interval_format |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed | invalid_interval_format |
同类错误代码
Class 22 数据异常 下的其他成员。
22000data_exception22001string_data_right_truncation22002null_value_no_indicator_parameter22003numeric_value_out_of_range22004null_value_not_allowed22005error_in_assignment22008datetime_field_overflow22009invalid_time_zone_displacement_value2200Bescape_character_conflict2200Cinvalid_use_of_escape_character2200Dinvalid_escape_octet2200Fzero_length_character_string2200Gmost_specific_type_mismatch2200Hsequence_generator_limit_exceeded2200Lnot_an_xml_document2200Minvalid_xml_document2200Ninvalid_xml_content2200Sinvalid_xml_comment2200Tinvalid_xml_processing_instruction22010invalid_indicator_parameter_value22011substring_error22012division_by_zero22013invalid_preceding_or_following_size22014invalid_argument_for_ntile_function22015interval_field_overflow22016invalid_argument_for_nth_value_function22018invalid_character_value_for_cast22019invalid_escape_character2201Binvalid_regular_expression2201Einvalid_argument_for_logarithm2201Finvalid_argument_for_power_function2201Ginvalid_argument_for_width_bucket_function2201Winvalid_row_count_in_limit_clause2201Xinvalid_row_count_in_result_offset_clause22021character_not_in_repertoire22022indicator_overflow22023invalid_parameter_value22024unterminated_c_string22025invalid_escape_sequence22026string_data_length_mismatch22027trim_error2202Earray_subscript_error2202Ginvalid_tablesample_repeat2202Hinvalid_tablesample_argument22030duplicate_json_object_key_value22031invalid_argument_for_sql_json_datetime_function22032invalid_json_text22033invalid_sql_json_subscript22034more_than_one_sql_json_item22035no_sql_json_item22036non_numeric_sql_json_item22037non_unique_keys_in_a_json_object22038singleton_sql_json_item_required22039sql_json_array_not_found2203Asql_json_member_not_found2203Bsql_json_number_not_found2203Csql_json_object_not_found2203Dtoo_many_json_array_elements2203Etoo_many_json_object_members2203Fsql_json_scalar_required2203Gsql_json_item_cannot_be_cast_to_target_type22P01floating_point_exception22P02invalid_text_representation22P03invalid_binary_representation22P04bad_copy_file_format22P05untranslatable_character22P06nonstandard_use_of_escape_character