pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Éric Paré <paree@LEXUM.UMontreal.CA>
To: pgsql-jdbc@postgresql.org
Subject: using index with bigint pkey
Date: Thu, 25 Nov 2004 18:39:36 -0500
Message-ID: <1101425976.3726.56.camel@localhost.localdomain> (raw)

Problem (I think) : planner usage of an index that is based on a bigint
field through jdbc ?

1) Planner perspective, in psql for example

I found that running a simple query on a bigint index does not behave as
expected in the planner.  This query does not use the index on 'a pkey
id column' if it is of bigint type :

create table test (
 id bigint primary key,
 field text not null
);

This will never use the index, whatever size of table, and is
independant of calling analyze.
explain select * from test where id = 123;

Solution: cast
explain select * from test where id = 123::bigint;
explain select * from test where id = '123';

This is the way the planner behaves, as far as shown by 'explain'.  I
don't really like this example since I wouldn't expect the planner to
use an index on an empty table.  It just serves the purpose.


2) Jdbc perspective

The setLong javadoc for PreparedStatement states that it prepares the
query sending the equivalent of the postgresql bigint parameter.

When I look at query activities in pg_stat_activity to feel the behavior
of database usage of my Java program, it shows that a query is ran
without casting ::bigint or 'string'.  Since the table is growing big, I
see that the queries aren't using the index on my bigint key (slow
queries).

I'd guess this is a postgresql bug, not a jdbc bug, right ?  Or is this
a feature ?

Patch solution with postgersql-jdbc :
I replaced setLong with setString(myLong.toString()) which casts to a
quoted string representation and uses the index.  Works for me.

Note on setup :
- postgresql 7.4.1 server
- postgresql-7.4.1-jdbc3.jar

-- 
Éric Paré for/pour LexUM
Université de Montréal
Centre de recherche en droit public
C.P. 6128, succ. Centre-ville
Montréal (Qc) Canada H3C 3J7
+1 514-343-6111 #0873
paree@lexum.umontreal.ca




view thread (2+ messages)  latest in thread

Message-ID: <1101425976.3726.56.camel@localhost.localdomain>
Permalink:  ../1101425976.3726.56.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1101425976.3726.56.camel@localhost.localdomain

 · 

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: paree@LEXUM.UMontreal.CA
  Subject: Re: using index with bigint pkey
  In-Reply-To: <1101425976.3726.56.camel@localhost.localdomain>
  References: <1101425976.3726.56.camel@localhost.localdomain>

* 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