# 42804 — 数据类型不匹配（datatype_mismatch）

> PostgreSQL SQLSTATE 42804（数据类型不匹配，datatype_mismatch）的源码证据、诊断与处理参考。
---

# 42804 — 数据类型不匹配（datatype_mismatch）

## 速览 {#at-a-glance}

`42804` 是 **datatype_mismatch（数据类型不匹配）**：表达式或定义类型不同于目标上下文。本页选择整数列使用文本默认值。

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 字段 | 值 |
| --- | --- |
| SQLSTATE | `42804` |
| 条件名 | `datatype_mismatch` |
| 状态 | `有效` |
| 已知存在于 | `7.4` |
| 锁定快照 | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| 宏 | `ERRCODE_DATATYPE_MISMATCH` |
| 别名 | `—` |

<!-- source facts: data/errcodes/42804.json -->
<!-- END SQLSTATE FACTS -->

## 含义 {#meaning}

`cookDefault` 路径报告 `column "%s" is of type %s but default expression is of type %s` 并提示重写或 cast。同一 SQLSTATE 还覆盖 INSERT/UPDATE 目标赋值的 `column "%s" is of type %s but expression is of type %s`，以及 CASE/UNION 公共类型选择的 `%s types %s and %s cannot be matched`。它不同于 `22P02`（已选类型的输入解析器拒绝值）和 `42846`（选定公共目标没有转换路径）。检查 `pg_attribute.atttypid`、`pg_type`、`pg_get_expr` 以及实际表达式上下文；自动提交 CREATE 失败后为 `IDLE`。

## 诊断 {#diagnosis}

确认目标列或结果类型，再检查 `pg_attribute.atttypid`、`pg_type`，以及默认值或生成表达式的 `pg_get_expr`。INSERT/UPDATE 要对照赋值表达式与目标列类型；CASE/UNION 要检查参与公共类型选择的每个分支或输入。先依据诊断中展开的源/目标类型和位置，再决定 cast。

## 处理 {#response}

让表达式直接产生目标类型，或添加已理解语义的有效 cast。赋值场景应修正产生方或转成目标列类型；CASE/UNION 应让分支具有预期的公共类型。`22P02` 的输入错误不能靠改字符串后就当成类型匹配；如果没有转换路径，也应按源码区分 `42846`，不能误写成 `42804`。案例用 `DEFAULT 1`，插入默认行并读回 `1`。显式事务中定义或赋值失败会进入 `INERROR`，应先回滚或回到合适的 savepoint；本案例的自动提交失败才会回到 `IDLE`。

## 实测诊断 {#messages}

固定源码组都是 `ERROR` 变体。本案例 `heap.c` 主模板为 `column "%s" is of type %s but default expression is of type %s`，提示为 `You will need to rewrite or cast the expression.`；赋值路径使用 `column "%s" is of type %s but expression is of type %s` 和同一提示；CASE/UNION 等公共类型路径使用 `%s types %s and %s cannot be matched`。值输入失败是 `22P02`；选定公共类型后无法转换表达式则是 `42846`。

## 代表案例 {#case}

注册表先定义整数列与显式文本默认值，再创建匹配定义、插入默认行并核对。

<!-- BEGIN SQLSTATE SNIPPET: default_type_mismatch -->

```sql
CREATE TABLE syntax_schema.mismatch_table (value integer DEFAULT 'x'::text);
CREATE TABLE syntax_schema.mismatch_table (value integer DEFAULT 1);
INSERT INTO syntax_schema.mismatch_table DEFAULT VALUES;
SELECT value FROM syntax_schema.mismatch_table;
```

<!-- END SQLSTATE SNIPPET -->

选定的 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态/恢复、修复、清理和一次性实例停止断言。详见 [`案例 JSON`](../../data/cases/42804.json) 与 [`作者证据`](../../data/evidence/42804.json)；私有清单和注册表哈希也记录在其中。

## 版本 {#versions}

锁定目录从 7.4 存在边界起包含该条件并列出相关快照。选定自然案例已在 PostgreSQL 18.6 与 10.21 通过；这是有界观察，不能推断所有中间版本或所有源码分支。

## 相关 {#related}

- [`22P02`](../22p02/)
- [`42846`](../42846/)
- [`42803`](../42803/)

## 来源 {#sources}

- `src.errcodes.REL_18_6` — fixed definition at `724edf9bde9d356724ad384a2e196edc3c9f80f7`; SHA-256 `6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba` ([source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L1-L1)).
- `src.datatype-default.18.6` — `src/backend/catalog/heap.c` lines 3413–3420 at `724edf9bde9d356724ad384a2e196edc3c9f80f7`; blob SHA-256 `e720ee58279590793edd0985b3c910970ef56e7361b8ef92e245370587e02a0e` ([source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/catalog/heap.c#L3413-L3420)).
- `src.datatype-default.10.23` — `src/backend/catalog/heap.c` lines 2679–2686 at `02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4`; blob SHA-256 `285e1e2f8024acb58497735bcbd8e605e7cfdd224e442becdee6a5b0e7ac7cf6` ([source](https://github.com/postgres/postgres/blob/02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4/src/backend/catalog/heap.c#L2679-L2686)).
- `src.assignment-target.18.6` — `src/backend/parser/parse_target.c` lines 575–596 at `724edf9bde9d356724ad384a2e196edc3c9f80f7`; blob SHA-256 `4f0cda351acc3e2854bf55514e69cf8b7de6ad82e1535045e4523581ca0d943c` ([source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_target.c#L575-L596)).
- `src.assignment-target.10.23` — `src/backend/parser/parse_target.c` lines 570–596 at `02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4`; blob SHA-256 `808bfadd7717d711bc71eb85b7ecd343c742ccc8f6f3087164ed20da098b6d7d` ([source](https://github.com/postgres/postgres/blob/02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4/src/backend/parser/parse_target.c#L570-L596)).
- `src.common-type.18.6` — `src/backend/parser/parse_coerce.c` lines 1327–1425 at `724edf9bde9d356724ad384a2e196edc3c9f80f7`; blob SHA-256 `6ac3ddab06edff28e9a694c64fb7f559315cf488bfa4392bc14c24e559f6bd2e` ([source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_coerce.c#L1327-L1425)).
- `src.common-type.10.23` — `src/backend/parser/parse_coerce.c` lines 1217–1311 at `02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4`; blob SHA-256 `ed778c872fe4f2b8742828795566c3eaa24e7af3ce747fbf844c374d673593d3` ([source](https://github.com/postgres/postgres/blob/02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4/src/backend/parser/parse_coerce.c#L1217-L1311)).
- `src.common-coerce.18.6` — `src/backend/parser/parse_coerce.c` lines 1564–1593 at `724edf9bde9d356724ad384a2e196edc3c9f80f7`; blob SHA-256 `6ac3ddab06edff28e9a694c64fb7f559315cf488bfa4392bc14c24e559f6bd2e` ([source](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_coerce.c#L1564-L1593)).
- `src.common-coerce.10.23` — `src/backend/parser/parse_coerce.c` lines 1349–1378 at `02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4`; blob SHA-256 `ed778c872fe4f2b8742828795566c3eaa24e7af3ce747fbf844c374d673593d3` ([source](https://github.com/postgres/postgres/blob/02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4/src/backend/parser/parse_coerce.c#L1349-L1378)).
- `src.calls.REL_18_6` / `src.calls.REL_10_23` — fixed call scans, SHA-256 `9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf` / `00d16d3eb01b71ccf1b245c8f3102f9d0ec9f36fb02777b8dd1b99fcb263040c`.
- `manifest.42804` / `snippet-registry.42804` — hashes are recorded in `evidence/42804.json` and each runtime record.
