Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gijb0-0008CQ-PK for pgsql-general@arkaria.postgresql.org; Sun, 13 Jan 2019 17:30:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gijay-0006aL-I6 for pgsql-general@arkaria.postgresql.org; Sun, 13 Jan 2019 17:30:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gijay-0006Zc-9n for pgsql-general@lists.postgresql.org; Sun, 13 Jan 2019 17:30:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gijav-0006Wq-KU for pgsql-general@postgresql.org; Sun, 13 Jan 2019 17:30:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x0DHUgL6009802; Sun, 13 Jan 2019 12:30:42 -0500 From: Tom Lane To: Michel Pelletier cc: pgsql-general@postgresql.org Subject: Re: Question about MemoryContextRegisterResetCallback In-reply-to: References: Comments: In-reply-to Michel Pelletier message dated "Sun, 13 Jan 2019 09:00:55 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9800.1547400642.1@sss.pgh.pa.us> Date: Sun, 13 Jan 2019 12:30:42 -0500 Message-ID: <9801.1547400642@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michel Pelletier writes: > 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 suppose what you're doing is returning a pointer to a GraphBLAS object as a Datum (or part of a pass-by-ref Datum)? If so, that's not going to work terribly well, because it ignores the problem that datatype- independent code is going to assume it can copy Datum values using datumCopy() or equivalent logic. More often than not, such copying is done to move the value into a different memory context in preparation for freeing the original context. If you delete the GraphBLAS object when the original context is deleted, you now have a dangling pointer in the copy. We did invent some infrastructure awhile ago that could potentially handle this sort of situation: it's the "expanded datum" stuff. The idea here would be that your representation involving a GraphBLAS pointer would be an efficient-to-operate-on expanded object. You would need to be able to serialize and deserialize that representation into plain self-contained Datums (probably varlena blobs), but hopefully GraphBLAS is capable of going along with that. You'd still need a memory context reset callback attached to each expanded object, to free the associated GraphBLAS object --- but expanded objects are explicitly aware of which context they're in, so at least in principle that should work. (I'm not sure anyone's actually tried to build an expanded-object representation that has external resources, so we might find there are some bugs to fix there.) Take a look at src/include/utils/expandeddatum.h src/backend/utils/adt/expandeddatum.c src/backend/utils/adt/array_expanded.c src/backend/utils/adt/expandedrecord.c regards, tom lane