获取PostgreSQL的RAISE NOTICE的所有通知

ale*_*x23 3 php postgresql

我有大的DB功能,有这样的多行

RAISE NOTICE 'some step completed';
Run Code Online (Sandbox Code Playgroud)

我希望在我的PHP应用程序中获得所有这些通知.我发现只有pg_last_notice()返回最后通知的函数.

有什么方法可以得到所有通知吗?


示例:DB功能:

CREATE OR REPLACE FUNCTION do_smth()
  RETURNS void AS
$BODY$

BEGIN

-- some actions
RAISE NOTICE 'Result of the actions:...';
-- some other actions
RAISE NOTICE 'Result of the other actions..';

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Run Code Online (Sandbox Code Playgroud)

PHP代码:

<?php
// ...
$db->exec("SELECT do_smth()"); // executing DB function

$last_notice = pg_last_notice($db_connection); 
// returns 'NOTICE: Result of the other actions..'
Run Code Online (Sandbox Code Playgroud)

Kou*_*rev 7

根据libpq通知文档 "默认通知处理函数在stderr上打印消息,但应用程序可以通过提供自己的处理函数来覆盖此行为."

在您的情况下,"应用程序"(php本身)通过指定自定义通知处理程序(称为_php_pgsql_notice_handler)来覆盖此行为:

Line #1367: PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RESVAL_P(return_value));
Run Code Online (Sandbox Code Playgroud)

这意味着PostgreSQL通知不会进一步传播到stderr,而是由该处理程序捕获和处理.

在处理程序本身(第827行)中,您可以看到每次发出通知时,php都会更新包含它的变量,并且不会将值附加到某个数组.因此,最后只有最后一个通知存在于该变量中,可通过以下方式获得calling pg_last_notice().

因此,看起来不可能从PHP中获取以前的PostgreSQL通知.

但是,如果您进一步查看相同的通知处理程序,您会看到它正在将通知写入错误日志,以防万一pgsql.ignore_notices = 0,pgsql.log_notices = 1并且E_NOTICE包含在error_reporting中.我想用一些php错误处理函数杂耍你将能够获得一些东西.