设为首页 收藏本站
查看: 3161|回复: 0

[经验分享] postgresql9.5.9相关的日志文件介绍

[复制链接]

尚未签到

发表于 2017-11-3 13:02:20 | 显示全部楼层 |阅读模式
一.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_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 100MB
log_min_messages = warning

参数解释:
log_directory = '/data/pgsql086/log'
这个参数只能在postgresql.conf文件中被设置。它决定存放数据库运行日志文件的目录。默认值是pg_log。可以是绝对路径,也可是相对路径(相对于数据库文件所在的路径)。

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
它决定数据库运行日志文件的名称。默认值是postgresql-%Y-%m-%d_%H%M%S.log。它的值可以包含%Y、%m、%d、%H、%M和%S这样的字符串,分别表示年、月、日、小时、分和秒。 如果参数的值中没有指定时间信息(没有出现%Y、%m、%d、%H、%M和%S中的任何一个),系统会自动在log_filename值的末尾加上文件创建的时间作为文件名,例如,如果log_filename的值是 server_log,那么在Sun Aug 29 19:02:33 2004 MST被创建的日志文件的名称将是server_log.1093827753,1093827753是Sun Aug 29 19:02:33 2004 MST在数据库内部的表示形式。这个参数只能在postgresql.conf文件中被设置。

log_rotation_age = 1d
单个日志文件的生存期,默认1天,在日志文件大小没有达到log_rotation_size时,一天只生成一个日志文件

log_rotation_size = 10MB
这个参数只能在postgresql.conf文件中被设置。单个日志文件的大小,如果时间没有超过log_rotation_age,一个日志文件最大只能到10M,否则将新生成一个日志文件。

log_min_messages = warning  默认是warning ,
控制写到数据库日志文件中的消息的级别。合法的取值是DEBUG5、DEBUG4、DEBUG3、DEBUG2、DEBUG1、INFO、NOTICE、WARNING、ERROR、 LOG、FATAL和PANIC,每个级别都包含排在它后面的所有级别中的信息。级别越高,数据库运行日志中记录的消息就越多。

重启postgresql服务:
[postgres@cacti log]$ pg_ctl status

pg_ctl: no server running
[postgres@cacti log]$ pg_ctl start
server starting
[postgres@cacti log]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "/data/pgsql086/log".

[postgres@cacti log]$ ll /data/pgsql086/log/*
-rw-------. 1 postgres postgres 30477 Nov  2 12:01 /data/pgsql086/log/postgres.log
-rw-------. 1 postgres postgres   794 Nov  2 15:14 /data/pgsql086/log/postgresql-2017-11-02_151410.csv
-rw-------. 1 postgres postgres    96 Nov  2 15:14 /data/pgsql086/log/postgresql-2017-11-02_151410.log
[postgres@cacti log]$ tailf /data/pgsql086/log/postgresql-2017-11-02_151410.csv
2017-11-02 15:14:10.385 CST,,,21315,,59fac5c2.5343,1,,2017-11-02 15:14:10 CST,,0,LOG,00000,"ending log output to stderr",,"Future log output will go to log destination ""csvlog"".",,,,,,,""
2017-11-02 15:14:10.387 CST,,,21317,,59fac5c2.5345,1,,2017-11-02 15:14:10 CST,,0,LOG,00000,"database system was shut down at 2017-11-02 15:13:29 CST",,,,,,,,,""
2017-11-02 15:14:10.389 CST,,,21317,,59fac5c2.5345,2,,2017-11-02 15:14:10 CST,,0,LOG,00000,"MultiXact member wraparound protections are now enabled",,,,,,,,,""
2017-11-02 15:14:10.392 CST,,,21321,,59fac5c2.5349,1,,2017-11-02 15:14:10 CST,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,""
2017-11-02 15:14:10.392 CST,,,21315,,59fac5c2.5343,2,,2017-11-02 15:14:10 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
2017-11-02 15:14:43.211 CST,,,21338,"127.0.0.1:33506",59fac5e3.535a,1,"",2017-11-02 15:14:43 CST,,0,LOG,00000,"connection received: host=127.0.0.1 port=33506",,,,,,,,,""
2017-11-02 15:14:43.212 CST,"postgres","postgres",21338,"127.0.0.1:33506",59fac5e3.535a,2,"authentication",2017-11-02 15:14:43 CST,2/5,0,LOG,00000,"connection authorized: user=postgres database=postgres",,,,,,,,,""
2017-11-02 15:14:55.416 CST,,,21339,"127.0.0.1:33507",59fac5ef.535b,1,"",2017-11-02 15:14:55 CST,,0,LOG,00000,"connection received: host=127.0.0.1 port=33507",,,,,,,,,""
2017-11-02 15:14:55.417 CST,"postgres","testdb03",21339,"127.0.0.1:33507",59fac5ef.535b,2,"authentication",2017-11-02 15:14:55 CST,3/1,0,LOG,00000,"connection authorized: user=postgres database=testdb03",,,,,,,,,""
2017-11-02 15:15:26.873 CST,"postgres","testdb03",21339,"127.0.0.1:33507",59fac5ef.535b,3,"SELECT",2017-11-02 15:14:55 CST,3/4,0,ERROR,42P01,"relation ""weather1"" does not exist",,,,,,"select * from weather1;",15,,"psql.bin"

上面两条是 postgresql-2011-03-15_000000.csv 日志文件的部分内容 ,由于日志文件的可读性
较差,于是可以通过下面方法将CSV日志导入到数据库表里。详细如下


将CSV日志导入数据库表里   
1--调整参数
log_destination = 'csvlog'
logging_collector = on

    这两个参数修改后,PG SERVER 需要重启。

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)
);

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "postgres_log_pkey" for table "postgres_log"
CREATE TABLE;

    备注:创建日志表 postgres_log 用来保存 CSV日志数据。

3--导入操作系统 csv 日志到表 postgres_log 表
skytf=# copy skytf.postgres_log from '/var/applog/pg_log/postgresql-2011-03-14_000000.csv' with csv;
COPY 26031

skytf=# copy skytf.postgres_log from '/var/applog/pg_log/postgresql-2011-03-15_000000.csv' with csv;
COPY 1297

      备注:文件形式导入导出数据需要以超级用户 postgres 连接到目标库。

4--常用日志分析sql
skytf=# select min(log_time),max(log_time) from skytf.postgres_log;
            min             |            max            
----------------------------+----------------------------
2011-03-14 14:04:07.275+08 | 2011-03-16 05:04:34.427+08
(1 row)

skytf=> select log_time,database_name,user_name,application_name,message from postgres_log where message like '%duration%';
          log_time          | database_name | user_name | application_name |                                                    mess
age                                                   
----------------------------+---------------+-----------+------------------+--------------------------------------------------------
-------------------------------------------------------
2011-03-15 00:23:38.957+08 | db_lbs        | lbs       |                  | duration: 1297.440 ms  execute <unnamed>:    SELECT cit
yname,province,the_geom as the_geom FROM china_city   
.......
      为了显示方便,上面只取一条记录。


提示:
只要重启数据库,就会产生新的postgresql运行日志文件,同时postgresql-2017-11-02_151410.csv只记录错误日志
[postgres@cacti log]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@cacti log]$ pg_ctl start
server starting
[postgres@cacti log]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "/data/pgsql086/log".

[postgres@cacti log]$ ll /data/pgsql086/log/
total 56
-rw-------. 1 postgres postgres 30477 Nov  2 12:01 postgres.log
-rw-------. 1 postgres postgres  4971 Nov  2 15:37 postgresql-2017-11-02_151410.csv
-rw-------. 1 postgres postgres    96 Nov  2 15:14 postgresql-2017-11-02_151410.log
-rw-------. 1 postgres postgres   794 Nov  2 15:37 postgresql-2017-11-02_153751.csv
-rw-------. 1 postgres postgres    96 Nov  2 15:37 postgresql-2017-11-02_153751.log


2.记录执行慢的SQL
log_min_duration_statement = 60
log_checkpoints = on
log_connections = on
log_disconnections = on
log_duration = on
log_line_prefix = '%m'
# 监控数据库中长时间的锁
log_lock_waits = on
# 记录DDL操作
log_statement = 'ddl'



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406496-1-1.html 上篇帖子: PostgreSQL 数据库的备份 下篇帖子: PostgreSQL字符集问题导致乱码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表