Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 1CE429FB2B3 for ; Wed, 17 Jan 2007 14:53:55 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 14268-01 for ; Wed, 17 Jan 2007 14:53:25 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.174]) by postgresql.org (Postfix) with ESMTP id 1567C9FB2AC for ; Wed, 17 Jan 2007 14:53:24 -0400 (AST) Received: by ug-out-1314.google.com with SMTP id k3so2016608ugf for ; Wed, 17 Jan 2007 10:53:22 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:mime-version:content-type:content-transfer-encoding:content-disposition; b=WKKzB5rS++biZo7WzI7Dh5cV6V6xHPlfGBEGADPoMgHWzk0b3mRtWUpY1xxPevYLKqVDep2C+ToXXFomCnW3q6JpMAUqw0Tr0iQ37cGQhEcuNpH5WtgVb7mOgFMSYWZ9CIIVOhURRA7qN/HW3rPMmF3nBiuLe5g9SHZvMOpDOqs= Received: by 10.82.120.14 with SMTP id s14mr1590390buc.1169060001899; Wed, 17 Jan 2007 10:53:21 -0800 (PST) Received: by 10.82.170.1 with HTTP; Wed, 17 Jan 2007 10:53:21 -0800 (PST) Message-ID: <1d4e0c10701171053t55e60a2epf3e395b5b97101b5@mail.gmail.com> Date: Wed, 17 Jan 2007 19:53:21 +0100 From: "Guillaume Smet" To: "PostgreSQL JDBC" Subject: Memory leaks using refcursors Cc: thierry.buffet@fr.nurun.com MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/110 X-Sequence-Number: 16475 Hi all, We have a problem with an application which uses heavily ref cursors to fetch results from the database. We often have the following case: - BEGIN - lots of queries using ref cursors (it can run for an hour for example). We close each result set when we don't use it anymore - COMMIT Even if we close the result sets, the memory of the PostgreSQL backend is not released until the transaction is commited at the end of the script. This is really a problem for us as PostgreSQL starts swapping during the transaction. The code used to manipulate the result set is: CallableStatement myCallableStatement = myConnexion.prepareCall("{? = Call cp_TestPostgresStandAlone()}"); myCallableStatement.registerOutParameter(1, Types.OTHER); myCallableStatement.execute(); ResultSet myResultSet = (ResultSet) myCallableStatement.getObject(1); /* simple manipulations of the result set */ myResultSet.close(); myCallableStatement.close(); After this two close() calls, the memory used by the cursor should be released and it's not the case. The stored proc is something like: CREATE OR REPLACE FUNCTION cp_TestPostgresStandAlone() RETURNS refcursor AS $BODY$ DECLARE stock_cursor refcursor; BEGIN OPEN stock_cursor FOR SELECT COUNT(*) FROM TestPostgresStandAlone; RETURN stock_cursor; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; AFAICS in the source code, the refcursor of a result set is not closed when we close the result set and I don't think PostgreSQL closes it by itself before the final commit. Is there any way to fix this behaviour? Thierry (in CC:) wrote a self contained test case to reproduce this memory leak. We can post it to the list if necessary. Thanks for your help. -- Guillaume