Get all notices of PostgreSQL's RAISE NOTICE

后端 未结 2 662
情书的邮戳
情书的邮戳 2020-12-21 05:43

I have big DB function which has multiple lines like this

RAISE NOTICE \'some step completed\';

I want to get all this notices in my PHP ap

相关标签:
2条回答
  • 2020-12-21 06:25

    According to the libpq notice documentation "The default notice handling function prints the message on stderr, but the application can override this behavior by supplying its own handling function."

    In your case the "application" (php itself) is overriding this behavior by specifying custom notice handler, called _php_pgsql_notice_handler:

    Line #1367: PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RESVAL_P(return_value));
    

    That means that PostgreSQL notices are not propagated further into stderr, but are captured and processed by that handler.

    In the handler itself (line #827) you can see that every time a notice is issued, php updates the variable holding it, and does not append the value to some array. Hence at the end only the last notice is present in that variable, obtainable by calling pg_last_notice().

    So, it looks like it is not possible to obtain previous PostgreSQL notices from within PHP.

    However, if you look further into the same notice handler, you would see that it is writing the notices to the error log, in case pgsql.ignore_notices = 0, pgsql.log_notices = 1 and E_NOTICE is included into the error_reporting. I guess with some php error handling functions juggling you will be able to obtain something.

    0 讨论(0)
  • 2020-12-21 06:38

    pg_last_notice() now accepts an optional parameter to specify an operation.

    This can be done with one of the following new constants:

    • PGSQL_NOTICE_LAST: to return last notice
    • PGSQL_NOTICE_ALL: to return all notices
    • PGSQL_NOTICE_CLEAR: to clear notices

    More info for Changed Functions in version 7.1 HERE.

    0 讨论(0)
提交回复
热议问题