一.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,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,"database system was shut down at 2017-11-02 15:13:29 CST",""
2017-11-02 15:14:10.389 CST,2,"MultiXact member wraparound protections are now enabled",""
2017-11-02 15:14:10.392 CST,21321,59fac5c2.5349,"autovacuum launcher started","database system is ready to accept connections",""
2017-11-02 15:14:43.211 CST,21338,"127.0.0.1:33506",59fac5e3.535a,"",2017-11-02 15:14:43 CST,"connection received: host=127.0.0.1 port=33506",""
2017-11-02 15:14:43.212 CST,"postgres","authentication",2/5,"connection authorized: user=postgres database=postgres",""
2017-11-02 15:14:55.416 CST,21339,"127.0.0.1:33507",59fac5ef.535b,2017-11-02 15:14:55 CST,"connection received: host=127.0.0.1 port=33507",""
2017-11-02 15:14:55.417 CST,"testdb03",3/1,"connection authorized: user=postgres database=testdb03",""
2017-11-02 15:15:26.873 CST,3,"SELECT",3/4,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'
参考博文:
http://www.cnblogs.com/alianbog/p/5596921.html
http://blog.csdn.net/shanzhizi/article/details/47616645