Perl + Postgresql:如果出现RAISE NOTICE,函数不会返回值

时间:2009-05-13 17:42:18

标签: perl postgresql dbi

我注意到当我使用DBI从Perl脚本调用PL / PgSQL或PL / Perl函数时,如果在函数中使用了RAISE NOTICE或elog(NOTICE),则它不会返回值。举例说明:

一个简单的表格:

CREATE TABLE "public"."table1" (
  "fld" INTEGER
) WITHOUT OIDS;

一个简单的功能:

CREATE OR REPLACE FUNCTION "public"."function1" () RETURNS integer AS
$body$
DECLARE
  myvar INTEGER;
BEGIN
  SELECT INTO myvar fld FROM table1 LIMIT 1;
  RETURN myvar;
END;
$body$
LANGUAGE 'plpgsql'

一段Perl脚本:

use DBI;
...
my $ref = $dbh->selectcol_arrayref('SELECT function1()');
print $$ref[0];

实际上,它会打印表中的值。

但是如果按照以下方式添加RAISE NOTICE,我就没有结果:

SELECT INTO myvar fld FROM table1 LIMIT 1;
RAISE NOTICE 'Testing';
RETURN myvar;

我是否遗漏了某些东西,或者这种行为是设计出来的?

2 个答案:

答案 0 :(得分:2)

检查数据库服务器的postgresql.conf文件中的client_min_messages设置。来自PostgreSQL 8.3文档:

  

client_min_messages(字符串)

     
    

控制将哪些消息级别发送到客户端。有效值为DEBUG5DEBUG4DEBUG3DEBUG2DEBUG1LOGNOTICEWARNINGERRORFATAL和PANIC。每个级别包括其后的所有级别。级别越晚,发送的消息越少。默认值为NOTICE。请注意,此处LOG的排名与log_min_messages中的排名不同。

  

答案 1 :(得分:1)

我无法使用Debian的Perl 5.10,DBI 1.605和DBD :: Pg 2.8.7对PostgreSQL 8.3.7重现这一点。我按预期打印出通知。

steve@steve@[local] =# create or replace function public.function1() returns integer language 'plpgsql' as $$ declare myvar integer; begin select into myvar fld from table1 limit 1; raise notice 'Testing'; return myvar; end; $$;
CREATE FUNCTION
steve@steve@[local] =#
[1]+  Stopped                 psql --cluster 8.3/steve
steve@arise:~$ DBI_TRACE=1 perl -MData::Dumper -MDBI -e '$dbh = DBI->connect(qw|dbi:Pg:dbname=steve;port=5433;host=/tmp steve steve|, {RaiseError=>1,PrintError=>0}); print Data::Dumper->new([$dbh->selectcol_arrayref("SELECT function1()")], [qw|result|])->Dump'
    DBI 1.605-ithread default trace level set to 0x0/1 (pid 5739) at DBI.pm line 273 via -e line 0
    Note: perl is running without the recommended perl -w option
    -> DBI->connect(dbi:Pg:dbname=steve;port=5433;host=/tmp, steve, ****, HASH(0x1c9ddf0))
    -> DBI->install_driver(Pg) for linux perl=5.010000 pid=5739 ruid=1000 euid=1000
       install_driver: DBD::Pg version 2.8.7 loaded from /usr/lib/perl5/DBD/Pg.pm
    <- install_driver= DBI::dr=HASH(0x1e06a68)
    !! warn: 0 CLEARED by call to connect method
    <- connect('dbname=steve;port=5433;host=/tmp', 'steve', ...)= DBI::db=HASH(0x1fd8e08) at DBI.pm line 638
    <- STORE('RaiseError', 1)= 1 at DBI.pm line 690
    <- STORE('PrintError', 0)= 1 at DBI.pm line 690
    <- STORE('AutoCommit', 1)= 1 at DBI.pm line 690
    <- STORE('Username', 'steve')= 1 at DBI.pm line 693
    <> FETCH('Username')= 'steve' ('Username' from cache) at DBI.pm line 693
    <- connected('dbi:Pg:dbname=steve;port=5433;host=/tmp', 'steve', ...)= undef at DBI.pm line 699
    <- connect= DBI::db=HASH(0x1fd8e08)
    <- STORE('dbi_connect_closure', CODE(0x1da2280))= 1 at DBI.pm line 708
NOTICE:  Testing
    <- selectcol_arrayref('SELECT function1()')= ( [ '2' ] ) [1 items] at -e line 1
$result = [
            '2'
          ];

我建议将您的问题隔离到一个小脚本(如上所述)并运行DBI_TRACE设置相当高,看看您看到的差异。也许还要查看DBD :: Pg的发行说明,看看他们是否提到它可能在过去被这些混淆了。使用DBI_TRACE=10我看到了:

PQexec
Begin pg_warn (message: NOTICE:  Testing
 DBIc_WARN: 1 PrintWarn: 1)
NOTICE:  Testing
End pg_warn
Begin _sqlstate

所以你应该在自己的输出中寻找类似的东西。