Postgresql的csv日志设置

PG的日志系统比较完善,除去系统启动时指定的日志,wal日志等外,下面主要介绍另一个详细的输出日志:csv log. 涉及到的参数文件:$PGDATA/postgresql.conf
涉及的主要参数:

log_destination = 'csvlog'
logging_collector = on
log_directory = '/home/postgres/pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

1.说明
要开启csv日志,需要设置logging_collector=on,否则输不出来。设置完了后需要 重启DB。
log_destination的默认参数是stderr,不修改的话只输出标准错误信息
log_directory是输出日志的路径
log_filename是输出日志的名称,采用默认.
还有其他参数具体可参考postgresql.conf中的where to log 部分。

2.查看:

[postgres@localhost pg_log]$ ll /home/postgres/pg_log/
total 4
-rw-------. 1 postgres postgres 2476 Jun 15 15:40 postgresql-2012-06-15_152431.csv
-rw-------. 1 postgres postgres    0 Jun 15 15:24 postgresql-2012-06-15_152431.log

[postgres@localhost pg_log]$ more postgresql-2012-06-15_152431.csv 
2012-06-15 15:24:31.258 CST,,,1882,,4fdae32f.75a,1,,2012-06-15 15:24:31 CST,,0,LOG,00000,"database system was shut down at 2012-06-1
5 15:24:19 CST",,,,,,,,,""
2012-06-15 15:24:31.259 CST,,,1882,,4fdae32f.75a,2,,2012-06-15 15:24:31 CST,,0,LOG,00000,"could not remove cache file ""base/16384/p
g_internal.init"": Not a directory",,,,,,,,,""
2012-06-15 15:24:31.282 CST,,,1885,,4fdae32f.75d,1,,2012-06-15 15:24:31 CST,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,""
2012-06-15 15:24:31.306 CST,,,1880,,4fdae32e.758,1,,2012-06-15 15:24:30 CST,,0,LOG,00000,"database system is ready to accept connect
ions",,,,,,,,,""
.....

3.日志入库
也就是将文本文件导入到数据库中,采用copy方式比较简单。建表:

CREATE TABLE pg_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)
);

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pg_log_pkey" for table "pg_log"
CREATE TABLE;

copy

copy pg_log from '/home/postgres/pg_log/postgresql-2012-06-15_152431.csv' with csv;
COPY 4

4.日志的关闭
把开启日志的过程做反向操作,设置相关值off即可

5.其他
该日志信息可以结合启动时的输出日志来查看系统的一个历史运行情况,是排查系统err有极方便的工具

posted @ 2021-11-01 10:54  数据库集中营  阅读(1095)  评论(0编辑  收藏  举报