PostgreSQL的日誌型別

2021-07-06 01:34:17 字數 4662 閱讀 8930

剛開始學習postgres的時候,可能對postgresql中的日誌概念比較模糊,到底有多少種日誌,哪些日誌是能刪除的,各自又記錄什麼樣的功能。

postgresql中有三種日誌,pg_log,pg_xlog和pg_clog。

一.安裝路徑

這三種資料庫後兩者一般的安裝路徑是$pgdata/下面的資料夾下,前者預設路徑是$pgdata/pg_log,通常可自己定義路徑和檔名

[postgres@testdb ~]$ cd $pgdata

[postgres@test

db pgdata]$ ls -l

total 88

drwx------ 10 postgres kenyon  4096 dec 12 15:58 base

drwx------  2 postgres kenyon  4096 dec 13  10:13 global

drwx------  2 postgres kenyon  4096 jun 21   2012 pg_clog

-rw-------  1 postgres  kenyon  4739 aug 10  09:31 pg_hba.conf

-rw-------  1 postgres  kenyon  1636 jun 21   2012 pg_ident.conf

drwx------  4 postgres kenyon  4096 jun 21   2012 pg_multixact

drwx------  2 postgres kenyon  4096 dec 12  18:24 pg_notify

drwx------  2 postgres kenyon  4096 jun 21   2012 pg_serial

drwx------  2 postgres kenyon  4096 dec 21  20:17 pg_stat_tmp

drwx------  2 postgres kenyon  4096 dec 14  15:01 pg_subtrans

drwx------  2 postgres kenyon  4096 jun 21   2012 pg_tblspc

drwx------  2 postgres kenyon  4096 jun 21   2012 pg_twophase

-rw-------  1 postgres  kenyon     4 jun 21     2012 pg_version

drwx------  3 postgres kenyon  4096 dec 13  15:20 pg_xlog

-rw-------  1 postgres  kenyon 19439 dec 13  18:23 postgresql.conf

-rw-------  1 postgres  kenyon    52 dec 12   18:24 postmaster.opts

-rw-------  1 postgres  kenyon    73 dec 12   18:24 postmaster.pid

[postgres@testdb

pgdata]$more postgresql.conf

log_destination = 'csvlog'

logging_collector = on

log_directory = '/home/postgres/pg_log'

log_filename = 'postgresql-%y-%m-%d_%h%m%s.log'

二 .用處 

pg_log 

這個日誌一般是記錄伺服器與db的狀態,比如各種error資訊,定位慢查詢sql,資料庫的啟動關閉資訊,發生checkpoint過於頻繁等的告警資訊,諸如此類。linux自帶的路徑一般在/var/log/postgres下面。該日誌有.csv格式和.log。個人建議用前一種,因為一般會按大小和時間自動切割,畢竟檢視乙個巨大的日誌檔案比檢視不同時間段的多個日誌要難得多。另外這種日誌是可以被清理刪除,壓縮打包或者轉移,同時並不影響db的正常執行。當我們有遇到db無法啟動或者更改引數沒有生效時,第乙個想到的就是檢視這個日誌。 

一般的結果如下: 

[postgres@testdb pg_log]$ ll

total 3996

-rw------- 1 postgres kenyon   15632   dec 12 21:59 postgresql-2012-12-12_182439.csv

-rw------- 1 postgres kenyon       0      dec 12 18:24 postgresql-2012-12-12_182439.log

-rw------- 1 postgres kenyon 3990717 dec 13 23:48 postgresql-2012-12-13_000000.csv

-rw------- 1 postgres kenyon       0     dec 13 00:00 postgresql-2012-12-13_000000.log

-rw------- 1 postgres kenyon   27518  dec 14 17:46 postgresql-2012-12-14_000000.csv

-rw------- 1 postgres kenyon       0     dec 14 00:00 postgresql-2012-12-14_000000.log

-rw------- 1 postgres kenyon       0     dec 15 00:00 postgresql-2012-12-15_000000.csv

-rw------- 1 postgres kenyon       0     dec 15 00:00 postgresql-2012-12-15_000000.log

-rw------- 1 postgres kenyon       0     dec 16 00:00 postgresql-2012-12-16_000000.csv

-rw------- 1 postgres kenyon       0     dec 16 00:00 postgresql-2012-12-16_000000.log

-rw------- 1 postgres kenyon   29713  dec 17 21:07 postgresql-2012-12-17_000000.csv

-rw------- 1 postgres kenyon       0     dec 17 00:00 postgresql-2012-12-17_000000.log

-rw------- 1 postgres kenyon     900    dec 18 20:49 postgresql-2012-12-18_000000.csv

-rw------- 1 postgres kenyon       0     dec 18 00:00 postgresql-2012-12-18_000000.log

-rw------- 1 postgres kenyon    4607   dec 19 18:55 postgresql-2012-12-19_000000.csv

-rw------- 1 postgres kenyon       0     dec 19 00:00 postgresql-2012-12-19_000000.log

-rw------- 1 postgres kenyon    1573   dec 20 20:38 postgresql-2012-12-20_000000.csv

-rw------- 1 postgres kenyon       0     dec 20 00:00 postgresql-2012-12-20_000000.log

-rw------- 1 postgres kenyon       0     dec 21 00:00 postgresql-2012-12-21_000000.csv

-rw------- 1 postgres kenyon       0     dec 21 00:00 postgresql-2012-12-21_000000.log

pg_xlog

這個日誌是記錄的postgresql的wal資訊,也就是一些事務日誌資訊(transaction log),預設單個大小是16m,原始碼安裝的時候可以更改其大小。這些資訊通常名字是類似'000000010000000000000013'這樣的檔案,這些日誌會在 

定時回滾恢復(

pitr), 

流複製(

replication stream)以及歸檔時能被用到,這些日誌是非常重要的,記錄著資料庫發生的各種事務資訊,不得隨意刪除或者移動這類日誌檔案,不然你的資料庫會有無法恢復的風險 

當你的歸檔或者流複製發生異常的時候,事務日誌會不斷地生成,有可能會造成你的磁碟空間被塞滿,最終導致db掛掉或者起不來。遇到這種情況不用慌,可以先關閉歸檔或者流複製功能,備份pg_xlog日誌到其他地方,但請不要刪除。然後刪除較早時間的的pg_xlog,有一定空間後再試著啟動postgres。 

pg_clog 

pg_clog這個檔案也是事務日誌檔案,但與pg_xlog不同的是它記錄的是事務的元資料(metadata),這個日誌告訴我們哪些事務完成了,哪些沒有完成。這個日誌檔案一般非常小,但是重要性也是相當高,不得隨意刪除或者對其更改資訊。

總結:pg_log記錄各種error資訊,以及伺服器與db的狀態資訊,可由使用者隨意更新刪除

pg_xlog與pg_clog記錄資料庫的事務資訊,不得隨意刪除更新,做物理備份時要記得備份著兩個日誌。

參考轉譯:

PostgreSQL的日誌型別

剛開始學習postgres的時候,可能對postgresql中的日誌概念比較模糊,到底有多少種日誌,哪些日誌是能刪除的,各自又記錄什麼樣的功能。postgresql中有三種日誌,pg log,pg xlog和pg clog。一.安裝路徑 這三種資料庫後兩者一般的安裝路徑是 pgdata 下面的資料夾...

postgresql 日誌配置

log destination csvlog csv模式輸出 logging collector on 收集日誌開啟 log directory pg log 輸出日期的陌路 log filename postgresql u.log 輸出的檔名加星期格式 log truncate on rotat...

PostgreSQL 型別轉換

類似oracle postgresql也有強大的型別轉換函式,下面僅舉兩個型別轉換例子。1 例子 postgres select 1 4 column?0 1 row 在pg裡如果想做除法並想保留小數,用上面的方法卻行不通,因為 運算結果為取整,並 且會截掉小數部分。2 型別轉換 postgres ...