Thread: Trigger function returning null

Trigger function returning null

From
Thomas Hallgren
Date:
In backend/commands/trigger.c the following can be found:
    /*     * Trigger protocol allows function to return a null pointer,     * but NOT to set the isnull result flag.
*/    if (fcinfo.isnull)        ereport(ERROR,
 

Why?

Regards,

Thomas Hallgren



Re: Trigger function returning null

From
Tom Lane
Date:
Thomas Hallgren <thhal@mailblocks.com> writes:
> In backend/commands/trigger.c the following can be found:
>      /*
>       * Trigger protocol allows function to return a null pointer,
>       * but NOT to set the isnull result flag.
>       */
>      if (fcinfo.isnull)
>          ereport(ERROR,

> Why?

Why not?

The real answer is "it's historical and I didn't see any need to
change it".  But one could argue that a function returning NULL
doesn't know it's supposed to be a trigger.
        regards, tom lane


Re: Trigger function returning null

From
Tom Lane
Date:
Thomas Hallgren <thhal@mailblocks.com> writes:
> Perhaps you should either change this behavior or make a note it in 
> "Writing Trigger Functions in C"?

Okay, I've changed the text thusly:

diff -r1.35 trigger.sgml
434,435c434,437
<     A trigger function must return either <symbol>NULL</> or a
<     <structname>HeapTuple</> pointer.  Be careful to return either
---
>     A trigger function must return either a
>     <structname>HeapTuple</> pointer or a <symbol>NULL</> pointer
>     (<emphasis>not</> a SQL NULL, that is, do not set isNull true).
>     Be careful to return either
        regards, tom lane


Re: Trigger function returning null

From
Thomas Hallgren
Date:
Tom Lane wrote:

>Why not?
>
>The real answer is "it's historical and I didn't see any need to
>change it".  But one could argue that a function returning NULL
>doesn't know it's supposed to be a trigger.
>  
>
The reason I ask is that this behavior just bit me in PL/Java. Triggers 
returning null didn't work. I've fixed it at my end by simply forcing 
the isnull to false for triggers. I just wanted to know if there was 
some deeper thought behind this that could result in side effects.

Perhaps you should either change this behavior or make a note it in 
"Writing Trigger Functions in C"? Even if the documented example use a 
local isnull, it's not evident that you have to do that.

Regards,

Thomas Hallgren