Search code examples
perlpostgresqldbi

Perl+Postgresql: a function does not return a value if RAISE NOTICE is present


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?


Solution

  • 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.