博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL jdbc 错误代码映射(SQLSTATE)
阅读量:5943 次
发布时间:2019-06-19

本文共 7194 字,大约阅读时间需要 23 分钟。

标签

PostgreSQL , SQLSTATE , 错误代码 , org.postgresql.util.PSQLState


背景

Does such a class enumerating the PostgreSQL error codes already exist?

Yes, it does: 

However, there are 238 error codes listed on the page you referenced, and org.postgresql.util.PSQLState only enumerates 41 values. Of those 41 values, only 33 are from that list of PostgreSQL error codes (roughly 14% coverage).

If you require any of the other constants, you will have to enumerate those yourself.

PostgreSQL 包含250个左右的错误代码,如下

grep -c ERRCODE errcodes.txt  250

src/backend/utils/errcodes.txt

00000    S    ERRCODE_SUCCESSFUL_COMPLETION                                  successful_completion  01000    W    ERRCODE_WARNING                                                warning  0100C    W    ERRCODE_WARNING_DYNAMIC_RESULT_SETS_RETURNED                   dynamic_result_sets_returned  01008    W    ERRCODE_WARNING_IMPLICIT_ZERO_BIT_PADDING                      implicit_zero_bit_padding  01003    W    ERRCODE_WARNING_NULL_VALUE_ELIMINATED_IN_SET_FUNCTION          null_value_eliminated_in_set_function  01007    W    ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED                          privilege_not_granted  ...........  XX000    E    ERRCODE_INTERNAL_ERROR                                         internal_error  XX001    E    ERRCODE_DATA_CORRUPTED                                         data_corrupted  XX002    E    ERRCODE_INDEX_CORRUPTED                                        index_corrupted

PostgreSQL jdbc 驱动封装的错误代码如下:

PSQLState

// begin constant state codes    public final static PSQLState UNKNOWN_STATE = new PSQLState("");      public final static PSQLState TOO_MANY_RESULTS = new PSQLState("0100E");      public final static PSQLState NO_DATA = new PSQLState("02000");      public final static PSQLState INVALID_PARAMETER_TYPE = new PSQLState("07006");      /**     * We could establish a connection with the server for unknown reasons. Could be a network     * problem.     */    public final static PSQLState CONNECTION_UNABLE_TO_CONNECT = new PSQLState("08001");      public final static PSQLState CONNECTION_DOES_NOT_EXIST = new PSQLState("08003");      /**     * The server rejected our connection attempt. Usually an authentication failure, but could be a     * configuration error like asking for a SSL connection with a server that wasn't built with SSL     * support.     */    public final static PSQLState CONNECTION_REJECTED = new PSQLState("08004");      /**     * After a connection has been established, it went bad.     */    public final static PSQLState CONNECTION_FAILURE = new PSQLState("08006");    public final static PSQLState CONNECTION_FAILURE_DURING_TRANSACTION = new PSQLState("08007");      /**     * The server sent us a response the driver was not prepared for and is either bizarre datastream     * corruption, a driver bug, or a protocol violation on the server's part.     */    public final static PSQLState PROTOCOL_VIOLATION = new PSQLState("08P01");      public final static PSQLState COMMUNICATION_ERROR = new PSQLState("08S01");      public final static PSQLState NOT_IMPLEMENTED = new PSQLState("0A000");      public final static PSQLState DATA_ERROR = new PSQLState("22000");    public final static PSQLState NUMERIC_VALUE_OUT_OF_RANGE = new PSQLState("22003");    public final static PSQLState BAD_DATETIME_FORMAT = new PSQLState("22007");    public final static PSQLState DATETIME_OVERFLOW = new PSQLState("22008");    public final static PSQLState DIVISION_BY_ZERO = new PSQLState("22012");    public final static PSQLState MOST_SPECIFIC_TYPE_DOES_NOT_MATCH = new PSQLState("2200G");    public final static PSQLState INVALID_PARAMETER_VALUE = new PSQLState("22023");      public final static PSQLState INVALID_CURSOR_STATE = new PSQLState("24000");      public final static PSQLState TRANSACTION_STATE_INVALID = new PSQLState("25000");    public final static PSQLState ACTIVE_SQL_TRANSACTION = new PSQLState("25001");    public final static PSQLState NO_ACTIVE_SQL_TRANSACTION = new PSQLState("25P01");    public final static PSQLState IN_FAILED_SQL_TRANSACTION = new PSQLState("25P02");      public final static PSQLState INVALID_SQL_STATEMENT_NAME = new PSQLState("26000");    public final static PSQLState INVALID_AUTHORIZATION_SPECIFICATION = new PSQLState("28000");      public final static PSQLState STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL = new PSQLState("2F003");      public final static PSQLState INVALID_SAVEPOINT_SPECIFICATION = new PSQLState("3B000");      public final static PSQLState SYNTAX_ERROR = new PSQLState("42601");    public final static PSQLState UNDEFINED_COLUMN = new PSQLState("42703");    public final static PSQLState UNDEFINED_OBJECT = new PSQLState("42704");    public final static PSQLState WRONG_OBJECT_TYPE = new PSQLState("42809");    public final static PSQLState NUMERIC_CONSTANT_OUT_OF_RANGE = new PSQLState("42820");    public final static PSQLState DATA_TYPE_MISMATCH = new PSQLState("42821");    public final static PSQLState UNDEFINED_FUNCTION = new PSQLState("42883");    public final static PSQLState INVALID_NAME = new PSQLState("42602");      public final static PSQLState OUT_OF_MEMORY = new PSQLState("53200");    public final static PSQLState OBJECT_NOT_IN_STATE = new PSQLState("55000");      public final static PSQLState SYSTEM_ERROR = new PSQLState("60000");    public final static PSQLState IO_ERROR = new PSQLState("58030");      public final static PSQLState UNEXPECTED_ERROR = new PSQLState("99999");

由于未完全对应PG的SQLSTATE,所以,建议可以使用getSQLState获取一下错误代码并输出,或者扩展PSQLState.java,映射完整的PostgreSQL SQLSTATE。

输出elog日志打印的相关源码位置

拿到了SQLSTATE代码,还不够,因为可能有多处代码报同一个错误,如果要定位更加详细的原因,可以配置数据库的log_error_verbosity参数,在报错误代码的同时,可输出源代码的位置。

例如1,在psql中,将错误代码以及源码输出到客户端:

postgres=# \set VERBOSITY verbose  postgres=# select t.oid,(select string_agg(relname, s',') from pg_class) from pg_class t;  ERROR:  42704: type "s" does not exist  LINE 1: select t.oid,(select string_agg(relname, s',') from pg_class...                                                   ^  LOCATION:  typenameType, parse_type.c:546

例如2,在postgresql的错误日志中拿到错误代码以及源码.

postgres=# set log_error_verbosity ='verbose';  SET  postgres=# select t.oid,(select string_agg(relname, s',') from pg_class) from pg_class t;  ERROR:  type "s" does not exist  LINE 1: select t.oid,(select string_agg(relname, s',') from pg_class...                                                   ^      错误日志:    2018-07-11 13:22:40.505 CST,"enterprisedb","postgres",28166,"[local]",5b45743c.6e06,5,"SELECT",2018-07-11 11:06:36 CST,4/80,0,ERROR,42704,"type ""s"" does not exist",,,,,,"select t.oid,(select string_agg(relname, s',') from pg_class) from pg_class t;",42,"typenameType, parse_type.c:546","psql.bin"

参考

log_error_verbosity (enum)

Controls the amount of detail written in the server log for each message that is logged. Valid values are TERSE, DEFAULT, and VERBOSE, each adding more fields to displayed messages. TERSE excludes the logging of DETAIL, HINT, QUERY, and CONTEXT error information. VERBOSE output includes the SQLSTATE error code (see also Appendix A) and the source code file name, function name, and line number that generated the error. Only superusers can change this setting.

转载地址:http://tczxx.baihongyu.com/

你可能感兴趣的文章
mysql+keepalived搭建高可用环境
查看>>
java实现插入排序
查看>>
Linux下php连接sql server 2008
查看>>
python字符串格式化
查看>>
golang版try..catch..
查看>>
关于html和CSS的几个基本知识点
查看>>
maven创建webapp项目
查看>>
删除git上已经提交的文件
查看>>
RESTFUL架构
查看>>
【Alpha】Daily Scrum Meeting第七次
查看>>
CF230b: T-primes(简单数论)
查看>>
【转载】设置Windows中gvim的默认配色方案和字体
查看>>
根据年月日,判断20XX年XX月XX日是 星期几
查看>>
迁移文件
查看>>
Nginx的启动、停止与重启
查看>>
**php队列的实现思路和详细过程
查看>>
POJ 2778 DNA Sequence —— (AC自动机+矩阵快速幂)
查看>>
ACM之路(16)—— 数位DP
查看>>
织梦首页常用调用标签
查看>>
关于new String(new byte[]{0})
查看>>