I understand. Thank you very much for clearing things up.
It helps me a lot, especially point 3.
"3. When you run SPI_connect () the memory context is switched
transparently for you to a special SPI memory context. When you run
SPI_finish () the original memory context (the one in effect before
SPI_connect) is restored. "
Best regards,
Yessica Brinkmann
It helps me a lot, especially point 3.
"3. When you run SPI_connect () the memory context is switched
transparently for you to a special SPI memory context. When you run
SPI_finish () the original memory context (the one in effect before
SPI_connect) is restored. "
Best regards,
Yessica Brinkmann
El mar., 26 nov. 2019 a las 15:25, Joe Conway (<mail@xxxxxxxxxxxxx>) escribió:
On 11/25/19 4:38 PM, Yessica Brinkmann wrote:
> Well, as I was told that I should save the
> CurrentMemoryContext before starting the SPI, Let's say it would be
> before doing SPI_connect (). Is this correct? And then I must use
> MemoryContextStrdup. As you told me the MemoryContextStrdup It is
> used to create a copy of a string in a specific memory context. Well,
> where in the source code should I use MemoryContextStrdup? After
> doing the SPI_connect () or where? I would use it from
> MemoryContextStrdup to copy the data variable as I understand it, But
> in what context would I have to create the copy? In a new context or
> what would the theme be like? Should I use AllocSetContextCreate to
> create the new context or what would the theme be like? And if I have
> to create the new context with AllocSetContextCreate, where in the
> source code will I have to create it? After doing SPI_connect () or
> where? The truth is that I also read the source code of
> https://github.com/cohenjo/pg_idx_advisor but I don't see that The
> MemoryContextStrdup that they told me to use has been used there.
> Sorry for the inconvenience and see the same thing again. But as I
> indicated, I also read the explanations they told me to read (which
> were explanations about the memory contexts in Postgresql mainly) but
> there were no examples of source code. And since there is nothing on
> the Internet of examples that will help me to use, I am asking
> again. And the truth is that I didn't find examples of this in the
> Postgres source code, just definitions, That is the source code
> where MemoryContextStrdup is defined. It may be very easy for you and
> you will see it very clearly, but for me it really is not, and there
> are no examples of use on the Internet. I really searched a lot and
> found nothing. I would greatly appreciate a help please.
Sorry but I am not going to try to address that wall of text ;-)
But here is some general information about how that stuff works:
---------------------------------
1. The most common pattern is something like this:
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(<some_memory_context>);
/* do stuff that allocates memory
* using PostgreSQL allocation functions
* e.g. palloc, pstrdup, other exported
* backend functions, etc
*/
MemoryContextSwitchTo(oldcontext);
2. MemoryContextStrdup() is similar to the above, except in that case
you do not need MemoryContextSwitchTo(). It directly allocates into
the specified memory context without all the switching back and
forth. If you are simply copying one string and need it in a context
other than what is current, it is more convenient. But either method
could be used.
3. When you run SPI_connect() the memory context is switched
transparently for you to a special SPI memory context. When you run
SPI_finish() the original memory context (the one in effect before
SPI_connect) is restored.
4. Depending on what you are trying to do, use method #1 or method #2 if
needed, including while doing SPI related things (in between
SPI_connect and SPI_finish)
Joe
--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development