仅在特定表中启用PostgreSQL中的日志记录

时间:2013-04-29 10:39:03

标签: java database postgresql

我想知道是否可以仅针对特定表启用PostgreSQL中的日志记录(即使用表级粒度进行日志记录)?

目前正在按以下方式打印日志。如果我们启用这样的完整日志记录,分配的硬盘空间将在不到30分钟的时间内完成。

GMT_jbpm_LOG:  00000: duration: 0.104 ms  bind <unnamed>: select instances0_.PROCESSINSTANCE_ as PROCESSI4_1_, instances0_.ID_ as ID1_1_, instances0_.NAME_ as NAME6_1_, instances0_.ID_ as ID1_54_0_, instances0_.VERSION_ as VERSION3_54_0_, instances0_.PROCESSINSTANCE_ as PROCESSI4_54_0_, instances0_.TASKMGMTDEFINITION_ as TASKMGMT5_54_0_, instances0_.CLASS_ as CLASS2_54_0_ from JBPM_MODULEINSTANCE instances0_ where instances0_.PROCESSINSTANCE_=$1
2013-03-27 09:42:09.039 GMT_jbpm_DETAIL:  parameters: $1 = '9646163'
2013-03-27 09:42:09.039 GMT_jbpm_LOCATION:  exec_bind_message, postgres.c:1784
2013-03-27 09:42:09.039 GMT_amq_LOG:  00000: duration: 0.023 ms
2013-03-27 09:42:09.039 GMT_amq_LOCATION:  exec_execute_message, postgres.c:1988
2013-03-27 09:42:09.039 GMT_jbpm_LOG:  00000: execute <unnamed>: select instances0_.PROCESSINSTANCE_ as PROCESSI4_1_, instances0_.ID_ as ID1_1_, instances0_.NAME_ as NAME6_1_, instances0_.ID_ as ID1_54_0_, instances0_.VERSION_ as VERSION3_54_0_, instances0_.PROCESSINSTANCE_ as PROCESSI4_54_0_, instances0_.TASKMGMTDEFINITION_ as TASKMGMT5_54_0_, instances0_.CLASS_ as CLASS2_54_0_ from JBPM_MODULEINSTANCE instances0_ where instances0_.PROCESSINSTANCE_=$1
2013-03-27 09:42:09.039 GMT_jbpm_DETAIL:  parameters: $1 = '9646163'
2013-03-27 09:42:09.039 GMT_jbpm_LOCATION:  exec_execute_message, postgres.c:1918
2013-03-27 09:42:09.039 GMT_jbpm_LOG:  00000: duration: 0.053 ms
2013-03-27 09:42:09.039 GMT_jbpm_LOCATION:  exec_execute_message, postgres.c:1988
2013-03-27 09:42:09.039 GMT_bnw_LOG:  00000: duration: 0.068 ms  parse <unnamed>: select workflowde0_.workflow_definition_id as workflow1_14_0_, workflowde0_.created as created14_0_, workflowde0_.deleted as deleted14_0_, workflowde0_.name as name14_0_, workflowde0_.version as version14_0_, workflowde0_.workspace_id as workspace6_14_0_ from workflow_definition workflowde0_ where workflowde0_.workflow_definition_id=$1
2013-03-27 09:42:09.039 GMT_bnw_LOCATION:  exec_parse_message, postgres.c:1367
2013-03-27 09:42:09.039 GMT_bnw_LOG:  00000: duration: 0.059 ms  parse <unnamed>: update worker set created=$1, deleted=$2, name=$3, full_name=$4, worker_type=$5, max_jobs_in_progress=$6, max_jobs_queued=$7, jobs_queued=$8, workspace_id=$9 where worker_id=$10
2013-03-27 09:42:09.039 GMT_bnw_LOCATION:  exec_parse_message, postgres.c:1367
2013-03-27 09:42:09.039 GMT_bnw_LOG:  00000: duration: 0.077 ms  bind <unnamed>: select workflowde0_.workflow_definition_id as workflow1_14_0_, workflowde0_.created as created14_0_, workflowde0_.deleted as deleted14_0_, workflowde0_.name as name14_0_, workflowde0_.version as version14_0_, workflowde0_.workspace_id as workspace6_14_0_ from workflow_definition workflowde0_ where workflowde0_.workflow_definition_id=$1
2013-03-27 09:42:09.039 GMT_bnw_DETAIL:  parameters: $1 = '7089580'

我想要做的是为我的名为worker表的表启用日志记录,但是请避免记录有关我数据库中任何其他表的事件。

重申 - PostgreSQL中是否有任何方法只记录特定表的信息?

UPDATE worker SET created=$1, deleted=$2, name=$3;

1 个答案:

答案 0 :(得分:1)

在PostgreSQL中进行表级别日志记录的唯一方法是使用触发器。您可能会发现PostgreSQL wiki中的audit trigger可以提供信息。如果您只想记录语句文本,那么使用语句文本执行RAISE NOTICE(或某个较低的日志级别)的语句级触发器就足够了。

在访问该表之前,您可以只通过SQL SET log_statement = 'all',但我怀疑这不是您想要的。

或者,使用专用数据库或专用用户以及SET用户或数据库上的参数。例如:

ALTER USER my_queue_user SET log_statement = 'all';

或:

ALTER DATABASE mydb SET log_statement = 'all';

请注意,用户只需使用SETSET LOCAL覆盖这些内容,因此 适合审核。