Unix Technical Forum

Memory leak in vac_update_relstats ?

This is a discussion on Memory leak in vac_update_relstats ? within the pgsql Hackers forums, part of the PostgreSQL category; --> Are we leaking memory in vac_update_relstats ? /* Fetch a copy of the tuple to scribble on */ ctup ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-15-2008, 09:06 PM
Pavan Deolasee
 
Posts: n/a
Default Memory leak in vac_update_relstats ?

Are we leaking memory in vac_update_relstats ?

/* Fetch a copy of the tuple to scribble on */
ctup = SearchSysCacheCopy(RELOID,
ObjectIdGetDatum(relid),
0, 0, 0);

This copy is not subsequently freed in the function.

Thanks,
Pavan


--
Pavan Deolasee
EnterpriseDB http://www.enterprisedb.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 09:06 PM
Heikki Linnakangas
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

Pavan Deolasee wrote:
> Are we leaking memory in vac_update_relstats ?
>
> /* Fetch a copy of the tuple to scribble on */
> ctup = SearchSysCacheCopy(RELOID,
> ObjectIdGetDatum(relid),
> 0, 0, 0);
>
> This copy is not subsequently freed in the function.


It's palloc'd in the current memory context, so it's not serious. It'll
be freed at the end of the transaction, if not before that. That's the
beauty of memory contexts; no need to worry about small allocations like
that.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 09:06 PM
NikhilS
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

Hi,

>
> It's palloc'd in the current memory context, so it's not serious. It'll
> be freed at the end of the transaction, if not before that. That's the
> beauty of memory contexts; no need to worry about small allocations like
> that.



That's the beauty of memory contexts for small allocations. But because of
the 'convenience' of memory contexts we sometimes tend to not pay attention
to doing explicit pfrees. As a general rule I think allocations in
TopMemoryContext should be critically examined. I was bitten by this undue
bloat recently while developing some code and valgrind is not of much help
in such cases because of this very beauty of memory contexts .

Regards,
Nikhils

--
EnterpriseDB http://www.enterprisedb.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 09:06 PM
NikhilS
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

Hi,

That's the beauty of memory contexts for small allocations. But because of
> the 'convenience' of memory contexts we sometimes tend to not pay attention
> to doing explicit pfrees. As a general rule I think allocations in
> TopMemoryContext should be critically examined. I was bitten by this undue
> bloat recently while developing some code and valgrind is not of much help
> in such cases because of this very beauty of memory contexts .




One specific case I want to mention here is hash_create(). For local hash
tables if HASH_CONTEXT is not specified, they get created in a context which
becomes a direct child of TopMemoryContext. Wouldn't it be a better idea to
create the table in CurrentMemoryContext?

If hash_destroy() is not explicitly invoked, this can cause a lot of bloat
especially if the intention was to use the hash table only for a while.

Regards,
Nikhils

Regards,
Nikhils


--
EnterpriseDB http://www.enterprisedb.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-15-2008, 09:06 PM
Tom Lane
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

NikhilS <nikkhils@gmail.com> writes:
> One specific case I want to mention here is hash_create(). For local hash
> tables if HASH_CONTEXT is not specified, they get created in a context which
> becomes a direct child of TopMemoryContext. Wouldn't it be a better idea to
> create the table in CurrentMemoryContext?


It works that way partly for historical reasons and mostly because the
vast majority of dynahash uses are for long-lived hash tables (a quick
search says that a default of CurrentMemoryContext would be correct for
only about one in ten of the current callers). I don't see any value in
changing it.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-15-2008, 09:06 PM
Pavan Deolasee
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

On 7/20/07, Heikki Linnakangas <heikki@enterprisedb.com> wrote:
>
> Pavan Deolasee wrote:
> > Are we leaking memory in vac_update_relstats ?
> >
> > /* Fetch a copy of the tuple to scribble on */
> > ctup = SearchSysCacheCopy(RELOID,
> > ObjectIdGetDatum(relid),
> > 0, 0, 0);
> >
> > This copy is not subsequently freed in the function.

>
> It's palloc'd in the current memory context, so it's not serious. It'll
> be freed at the end of the transaction, if not before that. That's the
> beauty of memory contexts; no need to worry about small allocations like
> that.



Right. But may be for code completeness, we should add that
missing heap_freetuple.

Thanks,
Pavan




--
Pavan Deolasee
EnterpriseDB http://www.enterprisedb.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-15-2008, 09:06 PM
Tom Lane
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

"Pavan Deolasee" <pavan.deolasee@gmail.com> writes:
> On 7/20/07, Heikki Linnakangas <heikki@enterprisedb.com> wrote:
>> It's palloc'd in the current memory context, so it's not serious.


> Right. But may be for code completeness, we should add that
> missing heap_freetuple.


Personally I've been thinking of mounting an effort to get rid of
unnecessary pfree's wherever possible. Particularly in user-defined
functions, "cleaning up" at the end is a waste of code space and
cycles too, because they're typically called in contexts that are
going to be reset immediately afterward.

In the case of vac_update_relstats, it's called only once per
transaction, so there's certainly no point in being a neatnik.
Stuff you need to worry about is functions that might be called
many times in the same memory context.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-15-2008, 09:07 PM
Gregory Stark
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

"Tom Lane" <tgl@sss.pgh.pa.us> writes:

> Personally I've been thinking of mounting an effort to get rid of
> unnecessary pfree's wherever possible. Particularly in user-defined
> functions, "cleaning up" at the end is a waste of code space and
> cycles too, because they're typically called in contexts that are
> going to be reset immediately afterward.


It seems like the impact of this is self-limiting though. The worst-case is
going to be something which executes an extra pfree for every tuple. Or
perhaps one for every expression in a complex query involving lots of
expressions. Saving a few extra pfrees per tuple isn't really going to buy
many cpu cycles.

> In the case of vac_update_relstats, it's called only once per
> transaction, so there's certainly no point in being a neatnik.
> Stuff you need to worry about is functions that might be called
> many times in the same memory context.


Fwiw there are user-space functions that can't leak memory. I'm sure everyone
knows about btree operators, but pgsql also assumes that type input and output
functions don't leak memory too (or else assignment leaks memory in the
function scope memory context and in a loop...).

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-15-2008, 09:07 PM
Tom Lane
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?

Gregory Stark <stark@enterprisedb.com> writes:
> It seems like the impact of this is self-limiting though. The worst-case is
> going to be something which executes an extra pfree for every tuple. Or
> perhaps one for every expression in a complex query involving lots of
> expressions. Saving a few extra pfrees per tuple isn't really going to buy
> many cpu cycles.


I can't tell you how many profiles I've looked at in which palloc/pfree
were *the* dominant consumers of CPU cycles. I'm not sure how much
could be saved this particular way, but I wouldn't dismiss it as
uninteresting. I've actually thought about making short-term memory
contexts use a variant MemoryContext type in which pfree was a no-op and
palloc was simplified by not worrying at all about recycling space.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-15-2008, 09:07 PM
Andrew Dunstan
 
Posts: n/a
Default Re: Memory leak in vac_update_relstats ?



Tom Lane wrote:
> I've actually thought about making short-term memory
> contexts use a variant MemoryContext type in which pfree was a no-op and
> palloc was simplified by not worrying at all about recycling space.
>
>
>


That sounds like a good idea to me.

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:17 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com