pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Yessica Brinkmann <yessica.brinkmann@gmail.com>
Cc: Jony Cohen <jony.cohenjo@gmail.com>
Cc: laurenz.albe@cybertec.at
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: I think that my data is saved correctly, but when printing again, other data appears
Date: Tue, 26 Nov 2019 13:25:18 -0500
Message-ID: <b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com> (raw)
In-Reply-To: <CABrYqSM8hWYsmA=a1OCiu4uu7HxOTbws42w_++Pvg=F1gWJyOQ@mail.gmail.com>
References: <CABrYqSOSaFoM+FWNhajxpygVLWZECLt4nvtAwz7a_gunDgfgzQ@mail.gmail.com>
	<17316.1572011977@sss.pgh.pa.us>
	<6cn43v-kpk6i5gmroah-8s3fxj-obuf01-7gthsn-w01z6b-lpzvej-661z71jmpmi8-1npytb57lzgg-2cagvflpw1wnfjg834kpo46kpvh3rt-d709lq-mzqirq-vv9lx095h9fk-83gie7-rgk77o-xjfd1r.1572017048567@email.android.com>
	<CABrYqSNRL7mmNWPE4qBWYQ-5oFWGYNY8aEVp4YW3J09V2k1HAA@mail.gmail.com>
	<CAD9xk19M0iteWgL6Zgrz-=k_XU=19WVqr=Frdz5VTCsFsasfmQ@mail.gmail.com>
	<CABrYqSNNhfY0QxKZKaDh3FAtEmZ4LzqO3Zq98-+pbFG32u84BQ@mail.gmail.com>
	<efb6bd16-268c-5360-3206-7f0fb5fef523@joeconway.com>
	<CABrYqSO6BZvBU8ONFfcgCtYbuQew+L+aqS0F=YEN=QJdzVaQ-A@mail.gmail.com>
	<CABrYqSM8hWYsmA=a1OCiu4uu7HxOTbws42w_++Pvg=F1gWJyOQ@mail.gmail.com>

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

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (13+ messages)  latest in thread

Message-ID: <b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com>
Permalink:  ../b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com/
Also on:    postgresql.org/message-id/b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: mail@joeconway.com, yessica.brinkmann@gmail.com, jony.cohenjo@gmail.com, laurenz.albe@cybertec.at
  Subject: Re: I think that my data is saved correctly, but when printing again, other data appears
  In-Reply-To: <b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com>
  References: <CABrYqSOSaFoM+FWNhajxpygVLWZECLt4nvtAwz7a_gunDgfgzQ@mail.gmail.com> <17316.1572011977@sss.pgh.pa.us> <6cn43v-kpk6i5gmroah-8s3fxj-obuf01-7gthsn-w01z6b-lpzvej-661z71jmpmi8-1npytb57lzgg-2cagvflpw1wnfjg834kpo46kpvh3rt-d709lq-mzqirq-vv9lx095h9fk-83gie7-rgk77o-xjfd1r.1572017048567@email.android.com> <CABrYqSNRL7mmNWPE4qBWYQ-5oFWGYNY8aEVp4YW3J09V2k1HAA@mail.gmail.com> <CAD9xk19M0iteWgL6Zgrz-=k_XU=19WVqr=Frdz5VTCsFsasfmQ@mail.gmail.com> <CABrYqSNNhfY0QxKZKaDh3FAtEmZ4LzqO3Zq98-+pbFG32u84BQ@mail.gmail.com> <efb6bd16-268c-5360-3206-7f0fb5fef523@joeconway.com> <CABrYqSO6BZvBU8ONFfcgCtYbuQew+L+aqS0F=YEN=QJdzVaQ-A@mail.gmail.com> <CABrYqSM8hWYsmA=a1OCiu4uu7HxOTbws42w_++Pvg=F1gWJyOQ@mail.gmail.com> <b4f58247-cd0c-217b-5dba-cd9b7302e19e@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox