Sample Header Ad - 728x90

Postgres C API: How can we copy a Datum?

0 votes
1 answer
149 views
As far as I understand, SPI_getbinval returns a pointer into the passed row. That is, the following would be unsafe:
dat = SPI_getbinval(SPI_tuptable->vals, SPI_tuptable->tupdesc, 1, &isnull);
SPI_finished();
if (isnull)
    PG_RETURN_NULL();
// Unsafe since SPI_finished() deallocates SPI_tuptable
return dat;
The SPI interface provides the utility SPI_copytuple. Is the following safe? Is there any problem with using SPI_tuptable->tupdesc? Moreover, is there a more efficient way to do this directly with the returned Datum instead of copying the entire HeapTuple?
// We copy the whole tuple into the outer context.  Better way to copy an individual Datum?
dat = SPI_getbinval(SPI_copytuple(SPI_tuptable->vals), SPI_tuptable->tupdesc, 1, &isnull);
SPI_finished();
if (isnull)
  PG_RETURN_NULL();
return dat;
Asked by user2959071 (63 rep)
Nov 15, 2021, 01:43 AM
Last activity: Nov 16, 2021, 05:32 AM