1. 程式人生 > >postgresql9.5.9相關的日誌文件介紹

postgresql9.5.9相關的日誌文件介紹

postgresql9.5.9相關的日誌文件介紹


一.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‘


參考博文:

http://www.cnblogs.com/alianbog/p/5596921.html

http://blog.csdn.net/shanzhizi/article/details/47616645

本文出自 “10931853” 博客,謝絕轉載!

postgresql9.5.9相關的日誌文件介紹