文档 / SQL 状态码 / Class 42 语法错误或访问规则冲突
42P21 collation_mismatch
排序规则不匹配
ERROR 源码确认 参考 未实测
- 条件名
collation_mismatch- 宏名称
ERRCODE_COLLATION_MISMATCH- 启用版本
- 9.1
- 状态
- 活跃
版本覆盖
速览
42P21(collation_mismatch)需要得到兼容结果时,不同排序规则选择可能发生冲突。
含义
当固定源码路径需要一个兼容的排序规则,却得到不兼容的选择时,会报 42P21。隐式选择会先记录为 COLLATE_CONFLICT;调用方允许没有共同排序规则时可以返回 InvalidOid,只有确实需要排序规则的调用方才把隐式冲突变成 ERROR。显式 COLLATE 冲突则立即失败。同一条件还覆盖递归 CTE 输出、继承或子表列定义,以及外键键列兼容性。
诊断
遇到隐式排序规则消息时保留两个名称,并检查调用方是否要求共同排序规则;不同的隐式操作数不会全部立即抛出 42P21。遇到显式排序规则消息时,找出两个 COLLATE 子句并统一预期选择。递归查询要比较非递归项与整体列排序规则。继承或分区子表错误要比较父表与子表列定义。外键要比较引用表和被引用表的键列:固定源码允许两边都是确定性排序规则时使用不同排序规则;只要任一排序规则不确定,两边就必须相同。保留 DETAIL 中的名称,不要把它笼统当成转成 text 就能解决的问题。
处理
表达式冲突应在表达式边界明确指定一个有意选择的 COLLATE,或统一显式子句及递归非递归项的排序规则。重试 DDL 前先对齐继承列或子表列定义。外键应选择兼容的键列排序规则;只要一侧不确定,就满足两边相同的严格要求,然后重新核对相等比较和索引语义。如果该 ERROR 发生在显式事务中,修正语句前先 ROLLBACK,或回滚到错误前的保存点;自动提交模式下连接回到空闲后再重试。
消息
固定源码中的代表性消息包括:
- ERROR message:
collation mismatch between implicit collations "%s" and "%s"; HINT:You can choose the collation by applying the COLLATE clause to one or both expressions. - ERROR message:
collation mismatch between explicit collations "%s" and "%s" - ERROR message:
recursive query "%s" column %d has collation "%s" in non-recursive term but collation "%s" overall; HINT:Use the COLLATE clause to set the collation of the non-recursive term. - ERROR message:
column "%s" has a collation conflict; DETAIL:"%s" versus "%s" - ERROR message:
inherited column "%s" has a collation conflict; DETAIL:"%s" versus "%s" - ERROR message:
child table "%s" has different collation for column "%s"; DETAIL:"%s" versus "%s" - ERROR message:
foreign key constraint "%s" cannot be implemented; DETAIL:Key columns "%s" of the referencing table and "%s" of the referenced table have incompatible collations: "%s" and "%s". If either collation is nondeterministic, then both collations have to be the same.
占位符由实际对象、列或参数填充。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
collation mismatch between implicit collations "%s" and "%s"
You can choose the collation by applying the COLLATE clause to one or both expressions.
You can choose the collation by applying the COLLATE clause to one or both expressions.
来源:src/backend/parser/parse_collate.c(lines 226-232) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
collation mismatch between explicit collations "%s" and "%s"
来源:src/backend/parser/parse_collate.c(lines 853-858) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
recursive query "%s" column %d has collation "%s" in non-recursive term but collation "%s" overall
Use the COLLATE clause to set the collation of the non-recursive term.
Use the COLLATE clause to set the collation of the non-recursive term.
来源:src/backend/parser/parse_cte.c(lines 394-401) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
column "%s" has a collation conflict
"%s" versus "%s"
"%s" versus "%s"
来源:src/backend/commands/tablecmds.c(lines 3297-3303) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
inherited column "%s" has a collation conflict
"%s" versus "%s"
"%s" versus "%s"
来源:src/backend/commands/tablecmds.c(lines 3463-3469) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
child table "%s" has different collation for column "%s"
"%s" versus "%s"
"%s" versus "%s"
来源:src/backend/commands/tablecmds.c(lines 7292-7298) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
foreign key constraint "%s" cannot be implemented
Key columns "%s" of the referencing table and "%s" of the referenced table have incompatible collations: "%s" and "%s". If either collation is nondeterministic, then both collations have to be the same.
Key columns "%s" of the referencing table and "%s" of the referenced table have incompatible collations: "%s" and "%s". If either collation is nondeterministic, then both collations have to be the same.
来源:src/backend/commands/tablecmds.c(lines 10474-10483) @ REL_18_6
适用范围:Placeholders remain dynamic source fields; no concrete runtime value is claimed.
版本
锁定目录显示该条件最早见于 PostgreSQL 9.1.0;行为说明固定在 PostgreSQL 18.6 源码,目录存在范围不等于每条消息或功能都从该版本开始。
来源
- 上游源码 src/backend/commands/tablecmds.c 第 10474–10483 行
- 上游源码 src/backend/parser/parse_collate.c 第 853–858 行
- 上游源码 src/backend/commands/tablecmds.c 第 3297–3303 行
- 上游源码 src/backend/commands/tablecmds.c 第 3463–3469 行
- 上游源码 src/backend/parser/parse_collate.c 第 226–232 行
- 上游源码 src/backend/parser/parse_cte.c 第 394–401 行
- 上游源码 src/backend/commands/tablecmds.c 第 7292–7298 行
- 上游源码 src/backend/parser/parse_collate.c 第 208 行
- 上游源码 src/backend/utils/errcodes.txt 第 352 行
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42P21 is the collation_mismatch condition in Class 42.
-
Selected fixed 18.6 paths reject explicit collation conflicts immediately, emit implicit conflicts only when a caller requires one common collation, and check recursive output, inherited or child columns, and foreign-key collation compatibility.
-
select_common_collation(..., none_ok=true) may return InvalidOid while retaining an implicit COLLATE_CONFLICT; a later caller that requires a collation can then report a separate indeterminate-collation condition.
-
Locked catalogue snapshots show this condition by PostgreSQL 9.1.0; source behavior here is fixed at PostgreSQL 18.6.
同类 SQL 状态码
| 状态码 | 条件名 | 宏名称 | 严重等级 | 版本 |
|---|---|---|---|---|
| 42000 | syntax_error_or_access_rule_violation | ERRCODE_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION |
ERROR | 7.4 |
| 语法错误或访问规则违反的总类,见具体子码。 | 活跃 | |||
| 42501 | insufficient_privilege | ERRCODE_INSUFFICIENT_PRIVILEGE |
ERROR | 7.4 |
| 当前角色对目标对象的权限不足。 | 活跃 | |||
| 42601 | syntax_error | ERRCODE_SYNTAX_ERROR |
ERROR | 7.4 |
| SQL 语句存在语法错误,解析器拒绝执行。 | 活跃 | |||
| 42602 | invalid_name | ERRCODE_INVALID_NAME |
ERROR | 7.4 |
| 名称不合法,如 enum 标签超出长度限制。 | 活跃 | |||
| 42611 | invalid_column_definition | ERRCODE_INVALID_COLUMN_DEFINITION |
ERROR | 7.4 |
| 列定义无效,如继承的父表默认值冲突。 | 活跃 | |||
| 42622 | name_too_long | ERRCODE_NAME_TOO_LONG |
ERROR | 7.4 |
| 标识符或 name 值超出长度上限。 | 活跃 | |||
| 42701 | duplicate_column | ERRCODE_DUPLICATE_COLUMN |
ERROR | 7.4 |
| 同一定义中重复指定了同名列。 | 活跃 | |||
| 42702 | ambiguous_column | ERRCODE_AMBIGUOUS_COLUMN |
ERROR | 7.4 |
| 列引用有歧义,多个表都有该列名。 | 活跃 | |||
| 42703 | undefined_column | ERRCODE_UNDEFINED_COLUMN |
ERROR | 7.4 |
| 引用的列在目标关系中不存在。 | 活跃 | |||
| 42704 | undefined_object | ERRCODE_UNDEFINED_OBJECT |
ERROR | 7.4 |
| 引用的数据库对象不存在,如触发器或类型。 | 活跃 | |||
| 42710 | duplicate_object | ERRCODE_DUPLICATE_OBJECT |
ERROR | 7.4 |
| 要定义的对象名已被占用,如重复 CREATE TYPE。 | 活跃 | |||
| 42712 | duplicate_alias | ERRCODE_DUPLICATE_ALIAS |
ERROR | 7.4 |
| 同一查询命名空间中重复使用了表别名。 | 活跃 | |||
| 42723 | duplicate_function | ERRCODE_DUPLICATE_FUNCTION |
ERROR | 7.4 |
| 同一模式下已存在同名同参数类型的函数。 | 活跃 | |||
| 42725 | ambiguous_function | ERRCODE_AMBIGUOUS_FUNCTION |
ERROR | 7.4 |
| 函数调用有多个候选,无法选出最佳匹配。 | 活跃 | |||
| 42803 | grouping_error | ERRCODE_GROUPING_ERROR |
ERROR | 7.4 |
| 分组查询输出了既未分组也未聚合的列。 | 活跃 | |||
| 42804 | datatype_mismatch | ERRCODE_DATATYPE_MISMATCH |
ERROR | 7.4 |
| 表达式类型与目标上下文要求的类型不符。 | 活跃 | |||
| 42809 | wrong_object_type | ERRCODE_WRONG_OBJECT_TYPE |
ERROR | 7.4 |
| 命令本身有效,但不能作用于该类对象。 | 活跃 | |||
| 42830 | invalid_foreign_key | ERRCODE_INVALID_FOREIGN_KEY |
ERROR | 7.4 |
| 外键在被引用列上找不到合格的唯一键。 | 活跃 | |||
| 42846 | cannot_coerce | ERRCODE_CANNOT_COERCE |
ERROR | 7.4 |
| 源类型到目标类型之间没有可用的 cast。 | 活跃 | |||
| 42883 | undefined_function | ERRCODE_UNDEFINED_FUNCTION |
ERROR | 7.4 |
| 按名称和参数类型找不到匹配的函数或操作符。 | 活跃 | |||
| 428C9 | generated_always | ERRCODE_GENERATED_ALWAYS |
ERROR | 10 |
| 向身份列或生成列赋值,这类列只接受 DEFAULT。 | 活跃 | |||
| 42939 | reserved_name | ERRCODE_RESERVED_NAME |
ERROR | 7.4 |
| 对象名使用了保留名称,如模式或表空间。 | 活跃 | |||
| 42P01 | undefined_table | ERRCODE_UNDEFINED_TABLE |
ERROR | 7.4 |
| 引用的表、视图或其他关系不存在。 | 活跃 | |||
| 42P02 | undefined_parameter | ERRCODE_UNDEFINED_PARAMETER |
ERROR | 7.4 |
| 查询引用了解析器未知的位置参数,如 $1。 | 活跃 | |||
| 42P03 | duplicate_cursor | ERRCODE_DUPLICATE_CURSOR |
ERROR | 7.4 |
| 游标或 portal 名称与已有的冲突。 | 活跃 | |||
| 42P04 | duplicate_database | ERRCODE_DUPLICATE_DATABASE |
ERROR | 7.4 |
| 集群中已存在同名数据库。 | 活跃 | |||
| 42P05 | duplicate_prepared_statement | ERRCODE_DUPLICATE_PSTATEMENT |
ERROR | 7.4 |
| 当前会话已有同名的服务端预备语句。 | 活跃 | |||
| 42P06 | duplicate_schema | ERRCODE_DUPLICATE_SCHEMA |
ERROR | 7.4 |
| 要创建的模式名在当前数据库中已存在。 | 活跃 | |||
| 42P07 | duplicate_table | ERRCODE_DUPLICATE_TABLE |
ERROR | 7.4 |
| 关系名已存在,或继承父项重复。 | 活跃 | |||
| 42P08 | ambiguous_parameter | ERRCODE_AMBIGUOUS_PARAMETER |
ERROR | 7.4 |
| 参数存在但无法推导出一致而具体的类型。 | 活跃 | |||
| 42P09 | ambiguous_alias | ERRCODE_AMBIGUOUS_ALIAS |
ERROR | 7.4 |
| 一个表引用对应多个关系命名空间项。 | 活跃 | |||
| 42P10 | invalid_column_reference | ERRCODE_INVALID_COLUMN_REFERENCE |
ERROR | 7.4 |
| 列引用不符合该操作的契约,如 COPY 生成列。 | 活跃 | |||
| 42P11 | invalid_cursor_definition | ERRCODE_INVALID_CURSOR_DEFINITION |
ERROR | 7.4 |
| 游标声明违反定义阶段规则,如 SCROLL 选项冲突。 | 活跃 | |||
| 42P12 | invalid_database_definition | ERRCODE_INVALID_DATABASE_DEFINITION |
ERROR | 7.4 |
| 数据库定义无效,核心源码中未见触发路径。 | 活跃 | |||
| 42P13 | invalid_function_definition | ERRCODE_INVALID_FUNCTION_DEFINITION |
ERROR | 7.4 |
| 函数、聚合或触发器声明在校验阶段失败。 | 活跃 | |||
| 42P14 | invalid_prepared_statement_definition | ERRCODE_INVALID_PSTATEMENT_DEFINITION |
ERROR | 7.4 |
| PREPARE 定义预备语句时失败。 | 活跃 | |||
| 42P15 | invalid_schema_definition | ERRCODE_INVALID_SCHEMA_DEFINITION |
ERROR | 7.4 |
| CREATE SCHEMA 的模式声明不一致被拒绝。 | 活跃 | |||
| 42P16 | invalid_table_definition | ERRCODE_INVALID_TABLE_DEFINITION |
ERROR | 7.4 |
| 表定义无效,如分区键或主键规则冲突。 | 活跃 | |||
| 42P17 | invalid_object_definition | ERRCODE_INVALID_OBJECT_DEFINITION |
ERROR | 7.4 |
| 对象定义无效,如继承、生成列或规则冲突。 | 活跃 | |||
| 42P18 | indeterminate_datatype | ERRCODE_INDETERMINATE_DATATYPE |
ERROR | 7.4 |
| 无法推断表达式或参数的数据类型。 | 活跃 | |||
| 42P19 | invalid_recursion | ERRCODE_INVALID_RECURSION |
ERROR | 8.4 |
| 递归查询结构不合法,执行前被检查拒绝。 | 活跃 | |||
| 42P20 | windowing_error | ERRCODE_WINDOWING_ERROR |
ERROR | 8.4 |
| 窗口函数调用或框架边界定义不合法。 | 活跃 | |||
| 42P21 | collation_mismatch | ERRCODE_COLLATION_MISMATCH |
ERROR | 9.1 |
| 参与运算的排序规则不兼容,产生冲突。 | 活跃 | |||
| 42P22 | indeterminate_collation | ERRCODE_INDETERMINATE_COLLATION |
ERROR | 9.1 |
| 操作需要唯一排序规则,但无法确定。 | 活跃 | |||