[geos-devel] geom_accum and memory management

strk strk at keybit.net
Tue Oct 28 12:18:32 EST 2003


The introduced geom_accum function will force a copy of DETOASTED
geometries to allow for final functions release of memory associated
with each array element. Without an explicit copy, PG_DETOAST_DATUM
might or not allocate new memory, so we will not be safe explicitly
pfree'ing result.

Now. 

Final aggregate functions (fastcollect, fastunion) will release
memory associated with each array element to reduce old memory
usage while allocating new memory for the resulting objects.

If we did not force a copy in the state transaction functions,
we would not able to tell whether or not we have a safely releasable
memory.

On the other hand, since the final functions can be invoked by the
user, they can be fed by an array of geometries NOT constructed by
the state transaction function, thus not guaranteed to be made of
safely releasable geometry objects. If you try yourself, you'll 
the backend die:

	select unite_finalfunc('{POINT(0 0 0), POINT(0 0 0)}');

1) One alternative to this is to just forget about prematurely pfreeing
   objects.

2) Another alternative is to use a two-dimensional array having a
   GEOMETRY and a flag stating its allocation status.

3) Yet another alternative is to not tell anyone about the presence
   of final function :)

Solution 2 will make user call more bad-looking and not error prone
(user can pretend it is safe to delete hand written geometries which
is not!), but will alow for a finer grained memory control. Ie. in
state transaction functions we might call PG_DETOAST_DATUM without
forcing a copy and then check whether geometry has been copied or not
thus probably reducing memory copies. Of course this has to be tested
since we won't be able to move allocation to an higher memory context,
and this seems to be really needed.

Solution 1 will be easier to implement, but might be memory expensive
(not much more then worst case for both collect and union).

Solution 3 is evil! ... but already implemented !

Place your bet...

--strk;




More information about the geos-devel mailing list