postgresql(8.2)的配置文件中有一个参数log_min_duration_statement,意思是只log执行时间大于设定值的语句,如果设为0,表示log所有语句;如果设为-1,表示不log任何语句。
看起来,这个配置选项对性能的调整是很有用的,比如可以设置:
log_min_duration_statement = 1000
则只log执行时间大于1s的语句,重点优化这些sql语句就好了。
然而,奇怪的,这个选项不太容易生效!经过反复试验,原来需要如下配置:
#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = off
log_connections = off
#log_disconnections = off
log_duration = off
log_line_prefix = '%t [%p]: [%l-1] ' # Special values:
# %u = user name
# %d = database name
# %r = remote host and port
# %h = remote host
# %p = PID
# %t = timestamp (no milliseconds)
# %m = timestamp with milliseconds
# %i = command tag
# %c = session id
# %l = session line number
# %s = session start timestamp
# %x = transaction id
# %q = stop here in non-session
# processes
# %% = '%'
# e.g. '<%u%%%d> '
log_statement = 'none' # none, mod, ddl, all
#log_statement = 'all' # none, mod, ddl, all
#log_hostname = off
注意看上面的其中两个选项的设置:
log_duration = off
log_statement = 'none'
这两个选项的意思是不log任何sql语句和执行时间,但是恰恰是关闭了这两个,log_min_duration_statement才会生效!可能postgresql内部 对这两个选项做了“互斥”处理吧。
其他回答(1)
最新回答
(1条回答)
七里田间
1级
2017-10-06 回答
postgresql的pl/pgsql语言是支持动态sql语句的(说execute immediate的是ecpg所支持的)。但是,要记得重要的一点: 是在pl/pgsql语言中支持。而pl/pgsql语言一个块结构的语言,它以begin ... end为块的开始与结束标识。这也就是说,要执行动态sql语句,就必须放到begin ... end块中,而不要想实现一个单独的动态sql语句。在sql server中,倒是可以轻松的实现,我们可以直接执行一个这样的动态sql:
execute?sp_executesql?n'select?1?as?val'而在postgresql中,就不要有此想法了。当然,sql server的这种动态sql语句的执行方法也有其局限与不便的地方。
在pl/pgsql中,执行动态sql的格式如下(摘录自说明文档):
execute?command-string?[?into?[strict]?target?]?[?using?expression?[,?...?]?];其中,
command-string就是要执行的动态sql语句(一定要记住:这里是sql语句,不是pl/pgsql语句,像raise notice就不能使用);
into子句是把sql查询到的值赋给into指定的变量;
using子句是前面的command-string中替代变量($1, $2, ...)的赋值;
示例:
do?$$
declare
v_c1?integer;
v_c2?integer;
begin
execute?'select?count(*)?as?c1,?count(*)?as?c2?from?(select?1?as?idx?union?select?11?as?idx?union?select?21?as?idx)?s?where?idx?>?$1'?
into?v_c1,?v_c2
using?10;
raise?notice?'%,?%',?v_c1,?v_c2;