千家信息网

分析PostgreSQL日志相关的配置参数log_XXX

发表于:2024-11-20 作者:千家信息网编辑
千家信息网最后更新 2024年11月20日,本篇内容主要讲解"分析PostgreSQL日志相关的配置参数log_XXX",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"分析PostgreSQL日志相关的
千家信息网最后更新 2024年11月20日分析PostgreSQL日志相关的配置参数log_XXX

本篇内容主要讲解"分析PostgreSQL日志相关的配置参数log_XXX",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"分析PostgreSQL日志相关的配置参数log_XXX"吧!

概览

在新initdb的数据库上查询pg_settings,可查询PostgreSQL中与log的参数包括:

[test@localhost ~]$ psqlExpanded 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_打头的参数有29个,下面从where、when、what这几个维度来解析这些参数,本节是第三部分,介绍what to log。

What to log

debug_print_parse
是否打印分析阶段的查询树,默认为off-不打印

debug_print_rewritten
是否打印查询重写阶段的查询树,默认为off-不打印

debug_print_plan
是否打印计划阶段的计划树,默认为off-不打印

debug_pretty_print
是否以优雅的方式打印?默认为on

log_checkpoints
是否记录checkpoint信息,默认为off。
在checkpoint发生时,PG会记录相关的信息

checkpoint_timeout = 1min   # range 30s-1dcheckpoint_completion_target = 0.5  # checkpoint target duration, 0.0 - 1.0#checkpoint_flush_after = 256kB   # measured in pages, 0 disables#checkpoint_warning = 30s   # 0 disableslog_checkpoints = on

插入数据,在检查点发生时,可以看到检查点的相关信息输出

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: time2019-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
记录登录信息,包括什么时候有连接请求,哪个用户连接的是哪个数据库;如连接失败,也会记录相关信息。

###修改配置信息[test@localhost ~]$ grep 'log_connections' $PGDATA/postgresql.conf# "postgres -c log_connections=on".  Some parameters can be changed at run timelog_connections = on[test@localhost ~]$ pg_ctl reloadserver 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.conflog_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
记录执行时间,仅记录执行时间,没有其他多余的信息。默认为on。

2019-12-30 14:36:49.149 CST [2224] LOG:  duration: 12.178 ms

log_error_verbosity
出现错误时的日志诊断信息级别,可选项包括:terse, default, or verbose,默认值为default
使用verbose,可以看到哪个源文件的哪一行,方便诊断

###创建测试对象[local:/data/run/test]:5000 test@testdb=# create view vw_tbl as select * from tbl;CREATE VIEW[local:/data/run/test]:5000 test@testdb=# ###terse2019-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###default2019-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 it2019-12-30 14:42:33.287 CST [2224] DETAIL:  view vw_tbl depends on table tbl2019-12-30 14:42:33.287 CST [2224] HINT:  Use DROP ... CASCADE to drop the dependent objects too.###verbose2019-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:4562019-12-30 14:43:32.790 CST [2224] ERROR:  2BP01: cannot drop table tbl because other objects depend on it2019-12-30 14:43:32.790 CST [2224] DETAIL:  view vw_tbl depends on table tbl2019-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
是否记录主机名称,默认为off。

log_line_prefix
每一行日志前的前缀。默认为'%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
%% - '%'

把该参数修改为'%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:4562019-12-30 15:20:40.148 CST test@testdb [2224] [local] ERROR:  2BP01: cannot drop table tbl because other objects depend on it2019-12-30 15:20:40.148 CST test@testdb [2224] [local] DETAIL:  view vw_tbl depends on table tbl2019-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(默认为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:4562019-12-30 15:27:18.462 CST test@testdb [2224] [local] LOG:  00000: duration: 0.542 ms2019-12-30 15:27:18.462 CST test@testdb [2224] [local] LOCATION:  exec_simple_query, postgres.c:12892019-12-30 15:27:36.559 CST test@testdb [5749] [local] LOG:  00000: duration: 0.639 ms2019-12-30 15:27:36.559 CST test@testdb [5749] [local] LOCATION:  exec_simple_query, postgres.c:12892019-12-30 15:27:44.470 CST test@testdb [2224] [local] LOG:  00000: duration: 3.592 ms2019-12-30 15:27:44.470 CST test@testdb [2224] [local] LOCATION:  exec_simple_query, postgres.c:12892019-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 222019-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。
设置该参数为all,而log_min_duration_statement设置为600
执行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;执行得很快,没有超过600ms,但由于设置了该参数为all,因此也会在日志中出现

2019-12-30 15:32:41.934 CST @ [2163]  LOG:  00000: received SIGHUP, reloading configuration files2019-12-30 15:32:41.934 CST @ [2163]  LOCATION:  SIGHUP_handler, postmaster.c:26352019-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:4562019-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:4562019-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:10452019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOG:  00000: duration: 0.375 ms2019-12-30 15:32:51.932 CST test@testdb [2224] [local] LOCATION:  exec_simple_query, postgres.c:12892019-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:10452019-12-30 15:33:03.691 CST test@testdb [2224] [local] LOG:  00000: duration: 1005.297 ms2019-12-30 15:33:03.691 CST test@testdb [2224] [local] LOCATION:  exec_simple_query, postgres.c:1289

把该参数设置为none,执行同样的SQL,日志输出中只有select pg_sleep(1);

2019-12-30 15:35:06.740 CST @ [2163]  LOG:  00000: received SIGHUP, reloading configuration files2019-12-30 15:35:06.740 CST @ [2163]  LOCATION:  SIGHUP_handler, postmaster.c:26352019-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:4562019-12-30 15:35:09.995 CST test@testdb [2224] [local] LOG:  00000: duration: 0.325 ms2019-12-30 15:35:09.995 CST test@testdb [2224] [local] LOCATION:  exec_simple_query, postgres.c:12892019-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
是否记录复制命令。

log_temp_files
是否记录大小超过该参数配置大小的临时文件。在执行大批量数据排序或者使用临时表时可以用于诊断。

2019-12-30 15:40:37.992 CST @ [2163]  LOG:  00000: received SIGHUP, reloading configuration files2019-12-30 15:40:37.992 CST @ [2163]  LOCATION:  SIGHUP_handler, postmaster.c:26352019-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:4562019-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 295075842019-12-30 15:42:07.897 CST test@testdb [2224] [local] LOCATION:  ReportTemporaryFileUsage, fd.c:1285

log_timezone
是否记录时区信息。

到此,相信大家对"分析PostgreSQL日志相关的配置参数log_XXX"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0