[postgis-devel] [postgis-users] Memory management in postgres (with liblwgeom functions in particular)

Paul Ramsey pramsey at cleverelephant.ca
Mon Mar 30 08:59:59 PDT 2015


On March 30, 2015 at 8:57:14 AM, Igor Stassiy (istassiy at gmail.com) wrote:
> > Let me ask one more question: will the memory allocated by malloc  
> in myextension also be allocated within a memory context by postgres  
> or not really?

No, dealing with external libraries must be done w/ care, because they will allocate memory outside the palloc memory manager so it is easy to accidentally leak. Which is a problem in the context of a long-running process like postgres.

- In the case of GEOS, we are just Really Careful to not leak memory.
- For libxml, it turns out that libxml allows callers to over-ride the default allocator/deallocators, so we swap in palloc/pfree and everything is good.
- For liblwgeom, of course we have the ability to provide custom allocator/deallocator and we do that there also.

In C++ you can over-ride new and delete to ensure your heap allocations are done using the PgSQL memory manager. How effective this is will depend to some extent on how complex your library is.

ATB,

P 



More information about the postgis-devel mailing list