Memory leaks using refcursors

From: "Guillaume Smet" <guillaume(dot)smet(at)gmail(dot)com>
To: "PostgreSQL JDBC" <pgsql-jdbc(at)postgresql(dot)org>
Cc: thierry(dot)buffet(at)fr(dot)nurun(dot)com
Subject: Memory leaks using refcursors
Date: 2007-01-17 18:53:21
Message-ID: 1d4e0c10701171053t55e60a2epf3e395b5b97101b5@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Guillaume Smet 2007-01-17 22:22:54 Re: Memory leaks using refcursors
Previous Message Csaba Nagy 2007-01-17 17:24:22 Re: Strange error using postgres 8.2 + JDBC 8.2 driver