2

PostgreSQL 和openGauss错误代码整理

李宏达 2022-10-24
874

一、 PostgreSQL 错误信息的设计

  • All messages emitted by the PostgreSQL server are assigned five-character error codes that follow the SQL standard’s conventions for “SQLSTATE” codes. Applications that need to know which error condition has occurred should usually test the error code, rather than looking at the textual error message. The error codes are less likely to change across PostgreSQL releases, and also are not subject to change due to localization of error messages. Note that some, but not all, of the error codes produced by PostgreSQL are defined by the SQL standard; some additional error codes for conditions not defined by the standard have been invented or borrowed from other databases.
  • PostgreSQL 服务器发出的所有消息都分配有五个字符的错误代码,遵循 SQL 标准的“SQLSTATE”代码约定。需要知道发生了哪种错误情况的应用程序通常应该测试错误代码,而不是查看文本错误消息。错误代码不太可能在 PostgreSQL 版本之间更改,并且也不会由于错误消息的本地化而发生更改。请注意,PostgreSQL 产生的一些但不是全部的错误代码是由 SQL 标准定义的;标准未定义的一些附加错误代码已被发明或从其他数据库借用。

  • According to the standard, the first two characters of an error code denote a class of errors, while the last three characters indicate a specific condition within that class. Thus, an application that does not recognize the specific error code might still be able to infer what to do from the error class.
  • 根据标准,错误代码的前两个字符表示一类错误,而后三个字符表示该类中的特定条件。因此,无法识别特定错误代码的应用程序可能仍然能够从错误类中推断出要做什么

    Table A. lists all the error codes defined in PostgreSQL 15.0. (Some are not actually used at present, but are defined by the SQL standard.) The error classes are also shown. For each error class there is a “standard” error code having the last three characters 000. This code is used only for error conditions that fall within the class but do not have any more-specific code assigned.

    表 A.1 列出了 PostgreSQL 15.0 中定义的所有错误代码。 (有些目前没有实际使用,但由SQL标准定义。)还显示了错误类。对于每个错误类别,都有一个最后三个字符为 000 的“标准”错误代码。此代码仅用于属于该类别但没有分配任何更具体代码的错误条件。

  • The symbol shown in the column “Condition Name” is the condition name to use in PL/pgSQL. Condition names can be written in either upper or lower case. (Note that PL/pgSQL does not recognize warning, as opposed to error, condition names; those are classes 00, 01, and 02.)
  • “条件名称”列中显示的符号是在 PL/pgSQL 中使用的条件名称。条件名称可以大写或小写。 (请注意,PL/pgSQL 不识别警告,而不是错误,条件名称;它们是类 00、01 和 02。)

  • For some types of errors, the server reports the name of a database object (a table, table column, data type, or constraint) associated with the error; for example, the name of the unique constraint that caused a unique_violation error. Such names are supplied in separate fields of the error report message so that applications need not try to extract them from the possibly-localized human-readable text of the message. As of PostgreSQL 9.3, complete coverage for this feature exists only for errors in SQLSTATE class 23 (integrity constraint violation), but this is likely to be expanded in future.
  • 对于某些类型的错误,服务器会报告与错误相关的数据库对象(表、表列、数据类型或约束)的名称;例如,导致 unique_violation 错误的唯一约束的名称。此类名称在错误报告消息的单独字段中提供,因此应用程序无需尝试从消息的可能本地化的人类可读文本中提取它们。从 PostgreSQL 9.3 开始,此功能的完全覆盖仅存在于 SQLSTATE 类 23 中的错误(违反完整性约束),但将来可能会扩展。

    二、 openGauss错误信息设计

    根据X/Open和SQL Access Group SQL CAE规范(1992)所进行的定义,SQLERROR返回SQLSTATE值。SQLSTATE值是包含五个字符的字符串,由2个字符的SQL错误类和3个字符的子类构成。五个字符包含数值或者大写字母, 代表各种错误或者警告条件的代码。成功的状态是由00000标识的。SQLSTATE代码大多数情况下都是定义在SQL标准里的。

    openGauss也遵循SQL标准返回错误码的SQLSTATE值,详细说明请参见

    三、 PostgreSQL and openGauss error code

    错误码SQLSTATE值 condition name 错误码含义 PG 独有 OG 独有 Class 02 — No Data (this is also a warning class per the SQL standard)/类 02 - 没有数据(按照SQL标准的要求,这也是警告类) Class 28 — Invalid Authorization Specification/类 28 - 非法授权声明 invalid_authorization_specification