X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D67C711F6201 for ; Tue, 4 Apr 2006 10:38:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 06754-08 for ; Tue, 4 Apr 2006 10:38:38 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from gauss.credativ.com (ipx11302.ipxserver.de [212.112.227.254]) by postgresql.org (Postfix) with ESMTP id C118711F60B9 for ; Tue, 4 Apr 2006 10:38:35 -0300 (ADT) Received: from bell.credativ.de (p50896F34.dip.t-dialin.net [80.137.111.52]) (using TLSv1 with cipher EXP1024-RC4-SHA (56/128 bits)) (No client certificate requested) by gauss.credativ.com (Postfix) with ESMTP id 2C14AD75F2 for ; Tue, 4 Apr 2006 15:38:26 +0200 (CEST) From: Peter Eisentraut To: pgsql-jdbc@postgresql.org Subject: Prepared statement leak Date: Tue, 4 Apr 2006 15:38:13 +0200 User-Agent: KMail/1.9.1 MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_GbnMEyCFsbgAWoN" Message-Id: <200604041538.14171.peter_e@gmx.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.105 required=5 tests=[AWL=0.105] X-Spam-Score: 0.105 X-Spam-Level: X-Archive-Number: 200604/33 X-Sequence-Number: 14967 --Boundary-00=_GbnMEyCFsbgAWoN Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline We seem to have identified a prepared statement leak in the JDBC driver. The actual application runs through Hibernate, so we have attempted to isolate the problem here. In the field, the problem causes the PostgreSQL server to run out of memory in linear time. We have tested with versions postgresql-8.0-315.jdbc3.jar postgresql-8.1-405.jdbc3.jar postgresql-8.2dev-501.jdbc3.jar with no difference. The two prerequisites for this problem to appear are: 1. PreparedStatement objects are rebound with different types, which causes the JDBC driver to replan the statement. 2. Batches are used. Attached is a test program that exhibits this. Create a database test with a table create table test (a int, b text); and run the program. I was initially at a loss about how to debug this problem so I wrote a patch for the backend to trace the prepared statements table, which turns out to clearly show the leak. The attached patch is for PostgreSQL 8.0 and writes the interesting output if log_min_messages is debug1. (There is some extra memory debugging code in there that I would not advise you to use.) Running the test program you see this in the server log near the end: DEBUG: prepared statement hash table size = 100 It should be near 0, of course. In the JDBC driver log output you see blocks like this: batch execute 3 queries, handler=org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler@750159, maxRows=0, fetchSiz FE=> Parse(stmt=S_1,query="INSERT INTO test VALUES ($1, $2)",oids={23,1042}) FE=> Bind(stmt=S_1,portal=null,$1=<101>,$2=) FE=> Describe(portal=null) FE=> Execute(portal=null,limit=1) FE=> Parse(stmt=S_2,query="INSERT INTO test VALUES ($1, $2)",oids={23,1043}) FE=> Bind(stmt=S_2,portal=null,$1=<201>,$2=) FE=> Describe(portal=null) FE=> Execute(portal=null,limit=1) FE=> Parse(stmt=S_3,query="INSERT INTO test VALUES ($1, $2)",oids={23,1042}) FE=> Bind(stmt=S_3,portal=null,$1=<301>,$2=) FE=> Describe(portal=null) FE=> Execute(portal=null,limit=1) FE=> Sync <=BE ParseComplete [S_3] <=BE BindComplete [null] <=BE NoData <=BE CommandStatus(INSERT 20239 1) <=BE ParseComplete [S_3] <=BE BindComplete [null] <=BE NoData <=BE CommandStatus(INSERT 20240 1) <=BE ParseComplete [S_3] <=BE BindComplete [null] <=BE NoData <=BE CommandStatus(INSERT 20241 1) <=BE ReadyForQuery(I) Notice that it seems to forget out S_1 and S_2 along the way. -- Peter Eisentraut http://developer.postgresql.org/~petere/ --Boundary-00=_GbnMEyCFsbgAWoN Content-Type: text/x-java; charset="us-ascii"; name="Test.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Test.java" import java.sql.*; class Test { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:6543/test?loglevel=2", "pei", ""); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO test VALUES (?, ?);"); for (int i = 0; i < 100; i++) { pstmt.setInt(1, 100 + i); pstmt.setObject(2, "test", Types.CHAR); pstmt.addBatch(); pstmt.setInt(1, 200 + i); pstmt.setObject(2, "test", Types.VARCHAR); pstmt.addBatch(); pstmt.setInt(1, 300 + i); pstmt.setObject(2, "test", Types.CHAR); pstmt.addBatch(); pstmt.executeBatch(); pstmt.clearBatch(); } pstmt.close(); pstmt = conn.prepareStatement("SELECT * FROM test;"); pstmt.execute(); pstmt.close(); conn.close(); } } --Boundary-00=_GbnMEyCFsbgAWoN Content-Type: text/x-diff; charset="us-ascii"; name="prepstmt-debug.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="prepstmt-debug.patch" diff -ur ../cvs-pgsql/src/backend/commands/prepare.c ./src/backend/commands/prepare.c --- ../cvs-pgsql/src/backend/commands/prepare.c 2006-01-14 17:09:45.000000000 +0100 +++ ./src/backend/commands/prepare.c 2006-03-31 22:41:45.000000000 +0200 @@ -357,6 +357,9 @@ entry->context = entrycxt; MemoryContextSwitchTo(oldcxt); + + elog(DEBUG1, "StorePreparedStatement(%s, %s, ...)", stmt_name, query_string); + elog(DEBUG1, "prepared statement hash table size = %ld", prepared_queries->hctl->nentries); } /* @@ -500,6 +503,9 @@ /* Now we can remove the hash table entry */ hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL); } + + elog(DEBUG1, "DropPreparedStatement(%s)", stmt_name); + elog(DEBUG1, "prepared statement hash table size = %ld", prepared_queries->hctl->nentries); } /* diff -ur ../cvs-pgsql/src/backend/tcop/postgres.c ./src/backend/tcop/postgres.c --- ../cvs-pgsql/src/backend/tcop/postgres.c 2006-01-14 17:09:48.000000000 +0100 +++ ./src/backend/tcop/postgres.c 2006-03-31 23:18:35.000000000 +0200 @@ -1162,6 +1162,8 @@ ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); oldcontext = MemoryContextSwitchTo(unnamed_stmt_context); + + elog(DEBUG1, "unnamed prepared statement setup"); } QueryContext = CurrentMemoryContext; diff -ur ../cvs-pgsql/src/backend/utils/mmgr/mcxt.c ./src/backend/utils/mmgr/mcxt.c --- ../cvs-pgsql/src/backend/utils/mmgr/mcxt.c 2005-01-04 20:40:57.000000000 +0100 +++ ./src/backend/utils/mmgr/mcxt.c 2006-04-01 13:11:16.000000000 +0200 @@ -23,6 +23,28 @@ #include "nodes/memnodes.h" #include "utils/memutils.h" +#include "utils/guc.h" + +#include + + +/* Obtain a backtrace and print it to stderr. */ +static void +print_trace(void) +{ + void *array[10]; + size_t size; + char **strings; + size_t i; + + size = backtrace(array, 10); + strings = backtrace_symbols(array, size); + + for (i = 0; i < size; i++) + fprintf (stderr, "BT(%d): %s\n", i, strings[i]); + + free (strings); +} /***************************************************************************** @@ -502,6 +524,13 @@ elog(ERROR, "invalid memory alloc request size %lu", (unsigned long) size); + if (log_min_messages <= DEBUG2) + { + fprintf(stderr, "MemoryContextAlloc(%s, %u)\n", context->name, size); + print_trace(); + MemoryContextStats(TopMemoryContext); + } + return (*context->methods->alloc) (context, size); } --Boundary-00=_GbnMEyCFsbgAWoN--