select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 22 · DATA EXCEPTION

invalid_argument_for_sql_json_datetime_function

SQLSTATE
22031
Condition name
invalid_argument_for_sql_json_datetime_function
Class
Data Exception
Source macro
ERRCODE_INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION
Evidence
Source path confirmed
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>22031</h1>

At a glance

A SQL/JSON datetime method received an invalid type, precision, or format. Fixed jsonpath execution reports an unrecognized format and hints to use a datetime template argument.

Messages

The datetime-method guards use these primary texts:

Guard Primary and hint
non-string item jsonpath item method .%s() can only be applied to a string
.datetime() format not recognized %s format is not recognized: "%s"; HINT: Use a datetime template argument to specify the input data format.
precision outside integer range time precision of jsonpath item method .%s() is out of range for type integer
invalid adjusted precision time precision of jsonpath item method .%s() is invalid

Meaning

The fixed executeDateTimeMethod path first requires a scalar string. .datetime(template) passes the explicit template to parse_datetime: when jspThrowErrors(cxt) is false, its ErrorSaveContext turns a parse failure into jperError; when throwing is enabled, the parser receives no save context and may throw its underlying error. The no-template .datetime(), .date(), .time(), .time_tz(), .timestamp(), and .timestamp_tz() paths try their enumerated ISO formats, saving each candidate failure in ErrorSaveContext even on a throwing execution; only after every candidate fails does the final 22031 RETURN_ERROR branch decide whether to throw or return jperError. Optional time precision is converted to an integer and checked before adjustment. A missing format, incompatible conversion, non-string item, or invalid precision uses 22031.

Diagnosis

Record the method name, input JSON item type, datetime text, template text if present, and precision argument. The .datetime() no-format match has the HINT to provide a template; the other methods use fixed ISO candidates and have no such template hint. Separate a scalar type mismatch from a format failure and from precision range/adjustment errors. lax controls structural auto-wrap/unwrap and structural-error handling; it does not generally suppress datetime parse or conversion errors. Check the executor's throwErrors/RETURN_ERROR path and the jsonb_path_* function's silent parameter or SQL/JSON ON ERROR clause to determine whether a saved parse error was returned or raised.

Response

Pass a string item to the method, use a template matching the datetime text for .datetime(), or choose the method whose ISO type matches the input. Keep precision within the integer and datetime typmod rules. If a non-ERROR ON ERROR behavior intentionally handles parse failure, repair or retain that behavior according to the application contract; otherwise correct the input or template. If an ERROR occurred in an explicit transaction, roll back or roll back to the existing savepoint before retrying; autocommit can retry the corrected expression.

Versions

The locked catalogue records this condition from 13.0; the fixed SQL/JSON datetime source path here is PostgreSQL 18.6. No natural runtime observation is claimed for this page.

Sources

The datetime method is src/backend/utils/adt/jsonpath_exec.c#L2326-2780, including string/type checks, explicit-template and ISO candidate parsing, ErrorSaveContext, conversions, and precision guards. The strict/lax/throw split is defined by #L235-249 and #L654-727. The structured evidence record retains the exact primary and hint roles; no natural runtime was run.

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

22031 is invalid_argument_for_sql_json_datetime_function in SQLSTATE Class 22.

Method: Read fixed definition.

src.errcodes.18.6

The fixed jsonpath datetime-method implementation first requires a string item. An explicit .datetime(template) call passes the template to parse_datetime with NULL ErrorSaveContext only when jspThrowErrors(cxt) is true; otherwise it saves the parse error and returns jperError. The no-template ISO candidate loop saves each candidate failure even on throwing execution, then the final RETURN_ERROR branch decides whether an unrecognized format throws 22031 or returns a path error. Lax controls structural auto-wrap/unwrap and structural errors; it does not generally suppress datetime parse/conversion errors. Precision, type conversion, and format guards remain 22031 paths.

Method: Read executeDateTimeMethod together with the jsonpath context flags and RETURN_ERROR macro.

Limits: Source confirmation is not a natural runtime observation.

src.path.0 src.jsonpath.datetime src.jsonpath.context-flags src.jsonpath.execute-context

The locked catalogue records 22031 from 13.0 without proving exact implementation introduction.

Method: Use catalogue boundary.

src.errcodes.18.6

Message templates

ERROR · message.0

Primary

%s format is not recognized: "%s"
ERROR · message.string-only

Primary

jsonpath item method .%s() can only be applied to a string
ERROR · message.format-unrecognized

Primary

%s format is not recognized: "%s"

Hint

Use a datetime template argument to specify the input data format.
ERROR · message.precision-range

Primary

time precision of jsonpath item method .%s() is out of range for type integer
ERROR · message.precision-invalid

Primary

time precision of jsonpath item method .%s() is invalid

Reproduction & repair cases

No reproduction case is attached to this condition.

Recorded runtime evidence

No runtime observation is attached to this entry.

Version history 1
  1. PG 12 → 13added

Definition snapshot: english-manuals:feaccad07b7621a267960c41ac3… · English manual source