[postgis-users] How/where does postgis hook a callback to free cached geos structures?

Stephen Woodbridge woodbri at swoodbridge.com
Sun Apr 21 12:03:16 PDT 2013


Hi Mark,

I'm trying to rewrite the wrappers for the pagc address standardizer 
such that I can create and cache the standardizer obj in a per query 
cache. I think the following code modeled after GetGeomCache will do 
what I need. The problem I'm having is that I need to somehow hook the 
query shutdown code with a callback that will allow me to free the 
standardizer.


void FreeStdCache(StdCache * cache)
{
   // free the cached objects
}

StdCache *GetStdCache(FunctionCallInfoData *fcinfo)
{
   MemoryContext old_context;
   StdCache *cache = fcinfo->flinfo->fn_extra;
   if (! cache) {
     old_context = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
     cache = palloc(sizeof(StdCache));
     MemoryContextSwitchTo(old_context);
     cache->std = std_init();
     fcinfo->flinfo->fn_extra = cache;

     // ########## not sure how to do the following #############
     ExprContext *econtext = ?????;
     RegisterExprContextCallback(econtext, FreeStdCache, cache);
   }
   return cache;
}

So my function is not an SRF. I would get called like:

select * from standardize_address(
   'lexicon', 'gazeteer', 'rules',
   '123 main st', 'boston ma 02001');

as a single request where we would construct the standardizer and then 
free it. But in a query like the following, we would construct it, cache 
it for each record, and free is when query shutdowns.

select (std).* from (
   select standardize_address('lexicon', 'gazeteer', 'rules', micro, 
macro) as std from table_to_standardize) as foo;


I'm not sure if I can use RegisterExprContextCallback() to do this or of 
there is a better way. And not sure how to get econtext? I think that 
might only be available for SRF functions.

I saw Mark's inquiry here:

http://postgresql.1045698.n5.nabble.com/Any-advice-about-function-caching-td1936551.html

but could not find the code that registers the callback in postgis.

Here is a similar post:

http://web.archiveorange.com/archive/v/alpsnw9p7b8CWMh7hBPj

But neither have an example of how the issue was resolved. So a little 
help or pointer would be appreciated.

Thanks,
   -Steve


More information about the postgis-users mailing list