Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iO07z-0003JK-LY for pgsql-general@arkaria.postgresql.org; Fri, 25 Oct 2019 13:59:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iO07y-0007n3-7m for pgsql-general@arkaria.postgresql.org; Fri, 25 Oct 2019 13:59:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iO07x-0007mw-R6 for pgsql-general@lists.postgresql.org; Fri, 25 Oct 2019 13:59:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iO07v-0003jV-Ct for pgsql-general@lists.postgresql.org; Fri, 25 Oct 2019 13:59:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x9PDxb0R017317; Fri, 25 Oct 2019 09:59:37 -0400 From: Tom Lane To: Yessica Brinkmann cc: pgsql-general@lists.postgresql.org Subject: Re: I think that my data is saved correctly, but when printing again, other data appears In-reply-to: References: Comments: In-reply-to Yessica Brinkmann message dated "Fri, 25 Oct 2019 08:37:06 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17315.1572011977.1@sss.pgh.pa.us> Date: Fri, 25 Oct 2019 09:59:37 -0400 Message-ID: <17316.1572011977@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Yessica Brinkmann writes: > I have a function called get_columnnames, which runs on a Postgresql server > and returns the names of the columns of a table in a Postgresql database. > In the first foreach, in the statement: idxcd-> varattnames [cont] = data; > column names are saved. Those column names are saved correctly. I could > verify this by printing some values. > But later, in the following foreach, when I reprint the values of the names > of the columns that had been saved in the idxcd-> varattnames [cont] = data > statement; They appear to me with errors. Yeah, because the "data" value is just a pointer into the tupdesc associated with the SPI result, and that disappears the moment you do SPI_finish(). You'd need to do something to copy the strings into a longer-lived context. A plain pstrdup() won't suffice because you're in a short-lived SPI context already inside that loop; but you could save CurrentMemoryContext before starting up SPI and then use MemoryContextStrdup. regards, tom lane