Question about MemoryContextRegisterResetCallback - Mailing list pgsql-general

From Michel Pelletier
Subject Question about MemoryContextRegisterResetCallback
Date
Msg-id CACxu=vKgpqDNySTpQWW6+ev-8-fwwkq90XBqOmx40V-PT0R8mQ@mail.gmail.com
Whole thread Raw
Responses Re: Question about MemoryContextRegisterResetCallback  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hello,

I'm working on an extension to wrap the GraphBLAS linear algebra package.  GraphBLAS provides a very flexible API over adjacency matrices for solving graph problems.  I've got Matrix and Vector types wrapped, build aggregators and extraction functions to pivot tables into matrices and back, and may of the core operations are supported for just one of the 960 different semirings that GraphBLAS supports, but i'm making good progress and with some advanced macro'ing I hope to provide complete API access.

This is no doubt the most complex bit of C wrapper I've done for postgres, and I've run into a bit of a snag.  GraphBLAS objects are opaque handles that have their own new/free functions.  After reading mmgr/README I have registered a callback with CurrentMemoryContext during my aggregator function that builds values. 


I've got tests that work very well, up until I declare a matrix or vector in a plpgsql function.  


When using these objects from a function, their free function seems be be called prematurely, as GraphBLAS raises an error that the object isn't initialized when it tries to compare two matrices with 'matrix_eq' (the free function "uninitializes" a handle).  If I use CurTransactionContext instead of CurrentMemoryContext, the function doesn't fail, but the server segfaults on rollback.

For the brave and curious the test can reproduce the error, if you have docker installed, just clone the repo and run './test.sh'.  (The first build takes a while due to compiling GraphBLAS). Here's an example failure:


Obviously there is something I'm doing wrong about these callbacks, thinking my free function is getting called immediately after the statement that creates it, so I'm not sure what context to register it under.  Should I create a new one?  Register it to the CurrentMemoryContext parent maybe?  Any help from the gurus on this would be greatly appreciated!

Thanks,

-Michel

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Static PostgreSQL Binaries (Linux + Windows)
Next
From: Tom Lane
Date:
Subject: Re: Question about MemoryContextRegisterResetCallback