您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“分析PostgreSQL日志相關(guān)的配置參數(shù)log_XXX”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“分析PostgreSQL日志相關(guān)的配置參數(shù)log_XXX”吧!
在新initdb的數(shù)據(jù)庫上查詢pg_settings,可查詢PostgreSQL中與log的參數(shù)包括:
[test@localhost ~]$ psql Expanded display is used automatically. psql (12.1) Type "help" for help. [local:/var/run/test]:5000 test@testdb=# select category,name,setting from pg_settings where name like 'log%' order by category; category | name | setting --------------------------------------+-----------------------------+-------------------------------- Reporting and Logging / What to Log | log_lock_waits | off Reporting and Logging / What to Log | log_checkpoints | off Reporting and Logging / What to Log | log_connections | off Reporting and Logging / What to Log | log_timezone | PRC Reporting and Logging / What to Log | log_temp_files | -1 Reporting and Logging / What to Log | log_disconnections | off Reporting and Logging / What to Log | log_duration | off Reporting and Logging / What to Log | log_error_verbosity | default Reporting and Logging / What to Log | log_statement | none Reporting and Logging / What to Log | log_replication_commands | off Reporting and Logging / What to Log | log_autovacuum_min_duration | -1 Reporting and Logging / What to Log | log_hostname | off Reporting and Logging / What to Log | log_line_prefix | %m [%p] Reporting and Logging / When to Log | log_min_duration_statement | -1 Reporting and Logging / When to Log | log_min_error_statement | error Reporting and Logging / When to Log | log_min_messages | warning Reporting and Logging / When to Log | log_transaction_sample_rate | 0 Reporting and Logging / Where to Log | log_destination | stderr Reporting and Logging / Where to Log | log_filename | postgresql-%Y-%m-%d_%H%M%S.log Reporting and Logging / Where to Log | logging_collector | off Reporting and Logging / Where to Log | log_truncate_on_rotation | off Reporting and Logging / Where to Log | log_rotation_size | 10240 Reporting and Logging / Where to Log | log_file_mode | 0600 Reporting and Logging / Where to Log | log_rotation_age | 1440 Reporting and Logging / Where to Log | log_directory | log Statistics / Monitoring | log_statement_stats | off Statistics / Monitoring | log_planner_stats | off Statistics / Monitoring | log_executor_stats | off Statistics / Monitoring | log_parser_stats | off (29 rows) [local:/var/run/test]:5000 test@testdb=#
log_打頭的參數(shù)有29個,下面從where、when、what這幾個維度來解析這些參數(shù),本節(jié)是第三部分,介紹what to log。
debug_print_parse
是否打印分析階段的查詢樹,默認(rèn)為off-不打印
debug_print_rewritten
是否打印查詢重寫階段的查詢樹,默認(rèn)為off-不打印
debug_print_plan
是否打印計劃階段的計劃樹,默認(rèn)為off-不打印
debug_pretty_print
是否以優(yōu)雅的方式打?。磕J(rèn)為on
log_checkpoints
是否記錄checkpoint信息,默認(rèn)為off。
在checkpoint發(fā)生時,PG會記錄相關(guān)的信息
checkpoint_timeout = 1min # range 30s-1d checkpoint_completion_target = 0.5 # checkpoint target duration, 0.0 - 1.0 #checkpoint_flush_after = 256kB # measured in pages, 0 disables #checkpoint_warning = 30s # 0 disables log_checkpoints = on
插入數(shù)據(jù),在檢查點發(fā)生時,可以看到檢查點的相關(guān)信息輸出
2019-12-30 14:27:03.618 CST [2224] LOG: duration: 106.374 ms statement: insert into tbl select x,'c1'||x from generate_series(1,10000) x; 2019-12-30 14:28:00.428 CST [2166] LOG: checkpoint starting: time 2019-12-30 14:28:06.387 CST [2166] LOG: checkpoint complete: wrote 59 buffers (0.4%); 0 WAL file(s) added, 0 removed, 0 recycled; write=5.949 s, sync=0.001 s, total=5.958 s; sync files=3, longest=0.001 s, average=0.000 s; distance=764 kB, estimate=764 kB
log_connections
記錄登錄信息,包括什么時候有連接請求,哪個用戶連接的是哪個數(shù)據(jù)庫;如連接失敗,也會記錄相關(guān)信息。
###修改配置信息 [test@localhost ~]$ grep 'log_connections' $PGDATA/postgresql.conf # "postgres -c log_connections=on". Some parameters can be changed at run time log_connections = on [test@localhost ~]$ pg_ctl reload server signaled [test@localhost ~]$ ###日志輸出 2019-12-30 14:33:02.527 CST [2634] LOG: connection received: host=[local] 2019-12-30 14:33:02.531 CST [2634] LOG: connection authorized: user=test database=testdb application_name=psql
log_disconnections
記錄連接斷開登錄信息
### [test@localhost ~]$ grep 'log_disconnections' $PGDATA/postgresql.conf log_disconnections = on [test@localhost ~]$ ###日志輸出 2019-12-30 14:34:57.646 CST [2734] LOG: disconnection: session time: 0:00:04.885 user=test database=testdb host=[local]
log_duration
記錄執(zhí)行時間,僅記錄執(zhí)行時間,沒有其他多余的信息。默認(rèn)為on。
2019-12-30 14:36:49.149 CST [2224] LOG: duration: 12.178 ms
log_error_verbosity
出現(xiàn)錯誤時的日志診斷信息級別,可選項包括:terse, default, or verbose,默認(rèn)值為default
使用verbose,可以看到哪個源文件的哪一行,方便診斷
###創(chuàng)建測試對象 [local:/data/run/test]:5000 test@testdb=# create view vw_tbl as select * from tbl; CREATE VIEW [local:/data/run/test]:5000 test@testdb=# ###terse 2019-12-30 14:41:30.395 CST [2163] LOG: parameter "log_error_verbosity" changed to "terse" 2019-12-30 14:41:39.689 CST [2224] ERROR: cannot drop table tbl because other objects depend on it ###default 2019-12-30 14:42:27.106 CST [2163] LOG: parameter "log_error_verbosity" changed to "default" 2019-12-30 14:42:33.287 CST [2224] ERROR: cannot drop table tbl because other objects depend on it 2019-12-30 14:42:33.287 CST [2224] DETAIL: view vw_tbl depends on table tbl 2019-12-30 14:42:33.287 CST [2224] HINT: Use DROP ... CASCADE to drop the dependent objects too. ###verbose 2019-12-30 14:43:29.654 CST [2163] LOG: 00000: parameter "log_error_verbosity" changed to "verbose" 2019-12-30 14:43:29.654 CST [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 14:43:32.790 CST [2224] ERROR: 2BP01: cannot drop table tbl because other objects depend on it 2019-12-30 14:43:32.790 CST [2224] DETAIL: view vw_tbl depends on table tbl 2019-12-30 14:43:32.790 CST [2224] HINT: Use DROP ... CASCADE to drop the dependent objects too. 2019-12-30 14:43:32.790 CST [2224] LOCATION: reportDependentObjects, dependency.c:1196
log_hostname
是否記錄主機名稱,默認(rèn)為off。
log_line_prefix
每一行日志前的前綴。默認(rèn)為’%m [%p] ‘,可使用的通配符包括:
%a – application name
%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
%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
%% – ‘%’
把該參數(shù)修改為’%m %u@%d %p %r’,reload后的日志輸出:
2019-12-30 15:20:31.707 CST @ [2163] LOG: 00000: parameter "log_line_prefix" changed to "%m %u@%d [%p] %r " 2019-12-30 15:20:31.707 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:20:40.148 CST test@testdb [2224] [local] ERROR: 2BP01: cannot drop table tbl because other objects depend on it 2019-12-30 15:20:40.148 CST test@testdb [2224] [local] DETAIL: view vw_tbl depends on table tbl 2019-12-30 15:20:40.148 CST test@testdb [2224] [local] HINT: Use DROP ... CASCADE to drop the dependent objects too. 2019-12-30 15:20:40.148 CST test@testdb [2224] [local] LOCATION: reportDependentObjects, dependency.c:1196
log_lock_waits
記錄等待時間超過deadlock_timeout(默認(rèn)為1s)的lock。
2019-12-30 15:27:06.637 CST @ [2163] LOG: 00000: parameter "log_lock_waits" changed to "on" 2019-12-30 15:27:06.637 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:27:18.462 CST test@testdb [2224] [local] LOG: 00000: duration: 0.542 ms 2019-12-30 15:27:18.462 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1289 2019-12-30 15:27:36.559 CST test@testdb [5749] [local] LOG: 00000: duration: 0.639 ms 2019-12-30 15:27:36.559 CST test@testdb [5749] [local] LOCATION: exec_simple_query, postgres.c:1289 2019-12-30 15:27:44.470 CST test@testdb [2224] [local] LOG: 00000: duration: 3.592 ms 2019-12-30 15:27:44.470 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1289 2019-12-30 15:27:58.024 CST test@testdb [5749] [local] LOG: 00000: process 5749 still waiting for AccessShareLock on relation 16385 of database 16384 after 1000.615 ms at character 22 2019-12-30 15:27:58.024 CST test@testdb [5749] [local] DETAIL: Process holding the lock: 2224. Wait queue: 5749. 2019-12-30 15:27:58.024 CST test@testdb [5749] [local] LOCATION: ProcSleep, proc.c:1493 ...
log_statement
記錄哪些語句,可選項包括none, ddl, mod, all。
設(shè)置該參數(shù)為all,而log_min_duration_statement設(shè)置為600
執(zhí)行SQL
[local:/data/run/test]:5000 test@testdb=# select 1; ?column? ---------- 1 (1 row) [local:/data/run/test]:5000 test@testdb=# select pg_sleep(1); pg_sleep ---------- (1 row)
雖然select 1;執(zhí)行得很快,沒有超過600ms,但由于設(shè)置了該參數(shù)為all,因此也會在日志中出現(xiàn)
2019-12-30 15:32:41.934 CST @ [2163] LOG: 00000: received SIGHUP, reloading configuration files 2019-12-30 15:32:41.934 CST @ [2163] LOCATION: SIGHUP_handler, postmaster.c:2635 2019-12-30 15:32:41.939 CST @ [2163] LOG: 00000: parameter "log_min_duration_statement" changed to "600" 2019-12-30 15:32:41.939 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:32:41.939 CST @ [2163] LOG: 00000: parameter "log_statement" changed to "all" 2019-12-30 15:32:41.939 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOG: 00000: statement: select 1; 2019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1045 2019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOG: 00000: duration: 0.375 ms 2019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1289 2019-12-30 15:33:02.686 CST test@testdb [2224] [local] LOG: 00000: statement: select pg_sleep(1); 2019-12-30 15:33:02.686 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1045 2019-12-30 15:33:03.691 CST test@testdb [2224] [local] LOG: 00000: duration: 1005.297 ms 2019-12-30 15:33:03.691 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1289
把該參數(shù)設(shè)置為none,執(zhí)行同樣的SQL,日志輸出中只有select pg_sleep(1);
2019-12-30 15:35:06.740 CST @ [2163] LOG: 00000: received SIGHUP, reloading configuration files 2019-12-30 15:35:06.740 CST @ [2163] LOCATION: SIGHUP_handler, postmaster.c:2635 2019-12-30 15:35:06.743 CST @ [2163] LOG: 00000: parameter "log_statement" changed to "none" 2019-12-30 15:35:06.743 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:35:09.995 CST test@testdb [2224] [local] LOG: 00000: duration: 0.325 ms 2019-12-30 15:35:09.995 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1289 2019-12-30 15:35:12.441 CST test@testdb [2224] [local] LOG: 00000: duration: 1001.645 ms statement: select pg_sleep(1); 2019-12-30 15:35:12.441 CST test@testdb [2224] [local] LOCATION: exec_simple_query, postgres.c:1296
log_replication_commands
是否記錄復(fù)制命令。
log_temp_files
是否記錄大小超過該參數(shù)配置大小的臨時文件。在執(zhí)行大批量數(shù)據(jù)排序或者使用臨時表時可以用于診斷。
2019-12-30 15:40:37.992 CST @ [2163] LOG: 00000: received SIGHUP, reloading configuration files 2019-12-30 15:40:37.992 CST @ [2163] LOCATION: SIGHUP_handler, postmaster.c:2635 2019-12-30 15:40:37.993 CST @ [2163] LOG: 00000: parameter "log_replication_commands" changed to "on" 2019-12-30 15:40:37.993 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 2019-12-30 15:40:37.993 CST @ [2163] LOG: 00000: parameter "log_temp_files" changed to "1024" 2019-12-30 15:40:37.993 CST @ [2163] LOCATION: ProcessConfigFileInternal, guc-file.l:456 ###日志輸出 2019-12-30 15:42:07.897 CST test@testdb [2224] [local] LOG: 00000: temporary file: path "base/pgsql_tmp/pgsql_tmp2224.4", size 29507584 2019-12-30 15:42:07.897 CST test@testdb [2224] [local] LOCATION: ReportTemporaryFileUsage, fd.c:1285
log_timezone
是否記錄時區(qū)信息。
到此,相信大家對“分析PostgreSQL日志相關(guān)的配置參數(shù)log_XXX”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。