2015-02-12 231 views
2

如何编写查询结果消息,通知用户查询成功与否以及受影响记录的数量,以便在PostgreSQL中记录文件。日志postgres查询消息

我曾试图改变log_statement为“所有”和log_min_duration_statement为0,但我得到的是查询文本。

是否有可能将这些消息重定向到Windows操作系统中的日志文件?

+0

我不我认为这是可能的。 – 2015-02-12 09:51:44

+0

一定有办法:)我希望 – Ucef 2015-02-12 11:49:50

回答

0

您可以使用GET DIAGNOSTICS为。而如果你是一个超级用户,你可以保存结果到一个文件...

create table tablename(version int); 
insert into tablename select 9; 
do 
$$ 
declare 
rc text; 
begin 
    update tablename set version=version where false; 
    GET DIAGNOSTICS rc = ROW_COUNT; 
    raise info '%',' changed: '||rc; 

    update tablename set version=version where true; 
    GET DIAGNOSTICS rc = ROW_COUNT; 
    raise info '%',' changed: '||rc; 

    raise info '%','If you are superuser you can save result to a file...'; 
    execute $e$copy(select '$e$||rc||$e$') to '/tmp/roes.log'$e$; 
    raise exception '%','raiseing error to rollback changes'; 
end; 
$$ 
; 

和结果看起来:

INFO: changed: 0 
INFO: changed: 1 
INFO: If you are superuser you can save result to a file... 
ERROR: raiseing error to rollback changes 

********** Error ********** 

ERROR: raiseing error to rollback changes 
SQL state: P0001