PostgreSQL错误代码

简介      

     PostgreSQL服务器发出的所有消息都分配了五个字符的错误代码, 这些代码遵循 SQL 的"SQLSTATE"代码的约定。 需要知道发生了什么错误条件的应用程序通常应该检测错误代码,而不是查看文本错误消息。

       根据标准,错误代码的前两个字符表示错误类别,而后三个字符表示在该类别内特定的条件。 因此,那些不能识别特定错误代码的应用仍然可以从错误类别中推断要做什么。

错误代码规则

参考:https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html

应用

postgresql.conf文件中的配置如下:

1logging_collector = on # Enable capturing of stderr and csvlog 2 # into log files. Required to be on for 3 # csvlogs. 4 # (change requires restart) 5 6log_error_verbosity = verbose # terse, default, or verbose messages

配置文件修改完成后重启数据库,查看data目录下的pg_log内的文件内容:

1[postgres@localhost pg_log]$ cat postgresql-2017-03-29_094442.log 2LOG: 00000: database system was shut down at 2017-03-29 09:44:41 CST 3LOCATION: StartupXLOG, xlog.c:5909 4LOG: 00000: MultiXact member wraparound protections are now enabled 5LOCATION: SetOffsetVacuumLimit, multixact.c:2629 6LOG: 00000: database system is ready to accept connections 7LOCATION: reaper, postmaster.c:2788 8LOG: 00000: autovacuum launcher started 9LOCATION: AutoVacLauncherMain, autovacuum.c:413

在数据库中执行如下操作:

postgres=#  create tablespace tbs1 location '/home1/aa';

查看data目录下的pg_log内的文件内容:

1ERROR: 58P01: directory "/home1/aa" does not exist 2LOCATION: create_tablespace_directories, tablespace.c:586 3STATEMENT: create tablespace tbs1 location '/home1/aa';

综上,当启用参数log_error_verbosity=VERBOSE时会在日志中打印错误代码。

相关代码

1/* 2 * Write error report to server's log 3 */ 4static void 5send_message_to_server_log(ErrorData *edata) 6{ 7 StringInfoData buf; 8 9 initStringInfo(&buf); 10 11 formatted_log_time[0] = '\0'; 12 13 log_line_prefix(&buf, edata); 14 appendStringInfo(&buf, "%s: ", error_severity(edata->elevel)); 15 16 if (Log_error_verbosity >= PGERROR_VERBOSE) 17 appendStringInfo(&buf, "%s: ", unpack_sql_state(edata->sqlerrcode)); 18 19 if (edata->message) 20 append_with_tabs(&buf, edata->message); 21 else 22 append_with_tabs(&buf, _("missing error text")); 23 24 if (edata->cursorpos > 0) 25 appendStringInfo(&buf, _(" at character %d"), 26 edata->cursorpos); 27 else if (edata->internalpos > 0) 28 appendStringInfo(&buf, _(" at character %d"), 29 edata->internalpos); 30 31 appendStringInfoChar(&buf, '\n'); 32 33 if (Log_error_verbosity >= PGERROR_DEFAULT) 34 { 35 if (edata->detail_log) 36 { 37 log_line_prefix(&buf, edata); 38 appendStringInfoString(&buf, _("DETAIL: ")); 39 append_with_tabs(&buf, edata->detail_log); 40 appendStringInfoChar(&buf, '\n'); 41 } 42 else if (edata->detail) 43 { 44 log_line_prefix(&buf, edata); 45 appendStringInfoString(&buf, _("DETAIL: ")); 46 append_with_tabs(&buf, edata->detail); 47 appendStringInfoChar(&buf, '\n'); 48 } 49 if (edata->hint) 50 { 51 log_line_prefix(&buf, edata); 52 appendStringInfoString(&buf, _("HINT: ")); 53 append_with_tabs(&buf, edata->hint); 54 appendStringInfoChar(&buf, '\n'); 55 } 56 if (edata->internalquery) 57 { 58 log_line_prefix(&buf, edata); 59 appendStringInfoString(&buf, _("QUERY: ")); 60 append_with_tabs(&buf, edata->internalquery); 61 appendStringInfoChar(&buf, '\n'); 62 } 63 if (edata->context && !edata->hide_ctx) 64 { 65 log_line_prefix(&buf, edata); 66 appendStringInfoString(&buf, _("CONTEXT: ")); 67 append_with_tabs(&buf, edata->context); 68 appendStringInfoChar(&buf, '\n'); 69 } 70 if (Log_error_verbosity >= PGERROR_VERBOSE) 71 { 72 /* assume no newlines in funcname or filename... */ 73 if (edata->funcname && edata->filename) 74 { 75 log_line_prefix(&buf, edata); 76 appendStringInfo(&buf, _("LOCATION: %s, %s:%d\n"), 77 edata->funcname, edata->filename, 78 edata->lineno); 79 } 80 else if (edata->filename) 81 { 82 log_line_prefix(&buf, edata); 83 appendStringInfo(&buf, _("LOCATION: %s:%d\n"), 84 edata->filename, edata->lineno); 85 } 86 } 87 } 88/* 89 * If the user wants the query that generated this error logged, do it. 90 */ 91 if (is_log_level_output(edata->elevel, log_min_error_statement) && 92 debug_query_string != NULL && 93 !edata->hide_stmt) 94 { 95 log_line_prefix(&buf, edata); 96 appendStringInfoString(&buf, _("STATEMENT: ")); 97 append_with_tabs(&buf, debug_query_string); 98 appendStringInfoChar(&buf, '\n'); 99 } 100 101#ifdef HAVE_SYSLOG 102 /* Write to syslog, if enabled */ 103 if (Log_destination & LOG_DESTINATION_SYSLOG) 104 { 105 int syslog_level; 106 107 switch (edata->elevel) 108 { 109 case DEBUG5: 110 case DEBUG4: 111 case DEBUG3: 112 case DEBUG2: 113 case DEBUG1: 114 syslog_level = LOG_DEBUG; 115 break; 116 case LOG: 117 case COMMERROR: 118 case INFO: 119 syslog_level = LOG_INFO; 120 break; 121 case NOTICE: 122 case WARNING: 123 syslog_level = LOG_NOTICE; 124 break; 125 case ERROR: 126 syslog_level = LOG_WARNING; 127 break; 128 case FATAL: 129 syslog_level = LOG_ERR; 130 break; 131 case PANIC: 132 default: 133 syslog_level = LOG_CRIT; 134 break; 135 } 136 137 write_syslog(syslog_level, buf.data); 138 } 139#endif /* HAVE_SYSLOG */ 140 141#ifdef WIN32 142 /* Write to eventlog, if enabled */ 143 if (Log_destination & LOG_DESTINATION_EVENTLOG) 144 { 145 write_eventlog(edata->elevel, buf.data, buf.len); 146 } 147#endif /* WIN32 */ 148 149 /* Write to stderr, if enabled */ 150 if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == DestDebug) 151 { 152 /* 153 * Use the chunking protocol if we know the syslogger should be 154 * catching stderr output, and we are not ourselves the syslogger. 155 * Otherwise, just do a vanilla write to stderr. 156 */ 157 if (redirection_done && !am_syslogger) 158 write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR); 159#ifdef WIN32 160 /* 161 * In a win32 service environment, there is no usable stderr. Capture 162 * anything going there and write it to the eventlog instead. 163 * 164 * If stderr redirection is active, it was OK to write to stderr above 165 * because that's really a pipe to the syslogger process. 166 */ 167 else if (pgwin32_is_service()) 168 write_eventlog(edata->elevel, buf.data, buf.len); 169#endif 170 else 171 write_console(buf.data, buf.len); 172 } 173 174 /* If in the syslogger process, try to write messages direct to file */ 175 if (am_syslogger) 176 write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR); 177 178 /* Write to CSV log if enabled */ 179 if (Log_destination & LOG_DESTINATION_CSVLOG) 180 { 181 if (redirection_done || am_syslogger) 182 { 183 /* 184 * send CSV data if it's safe to do so (syslogger doesn't need the 185 * pipe). First get back the space in the message buffer. 186 */ 187 pfree(buf.data); 188 write_csvlog(edata); 189 } 190 else 191 { 192 /* 193 * syslogger not up (yet), so just dump the message to stderr, 194 * unless we already did so above. 195 */ 196 if (!(Log_destination & LOG_DESTINATION_STDERR) && 197 whereToSendOutput != DestDebug) 198 write_console(buf.data, buf.len); 199 pfree(buf.data); 200 } 201 } 202 else 203 { 204 pfree(buf.data); 205 } 206}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

微信小程序new Date()转换时间异常问题

微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT