Problems with MemoryContextSwitchTo ()

Started by Yessica Brinkmannover 5 years ago13 messagesgeneral
Jump to latest
#1Yessica Brinkmann
brinkmann.yessica@gmail.com

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the thesis,
when I had a problem to be able to compile my program in a moment, and by
accident some lines of source code were moved.
And for this reason, I think I have problems again with the context switch
issue, since at some point my context switch stopped working for me, I
think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which in
the second foreach, is printing the values of idxcd-> varattnnames [i] the
null value.
This second foreach, I only do it to test if the data is really saved well
and if I can recover it properly.
And since the data is not retrieved properly, or is not saved properly, in
the following functions of my program, the value of idxcd-> varattnnames
[i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

Attachments:

get_columnames.txttext/plain; charset=US-ASCII; name=get_columnames.txtDownload
#2Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Yessica Brinkmann (#1)
Re: Problems with MemoryContextSwitchTo ()

Good Morning.
I will greatly appreciate a help please on this subject. I actually did
several tests already and tried a lot to fix it myself. But I am not able
to. And I really need to know this in order to finish my university thesis. If
someone can help me please.
Best regards,
Yessica Brinkmann

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Show quoted text

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the thesis,
when I had a problem to be able to compile my program in a moment, and by
accident some lines of source code were moved.
And for this reason, I think I have problems again with the context switch
issue, since at some point my context switch stopped working for me, I
think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which in
the second foreach, is printing the values of idxcd-> varattnnames [i] the
null value.
This second foreach, I only do it to test if the data is really saved well
and if I can recover it properly.
And since the data is not retrieved properly, or is not saved properly, in
the following functions of my program, the value of idxcd-> varattnnames
[i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Yessica Brinkmann (#2)
Re: Problems with MemoryContextSwitchTo ()

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually did
several tests already and tried a lot to fix it myself. But I am not able
to. And I really need to know this in order to finish my university
thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

Show quoted text

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the thesis,
when I had a problem to be able to compile my program in a moment, and by
accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which in
the second foreach, is printing the values of idxcd-> varattnnames [i] the
null value.
This second foreach, I only do it to test if the data is really saved
well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved properly,
in the following functions of my program, the value of idxcd-> varattnnames
[i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#4Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Yessica Brinkmann (#1)
Re: Problems with MemoryContextSwitchTo ()

Good Morning.<br><br>I will greatly appreciate a help please on this subject.I actually did several tests already and tried a lot to fix it myself. But I am not able to. And I really need to know this in order to finish my university thesis. If someone can help me please.<br><br>Best regards,<br><br>Yessica Brinkmann<br><br><br><div class="quote" style="line-height: 1.5"><br><br>-------- Mensaje original --------<br>Asunto: Problems with MemoryContextSwitchTo ()<br>De: Yessica Brinkmann <brinkmann.yessica@gmail.com><br>Para: pgsql-general@lists.postgresql.org<br>CC: <br><br><br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">
<table class="gmail-Bs gmail-nH gmail-iY gmail-bAt" cellpadding="0"><tbody><tr><td class="gmail-Bu gmail-bAn"><br></td><td class="gmail-Bu gmail-yM"><br></td><td class="gmail-Bu gmail-y3">
Hello.<br>
I think several of you will already remember me. I&#39;m the one with the IndexAdviser topic. Only that I changed my email address.<br>
As you may recall, I am doing my thesis on the subject of IndexAdviser modifications.<br>
I really appreciate the help they have given me in various Postgresql groups.<br>
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a
moment, and by accident some lines of source code were moved.<br>
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for
me, I think because of the issue that some lines of source code were
moved.<br>
Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-&gt;
varattnnames [i] the null value.<br>
This second foreach, I only do it to test if the data is really saved well and if I can recover it properly.<br>
And since the data is not retrieved properly, or is not saved properly,
in the following functions of my program, the value of idxcd-&gt;
varattnnames [i] continues to appear as null.<br>
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-&gt; varattnnames [i], in
the second foreach, if it is due to an error in the context switch, or
why it could be .<br>
<div class="gmail-oSioSc"><div id="gmail-tw-target"><div id="gmail-kAz1tf" class="gmail-g9WsWb"><div class="gmail-tw-ta-container gmail-hide-focus-ring gmail-tw-nfl" id="gmail-tw-target-text-container" tabindex="0"><pre class="gmail-tw-data-text gmail-tw-text-large gmail-XcVN5d gmail-tw-ta" id="gmail-tw-target-text" style="text-align:left" dir="ltr"><span lang="en">I send my function as an attachment.</span></pre></div></div></div></div>

<br>
Best regards,<br>
Yessica Brinkmann.

</td></tr></tbody></table>

</div>
</blockquote></div>

#5Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Yessica Brinkmann (#1)
Re: Problems with MemoryContextSwitchTo ()

static List*
get_columnnames( List* candidates )
{
int proc;
int ret;
StringInfoData query; /* string for Query */
StringInfoData cols; /* string for Columns */
MemoryContext outerContext;
ListCell *cell;

IndexCandidate* idxcd;

elog( DEBUG3, "IND ADV: get_column_names: ENTER" );

initStringInfo( &query );
initStringInfo( &cols );

foreach( cell, candidates ) /* foreach cell in candidates */
{

int i;

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

if (!idxcd->idxused)
continue;

/* pfree() the memory allocated for the previous candidate. FIXME: Avoid
* meddling with the internals of a StringInfo, and try to use an API.
*/
if( cols.len > 0 )
{
initStringInfo(&cols);
} /*IF col.len>0*/

if( query.len > 0 )
{
initStringInfo(&query);
} /*IF col.len>0*/

elog(INFO,"reloid:%d", idxcd->reloid);
appendStringInfo( &query, "select a.attname from pg_class c,pg_attribute a where c.oid=%d AND a.attrelid = c.oid AND (", idxcd->reloid);

/*elog(INFO,"QUERY:%s", query.data);*/

/*elog(INFO,"ncols:%d", idxcd->ncols);*/

for (i = 0; i < idxcd->ncols; ++i)
{
/*elog(INFO,"i:%d", i);*/
/*elog(INFO,"var attno i:%d", idxcd->varattno[i]);*/
/*elog(INFO,"cols:%s", cols.data);*/
appendStringInfo( &cols, "%s a.attnum=%d", (i>0 ? " OR" : ""), idxcd->varattno[i]);
/*elog(INFO,"cols:%s", cols.data);*/
/*elog(INFO,"i:%d", i);*/
elog(INFO,"varattno i:%d", idxcd->varattno[i]);

}/* foreach col in varattno*/

/*elog(INFO,"PASA EL FOR");*/
appendStringInfo( &cols, "%s", ")");

/* FIXME: Mention the column names explicitly after the table name. */
appendStringInfo( &query, "%s;", cols.data);

elog(INFO,"QUERY:%s", query.data);
/*elog(INFO,"LONGITUD:%d", query.len);*/

if( query.len > 0 ) /* if we generated any SQL */
{

outerContext = CurrentMemoryContext;
if( SPI_connect() == SPI_OK_CONNECT )
{
/*elog(INFO,"CONECTADO:%d", query.len);*/

ret=SPI_exec(query.data, 0);
proc=SPI_processed;
TupleDesc tupdesc=SPI_tuptable->tupdesc;
SPITupleTable *tuptable=SPI_tuptable;
char buf[8192];
if( ret>0 )
{
/*elog(INFO,"EJECUTA:%d", query.len);*/

if( SPI_tuptable != NULL)
{

//TupleDesc tupdesc;

//SPITupleTable *tuptable = SPI_tuptable;

//tupdesc = tuptable->tupdesc;

elog(INFO,"procantesciclo:%d", proc);
int i,j;
for(j=0;j<proc;j++)
{
HeapTuple tuple;
elog(INFO,"procdespuesciclo:%d", proc);
/*cada fila*/
int cont=0;
tuple=tuptable->vals[j];
if (tuple!=NULL)
{

for (i=1,buf[0]=0;i<=tupdesc->natts;i++)
{
char *data;

/* cada columna de cada fila*/

data=SPI_getvalue(tuple,tupdesc,i);
elog(INFO,"data:%s", data);

idxcd->varattnombres[i]=MemoryContextStrdup(outerContext, data);

elog(INFO,"valorgc:%s", idxcd->varattnombres[i]);
elog(INFO,"indice:%d", cont);
cont++;
snprintf(buf + strlen (buf), sizeof(buf) - strlen(buf), " %s%s", SPI_getvalue(tuple, tupdesc, i),(i == tupdesc->natts) ? " " : " |");

} /* (i=0,buf[0]=0;i<tupdesc->natts;i++)*/
elog (INFO, "EXECQ: %s", buf);
} /* if (tuple!=null)*/
else
elog( WARNING, "IND ADV: tuple is null." );
} /* (j=0;j<proc;j++)*/
} /*if( SPI_tuptable != NULL)*/
else
elog( WARNING, "IND ADV: SPI_tuptable is null." );
}
else
elog( WARNING, "IND ADV: SPI_execute failed while select." );
if( SPI_finish() != SPI_OK_FINISH )
elog( WARNING, "IND ADV: SPI_finish failed while select." );

} /*if( SPI_connect() == SPI_OK_CONNECT )*/

else
elog( WARNING, "IND ADV: SPI_connect failed while select." );

} /*if( query.len > 0 )*/
elog (INFO, "if( query.len > 0");

/*if( query.len > 0 )*/

} /* foreach cell in candidates */
elog (INFO, "/* foreach cell in candidates */");

foreach( cell, candidates ) /* foreach cell in candidates */
{

MemoryContext oldContext = MemoryContextSwitchTo( outerContext );
MemoryContextSwitchTo( oldContext );

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

if (!idxcd->idxused)
continue;

int i;
for (i = 0; i < idxcd->ncols; ++i)
{

/*elog(INFO,"cols:%s", cols.data);*/
elog(INFO,"i:%d", i);
elog(INFO,"varattnombres i:%s", idxcd->varattnombres[i]);
elog(INFO,"varattno i:%d", idxcd->varattno[i]);

}/* foreach col in varattno*/

}

/* TODO: Propose to -hackers to introduce API to free a StringInfoData . */
if ( query.len > 0 )
pfree( query.data );

elog( DEBUG3, "IND ADV: select: EXIT" );
elog (INFO, "retornando get_columnnames");
return candidates;
}

Attachments:

get_columnames.txttext/plain; name=get_columnames.txtDownload
#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Yessica Brinkmann (#5)
Re: Problems with MemoryContextSwitchTo ()

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames, which in
the second foreach, is printing the values of idxcd-> varattnnames [i] the
null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface, and
theoretically it can clean result of previous query.

Regards

Pavel

Show quoted text

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually did
several tests already and tried a lot to fix it myself. But I am not
able to. And I really need to know this in order to finish my university
thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value.
This second foreach, I only do it to test if the data is really saved
well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved properly,
in the following functions of my program, the value of idxcd-> varattnnames
[i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#7Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Pavel Stehule (#6)
Re: Problems with MemoryContextSwitchTo ()

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the previous
query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in the
query, since I print the values of idxcd-> varattnnames, but it is printing
null.
And I think the memory context switch is fine too, so I really don't know
why it prints null to me.
I will greatly appreciate a help please. I can't really understand why it
prints null in the second foreach, even though I already tried various
changes.
Best regards,
Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<pavel.stehule@gmail.com>)
escribió:

Show quoted text

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames, which in
the second foreach, is printing the values of idxcd-> varattnnames [i] the
null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface, and
theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually did
several tests already and tried a lot to fix it myself. But I am not
able to. And I really need to know this in order to finish my
university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value.
This second foreach, I only do it to test if the data is really saved
well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved properly,
in the following functions of my program, the value of idxcd-> varattnnames
[i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: Yessica Brinkmann (#7)
Re: Problems with MemoryContextSwitchTo ()

čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the previous
query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in the
query, since I print the values of idxcd-> varattnnames, but it is printing
null.
And I think the memory context switch is fine too, so I really don't know
why it prints null to me.
I will greatly appreciate a help please. I can't really understand why it
prints null in the second foreach, even though I already tried various
changes.

do you use Postgres with active assertions ?

postgres=# show debug_assertions ;
┌──────────────────┐
│ debug_assertions │
╞══════════════════╡
│ on │
└──────────────────┘
(1 row)

maybe you somewhere allocate less memory then it is necessary and some
memset rewrites memory with your data. When you have a problem with memory,
then you should to very carefully check a) if memory is allocated in good
context, b) if memory was allocated enough (sometimes I just allocated x +
100000 for testing).

Can you write a reproducer ? code that I can check at my home. Postgres is
pretty complex system, and it is hard to fix some issue without access to
code

Best regards,

Show quoted text

Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<pavel.stehule@gmail.com>)
escribió:

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface, and
theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually
did several tests already and tried a lot to fix it myself. But I am
not able to. And I really need to know this in order to finish my
university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of IndexAdviser
modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value.
This second foreach, I only do it to test if the data is really saved
well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved
properly, in the following functions of my program, the value of idxcd->
varattnnames [i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#8)
Re: Problems with MemoryContextSwitchTo ()

čt 17. 9. 2020 v 6:09 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the
previous query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in the
query, since I print the values of idxcd-> varattnnames, but it is printing
null.
And I think the memory context switch is fine too, so I really don't know
why it prints null to me.
I will greatly appreciate a help please. I can't really understand why
it prints null in the second foreach, even though I already tried various
changes.

do you use Postgres with active assertions ?

postgres=# show debug_assertions ;
┌──────────────────┐
│ debug_assertions │
╞══════════════════╡
│ on │
└──────────────────┘
(1 row)

maybe you somewhere allocate less memory then it is necessary and some
memset rewrites memory with your data. When you have a problem with memory,
then you should to very carefully check a) if memory is allocated in good
context, b) if memory was allocated enough (sometimes I just allocated x +
100000 for testing).

Can you write a reproducer ? code that I can check at my home. Postgres is
pretty complex system, and it is hard to fix some issue without access to
code

This is some messy in your code

MemoryContext oldContext = MemoryContextSwitchTo( outerContext );
MemoryContextSwitchTo( oldContext );

Unfortunately, your code is not well structured - it is hard to read it.
One hint - if you find an issue - try to reduce lines of your code which is
possible until you reproduce this issue or until you understand this issue.

The sequence of statements is a little bit obscure :)

initStringInfo( &query );

*/
if( cols.len > 0 )
{
initStringInfo(&cols);
} /*IF col.len>0*/

Instead

foreach( cell, candidates ) /* foreach cell in candidates */
{

int i;

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

you can write

idxcd = (IndexCandidate *) linitial(candidates);

The problem in your example is fact so it is a mix of copy/paste fragments.
I wrote similar code, and almost everyone I know (when I started). But it
is not possible to fix this code - start from scratch. The code must be
readable (every time). And when I write code inside some complex
environment (like Postgres is), I write code in very small fragments, in
very small steps, and every time I try to compile, restart and check the
result. Then I know what line, or what statement is the problem.

Regards

Pavel

Show quoted text

Best regards,

Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<
pavel.stehule@gmail.com>) escribió:

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface, and
theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually
did several tests already and tried a lot to fix it myself. But I am
not able to. And I really need to know this in order to finish my
university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with the
IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of
IndexAdviser modifications.
I really appreciate the help they have given me in various Postgresql
groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value.
This second foreach, I only do it to test if the data is really saved
well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved
properly, in the following functions of my program, the value of idxcd->
varattnnames [i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why
the function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#10Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Pavel Stehule (#9)
Re: Problems with MemoryContextSwitchTo ()

<br><br>Good Morning. Thank you so much for the answers. Now I have to go to work, but when I come back I will be looking at the answers in detail, and I will answer. Unfortunately I have to work and study as well, so right now I can't see the answers in detail.<br>Best regards,<br>Yessica Brinkmann. <br><div class="quote" style="line-height: 1.5"><br><br>-------- Mensaje original --------<br>Asunto: Re: Problems with MemoryContextSwitchTo ()<br>De: Pavel Stehule <pavel.stehule@gmail.com><br>Para: Yessica Brinkmann <brinkmann.yessica@gmail.com><br>CC: pgsql-general <pgsql-general@lists.postgresql.org><br><br><br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">čt 17. 9. 2020 v 6:09 odesílatel Pavel Stehule &lt;<a href="mailto:pavel.stehule@gmail.com">pavel.stehule@gmail.com</a>&gt; napsal:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann &lt;<a href="mailto:brinkmann.yessica@gmail.com" target="_blank">brinkmann.yessica@gmail.com</a>&gt; napsal:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">
<span lang="en"><span title="">Good night,</span><br><span title="">Thank you very much for the answer.</span><br><span title="">Excuse me please just answered, I was at my job.</span><br><span title="">And yes, I get the results of the list with SPI_exec.</span><br><span title="">And I am also using SPI_finish ().</span> <span title="">But I save the results of the previous query in the array idxcd-&gt;
varattnnames.</span><br><span title="">And in the second foreach it should print me the values that I got in the query, since I print the values of idxcd-&gt; varattnnames, but it is printing null.</span><br><span title="">And I think the memory context switch is fine too, so I really don&#39;t know why it prints null to me.</span><br><span title="">I will greatly appreciate a help please.</span> <span title="">I can&#39;t really understand why it prints null in the second foreach, even though I already tried various changes.</span><br></span></div></blockquote><br></div><div class="gmail_quote">do you use Postgres with active assertions ?</div><div class="gmail_quote"><br></div><div class="gmail_quote">postgres=# show debug_assertions ;<br>┌──────────────────┐<br>│ debug_assertions │<br>╞══════════════════╡<br>│ on               │<br>└──────────────────┘<br>(1 row)<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">maybe you somewhere allocate less memory then it is necessary and some memset rewrites memory with your data. When you have a problem with memory, then you should to very carefully check a) if memory is allocated in good context, b) if memory was allocated enough (sometimes I just allocated x + 100000 for testing).</div><div class="gmail_quote"><br></div><div class="gmail_quote">Can you write a reproducer ? code that I can check at my home. Postgres is pretty complex system, and it is hard to fix some issue without access to code</div><div class="gmail_quote"><br></div></div></blockquote><div><br></div><div>This is some messy in your code</div><div><br></div><div><span style="font-family:monospace">MemoryContext oldContext = MemoryContextSwitchTo( outerContext );<br> MemoryContextSwitchTo( oldContext ); <br></span></div><div><br></div><div>Unfortunately, your code is not well structured - it is hard to read it. One hint - if you find an issue - try to reduce lines of your code which is possible until you reproduce this issue or until you understand this issue.</div><div><br></div><div>The sequence of statements is a little bit obscure :)</div><div><br></div><div><span style="font-family:monospace">initStringInfo( &amp;query );</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"> */<br> if( cols.len &gt; 0 )<br> {<br> initStringInfo(&amp;cols);<br> } /*IF col.len&gt;0*/</span></div><div><br></div><div>Instead <br></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">foreach( cell, candidates ) /* foreach cell in candidates */<br> {<br></span> <span style="font-family:monospace"><br> int i;<br></span> <span style="font-family:monospace"><br></span> <span style="font-family:monospace"><br></span> <span style="font-family:monospace"><br>    /*elog (INFO, &quot;Ingresando a foreach&quot;);*/<br> idxcd = (IndexCandidate*)lfirst( cell );<br></span> <span style="font-family:monospace"><br> if (idxcd == NULL) {<br>            elog( INFO, &quot;idxcd IS NULL&quot; );<br>            continue; /* Or is that fatal enough to break instead? */<br>        }</span></div><div><br></div><div><br></div><div>you can write<br></div><div><br></div><div><span style="font-family:monospace">idxcd = (IndexCandidate *) linitial(candidates);</span></div><div><br></div><div>The problem in your example is fact so it is a mix of copy/paste fragments. I wrote similar code, and almost everyone I know (when I started). But it is not possible to fix this code - start from scratch. The code must be readable (every time). And when I write code inside some complex environment (like Postgres is), I write code in very small fragments, in very small steps, and every time I try to compile, restart and check the result. Then I know what line, or what statement is the problem. <br></div><div><br></div><div>Regards</div><div><br></div><div>Pavel<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span lang="en"></span><div><span lang="en"><span title="">Best regards,</span></span></div><div><span lang="en"><span title="">Yessica Brinkmann<br></span></span></div>

</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (&lt;<a href="mailto:pavel.stehule@gmail.com" target="_blank">pavel.stehule@gmail.com</a>&gt;) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann &lt;<a href="mailto:brinkmann.yessica@gmail.com" target="_blank">brinkmann.yessica@gmail.com</a>&gt; napsal:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Well, the fact is that I have a function called get_columnnames, which in the second foreach, is printing the values of idxcd-&gt; varattnnames [i] the null value. This is the error. </blockquote><div><br></div><div>How you get this list? It is result of SPI_exec*** ?<br></div><div><br></div><div>There is SPI_finish(), that cleaning a memory used by SPI interface, and theoretically it can clean result of previous query.</div><div><br></div><div>Regards</div><div><br></div><div>Pavel</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="line-height:1.5"><br><br>-------- Mensaje original --------<br>Asunto: Re: Problems with MemoryContextSwitchTo ()<br>De: Pavel Stehule <u></u><br>Para: Yessica Brinkmann <u></u><br>CC: pgsql-general <u></u><br><br><br type="attribution"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann &lt;<a href="mailto:brinkmann.yessica@gmail.com" target="_blank">brinkmann.yessica@gmail.com</a>&gt; napsal:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">
<span lang="en"><span title="">Good Morning.</span><br></span><div><span lang="en"><span title="">I will greatly appreciate a help please on this subject.</span> <span title="">I actually did several tests already and tried a lot to fix it myself.</span> <span title="">But I am not able to.</span> <span title="">And I really need to know this in order to finish my university thesis.</span> <span title="">If someone can help me please.</span><br></span></div><div>Best regards,</div><div>Yessica Brinkmann<br></div></div></blockquote><div><br></div><div>please, can you attach your code, and can you show the error message? <br></div><div><br></div><div>It is hard to say what is the problem from your mail?</div><div><br></div><div>Regards</div><div><br></div><div>Pavel<br></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div><span lang="en"></span></div>

<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (&lt;<a href="mailto:brinkmann.yessica@gmail.com" target="_blank">brinkmann.yessica@gmail.com</a>&gt;) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">
<table cellpadding="0"><tbody><tr><td><br></td><td><br></td><td>
Hello.<br>
I think several of you will already remember me. I&#39;m the one with the IndexAdviser topic. Only that I changed my email address.<br>
As you may recall, I am doing my thesis on the subject of IndexAdviser modifications.<br>
I really appreciate the help they have given me in various Postgresql groups.<br>
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a
moment, and by accident some lines of source code were moved.<br>
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for
me, I think because of the issue that some lines of source code were
moved.<br>
Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-&gt;
varattnnames [i] the null value.<br>
This second foreach, I only do it to test if the data is really saved well and if I can recover it properly.<br>
And since the data is not retrieved properly, or is not saved properly,
in the following functions of my program, the value of idxcd-&gt;
varattnnames [i] continues to appear as null.<br>
I will appreciate a lot please help, if you can tell me please why the
function prints null in the values of idxcd-&gt; varattnnames [i], in
the second foreach, if it is due to an error in the context switch, or
why it could be .<br>
<div><div id="gmail-m_-882624099522786493gmail-m_-4765376118179730423gmail-m_9008552883760584857gmail-m_6085642464088391972gmail-m_-1533817038986493541gmail-m_8308262266088241857gmail-tw-target"><div id="gmail-m_-882624099522786493gmail-m_-4765376118179730423gmail-m_9008552883760584857gmail-m_6085642464088391972gmail-m_-1533817038986493541gmail-m_8308262266088241857gmail-kAz1tf"><div id="gmail-m_-882624099522786493gmail-m_-4765376118179730423gmail-m_9008552883760584857gmail-m_6085642464088391972gmail-m_-1533817038986493541gmail-m_8308262266088241857gmail-tw-target-text-container"><pre id="gmail-m_-882624099522786493gmail-m_-4765376118179730423gmail-m_9008552883760584857gmail-m_6085642464088391972gmail-m_-1533817038986493541gmail-m_8308262266088241857gmail-tw-target-text" style="text-align:left" dir="ltr"><span lang="en">I send my function as an attachment.</span></pre></div></div></div></div>

<br>
Best regards,<br>
Yessica Brinkmann.

</td></tr></tbody></table>

</div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div></blockquote></div></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div>

#11Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Yessica Brinkmann (#10)
Re: Problems with MemoryContextSwitchTo ()

Thank you very much for the answers, and for the advice given. I will try
to put them into practice.
Best regards,
Yessica Brinkmann

El jue., 17 sept. 2020 a las 6:46, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Show quoted text

Good Morning. Thank you so much for the answers. Now I have to go to work,
but when I come back I will be looking at the answers in detail, and I will
answer. Unfortunately I have to work and study as well, so right now I
can't see the answers in detail.
Best regards,
Yessica Brinkmann.

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

čt 17. 9. 2020 v 6:09 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the
previous query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in
the query, since I print the values of idxcd-> varattnnames, but it is
printing null.
And I think the memory context switch is fine too, so I really don't
know why it prints null to me.
I will greatly appreciate a help please. I can't really understand why
it prints null in the second foreach, even though I already tried various
changes.

do you use Postgres with active assertions ?

postgres=# show debug_assertions ;
┌──────────────────┐
│ debug_assertions │
╞══════════════════╡
│ on │
└──────────────────┘
(1 row)

maybe you somewhere allocate less memory then it is necessary and some
memset rewrites memory with your data. When you have a problem with memory,
then you should to very carefully check a) if memory is allocated in good
context, b) if memory was allocated enough (sometimes I just allocated x +
100000 for testing).

Can you write a reproducer ? code that I can check at my home. Postgres
is pretty complex system, and it is hard to fix some issue without access
to code

This is some messy in your code

MemoryContext oldContext = MemoryContextSwitchTo( outerContext );
MemoryContextSwitchTo( oldContext );

Unfortunately, your code is not well structured - it is hard to read it.
One hint - if you find an issue - try to reduce lines of your code which is
possible until you reproduce this issue or until you understand this issue.

The sequence of statements is a little bit obscure :)

initStringInfo( &query );

*/
if( cols.len > 0 )
{
initStringInfo(&cols);
} /*IF col.len>0*/

Instead

foreach( cell, candidates ) /* foreach cell in candidates */
{

int i;

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

you can write

idxcd = (IndexCandidate *) linitial(candidates);

The problem in your example is fact so it is a mix of copy/paste
fragments. I wrote similar code, and almost everyone I know (when I
started). But it is not possible to fix this code - start from scratch. The
code must be readable (every time). And when I write code inside some
complex environment (like Postgres is), I write code in very small
fragments, in very small steps, and every time I try to compile, restart
and check the result. Then I know what line, or what statement is the
problem.

Regards

Pavel

Best regards,

Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<
pavel.stehule@gmail.com>) escribió:

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames, which
in the second foreach, is printing the values of idxcd-> varattnnames [i]
the null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface,
and theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually
did several tests already and tried a lot to fix it myself. But I am
not able to. And I really need to know this in order to finish my
university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with
the IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of
IndexAdviser modifications.
I really appreciate the help they have given me in various
Postgresql groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value.
This second foreach, I only do it to test if the data is really
saved well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved
properly, in the following functions of my program, the value of idxcd->
varattnnames [i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why
the function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#12Yessica Brinkmann
brinkmann.yessica@gmail.com
In reply to: Yessica Brinkmann (#11)
Re: Problems with MemoryContextSwitchTo ()

I already discovered the problem, it was only a counter initialization
issue, it had nothing to do with memory contexts, that part was fine. Thank
God I could already understand the problem and solve it.
Anyway thank you very much.
Best regards,
Yessica Brinkmann

El jue., 17 sept. 2020 a las 22:26, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Show quoted text

Thank you very much for the answers, and for the advice given. I will try
to put them into practice.
Best regards,
Yessica Brinkmann

El jue., 17 sept. 2020 a las 6:46, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Good Morning. Thank you so much for the answers. Now I have to go to
work, but when I come back I will be looking at the answers in detail, and
I will answer. Unfortunately I have to work and study as well, so right now
I can't see the answers in detail.
Best regards,
Yessica Brinkmann.

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

čt 17. 9. 2020 v 6:09 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the
previous query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in
the query, since I print the values of idxcd-> varattnnames, but it is
printing null.
And I think the memory context switch is fine too, so I really don't
know why it prints null to me.
I will greatly appreciate a help please. I can't really understand why
it prints null in the second foreach, even though I already tried various
changes.

do you use Postgres with active assertions ?

postgres=# show debug_assertions ;
┌──────────────────┐
│ debug_assertions │
╞══════════════════╡
│ on │
└──────────────────┘
(1 row)

maybe you somewhere allocate less memory then it is necessary and some
memset rewrites memory with your data. When you have a problem with memory,
then you should to very carefully check a) if memory is allocated in good
context, b) if memory was allocated enough (sometimes I just allocated x +
100000 for testing).

Can you write a reproducer ? code that I can check at my home. Postgres
is pretty complex system, and it is hard to fix some issue without access
to code

This is some messy in your code

MemoryContext oldContext = MemoryContextSwitchTo( outerContext );
MemoryContextSwitchTo( oldContext );

Unfortunately, your code is not well structured - it is hard to read it.
One hint - if you find an issue - try to reduce lines of your code which is
possible until you reproduce this issue or until you understand this issue.

The sequence of statements is a little bit obscure :)

initStringInfo( &query );

*/
if( cols.len > 0 )
{
initStringInfo(&cols);
} /*IF col.len>0*/

Instead

foreach( cell, candidates ) /* foreach cell in candidates */
{

int i;

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

you can write

idxcd = (IndexCandidate *) linitial(candidates);

The problem in your example is fact so it is a mix of copy/paste
fragments. I wrote similar code, and almost everyone I know (when I
started). But it is not possible to fix this code - start from scratch. The
code must be readable (every time). And when I write code inside some
complex environment (like Postgres is), I write code in very small
fragments, in very small steps, and every time I try to compile, restart
and check the result. Then I know what line, or what statement is the
problem.

Regards

Pavel

Best regards,

Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<
pavel.stehule@gmail.com>) escribió:

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface,
and theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I actually
did several tests already and tried a lot to fix it myself. But I
am not able to. And I really need to know this in order to finish
my university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with
the IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of
IndexAdviser modifications.
I really appreciate the help they have given me in various
Postgresql groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the context
switch issue, since at some point my context switch stopped working for me,
I think because of the issue that some lines of source code were moved.
Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value.
This second foreach, I only do it to test if the data is really
saved well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved
properly, in the following functions of my program, the value of idxcd->
varattnnames [i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why
the function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.

#13Pavel Stehule
pavel.stehule@gmail.com
In reply to: Yessica Brinkmann (#12)
Re: Problems with MemoryContextSwitchTo ()

pá 18. 9. 2020 v 6:35 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

I already discovered the problem, it was only a counter initialization
issue, it had nothing to do with memory contexts, that part was fine. Thank
God I could already understand the problem and solve it.
Anyway thank you very much.
Best regards,
Yessica Brinkmann

Great! These problems forward us :). It is very common so the problem is
somewhere else than we are finding.

Pavel

Show quoted text

El jue., 17 sept. 2020 a las 22:26, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Thank you very much for the answers, and for the advice given. I will
try to put them into practice.
Best regards,
Yessica Brinkmann

El jue., 17 sept. 2020 a las 6:46, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Good Morning. Thank you so much for the answers. Now I have to go to
work, but when I come back I will be looking at the answers in detail, and
I will answer. Unfortunately I have to work and study as well, so right now
I can't see the answers in detail.
Best regards,
Yessica Brinkmann.

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

čt 17. 9. 2020 v 6:09 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good night,
Thank you very much for the answer.
Excuse me please just answered, I was at my job.
And yes, I get the results of the list with SPI_exec.
And I am also using SPI_finish (). But I save the results of the
previous query in the array idxcd-> varattnnames.
And in the second foreach it should print me the values that I got in
the query, since I print the values of idxcd-> varattnnames, but it is
printing null.
And I think the memory context switch is fine too, so I really don't
know why it prints null to me.
I will greatly appreciate a help please. I can't really understand
why it prints null in the second foreach, even though I already tried
various changes.

do you use Postgres with active assertions ?

postgres=# show debug_assertions ;
┌──────────────────┐
│ debug_assertions │
╞══════════════════╡
│ on │
└──────────────────┘
(1 row)

maybe you somewhere allocate less memory then it is necessary and some
memset rewrites memory with your data. When you have a problem with memory,
then you should to very carefully check a) if memory is allocated in good
context, b) if memory was allocated enough (sometimes I just allocated x +
100000 for testing).

Can you write a reproducer ? code that I can check at my home. Postgres
is pretty complex system, and it is hard to fix some issue without access
to code

This is some messy in your code

MemoryContext oldContext = MemoryContextSwitchTo( outerContext );
MemoryContextSwitchTo( oldContext );

Unfortunately, your code is not well structured - it is hard to read it.
One hint - if you find an issue - try to reduce lines of your code which is
possible until you reproduce this issue or until you understand this issue.

The sequence of statements is a little bit obscure :)

initStringInfo( &query );

*/
if( cols.len > 0 )
{
initStringInfo(&cols);
} /*IF col.len>0*/

Instead

foreach( cell, candidates ) /* foreach cell in candidates */
{

int i;

/*elog (INFO, "Ingresando a foreach");*/
idxcd = (IndexCandidate*)lfirst( cell );

if (idxcd == NULL) {
elog( INFO, "idxcd IS NULL" );
continue; /* Or is that fatal enough to break instead? */
}

you can write

idxcd = (IndexCandidate *) linitial(candidates);

The problem in your example is fact so it is a mix of copy/paste
fragments. I wrote similar code, and almost everyone I know (when I
started). But it is not possible to fix this code - start from scratch. The
code must be readable (every time). And when I write code inside some
complex environment (like Postgres is), I write code in very small
fragments, in very small steps, and every time I try to compile, restart
and check the result. Then I know what line, or what statement is the
problem.

Regards

Pavel

Best regards,

Yessica Brinkmann

El mié., 16 sept. 2020 a las 9:16, Pavel Stehule (<
pavel.stehule@gmail.com>) escribió:

st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value. This is the error.

How you get this list? It is result of SPI_exec*** ?

There is SPI_finish(), that cleaning a memory used by SPI interface,
and theoretically it can clean result of previous query.

Regards

Pavel

-------- Mensaje original --------
Asunto: Re: Problems with MemoryContextSwitchTo ()
De: Pavel Stehule
Para: Yessica Brinkmann
CC: pgsql-general

st 16. 9. 2020 v 13:32 odesílatel Yessica Brinkmann <
brinkmann.yessica@gmail.com> napsal:

Good Morning.
I will greatly appreciate a help please on this subject. I
actually did several tests already and tried a lot to fix it myself. But
I am not able to. And I really need to know this in order to
finish my university thesis. If someone can help me please.
Best regards,
Yessica Brinkmann

please, can you attach your code, and can you show the error
message?

It is hard to say what is the problem from your mail?

Regards

Pavel

El mié., 16 sept. 2020 a las 0:42, Yessica Brinkmann (<
brinkmann.yessica@gmail.com>) escribió:

Hello.
I think several of you will already remember me. I'm the one with
the IndexAdviser topic. Only that I changed my email address.
As you may recall, I am doing my thesis on the subject of
IndexAdviser modifications.
I really appreciate the help they have given me in various
Postgresql groups.
Well, I was really nearing the end of the programming part of the
thesis, when I had a problem to be able to compile my program in a moment,
and by accident some lines of source code were moved.
And for this reason, I think I have problems again with the
context switch issue, since at some point my context switch stopped working
for me, I think because of the issue that some lines of source code were
moved.
Well, the fact is that I have a function called get_columnnames,
which in the second foreach, is printing the values of idxcd-> varattnnames
[i] the null value.
This second foreach, I only do it to test if the data is really
saved well and if I can recover it properly.
And since the data is not retrieved properly, or is not saved
properly, in the following functions of my program, the value of idxcd->
varattnnames [i] continues to appear as null.
I will appreciate a lot please help, if you can tell me please why
the function prints null in the values of idxcd-> varattnnames [i], in the
second foreach, if it is due to an error in the context switch, or why it
could be .

I send my function as an attachment.

Best regards,
Yessica Brinkmann.