Re: PG_RETURN_INT64 vs PointerGetDatum & ANALYZE

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Sergey E(dot) Koposov" <math(at)sai(dot)msu(dot)ru>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PG_RETURN_INT64 vs PointerGetDatum & ANALYZE
Date: 2006-07-15 21:39:32
Message-ID: 18064.1152999572@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Sergey E. Koposov" <math(at)sai(dot)msu(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 2006-07-15 22:04:40 Re: Forcing wal rotation
Previous Message Josh Berkus 2006-07-15 20:31:32 Re: postgresql.conf basic analysis tool