选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 42 语法错误或访问规则冲突

42P21 collation_mismatch

排序规则不匹配

ERROR 源码确认 参考 未实测

条件名
collation_mismatch
宏名称
ERRCODE_COLLATION_MISMATCH
启用版本
9.1
状态
活跃

版本覆盖

速览

42P21collation_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"
HINT You can choose the collation by applying the COLLATE clause to one or both expressions.
主消息 · errhint 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
HINT Use the COLLATE clause to set the collation of the non-recursive term.
主消息 · errhint 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
DETAIL "%s" versus "%s"
主消息 · errdetail "%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
DETAIL "%s" versus "%s"
主消息 · errdetail "%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"
DETAIL "%s" versus "%s"
主消息 · errdetail "%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
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.
主消息 · errdetail 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 源码,目录存在范围不等于每条消息或功能都从该版本开始。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

同类错误代码

Class 42 语法错误或访问规则冲突 下的其他成员。