22007
Read PG 18 manual ↗invalid_datetime_format
- SQLSTATE
- 22007
- Condition name
- invalid_datetime_format
- Class
- Data Exception
- Source macro
- ERRCODE_INVALID_DATETIME_FORMAT
- Evidence
- Observed at runtime in the source evidence
English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗
At a glance
22007 is invalid_datetime_format. The fixed interval-formatting path reports an invalid format specification and explains that intervals are not tied to specific calendar dates.
The shared case formats interval '1 day' with the invalid ID mask, then uses DD as the repaired interval format. Send the two SELECT statements separately; the first is expected to fail before the repaired format runs. This verifies the interval-formatting path only; the runner owns session cleanup.
SELECT to_char(interval '1 day', 'ID');
SELECT to_char(interval '1 day', 'DD');
Calibration observed the interval DCH formatting path: an invalid format reported invalid format specification for an interval value with hint Intervals are not tied to specific calendar dates.; a valid DD format returned 01, and both runner autocommit sessions returned to IDLE. This run covers interval formatting only; general date parsing and DateStyle remain source/document scope.
Messages
The INVALID_FOR_INTERVAL guard raises ERROR with primary invalid format specification for an interval value and HINT Intervals are not tied to specific calendar dates. The cited branch has no separate DETAIL. In the fixed datetime input parser, the DTERR_BAD_FORMAT/default branch maps 22007 to the common primary template invalid input syntax for type %s: "%s"; the adjacent field-overflow and month/day-overflow branches use 22008 instead, with the latter adding HINT Perhaps you need a different "DateStyle" setting. Other datetime-format parsers can use 22007 with different primary/detail/hint variants, so retain the full diagnostic.
Meaning
22007 means the datetime input or format specification is invalid for the selected conversion. The fixed 18.6 interval-formatting path reports invalid format specification for an interval value and explains that intervals are not tied to specific calendar dates. In this DCH path, ID is a calendar-specific day-of-week token rejected for an interval, while DD is accepted. Fixed date input parsing routes text through ParseDateTime/DecodeDateTime and DateTimeParseError: the bad-format/default branch uses invalid input syntax for type %s: "%s"; field overflow belongs to 22008, whose month/day ambiguity branch can suggest a different DateStyle. The 18.6 documentation says DateStyle selects the interpretation order for ambiguous numeric input. DateTimeParseError can fill an ErrorSaveContext instead of throwing, so soft-validation callers differ from normal cast/input calls that propagate ERROR.
Diagnosis
Record the original text, target type, DateStyle, TimeZone, format mask, and whether the operation was a cast, input function, to_date/to_timestamp, or interval formatting. Test the same text under the deployed session settings before changing the data. Distinguish an invalid token or separator from a field such as month/day that parsed but is out of range; those paths may produce 22008 instead.
Response
Make the input unambiguous—prefer an explicit format or ISO form where appropriate—and set session parsing options deliberately at the application boundary. For interval formatting, use a mask supported for intervals rather than a calendar-date mask. The frozen case used autocommit, so the failed formatting call left the session IDLE; inside an explicit transaction, roll back the transaction or roll back to a pre-existing savepoint before continuing. Reject or repair invalid text before writing; do not silently reinterpret it under a different DateStyle.
Versions
The locked catalogue records this condition from 7.4 in the listed formal snapshots and 19beta3; fixed source coverage is PostgreSQL 18.6.
Sources
The fixed interval-formatting path is formatting.c#L555-L558. Fixed date input dispatch is in date.c#L110-L178, and the common parser error mapping is in datetime.c#L4195-L4266. PostgreSQL 18's date/time input documentation documents DateStyle and ambiguous date text. The structured evidence record pins the fixed sources, message, and hint.
Source evidence
Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.
22007 is invalid_datetime_format in SQLSTATE Class 22.
Method: Read fixed definition.
The interval/time formatting guard rejects calendar-specific format tokens for interval values and supplies a fixed hint explaining the calendar boundary.
Method: Read the INVALID_FOR_INTERVAL macro and both message fields.
Limits: The selected runtime covers interval formatting only.
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.
Method: Read the fixed date input/parser and error mapping, then the same-version documentation.
Limits: This confirms source and documentation scope only; the selected runtime covers interval formatting, not DateStyle parsing.
src.path.date-input src.path.datetime-error doc.datetime-input.18.6
The locked catalogue records 22007 from 7.4 without proving an exact implementation introduction date.
Method: Use catalogue boundary.
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.
Method: Execute the shared registry and inspect selected summaries.
Limits: The observation does not generalize to DateStyle parsing or every date/time formatter.
- src/backend/utils/errcodes.txt · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 raw/calls/REL_18_6.jsonl· ·- src/backend/utils/adt/formatting.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/utils/adt/date.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - src/backend/utils/adt/datetime.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - doc/src/sgml/datatype.sgml · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 verify/cases/22007/snippets.json· ·
Message templates
ERROR · message.interval
Primary
invalid format specification for an interval value
Hint
Intervals are not tied to specific calendar dates.
ERROR · message.datetime-input
Primary
invalid input syntax for type %s: "%s"
Reproduction & repair cases
invalid_interval_format · PG 10, 18
Preconditions
Trigger: Format an interval with the date-only field ID, which is not defined for intervals.
Expected assertions
- 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
Repair: Use an interval-compatible format such as DD, or convert a value whose semantics really require a calendar date before formatting.
Cleanup: Drop the case schema with an owner connection.
Recorded runtime evidence
18.6 (Homebrew) · passed
Run: 22007-data1-latest
{
"sqlstate": "22007",
"status_after_repair": "IDLE"
}10.21 (Debian 10.21-1.pgdg90+1) · passed
Run: 22007-data1-pg10
{
"sqlstate": "22007",
"status_after_repair": "IDLE"
}Definition snapshot: english-manuals:bf27ee1b762f0a83d6deee2f3f4… · English manual source