Re: Problems with MemoryContextSwitchTo ()

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Yessica Brinkmann <brinkmann(dot)yessica(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Problems with MemoryContextSwitchTo ()
Date: 2020-09-17 06:03:57
Message-ID: CAFj8pRBaSiWXVHE5tFW94DjjE0MvVp40kcZ_OXv12HTxXaqFQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

>
>
> čt 17. 9. 2020 v 3:16 odesílatel Yessica Brinkmann <
> brinkmann(dot)yessica(at)gmail(dot)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(dot)stehule(at)gmail(dot)com>) escribió:
>>
>>>
>>>
>>> st 16. 9. 2020 v 15:09 odesílatel Yessica Brinkmann <
>>> brinkmann(dot)yessica(at)gmail(dot)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(dot)yessica(at)gmail(dot)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(dot)yessica(at)gmail(dot)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.
>>>>>>
>>>>>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Øystein Kolsrud 2020-09-17 09:19:07 PostgreSQL processes use large amount of private memory on Windows
Previous Message Pavel Stehule 2020-09-17 04:09:21 Re: Problems with MemoryContextSwitchTo ()