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

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

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

22007 invalid_datetime_format

ERROR 已实测 详解 实测通过

类别
Class 22 数据异常
严重等级
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
HINT Intervals are not tied to specific calendar dates.

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

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

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

含义

22007 表示所选日期时间转换的输入或格式说明无效。固定 18.6 interval 格式化路径报告 invalid format specification for an interval value,并说明 interval 不绑定到特定日历日期。在这个 DCH 路径中,ID 是带日历语义的星期几 token,interval 不支持;DD 则可用。固定日期输入路径会通过 ParseDateTime/DecodeDateTimeDateTimeParseError 处理文本:bad-format/default 分支使用 invalid input syntax for type %s: "%s";字段越界属于 22008,其月日歧义分支才可能提示调整 DateStyle。18.6 文档说明 DateStyle 选择含糊数字日期的解释顺序。DateTimeParseError 可以填充 ErrorSaveContext 而不直接抛错,因此 soft-validation 调用与正常 cast/input 传播 ERROR 的行为不同。

诊断

记录原始文本、目标类型、DateStyleTimeZone、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。

来源

证据

断言

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

  • 22007 is invalid_datetime_format in SQLSTATE Class 22.

    核实方式Read fixed definition.

    来源src/backend/utils/errcodes.txt

  • The interval/time formatting guard rejects calendar-specific format tokens for interval values and supplies a fixed hint explaining the calendar boundary.

    核实方式Read the INVALID_FOR_INTERVAL macro and both message fields.

    不覆盖The selected runtime covers interval formatting only.

    来源src/backend/utils/adt/formatting.c

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

    核实方式Read the fixed date input/parser and error mapping, then the same-version documentation.

    不覆盖This confirms source and documentation scope only; the selected runtime covers interval formatting, not DateStyle parsing.

    来源src/backend/utils/adt/date.c · src/backend/utils/adt/datetime.c · doc/src/sgml/datatype.sgml

  • The locked catalogue records 22007 from 7.4 without proving an exact implementation introduction date.

    核实方式Use catalogue boundary.

    来源src/backend/utils/errcodes.txt

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

    核实方式Execute the shared registry and inspect selected summaries.

    不覆盖The observation does not generalize to DateStyle parsing or every date/time formatter.

    来源verify/cases/22007/snippets.json

运行记录

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

同类错误代码

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