Thread: PL/pgSQL NOT NULL variables

PL/pgSQL NOT NULL variables

From
Richard Poole
Date:
This is my first attempt at using PL/pgSQL so I may be carrying around
a grotesque misconception of some sort...

I can't get NOT NULL variables to work at all in PL/pgSQL. Here's a
simple function which uses a default value for a variable:

test=> create function t1() returns integer as '
test'> declare foo integer := 42;
test'> begin return foo; end;
test'> ' language 'plpgsql';
CREATE
test=> select t1();
 t1
----
 42
(1 row)


No problem at all. But if I create a function exactly identical except
for making the variable NOT NULL:

test=> create function t2() returns integer as '
test'> declare foo integer not null := 42;
test'> begin return foo; end;
test'> ' language 'plpgsql';
CREATE
test=> select t2();
ERROR:  NULL assignment to variable 'foo' declared NOT NULL

I've tried using DEFAULT instead of :=, which the docs suggest should
be equivalent, and the same thing happens. Similarly when I use type
"text". As there are no NOT NULL variables in the examples or the
regression tests, I suspect I may have found a bug. Does anybody have
NOT NULL working in a PL/pgSQL function? Can anybody shed any light
on what I may be doing wrong?

7.02 on Debian GNU/Linux, FWIW.

Richard

Re: PL/pgSQL NOT NULL variables

From
Tom Lane
Date:
Richard Poole <richard.poole@vi.net> writes:
> I can't get NOT NULL variables to work at all in PL/pgSQL.

Looks like a bug to me too ... will look into it.

            regards, tom lane

Re: PL/pgSQL NOT NULL variables

From
Tom Lane
Date:
Richard Poole <richard.poole@vi.net> writes:
> I can't get NOT NULL variables to work at all in PL/pgSQL.

Doesn't look like they've ever worked :-(.  If you need a patch for
7.0.*, line 1907 of src/pl/plpgsql/src/pl_exec.c should read

            if (*isNull && var->notnull)

not

            if (isNull && var->notnull)


            regards, tom lane