42P21 — 排序规则不匹配
参与运算的排序规则不兼容,产生冲突。
英文手册覆盖始于 PostgreSQL 8.1. 来源历史与运行验证各自保留独立版本边界。
当前阅读 PG 18·选择有来源记录的版本
- aliases
- 未知
- class name
- Syntax Error or Access Rule Violation
- condition name
- collation_mismatch
- lang
- en
- sqlstate
- 42P21
版本定义 PG 18
42P21 — 排序规则不匹配
速览 {#at-a-glance}
42P21(collation_mismatch)需要得到兼容结果时,不同排序规则选择可能发生冲突。
含义 {#meaning}
当固定源码路径需要一个兼容的排序规则,却得到不兼容的选择时,会报 42P21。隐式选择会先记录为 COLLATE_CONFLICT;调用方允许没有共同排序规则时可以返回 InvalidOid,只有确实需要排序规则的调用方才把隐式冲突变成 ERROR。显式 COLLATE 冲突则立即失败。同一条件还覆盖递归 CTE 输出、继承或子表列定义,以及外键键列兼容性。
诊断 {#diagnosis}
遇到隐式排序规则消息时保留两个名称,并检查调用方是否要求共同排序规则;不同的隐式操作数不会全部立即抛出 42P21。遇到显式排序规则消息时,找出两个 COLLATE 子句并统一预期选择。递归查询要比较非递归项与整体列排序规则。继承或分区子表错误要比较父表与子表列定义。外键要比较引用表和被引用表的键列:固定源码允许两边都是确定性排序规则时使用不同排序规则;只要任一排序规则不确定,两边就必须相同。保留 DETAIL 中的名称,不要把它笼统当成转成 text 就能解决的问题。
处理 {#response}
表达式冲突应在表达式边界明确指定一个有意选择的 COLLATE,或统一显式子句及递归非递归项的排序规则。重试 DDL 前先对齐继承列或子表列定义。外键应选择兼容的键列排序规则;只要一侧不确定,就满足两边相同的严格要求,然后重新核对相等比较和索引语义。如果该 ERROR 发生在显式事务中,修正语句前先 ROLLBACK,或回滚到错误前的保存点;自动提交模式下连接回到空闲后再重试。
消息 {#messages}
固定源码中的代表性消息包括:
- 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.
占位符由实际对象、列或参数填充。
版本 {#versions}
锁定目录显示该条件最早见于 PostgreSQL 9.1.0;行为说明固定在 PostgreSQL 18.6 源码,目录存在范围不等于每条消息或功能都从该版本开始。
相关 {#related}
来源 {#sources}
源码消息、行号和证据边界见 作者证据。
src/backend/parser/parse_collate.c:208-234(fixed source)src/backend/parser/parse_collate.c:804-835(fixed source)src/backend/parser/parse_cte.c:394-401(fixed source)src/backend/commands/tablecmds.c:3297-3303(fixed source)src/backend/commands/tablecmds.c:3463-3469(fixed source)src/backend/commands/tablecmds.c:7292-7298(fixed source)src/backend/commands/tablecmds.c:10474-10483(fixed source)
比较版本
SQLSTATE 证据与已记录的运行版本
claims · identity.class-and-condition
{
"claim_id": "identity.class-and-condition",
"limits": "Identity does not enumerate all source branches or client failures.",
"method": "Read fixed REL_18_6 errcodes.txt and locked facts.",
"position": 0,
"runtime": [],
"sources": [
"src.errcodes.42P21.18.6"
],
"statement": "42P21 is the collation_mismatch condition in Class 42."
}claims · mechanism.selected-source-path
{
"claim_id": "mechanism.selected-source-path",
"limits": "Representative confirmed core paths only; not exhaustive historical or extension coverage.",
"method": "Read complete parse_collate.c derivation context and the selected resolved report groups in parse_cte.c and tablecmds.c.",
"position": 1,
"runtime": [],
"sources": [
"src.call.42P21.69245d264e1d0e386e33036f",
"src.call.42P21.0ba27778e76c3d44b1cc79f6",
"src.call.42P21.93a028fdb350e0d054f123d8",
"src.call.42P21.16107e45585553ed56b18241",
"src.call.42P21.5fcad21fbefb1ae15acf4c73",
"src.call.42P21.ef5edd918af762963c8e072c",
"src.call.42P21.04a0360be247ab8ca050e826",
"src.context.42P21.parse_collate.common"
],
"statement": "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."
}claims · boundary.implicit-none-ok
{
"claim_id": "boundary.implicit-none-ok",
"limits": "This is source control-flow evidence, not a natural runtime observation.",
"method": "Read select_common_collation and the COLLATE_CONFLICT/COLLATE_EXPLICIT branches in fixed parse_collate.c.",
"position": 2,
"runtime": [],
"sources": [
"src.context.42P21.parse_collate.common"
],
"statement": "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."
}claims · versions.catalogue-boundary
{
"claim_id": "versions.catalogue-boundary",
"limits": "First observed release is a lower bound, not precise behavioral introduction.",
"method": "Read locked catalogue facts and definition snapshot.",
"position": 3,
"runtime": [],
"sources": [
"src.errcodes.42P21.18.6"
],
"statement": "Locked catalogue snapshots show this condition by PostgreSQL 9.1.0; source behavior here is fixed at PostgreSQL 18.6."
}messages · message.variant-1
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-1",
"path": "",
"position": 0,
"raw": {
"detail_templates": [],
"hint_templates": [
"You can choose the collation by applying the COLLATE clause to one or both expressions."
],
"id": "message.variant-1",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "collation mismatch between implicit collations \"%s\" and \"%s\"",
"roles": [
{
"role": "errmsg",
"template": "collation mismatch between implicit collations \"%s\" and \"%s\""
},
{
"role": "errhint",
"template": "You can choose the collation by applying the COLLATE clause to one or both expressions."
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.69245d264e1d0e386e33036f"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.69245d264e1d0e386e33036f"
],
"templates": [
{
"kind": "primary",
"literal": "collation mismatch between implicit collations \" \" and \" \"",
"position": 0,
"role": "",
"template": "collation mismatch between implicit collations \"%s\" and \"%s\""
},
{
"kind": "hint",
"literal": "You can choose the collation by applying the COLLATE clause to one or both expressions.",
"position": 1,
"role": "",
"template": "You can choose the collation by applying the COLLATE clause to one or both expressions."
},
{
"kind": "primary",
"literal": "You can choose the collation by applying the COLLATE clause to one or both expressions.",
"position": 2,
"role": "errhint",
"template": "You can choose the collation by applying the COLLATE clause to one or both expressions."
}
]
}messages · message.variant-2
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-2",
"path": "",
"position": 1,
"raw": {
"detail_templates": [],
"hint_templates": [],
"id": "message.variant-2",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "collation mismatch between explicit collations \"%s\" and \"%s\"",
"roles": [
{
"role": "errmsg",
"template": "collation mismatch between explicit collations \"%s\" and \"%s\""
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.0ba27778e76c3d44b1cc79f6"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.0ba27778e76c3d44b1cc79f6"
],
"templates": [
{
"kind": "primary",
"literal": "collation mismatch between explicit collations \" \" and \" \"",
"position": 0,
"role": "",
"template": "collation mismatch between explicit collations \"%s\" and \"%s\""
}
]
}messages · message.variant-3
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-3",
"path": "",
"position": 2,
"raw": {
"detail_templates": [],
"hint_templates": [
"Use the COLLATE clause to set the collation of the non-recursive term."
],
"id": "message.variant-3",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "recursive query \"%s\" column %d has collation \"%s\" in non-recursive term but collation \"%s\" overall",
"roles": [
{
"role": "errmsg",
"template": "recursive query \"%s\" column %d has collation \"%s\" in non-recursive term but collation \"%s\" overall"
},
{
"role": "errhint",
"template": "Use the COLLATE clause to set the collation of the non-recursive term."
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.93a028fdb350e0d054f123d8"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.93a028fdb350e0d054f123d8"
],
"templates": [
{
"kind": "primary",
"literal": "recursive query \" \" column has collation \" \" in non-recursive term but collation \" \" overall",
"position": 0,
"role": "",
"template": "recursive query \"%s\" column %d has collation \"%s\" in non-recursive term but collation \"%s\" overall"
},
{
"kind": "hint",
"literal": "Use the COLLATE clause to set the collation of the non-recursive term.",
"position": 1,
"role": "",
"template": "Use the COLLATE clause to set the collation of the non-recursive term."
},
{
"kind": "primary",
"literal": "Use the COLLATE clause to set the collation of the non-recursive term.",
"position": 2,
"role": "errhint",
"template": "Use the COLLATE clause to set the collation of the non-recursive term."
}
]
}messages · message.variant-4
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-4",
"path": "",
"position": 3,
"raw": {
"detail_templates": [
"\"%s\" versus \"%s\""
],
"hint_templates": [],
"id": "message.variant-4",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "column \"%s\" has a collation conflict",
"roles": [
{
"role": "errmsg",
"template": "column \"%s\" has a collation conflict"
},
{
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.16107e45585553ed56b18241"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.16107e45585553ed56b18241"
],
"templates": [
{
"kind": "primary",
"literal": "column \" \" has a collation conflict",
"position": 0,
"role": "",
"template": "column \"%s\" has a collation conflict"
},
{
"kind": "detail",
"literal": "\" \" versus \" \"",
"position": 1,
"role": "",
"template": "\"%s\" versus \"%s\""
},
{
"kind": "primary",
"literal": "\" \" versus \" \"",
"position": 2,
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
]
}messages · message.variant-5
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-5",
"path": "",
"position": 4,
"raw": {
"detail_templates": [
"\"%s\" versus \"%s\""
],
"hint_templates": [],
"id": "message.variant-5",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "inherited column \"%s\" has a collation conflict",
"roles": [
{
"role": "errmsg",
"template": "inherited column \"%s\" has a collation conflict"
},
{
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.5fcad21fbefb1ae15acf4c73"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.5fcad21fbefb1ae15acf4c73"
],
"templates": [
{
"kind": "primary",
"literal": "inherited column \" \" has a collation conflict",
"position": 0,
"role": "",
"template": "inherited column \"%s\" has a collation conflict"
},
{
"kind": "detail",
"literal": "\" \" versus \" \"",
"position": 1,
"role": "",
"template": "\"%s\" versus \"%s\""
},
{
"kind": "primary",
"literal": "\" \" versus \" \"",
"position": 2,
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
]
}messages · message.variant-6
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-6",
"path": "",
"position": 5,
"raw": {
"detail_templates": [
"\"%s\" versus \"%s\""
],
"hint_templates": [],
"id": "message.variant-6",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "child table \"%s\" has different collation for column \"%s\"",
"roles": [
{
"role": "errmsg",
"template": "child table \"%s\" has different collation for column \"%s\""
},
{
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.ef5edd918af762963c8e072c"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.ef5edd918af762963c8e072c"
],
"templates": [
{
"kind": "primary",
"literal": "child table \" \" has different collation for column \" \"",
"position": 0,
"role": "",
"template": "child table \"%s\" has different collation for column \"%s\""
},
{
"kind": "detail",
"literal": "\" \" versus \" \"",
"position": 1,
"role": "",
"template": "\"%s\" versus \"%s\""
},
{
"kind": "primary",
"literal": "\" \" versus \" \"",
"position": 2,
"role": "errdetail",
"template": "\"%s\" versus \"%s\""
}
]
}messages · message.variant-7
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-7",
"path": "",
"position": 6,
"raw": {
"detail_templates": [
"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."
],
"hint_templates": [],
"id": "message.variant-7",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "foreign key constraint \"%s\" cannot be implemented",
"roles": [
{
"role": "errmsg",
"template": "foreign key constraint \"%s\" cannot be implemented"
},
{
"role": "errdetail",
"template": "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."
}
],
"severity_source": "ERROR",
"sources": [
"src.call.42P21.04a0360be247ab8ca050e826"
],
"sqlstate": "42P21"
},
"severity": "ERROR",
"sources": [
"src.call.42P21.04a0360be247ab8ca050e826"
],
"templates": [
{
"kind": "primary",
"literal": "foreign key constraint \" \" cannot be implemented",
"position": 0,
"role": "",
"template": "foreign key constraint \"%s\" cannot be implemented"
},
{
"kind": "detail",
"literal": "Key columns \" \" of the referencing table and \" \" of the referenced table have incompatible collations: \" \" and \" \". If either collation is nondeterministic, then both collations have to be the same.",
"position": 1,
"role": "",
"template": "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."
},
{
"kind": "primary",
"literal": "Key columns \" \" of the referencing table and \" \" of the referenced table have incompatible collations: \" \" and \" \". If either collation is nondeterministic, then both collations have to be the same.",
"position": 2,
"role": "errdetail",
"template": "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."
}
]
}sources · src.errcodes.42P21.18.6
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "line 352",
"path": "src/backend/utils/errcodes.txt",
"position": 0,
"sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
"source_id": "src.errcodes.42P21.18.6",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L352"
}sources · src.call.42P21.69245d264e1d0e386e33036f
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 226-232",
"path": "src/backend/parser/parse_collate.c",
"position": 1,
"sha256": "e92a01d1a7279f108ddfac1f0324f6d142b1496360ddac3dfb778564c4226940",
"source_id": "src.call.42P21.69245d264e1d0e386e33036f",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_collate.c#L226-L232"
}sources · src.call.42P21.0ba27778e76c3d44b1cc79f6
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 853-858",
"path": "src/backend/parser/parse_collate.c",
"position": 2,
"sha256": "e92a01d1a7279f108ddfac1f0324f6d142b1496360ddac3dfb778564c4226940",
"source_id": "src.call.42P21.0ba27778e76c3d44b1cc79f6",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_collate.c#L853-L858"
}sources · src.call.42P21.93a028fdb350e0d054f123d8
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 394-401",
"path": "src/backend/parser/parse_cte.c",
"position": 3,
"sha256": "908eef14ac26654fe0b9c176ef50e56ba710b7fcd8e1239b944d7c2152fc2d6b",
"source_id": "src.call.42P21.93a028fdb350e0d054f123d8",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_cte.c#L394-L401"
}sources · src.call.42P21.16107e45585553ed56b18241
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 3297-3303",
"path": "src/backend/commands/tablecmds.c",
"position": 4,
"sha256": "422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9",
"source_id": "src.call.42P21.16107e45585553ed56b18241",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L3297-L3303"
}sources · src.call.42P21.5fcad21fbefb1ae15acf4c73
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 3463-3469",
"path": "src/backend/commands/tablecmds.c",
"position": 5,
"sha256": "422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9",
"source_id": "src.call.42P21.5fcad21fbefb1ae15acf4c73",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L3463-L3469"
}sources · src.call.42P21.ef5edd918af762963c8e072c
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 7292-7298",
"path": "src/backend/commands/tablecmds.c",
"position": 6,
"sha256": "422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9",
"source_id": "src.call.42P21.ef5edd918af762963c8e072c",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L7292-L7298"
}sources · src.call.42P21.04a0360be247ab8ca050e826
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 10474-10483",
"path": "src/backend/commands/tablecmds.c",
"position": 7,
"sha256": "422dc8e833df4940755973c757e338295822ba3e4c7266c40669f49f96eb15a9",
"source_id": "src.call.42P21.04a0360be247ab8ca050e826",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L10474-L10483"
}sources · src.context.42P21.parse_collate.common
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 208-234 and 804-835",
"path": "src/backend/parser/parse_collate.c",
"position": 8,
"sha256": "e92a01d1a7279f108ddfac1f0324f6d142b1496360ddac3dfb778564c4226940",
"source_id": "src.context.42P21.parse_collate.common",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_collate.c#L208"
}sources · src.calls.REL_18_6.42P21
{
"commit": "",
"docs_url": "",
"kind": "local_artifact",
"location": "",
"path": "raw/calls/REL_18_6.jsonl",
"position": 9,
"sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
"source_id": "src.calls.REL_18_6.42P21",
"tag": "",
"url": ""
}来源引用
定义来源
center · PostgreSQL 18 · english-manuals:b5d4c7d86be08cb3fc3124799c6c721a76c6e26b8ee8a84e6887686f790435f2
正文语言: zh-Hans · english-manuals:b5d4c7d86be08cb3fc3124799c6c721a76c6e26b8ee8a84e6887686f790435f2