postgresql日志配置


背景

DBA在排查數據庫問題時候,首先查看的就是日志。PG數據庫在有的版本中,默認是不會寫記錄日志的,盡管啟動項pg_ctl中提供了一個-l參數,但是只是用來分析啟動時候的參數,在數據庫運行過程中也需要查看日志中出現的告警,錯誤,還有所有人最關心的慢查詢。本文就是來分析postgresql.conf中的關於日志的參數配置。

日志參數

本文以postgresql 9.6版本進行說明,其他版本可能略有不同。

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -

#log_destination = 'stderr'		# Valid values are combinations of    # stderr, csvlog, syslog, and eventlog四選一,默認stderr
					# stderr, csvlog, syslog, and eventlog,           # 也可以使用csvlog,這樣輸出的就是一個csv文件,這個好處是
					# depending on platform.  csvlog                  # csv文件可以作為外表表導入數據庫進行檢索
					# requires logging_collector to be on.            # 這個參數有效的前提是logging_collector也必須打開為on

# This is used when logging to stderr:
logging_collector = on		# Enable capturing of stderr and csvlog   #這個參數為on時,pg數據庫就開始記錄日志了,但是默認為off
					# into log files. Required to be on for           #這個參數更改后需要restart數據庫
					# csvlogs.
					# (change requires restart)

# These are only used if logging_collector is on:
log_directory = 'pg_log'		# directory where log files are written, # 該參數是配置日志的目錄,可以是絕對目錄,也可以是相對目錄
					# can be absolute or relative to PGDATA              # 相對目錄要設置PGDATA的值,如果pg_log文件夾不存在要新建
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'	# log file name pattern, # 該參數是配置log的名字,一般用這個就行了,不用修改
					# can include strftime() escapes
#log_file_mode = 0600			# creation mode for log files,           # 日志文件的權限,默認是600也不用更改
					# begin with 0 to use octal notation
#log_truncate_on_rotation = off		# If on, an existing log file with the # 這個參數沒必要開啟,因為如果出現同名的日志文件,開啟會
					# same name as the new log file will be                # 清空原來日志,而不是在原來的基礎上增加。但是在有一種情況
					# truncated rather than appended to.                   # 下,可以設置為on,就是日志文件以星期格式命名,一周一輪回
					# But such truncation only occurs on                   # 默認就保留了7天日志,這個是一個巧妙的日志處理方法。
					# time-driven rotation, not on restarts
					# or size-driven rotation.  Default is
					# off, meaning append to existing files
					# in all cases.
#log_rotation_age = 1d			# Automatic rotation of logfiles will     # 單個日志的生存期,默認為1天,在日志文件沒有達到log_rotation_size
					# happen after that time.  0 disables.                # 時,一天只生成一個日志文件
#log_rotation_size = 10MB		# Automatic rotation of logfiles will     # 單個日志文件大小,如果時間沒有超過log_rotation_age,一個日志
					# happen after that much log output.                  # 文件最大只能是10M,否則生成一個新的日志文件
					# 0 disables.

# These are relevant when logging to syslog:
#syslog_facility = 'LOCAL0'                                             #這幾個參數是在上面的log_destination設置為syslog需要配置的,很少用
#syslog_ident = 'postgres'
#syslog_sequence_numbers = on
#syslog_split_messages = on

# This is only relevant when logging to eventlog (win32):               # 這幾個參數是在上面的log_destination設置為eventlog需要配置的,很少用
# (change requires restart)
#event_source = 'PostgreSQL'

# - When to Log -

#log_min_messages = warning		# values in order of decreasing detail:  # 控制寫到服務器日志里的信息的詳細程度。有效值是DEBUG5, DEBUG4, 
					#   debug5                                           # DEBUG3,DEBUG2,DEBUG1, INFO,NOTICE,WARNING, ERROR,LOG
					#   debug4                                           # FATAL, and PANIC。每個級別都包含它后面的級別。越靠后的數值 
					#   debug3                                           # 發往服務器日志的信息越少,缺省是WARNING。
					#   debug2
					#   debug1
					#   info
					#   notice
					#   warning
					#   error
					#   log
					#   fatal
					#   panic

#log_min_error_statement = error	# values in order of decreasing detail:
					#   debug5                                           # 控制是否在服務器日志里輸出那些導致錯誤條件的 SQL 語句。
					#   debug4                                           # 所有導致一個特定級別(或者更高級別)錯誤的 SQL 語句都要
					#   debug3                                           # 被記錄。有效的值有DEBUG5, DEBUG4,DEBUG3, 
					#   debug2                                           # DEBUG2,DEBUG1,INFO,NOTICE,WARNING,ERROR,LOG,FATAL
					#   debug1                                           # ,和PANIC。缺省是ERROR,表示所有導致錯誤、致命錯誤、恐慌的
					#   info                                             # SQL語句都將被記錄。
					#   notice
					#   warning
					#   error
					#   log
					#   fatal
					#   panic (effectively off)

log_min_duration_statement = 0	# -1 is disabled, 0 logs all statements  # 這個參數非常重要,是排查慢查詢的好工具,-1是關閉記錄這類日志
					# and their durations, > 0 logs only                 # 0 是記錄所有的查詢SQL,如果設置為大於0(毫秒),則超過該值的
					# statements running at least this number            # 執行時間的sql會記錄下來
					# of milliseconds


# - What to Log -

#debug_print_parse = off                                                 # 調試類的,沒必要打開該類日志
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = on
#log_checkpoints = off                                                   # 記錄發生檢查點的日志
#log_connections = off                                                   # 記錄客戶連接的日志
#log_disconnections = off                                                # 記錄客戶斷開的日志
#log_duration = off                                                      # 記錄每條SQL語句執行完成消耗的時間,將此配置設置為on,用於統計
                                                                         # 哪些SQL語句耗時較長。一般用上面那個log_min_duration_statement即可
#log_error_verbosity = default		# terse, default, or verbose messages
#log_hostname = on
log_line_prefix = '%m %p %u %d %r %e'			# special values:            # 日志輸出格式(%m,%p實際意義配置文件中有解釋),可根據自己需要設置
					#   %a = application name                            # (能夠記錄時間,用戶名稱,數據庫名稱,客戶端IP和端口,sql語句方便定位問題)
					#   %u = user name
					#   %d = database name
					#   %r = remote host and port
					#   %h = remote host
					#   %p = process ID
					#   %t = timestamp without milliseconds
					#   %m = timestamp with milliseconds
					#   %n = timestamp with milliseconds (as a Unix epoch)
					#   %i = command tag
					#   %e = SQL state
					#   %c = session ID
					#   %l = session line number
					#   %s = session start timestamp
					#   %v = virtual transaction ID
					#   %x = transaction ID (0 if none)
					#   %q = stop here in non-session
					#        processes
					#   %% = '%'
					# e.g. '<%u%%%d> '
#log_lock_waits = off			# log lock waits >= deadlock_timeout   # 控制當一個會話等待時間超過deadlock_timeout而被鎖時是否產生一個
                                                                       # 日志信息。在判斷一個鎖等待是否會影響性能時是有用的,缺省是off。
#log_statement = 'none'			# none, ddl, mod, all                  # none, ddl, mod, all ---- 控制記錄哪些SQL語句。
                                                                       # none不記錄,ddl記錄所有數據定義命令,比如CREATE,ALTER,和DROP語句。
                                                                       # mod記錄所有ddl語句,加上數據修改語句INSERT,UPDATE等,all記錄所有執行的語句,
                                                                       # 將此配置設置為all可跟蹤整個數據庫執行的SQL語句。
#log_replication_commands = off
#log_temp_files = -1			# log temporary files equal or larger
					# than the specified size in kilobytes;
					# -1 disables, 0 logs all temp files
log_timezone = 'PRC'                                                   # 日志時區,最好和服務器設置同一個時區,方便問題定位


# - Process Title -

#cluster_name = ''			# added to process titles if nonempty
					# (change requires restart)
#update_process_title = on

總結

在實際安裝中,強烈建議開啟日志,並配置適當的日志格式便於發現和跟蹤問題,不論是研發還是運維還是DBA,日志永遠是排查問題的第一步。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM