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

发布于 2024-07-21 04:12:15 字数 801 浏览 10 评论 0原文

我注意到,当我使用 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;

我是否遗漏了某些内容或此类行为是设计使然?

I noticed that when I call a PL/PgSQL or PL/Perl function from a Perl script using DBI, it does not return a value if a RAISE NOTICE or elog(NOTICE) is used in the function. To illustrate:

A simple table:

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

A simple function:

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'

A piece of Perl script:

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

As it is, it prints the value from the table.

But I get no result if I add RAISE NOTICE as follows:

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

Am I missing something or such behavior is by design?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-07-28 04:12:15

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

client_min_messages(字符串)

<块引用>

控制发送到客户端的消息级别。 有效值为 DEBUG5DEBUG4DEBUG3DEBUG2DEBUG1>日志通知警告错误致命和恐慌。 每个级别都包含其后的所有级别。 级别越晚,发送的消息越少。 默认为注意。 请注意,LOG 此处的排名与 log_min_messages 中的排名不同。

Check the client_min_messages setting in your database server's postgresql.conf file. From the PostgreSQL 8.3 docs:

client_min_messages (string)

Controls which message levels are sent to the client. Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, LOG, NOTICE, WARNING, ERROR, FATAL, and PANIC. Each level includes all the levels that follow it. The later the level, the fewer messages are sent. The default is NOTICE. Note that LOG has a different rank here than in log_min_messages.

最偏执的依靠 2024-07-28 04:12:15

我无法使用 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

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

I can't reproduce this, using Debian's Perl 5.10, DBI 1.605 and DBD::Pg 2.8.7 against PostgreSQL 8.3.7. I get the notice printed out as expected.

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'
          ];

I suggest isolating your problem to a small script (like above) and running it with DBI_TRACE set fairly high any seeing what differences you see. Maybe also looking at the release notes for DBD::Pg and seeing if they mention it maybe having been confused by these in the past. With DBI_TRACE=10 I see this:

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

So you should be looking for something like that in your own output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文