From: heikki.linnakangas at enterprisedb.com (Heikki Linnakangas) Date: Thu, 21 Jun 2012 09:52:31 +0300 Subject: [Pljava-dev] TupleDesc reference leak Message-ID: <4FE2C4AF.4000700@enterprisedb.com> Hi, A customer ran into the issue reported through the bug tracker, #1010962: http://pgfoundry.org/tracker/?func=detail&atid=334&aid=1010962&group_id=1000038. If a PL/Java function takes a ResultSet as argument, you get warnings like: WARNING: TupleDesc reference leak: TupleDesc 124e120 (139824,-1) still referenced Grepping through the source, there seems to be two places where PL/Java gets a reference to a TupleDesc by calling lookup_rowtype_tupdsc(), but fails to release it. One is in HeapTupleHeader.c: > jobject HeapTupleHeader_getTupleDesc(HeapTupleHeader ht) > { > return TupleDesc_create( > lookup_rowtype_tupdesc( > HeapTupleHeaderGetTypeId(ht), > HeapTupleHeaderGetTypMod(ht))); > } This is the one that's leaking in the test case posted in the bug tracker. Fortunately, this is easy to fix. TupleDesc_create() creates a copy of the tuple descriptor, so we just need to release it after the call. Patch attached. The other is in Type.c, in Type_fromOid function: > if(typeClass != 0) > { > TupleDesc tupleDesc = lookup_rowtype_tupdesc_noerror(typeId, -1, true); > type = (Type)UDT_registerUDT(typeClass, typeId, typeStruct, tupleDesc, false); > JNI_deleteLocalRef(typeClass); > goto finally; > } I'm not sure what to do about that one. UDT_registerUDT() puts the TupleDesc Reference to a field in the UDT instance it creates. It's not clear to me what the lifecycle of that instance is. If we just created a copy of the TupleDesc, would we leak memory? Can the UDT object persist over transactions, in which case it holds a dangling pointer to the TupleDesc? Anyway, please apply the attached patch, at least. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: fix-tupledesc-leak-1.patch Type: text/x-diff Size: 1077 bytes Desc: not available URL: