Re: PG_RETURN_INT64 vs PointerGetDatum & ANALYZE - Mailing list pgsql-hackers

From Tom Lane
Subject Re: PG_RETURN_INT64 vs PointerGetDatum & ANALYZE
Date
Msg-id 18064.1152999572@sss.pgh.pa.us
Whole thread Raw
In response to PG_RETURN_INT64 vs PointerGetDatum & ANALYZE  ("Sergey E. Koposov" <math@sai.msu.ru>)
List pgsql-hackers
"Sergey E. Koposov" <math@sai.msu.ru> writes:
> C function definition:

> PG_FUNCTION_INFO_V1(pgq3c_ang2ipix);
> Datum pgq3c_ang2ipix(PG_FUNCTION_ARGS)
> {
>      static q3c_ipix_t ipix_buf;
>      ipix_buf ++;
>      elog(WARNING,"%lld",ipix_buf);
>      return PointerGetDatum((&ipix_buf));
> }

This code is wrong on its face: it can't support multiple calls to the
function within a single query, because each call will damage the
previous call's result.  Try something like
select pgq3c_ang2ipix(...), pgq3c_ang2ipix(...) from ...

and you'll get bizarre behavior.

You need to return a palloc'd result, rather than returning pointers to
the same static variable on successive calls.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Josh Berkus
Date:
Subject: Re: postgresql.conf basic analysis tool
Next
From: Hannu Krosing
Date:
Subject: Re: Forcing wal rotation