Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 39A9B1EC5BE8; Thu, 2 Feb 2012 19:30:37 -0400 (AST) Received: from mail-yx0-f174.google.com ([209.85.213.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rt66t-0004N9-2p; Thu, 02 Feb 2012 23:30:37 +0000 Received: by yenm8 with SMTP id m8so1384838yen.19 for ; Thu, 02 Feb 2012 15:30:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=dqFDtphk58W0tprPC5lMj2LULxWqYCXPgOG47+ZwOgk=; b=DjlrbM2O4S7xp+yixp8Btuzp7jpBXio1CO3uAj7NXIZyLmZmRhXFAr7L1lYQ8yvflJ STv3KsE5ZzH7Ro+vqJGAeT2V1C4AILK99uPlJp77B0q+JVUC5an80X/WJ2s4KDj8PMcp ju6TEghe2stJ7x7gqeOTO/h8hwcvkmxK8zUp4= Received: by 10.236.179.38 with SMTP id g26mr7319240yhm.100.1328225421267; Thu, 02 Feb 2012 15:30:21 -0800 (PST) Received: from sexy.corp.trumpet.io ([207.86.77.58]) by mx.google.com with ESMTPS id n72sm6102140yhh.21.2012.02.02.15.30.18 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 02 Feb 2012 15:30:19 -0800 (PST) Subject: Re: [JDBC] BUG #6293: JDBC driver performance Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=windows-1252 From: Steven Schlansker In-Reply-To: Date: Thu, 2 Feb 2012 15:30:17 -0800 Cc: Teun Hoogendoorn , pgsql-bugs@postgresql.org, pgsql-jdbc@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <201111150859.pAF8xKZW012012@wwwmaster.postgresql.org> To: Kris Jurka X-Mailer: Apple Mail (2.1251.1) X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201202/44 X-Sequence-Number: 32387 On Nov 16, 2011, at 1:29 PM, Kris Jurka wrote: >=20 >=20 > On Tue, 15 Nov 2011, Teun Hoogendoorn wrote: >=20 >>=20 >> The following bug has been logged online: >>=20 >> Bug reference: 6293 >> PostgreSQL version: 9.1 >> Description: JDBC driver performance >> Details:=20 >>=20 >> Using the postgresql-9.1-901.jdbc3.jar driver instead of >> postgresql-9.0-801.jdbc3.jar drops performance dramatically.=20 >>=20 >> I think it has something to do with using ResultSetMetaData in Java. = The >> postgres log shows me hundreds of identical query's when retrieving = the >> ResultSetMetaData for a single query. I'm not using an ORM framework, = just >> simple JDBC calls. >=20 > The 9.1 JDBC driver was changed to try and fetch all metadata for the=20= > entire resultset in one query instead of potentially issuing multiple=20= > queries for each column. So this change was supposed to improve = things. >=20 > Looking at the code, the caching pattern has changed slightly, so now = it's=20 > important to hold onto the same ResultSetMetaData instance. That is = you=20 > need to do: >=20 ... >=20 > Does this explain your problem? If not, can you provide more details = on=20 > how you access and use ResultSetMetaData? I can independently confirm this problem, and it was quite a surprise to=20= us when we upgraded! There is another unfortunate side effect - the updateXXX methods=20 (i.e. AbstractJdbc2ResultSet.updateString) now are horrifyingly slow. = Every invocation seems to call = AbstractJdbc2ResultSetMetaData.getBaseColumnName on a *new* ResultSetMetaData. We have some code which reads like: row.updateString("address", addr.getAddress()); row.updateString("address2", addr.getAddress2()); row.updateString("city", addr.getCity()); row.updateString("state", addr.getState()); row.updateString("zip", addr.getPostalCode()); =85 (snip 20-some similar lines) Each of these does (AbstractJdbc2ResultSet:2932) PGResultSetMetaData md =3D (PGResultSetMetaData)getMetaData(); updateValues.put(md.getBaseColumnName(columnIndex), value); And each getMetaData() gets a new one (Jdbc4ResultSet:31) public java.sql.ResultSetMetaData getMetaData() throws SQLException { checkClosed(); return new Jdbc4ResultSetMetaData(connection, fields); } Which then builds the huge query referred to in the original bug report. On my (very small) database, it takes ~400ms to execute the giant query that is constructed. So the end result is that something that used to work now becomes terrifyingly slow if you update to 9.1-901. You can definitely argue that doing a bunch of updateString calls is not the right thing to do, but this *did* use to work so I would call this a potentially very serious regression. An easy way to mitigate this a little bit would be to cache the metadata within the ResultSet so it is only constructed once. This would = alleviate the immediate problem from my viewpoint. If you would like more information, I do have the problem reproduced = here in a controlled environment and would love nothing more than to test patches or provide whatever information might be helpful to fix this = bug. I'll even throw in a patch if you can tell me the "right" way to fix = this :-) Regards, Steven Schlansker