PostgreSQL Internals

PostgreSQL错误日志

2019-05-18  本文已影响0人  DavidLi2010

日志接口

PG有新旧两种样式的接口。

新样式:

ereport(ERROR,
        (errcode(ERRCODE_UNDEFINED_CURSOR),
         errmsg("portal \"%s\" not found", stmt->portalname),
         ... other errxxx() fields as needed ...));

旧样式:

elog(ERROR, "portal \"%s\" not found", stmt->portalname);

不论新旧接口,在内部都是创建了一个stack entry保存数据:

/*
 * ErrorData holds the data accumulated during any one ereport() cycle.
 * Any non-NULL pointers must point to palloc'd data.
 * (The const pointers are an exception; we assume they point at non-freeable
 * constant strings.)
 */
typedef struct ErrorData
{
    int         elevel;         /* error level */
    bool        output_to_server;   /* will report to server log? */
    bool        output_to_client;   /* will report to client? */
    bool        show_funcname;  /* true to force funcname inclusion */
    bool        hide_stmt;      /* true to prevent STATEMENT: inclusion */
    bool        hide_ctx;       /* true to prevent CONTEXT: inclusion */
    const char *filename;       /* __FILE__ of ereport() call */
    int         lineno;         /* __LINE__ of ereport() call */
    const char *funcname;       /* __func__ of ereport() call */
    const char *domain;         /* message domain */
    const char *context_domain; /* message domain for context message */
    int         sqlerrcode;     /* encoded ERRSTATE */
    char       *message;        /* primary error message (translated) */
    char       *detail;         /* detail error message */
    char       *detail_log;     /* detail error message for server log only */
    char       *hint;           /* hint message */
    char       *context;        /* context message */
    const char *message_id;     /* primary message's id (original string) */
    char       *schema_name;    /* name of schema */
    char       *table_name;     /* name of table */
    char       *column_name;    /* name of column */
    char       *datatype_name;  /* name of datatype */
    char       *constraint_name;    /* name of constraint */
    int         cursorpos;      /* cursor index into query string */
    int         internalpos;    /* cursor index into internalquery */
    char       *internalquery;  /* text of internally-generated query */
    int         saved_errno;    /* errno at entry */

    /* context containing associated non-constant strings */
    struct MemoryContextData *assoc_context;
} ErrorData;

日志级别

PG的日志级别定义如下:

/* Error level codes */
#define DEBUG5      10          /* Debugging messages, in categories of
                                 * decreasing detail. */
#define DEBUG4      11
#define DEBUG3      12
#define DEBUG2      13
#define DEBUG1      14          /* used by GUC debug_* variables */
#define LOG         15          /* Server operational messages; sent only to
                                 * server log by default. */
#define LOG_SERVER_ONLY 16      /* Same as LOG for server reporting, but never
                                 * sent to client. */
#define COMMERROR   LOG_SERVER_ONLY /* Client communication problems; same as
                                     * LOG for server reporting, but never
                                     * sent to client. */
#define INFO        17          /* Messages specifically requested by user (eg
                                 * VACUUM VERBOSE output); always sent to
                                 * client regardless of client_min_messages,
                                 * but by default not sent to server log. */
#define NOTICE      18          /* Helpful messages to users about query
                                 * operation; sent to client and not to server
                                 * log by default. */
#define WARNING     19          /* Warnings.  NOTICE is for expected messages
                                 * like implicit sequence creation by SERIAL.
                                 * WARNING is for unexpected messages. */
#define ERROR       20          /* user error - abort transaction; return to
                                 * known state */
/* Save ERROR value in PGERROR so it can be restored when Win32 includes
 * modify it.  We have to use a constant rather than ERROR because macros
 * are expanded only when referenced outside macros.
 */
#ifdef WIN32
#define PGERROR     20
#endif
#define FATAL       21          /* fatal error - abort process */
#define PANIC       22          /* take down the other backends with me */

在日志级别大于等于ERROR时,日志接口不会返回。ERROR级别时通过longjmp向上抛出,FATAL级别时进程退出,PANIC级别时进程直接abort。

同时PG可以将错误消息发送到客户端。

日志目的地

PG支持将日志写到标准输出、syslog、eventlog或者csvlog中。

/* Log destination bitmap */
#define LOG_DESTINATION_STDERR   1
#define LOG_DESTINATION_SYSLOG   2
#define LOG_DESTINATION_EVENTLOG 4
#define LOG_DESTINATION_CSVLOG   8
上一篇下一篇

猜你喜欢

热点阅读