Re: I think that my data is saved correctly, but when printing again, other data appears

Joe Conway <[email protected]>
Newsgroups gmane.comp.db.postgresql.general
Message-ID <[email protected]>
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
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEg14x9eymXoJyHrH+N/L3QzX32GUFAl3dbg4ACgkQN/L3QzX3
2GV/gg//WjCeRkySRcjTo70mNOHgXZ7OyqHWRUjBne+/AxKeFBD3jWdbs1DlMt+t
8w38sdsde9a5mxGbFGdnmZas60sEqD1hf4uuuhnTqJs469m3D95GewrHmRV8yUYH
dcK/kf7SAvh8AO1j123kUpOnmr0eHGmyiFrGp+P3UukYnAVqj05Qgs10bmNKC3DI
r0J2Qop4hXQSki+Dd60HzP2HSU093E/zDuYvh2WfeNMAgP3F3CZg8SgshVxgM6IZ
aTP7MPAMtk6z90ULVCguvvpYosMUYa0EaOXemIplsPJUE0qvgWt07vzCLo/Io+op
LeKTmqn6QIbT+yfK+XJlcn6csoTaMCrEbwv/v3Wj9At1u4Culkav6DXIvkNir0jn
JRgNvHe8XCHR2FpbYjwkvBK25G5b44XBiQw7zAmbxJ1/HJmEtRPQ722uFMS38TGY
k/3JfM5G/DFADa4WrrKjhhmZ+taMjd50lBAB5+TZDtL97Hz+gzateouI54OgR1ML
7mthbmVKtS40t/KOI8dJFGGksDVbBnWzyhwIIZ6B4ALsIe0TOxEla1McmOWgvMDx
gGcI6J0STm+8iK0j7SqnyWSSE5zMwFrfgzvpB7/jdOoMoXeZp540ACGOkXWHAIXg
p9o3S5UjwCZL6mOe15QA8Qw2lAaJyWuL5Q6aOUGyrysf+3RgS98=
=8Eml
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.