是否可以在PostgreSQL的日志活动中排除特定用户?


10

我需要监视数据库中用户的活动。我在中设置了以下参数postgresql.conf

log_min_duration_statement = 0
log_connections = on
log_disconnections = on
log_line_prefix = '%t %a %d %h %u |'

但是,我意识到大多数日志文件都充满了postgres用户执行的语句,我为维护任务编写的脚本使用了这些语句:重新计算实例化视图,pg_dump,pg_restore,将视图提取为列表文件等,其结果是每日的大小超过12 Mb的日志文件。

有没有办法从日志中排除特定用户的活动?


3
IIRC ALTER USER ... SET log_connections = off
Craig Ringer

很棒,我会做到的。
塞巴斯蒂安·克莱门特

1
@CraigRinger以dezso(超级用户)身份登录,ERROR: parameter "log_connections" cannot be set after connection start尝试访问时总是得到ALTER ROLE bob SET log_connections = off
dezso 2015年

@dezso德拉特。我为什么获得IIRC资格的部分原因...不确定。
Craig Ringer 2015年

1
@CraigRinger我昨天尝试过这个,认为有可能-然后赞成这个问题:D
dezso 2015年

Answers:


5

使用该ALTER ROLE ... SET parameter;命令,可以定制用户特定的日志参数。请注意,该参数仅在注销后生效。

设置log_min_duration_statement = -1(第一次登录):

psql控制台

P:\>psql -U postgres -h 132.156.208.45 -d my_db

my_db=# SHOW log_min_duration_statement;
 log_min_duration_statement
----------------------------
 0
(1 ligne)

my_db=# SELECT COUNT(*) FROM organisms;
 count
-------
   153
(1 ligne)

my_db=# ALTER ROLE postgres SET log_min_duration_statement=-1;
ALTER ROLE

my_db=# SHOW log_min_duration_statement;
 log_min_duration_statement
----------------------------
 0
(1 ligne)

my_db=# SELECT COUNT(*) FROM mv_rings;
 count
--------
 115270
(1 ligne)

my_db=# \q

记录结果:

请注意,在该会话中,即使设置了log_min_duration_statement = -1,所有语句在日志中都是可见的。

2015-10-15 14:14:01 EDT [unknown] my_db 123.456.789.012 postgres |LOG:  connection authorized: user=postgres database=my_db
2015-10-15 14:14:08 EDT psql my_db 123.456.789.012 postgres |LOG:  duration: 0.000 ms  statement: SHOW log_min_duration_statement;
2015-10-15 14:15:26 EDT psql my_db 123.456.789.012 postgres |LOG:  duration: 32.000 ms  statement: SELECT COUNT(*) FROM organisms;
2015-10-15 14:15:45 EDT psql my_db 123.456.789.012 postgres |LOG:  duration: 31.000 ms  statement: ALTER ROLE postgres SET log_min_duration_statement=-1;
2015-10-15 14:16:08 EDT psql my_db 123.456.789.012 postgres |LOG:  duration: 0.000 ms  statement: SHOW log_min_duration_statement;
2015-10-15 14:17:10 EDT psql my_db 123.456.789.012 postgres |LOG:  duration: 2059.000 ms  statement: SELECT COUNT(*) FROM mv_rings;
2015-10-15 14:17:29 EDT psql my_db 123.456.789.012 postgres |LOG:  disconnection: session time: 0:03:27.450 user=postgres database=my_db host=123.456.789.012 port=65269

看到log_min_duration_statement的效果(第二次登录):

psql控制台

P:\>psql -U postgres -h 132.156.208.45 -d my_db

my_db=# SHOW log_min_duration_statement;
 log_min_duration_statement
----------------------------
 -1
(1 ligne)

my_db=# SELECT COUNT(*) FROM germplasms;
 count
--------
 475290
(1 ligne)

my_db=# \q

记录结果:

与预期的一样,没有记录任何语句。

2015-10-15 14:17:44 EDT [unknown] my_db 123.456.789.012 postgres |LOG:  connection authorized: user=postgres database=my_db
2015-10-15 14:20:27 EDT psql my_db 123.456.789.012 postgres |LOG:  disconnection: session time: 0:02:43.333 user=postgres database=my_db host=123.456.789.012 port=49372
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.