Question about MemoryContextRegisterResetCallback

From: Michel Pelletier <pelletier(dot)michel(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Question about MemoryContextRegisterResetCallback
Date: 2019-01-13 17:00:55
Message-ID: CACxu=vKgpqDNySTpQWW6+ev-8-fwwkq90XBqOmx40V-PT0R8mQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

https://github.com/michelp/pggraphblas/blob/master/src/matrix.c#L80

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

https://github.com/michelp/pggraphblas/blob/master/test.sql#L103

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:

https://gist.github.com/michelp/1ba3cc79996b8d3a963d974224a78f2d

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2019-01-13 17:30:42 Re: Question about MemoryContextRegisterResetCallback
Previous Message Tom Lane 2019-01-13 14:35:10 Re: Static PostgreSQL Binaries (Linux + Windows)