Why does SPI_connect change the memory context?

Поиск
Список
Период
Сортировка
Искать

Why does SPI_connect change the memory context?

От:
Jeff Davis <pgsql@j-davis.com>
Дата:
SPI_connect() changes the memory context to a newly-created one, and
then SPI_finish() restores it. That seems a bit dangerous because the
caller might not be expecting it. Is there a reason it doesn't just
change to the new memory context as-needed?

spi.c:161:

   /* ... and switch to procedure's context */
    _SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current-
>procCxt);

Regards,
	Jeff Davis




Re: Why does SPI_connect change the memory context?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Jeff Davis  writes:
> SPI_connect() changes the memory context to a newly-created one, and
> then SPI_finish() restores it. That seems a bit dangerous because the
> caller might not be expecting it. Is there a reason it doesn't just
> change to the new memory context as-needed?

Because the expectation is that palloc inside the SPI procedure will
allocate in a procedure-specific context.  If the caller isn't expecting
that, they haven't read the documentation, specifically

https://www.postgresql.org/docs/devel/spi-memory.html

which says

  
   SPI_connect creates a new memory context and
   makes it current.  SPI_finish restores the
   previous current memory context and destroys the context created by
   SPI_connect.  These actions ensure that
   transient memory allocations made inside your C function are
   reclaimed at C function exit, avoiding memory leakage.
  

			regards, tom lane


FAQ