Re: Memory context at PG_init call ? - Mailing list pgsql-hackers

From Robert Haas
Subject Re: Memory context at PG_init call ?
Date
Msg-id CA+TgmoYZUxz3PupcXaBTfJzu0sUur8JPMcjPJA2HU_F2rjsPrQ@mail.gmail.com
Whole thread Raw
In response to Memory context at PG_init call ?  (Sandro Santilli <strk@keybit.net>)
List pgsql-hackers
On Tue, Jun 23, 2015 at 7:41 AM, Sandro Santilli <strk@keybit.net> wrote:
> Empirically, I seem to be getting the _PG_init call for a module while
> the active memory context lifetime is that of the function call which
> first needed to load the shared object.
>
> Is this the case ? Documented anywhere ?
> Initializing memory meant to be alive for the whole lifetime of a backend
> in that function is a bit complex if that's confirmed.

If you want something that lasts for the lifetime of the backend, just do

MemoryContext oldctx = MemoryContextSwitchTo(TopMemoryContext);
...
MemoryContextSwitchTo(oldctx);

I'm not sure what context you should expect in _PG_init(), although
what you mention doesn't sound unreasonable.  But generally if you
want a long-lived context you should establish that explicitly
yourself.  We try to keep short-lived contexts active whenever
possible because that avoids long-term leaks.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: less log level for success dynamic background workers for 9.5
Next
From: Robert Haas
Date:
Subject: Re: Insufficient locking for ALTER DEFAULT PRIVILEGES