select open change scope Open full search

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

SQLSTATE / CLASS 42 · SYNTAX ERROR OR ACCESS RULE VIOLATION

cannot_coerce

SQLSTATE
42846
Condition name
cannot_coerce
Class
Syntax Error or Access Rule Violation
Source macro
ERRCODE_CANNOT_COERCE
Evidence
Observed at runtime in the source evidence
Analysis & operational context

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

<h1>42846 — Cannot coerce</h1>

At a glance

42846 is cannot_coerce: no applicable cast connects the source and target types. The selected expression asks for integer to date.

Meaning

The fixed primary is cannot cast type %s to %s. It is not 22P02 (a bad value for a valid cast) or 42804 (an expression type mismatch in a target context). Inspect pg_cast and the expression context; the selected autocommit connection remains IDLE after the impossible cast.

Diagnosis

Read both source and target types from the diagnostic and inspect pg_cast and the expression context. Confirm whether the application needs a typed literal or a documented conversion function.

Response

Construct a value in the target type or use a documented conversion function. The case uses DATE '2026-01-01'; it does not invent an integer-to-date business mapping or hide input errors with NULLIF. In an explicit transaction, the failed cast leaves INERROR; roll back or return to a suitable savepoint before retrying. The selected autocommit path returns to IDLE.

Observed diagnostics

The selected parse_expr.c group is explicit ERROR with primary cannot cast type %s to %s and no detail or hint.

Representative case

The registry evaluates SELECT 1::integer::date, then a date-typed literal and asserts the exact returned date.

SELECT 1::integer::date;
SELECT DATE '2026-01-01';

The selected 18.6 and 10.21 runs passed SQLSTATE, severity, state/recovery, repair, cleanup, and isolated-target stop assertions. See case JSON and authored evidence; private manifest and registry hashes are recorded there.

Versions

The locked catalogue contains this condition from the 7.4 presence bound through the listed snapshots. The selected natural case passed on PostgreSQL 18.6 and 10.21; that bounded result does not infer every intermediate release or every source branch.

Sources

  • src.errcodes.REL_18_6 — fixed definition at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba (source).
  • src.cannot-coerce.18.6src/backend/parser/parse_expr.c lines 2782–2787 at 724edf9bde9d356724ad384a2e196edc3c9f80f7; blob SHA-256 63c37770872a08978f931c31f801666a08e3bc8e51c2c65ff60151a1d139d55b (source).
  • src.cannot-coerce.10.23src/backend/parser/parse_expr.c lines 2739–2744 at 02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4; blob SHA-256 2700ba4a37d0715b66be76c900c7da7cf6a4d818eb71178e7b9a51e48e59e136 (source).
  • src.calls.REL_18_6 / src.calls.REL_10_23 — fixed call scans, SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf / 00d16d3eb01b71ccf1b245c8f3102f9d0ec9f36fb02777b8dd1b99fcb263040c.
  • manifest.42846 / snippet-registry.42846 — hashes are recorded in evidence/42846.json and each runtime record.

Source evidence

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

42846 is the cannot_coerce condition in Class 42.

Method: Read fixed errcodes.txt and locked catalogue metadata.

Limits: Identity does not identify every backend call that can reuse this SQLSTATE.

src.errcodes.REL_18_6

The selected impossible_cast follows a resolved PostgreSQL source-call group; other branches can use different dynamic fields.

Method: Trace the REL_18_6 and REL_10_23 source-call records at the locked commits.

Limits: This is one mechanism boundary, not an exhaustive inventory of the code.

src.cannot-coerce.18.6 src.cannot-coerce.10.23

The selected impossible_cast passed on isolated PostgreSQL 18.6 and 10.21 targets.

Method: Run the shared SQL registry and inspect SQLSTATE, severity, state, repair, cleanup, and target stop.

Limits: Scope is limited to these statements, psycopg, and two versions.

manifest.42846 snippet-registry.42846

The locked catalogue records 42846 from the 7.4 presence bound through the listed snapshots; runtime scope is 18.6 and 10.21.

Method: Read generated facts, fixed calls, and selected summaries.

Limits: Definition presence is not an exact behavioral introduction; two runtime versions do not prove every intermediate behavior.

src.errcodes.REL_18_6 src.calls.REL_18_6 src.calls.REL_10_23

Message templates

explicit ERROR · message.impossible_cast

Primary

cannot cast type %s to %s

Reproduction & repair cases

impossible_cast · PG 10, 18

Preconditions

  • A runner-owned disposable target is provisioned.

Trigger: Explicitly cast an integer expression to date.

Expected assertions

  • SQLSTATE is 42846 with cannot cast type integer to date
  • The failed autocommit session remains IDLE
  • A date-typed literal returns the intended date

Repair: Use a source type with a documented cast or construct a value in the target type; changing an input string is not the same as making an impossible type cast valid.

Cleanup: Close the runner connection; no persistent objects are created.

Recorded runtime evidence

18.6 (Homebrew) · passed

Run: syntax2-42846-latest

{
  "diagnostic": {
    "text": "cannot cast type integer to date\nLINE 1: SELECT 1::integer::date\n                         ^",
    "context": null,
    "severity": "ERROR",
    "sqlstate": "42846",
    "table_name": null,
    "column_name": null,
    "schema_name": null,
    "source_file": "parse_expr.c",
    "source_line": "2787",
    "message_hint": null,
    "datatype_name": null,
    "exception_type": "CannotCoerce",
    "internal_query": null,
    "message_detail": null,
    "constraint_name": null,
    "message_primary": "cannot cast type integer to date",
    "source_function": "transformTypeCast",
    "internal_position": null,
    "statement_position": "18",
    "severity_nonlocalized": "ERROR"
  },
  "repair_value": "2026-01-01",
  "failed_status": "IDLE",
  "repair_status": "IDLE",
  "connection_autocommit": true
}
10.21 (Debian 10.21-1.pgdg90+1) · passed

Run: syntax2-42846-pg10

{
  "diagnostic": {
    "text": "cannot cast type integer to date\nLINE 1: SELECT 1::integer::date\n                         ^",
    "context": null,
    "severity": "ERROR",
    "sqlstate": "42846",
    "table_name": null,
    "column_name": null,
    "schema_name": null,
    "source_file": "parse_expr.c",
    "source_line": "2735",
    "message_hint": null,
    "datatype_name": null,
    "exception_type": "CannotCoerce",
    "internal_query": null,
    "message_detail": null,
    "constraint_name": null,
    "message_primary": "cannot cast type integer to date",
    "source_function": "transformTypeCast",
    "internal_position": null,
    "statement_position": "18",
    "severity_nonlocalized": "ERROR"
  },
  "repair_value": "2026-01-01",
  "failed_status": "IDLE",
  "repair_status": "IDLE",
  "connection_autocommit": true
}

Definition snapshot: english-manuals:26a39a3f19421de39f3b271cb11… · English manual source