postgresql 日志配置

PostgreSQL有3种日志,分别是pg_log(数据库运行日志)、pg_xlog(WAL 日志,即重做日志)、pg_clog(事务提交日志,记录的是事务的元数据)

pg_log默认是关闭的,需要设置参数启用此日志。pg_xlog和pg_clog都是强制打开的,无法关闭。

1.启用pg_log并配置日志参数
log_destination = ‘csvlog’
logging_collector = on
log_directory = ‘pg_log’
log_filename = ‘postgresql-%Y-%m-%d.log’
log_rotation_age = 1d
log_min_messages = info
log_min_error_statement = info
– 记录执行慢的SQL
log_min_duration_statement = 60
– log_checkpoints = on
– log_connections = on
– log_disconnections = on
– log_duration = on
log_line_prefix = ‘%t,%r,user=%u,db=%d’
– 监控数据库中长时间的锁
log_lock_waits = on
– 记录DDL操作
log_statement = ‘ddl’

2.创建日志记录表
CREATE TABLE postgres_log
(
log_time timestamp(3) with time zone,
user_name text,
database_name text,
process_id integer,
connection_from text,
session_id text,
session_line_num bigint,
command_tag text,
session_start_time timestamp with time zone,
virtual_transaction_id text,
transaction_id bigint,
error_severity text,
sql_state_code text,
message text,
detail text,
hint text,
internal_query text,
internal_query_pos integer,
context text,
query text,
query_pos integer,
location text,
application_name text,
PRIMARY KEY (session_id, session_line_num)
);

你可能感兴趣的:(postgresql教程)