pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Prepared statement leak
Date: Tue, 4 Apr 2006 15:38:13 +0200
Message-ID: <200604041538.14171.peter_e@gmx.net> (raw)

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=<test>)
 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=<test>)
 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=<test>)
 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/

Attachments:

Test.java (text/x-java, 834B) download 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(); } }
prepstmt-debug.patch (text/x-diff, 2.4K) download 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 <execinfo.h> + + +/* 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); }

view thread (5+ messages)  latest in thread

Message-ID: <200604041538.14171.peter_e@gmx.net>
Permalink:  ../200604041538.14171.peter_e@gmx.net/
Also on:    postgresql.org/message-id/200604041538.14171.peter_e@gmx.net

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: peter_e@gmx.net
  Subject: Re: Prepared statement leak
  In-Reply-To: <200604041538.14171.peter_e@gmx.net>
  References: <200604041538.14171.peter_e@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox