一.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。可以是絕對路徑,也可是相對路徑(相對于資料庫檔案所在的路徑)。
它決定資料庫運作日志檔案的名稱。預設值是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檔案中被設定。
單個日志檔案的生存期,預設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--調整參數
這兩個參數修改後,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]$ 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'
參考博文:
<a href="http://www.cnblogs.com/alianbog/p/5596921.html" target="_blank">http://www.cnblogs.com/alianbog/p/5596921.html</a>
http://blog.csdn.net/shanzhizi/article/details/47616645
本文轉自 wjw555 51CTO部落格,原文連結:http://blog.51cto.com/wujianwei/1978449