[postgis-devel] [off-topic] Integrating C++ singletons into postgresql extensions???

Sandro Santilli strk at keybit.net
Sat Oct 18 00:24:17 PDT 2014


On Fri, Oct 17, 2014 at 02:32:54PM -0400, Stephen Woodbridge wrote:

> 1. Any insights into singletons working/not working within the
> postgresql server extensions? My understanding is that these are
> implemented as global static classes with a lifetime of the process
> and they have no destructor, so I'm a little worried about memory
> leaks or multiple queries sharing the singleton.

They work fine, you'll have one copy of your singletons per session.
You'll have to take care of leaks and state (single-threaded).

See postgis_module.c, you can define _PG_init and _PG_fini functions
for setup/teardown.

> 2. Is there another way of doing this for C++ code in the server?

The problem with singleton is usually creation/destruction order,
if you don't control it carefully you may have bad surprises.
The init/fini facility may help with that.

> 3. What does PostGIS or GEOS do for logging?

PostGIS registers a function with GEOS, which uses it for notices
and errors. The function calls PostgreSQL functions that internally
have a state driving where the log is actually sent.

--strk;

  ()   Free GIS & Flash consultant/developer
  /\   http://strk.keybit.net/services.html



More information about the postgis-devel mailing list