1

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

目前正在以下列方式打印日志。如果我们像这样启用完整的日志记录,分配的 HDD 空间将在不到 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'

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

重申一下 - PostgreSQL 中是否有任何方法可以仅记录特定表的信息?

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

1 回答 1

5

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

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

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

ALTER USER my_queue_user SET log_statement = 'all';

或者:

ALTER DATABASE mydb SET log_statement = 'all';

请注意,用户可以用SETor覆盖这些SET LOCAL,因此它们适合审计。

于 2013-04-29T11:36:52.277 回答