目录
1、说明. 1
2、开启的参数. 1
3、修改配置文件. 1
4、使数据库重新加载配置文件. 2
4.1、用超级用户运行. 2
4.2、用UNIX的kill手动发起HUP信号. 2
4.3、使用pg_ctl命令触发SIGHUP信号. 2
5、检查配置文件是否重新加载. 2
6、会话中临时改参数. 4
7、查看审计日志. 4
pg自带审计日志功能,需要按需开启。
postgres=# select name, context from pg_settings t
postgres-# where t.name in ('logging_collector','log_destination','log_truncate_on_rotation','log_connections','log_disconnections','log_error_verbosity','log_statement','log_min_duration_statement','log_checkpoints','log_lock_waits','deadlock_timeout');
name | context
----------------------------+-------------------
deadlock_timeout | superuser
log_checkpoints | sighup
log_connections | superuser-backend
log_destination | sighup
log_disconnections | superuser-backend
log_error_verbosity | superuser
log_lock_waits | superuser
log_min_duration_statement | superuser
log_statement | superuser
log_truncate_on_rotation | sighup
logging_collector | postmaster
(11 行记录)
postgres=#
internal: 编译期间的设置,只有重新编译才能生效。
postmaster: 只有服务重启才能生效。
sighup: 给服务器发送HUP信号会是服务器重新加载postgresql.conf配置,可以立即生效。
backend: 与sighup类似,但是不影响正在运行的会话,只在新会话中生效
superuser: 使用superuser(如postgres)才能更改,不用重新加载所有配置即可生效。
user: 单个会话用户可以在任意时间做修改,只会影响该会话。
postgresql.conf默认位于$PGDATA目录下。
vi /usr/data/pgsql/data/postgresql.conf
直接修改该文件中的项为下面的值,没有的参数新增即可。
logging_collector = on
log_destination = 'csvlog'
log_truncate_on_rotation = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose
log_statement = ddl
log_min_duration_statement = 60s
log_checkpoints = on
log_lock_waits = on
deadlock_timeout = 1s
重新加载数据库配置的方法有三种:
postgres=# SELECT pg_reload_conf();
$kill -HUP PID
su - postgres
$pg_ctl reload -D $PGDATA -s
推介第一和第三种。
postgres@memberpostsql1[/usr/data/pgsql/data]$psql -h xxx.xxx.133.74 -p 5432 postgres postgres
用户 postgres 的口令:
psql (11.4)
输入 "help" 来获取帮助信息.
postgres=# show logging_collector;
logging_collector
-------------------
on
(1 行记录)
postgres=# show log_destination;
log_destination
-----------------
csvlog
(1 行记录)
postgres=# show log_truncate_on_rotation
postgres-# ;
log_truncate_on_rotation
--------------------------
on
(1 行记录)
postgres=# show log_connections;
log_connections
-----------------
on
(1 行记录)
postgres=# show log_disconnections;
log_disconnections
--------------------
on
(1 行记录)
postgres=# show log_error_verbosity;
log_error_verbosity
---------------------
verbose
(1 行记录)
postgres=# show log_statement;
log_statement
---------------
ddl
(1 行记录)
postgres=# show log_min_duration_statement;
log_min_duration_statement
----------------------------
1min
(1 行记录)
postgres=# show log_checkpoints;
log_checkpoints
-----------------
on
(1 行记录)
postgres=# show log_lock_waits;
log_lock_waits
----------------
on
(1 行记录)
postgres=# show deadlock_timeout;
deadlock_timeout
------------------
1s
(1 行记录)
postgres=#
postgres=# set deadlock_timeout = '1s';
SET
postgres=# show deadlock_timeout;
deadlock_timeout
------------------
1s
(1 行记录)
这样只在当前会话中生效。
postgres@memberpostsql1[/usr/data/pgsql/data]$psql -h xxx.xxx.133.74 -p 5432 postgres postgres
用户 postgres 的口令:
psql (11.4)
输入 "help" 来获取帮助信息.
postgres=# show log_directory;
log_directory
----------------------
/usr/data/pgsql/logs
(1 行记录)
postgres=#
postgres@memberpostsql1[/usr/data/pgsql/logs]$cd /usr/data/pgsql/logs
postgres@memberpostsql1[/usr/data/pgsql/logs]$ll
总用量 99840
-rw-------. 1 postgres postgres 14552915 8月 30 23:59 postgresql-Fri.csv
-rw-------. 1 postgres postgres 0 8月 30 00:00 postgresql-Fri.log
-rw-------. 1 postgres postgres 14349601 9月 2 23:59 postgresql-Mon.csv
-rw-------. 1 postgres postgres 0 9月 2 00:00 postgresql-Mon.log
-rw-------. 1 postgres postgres 14248029 8月 31 23:59 postgresql-Sat.csv
-rw-------. 1 postgres postgres 0 8月 31 00:00 postgresql-Sat.log
-rw-------. 1 postgres postgres 14359044 9月 1 23:59 postgresql-Sun.csv
-rw-------. 1 postgres postgres 0 9月 1 00:00 postgresql-Sun.log
-rw-------. 1 postgres postgres 14596250 8月 29 23:59 postgresql-Thu.csv
-rw-------. 1 postgres postgres 0 8月 29 00:00 postgresql-Thu.log
-rw-------. 1 postgres postgres 14592287 9月 4 00:00 postgresql-Tue.csv
-rw-------. 1 postgres postgres 0 9月 3 00:00 postgresql-Tue.log
-rw-------. 1 postgres postgres 15497997 9月 4 20:23 postgresql-Wed.csv
-rw-------. 1 postgres postgres 0 9月 4 00:00 postgresql-Wed.log
我们设置的审计日志格式是csv,所以.csv的文件就是审计日志文件。
postgres@memberpostsql1[/usr/data/pgsql/logs]$tail -n 20 postgresql-Wed.csv
FROM (SELECT
(SELECT sum(tup_inserted) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""插入"",
(SELECT sum(tup_updated) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""更新"",
(SELECT sum(tup_deleted) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""删除""
) t
UNION ALL
SELECT 'to_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(tup_fetched) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""获取"",
(SELECT sum(tup_returned) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""返回""
) t
UNION ALL
SELECT 'bio_stats' AS chart_name, row_to_json(t) AS chart_data
FROM (SELECT
(SELECT sum(blks_read) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""读取"",
(SELECT sum(blks_hit) FROM pg_stat_database WHERE datname = (SELECT datname FROM pg_database WHERE oid = 17900)) AS ""命中""
) t
",,"backend_read_statsfile, pgstat.c:5621","pgAdmin 4 - DB:merchant"
2019-09-04 20:25:12.894 CST,,,120670,,5d6fad1e.1d75e,1,,2019-09-04 20:25:02 CST,5/0,0,日志,00000,"由于统计收集器无响应而使用旧的统计信息来代替当前的统计信息",,,,,,,,"backend_read_statsfile, pgstat.c:5621",""
2019-09-04 20:25:14.579 CST,,,49742,,5d171dc8.c24e,58888,,2019-06-29 16:14:00 CST,1/0,0,日志,00000,"由于统计收集器无响应而使用旧的统计信息来代替当前的统计信息",,,,,,,,"backend_read_statsfile, pgstat.c:5621",""
postgres@memberpostsql1[/usr/data/pgsql/logs]$