JDBC patch, so that bigint indexes get used

From: Teofilis Martisius <teo(at)taurus(dot)mediaworks(dot)lt>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: JDBC patch, so that bigint indexes get used
Date: 2002-08-29 11:44:29
Message-ID: Pine.LNX.4.21.0208291443220.32518-100000@taurus.mediaworks.lt
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello,

Are you aware of this old problem that postgresql doesn't use bigint indexes
when comparing with integers? Well, adding quotes or explicit casts fixes this.

=> create table aaa (aaa bigint);
=> create index aaa_aaa on aaa(aaa);
=> explain select * from aaa where aaa=2;
Seq Scan on aaa (cost=0.00..22.50 rows=5 width=8)
=> explain select * from aaa where aaa='2';
Index Scan using aaa_aaa on aaa (cost=0.00..17.07 rows=5 width=8)
=> explain select * from aaa where aaa=2::bigint;
Index Scan using aaa_aaa on aaa (cost=0.00..17.07 rows=5 width=8)

I just finished fixing JDBC driver (one that comes with postgres 7.2.2)
to add quotes to PreparedStatement.setLong(), and PreparedStatement.setObject()
methods, when setting numeric values. It gave me a big performance improvement.

I appended the patch. Apply it to $postgresql/src/interfaces/jdbc tree.

Teofilis Martisius
teo(at)mediaworks(dot)lt

diff -u -r /usr/src/postgresql-7.2.2/src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java ./org/postgresql/jdbc1/PreparedStatement.java
--- /usr/src/postgresql-7.2.2/src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java 2002-01-15 09:37:33.000000000 +0200
+++ ./org/postgresql/jdbc1/PreparedStatement.java 2002-08-29 15:47:23.000000000 +0200
@@ -203,7 +203,7 @@
*/
public void setLong(int parameterIndex, long x) throws SQLException
{
- set(parameterIndex, Long.toString(x));
+ set(parameterIndex, "'"+Long.toString(x)+"'");
}

/*
diff -u -r /usr/src/postgresql-7.2.2/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java ./org/postgresql/jdbc2/PreparedStatement.java
--- /usr/src/postgresql-7.2.2/src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java 2002-01-15 09:37:33.000000000 +0200
+++ ./org/postgresql/jdbc2/PreparedStatement.java 2002-08-29 16:17:32.000000000 +0200
@@ -211,7 +211,7 @@
*/
public void setLong(int parameterIndex, long x) throws SQLException
{
- set(parameterIndex, Long.toString(x));
+ set(parameterIndex, "'"+Long.toString(x)+"'");
}

/*
@@ -674,7 +674,7 @@
if (x instanceof Boolean)
set(parameterIndex, ((Boolean)x).booleanValue() ? "1" : "0");
else
- set(parameterIndex, x.toString());
+ set(parameterIndex, "'"+x.toString()+"'");
break;
case Types.CHAR:
case Types.VARCHAR:

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Tom Miller 2002-08-29 13:25:19 uPortal
Previous Message Padraic Renaghan 2002-08-29 03:43:04 Re: Batch Update updatecounts when error happens