Thread: detecting NULL column in SPI function

detecting NULL column in SPI function

From
Louis-David Mitterrand
Date:
Hello,

In a SPI function I have this code snippet:

    double startprice = *DatumGetFloat64(SPI_getbinval(
                    auction_tt->vals[i], auction_tt->tupdesc,
                    SPI_fnumber(auction_tt->tupdesc, "startprice"), &isnull));

To extract a value from the startprice column. It only works if
"startprice" is not NULL, otherwise the backend crashes.

In the provided example SPI functions I couldn't determine a good way of
testing for NULL on columns. Should I first try to return the Datum,
test it for NULL, then run DatumGetFloat64() on it? Isn't there a
simpler way?

Thanks in advance for your help, cheers,

--
Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.org

 Hand, n.:
         A singular instrument worn at the end of a human arm and
         commonly thrust into somebody's pocket.
                         -- Ambrose Bierce, "The Devil's Dictionary"

Re: detecting NULL column in SPI function

From
Tom Lane
Date:
Louis-David Mitterrand <cunctator@apartia.ch> writes:
> In the provided example SPI functions I couldn't determine a good way of
> testing for NULL on columns. Should I first try to return the Datum,
> test it for NULL, then run DatumGetFloat64() on it? Isn't there a
> simpler way?

Yes.  No.  The Datum is a pointer, so if you indirect through it
before having checked for null-ness, you WILL get a crash.

            regards, tom lane