pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Dave Cramer <pg@fastcrypt.com>
Cc: Mark Lewis <mark.lewis@mir3.com>
Cc: Kris Jurka <books@ejurka.com>
Cc: Tjioe Ai Xin <xinxincute@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Can PostgreSQL do data type automated casting in
Date: Thu, 24 Nov 2005 18:00:11 +1300
Message-ID: <438548DB.8070407@opencloud.com> (raw)
In-Reply-To: <6F909ADC-23ED-40CF-A2B5-41DE61606A26@fastcrypt.com>
References: <200511211718.26679.xinxincute@gmail.com>
	<Pine.BSO.4.61.0511211806430.23994@leary.csoft.net>
	<1132616246.4154.10.camel@archimedes>
	<6F909ADC-23ED-40CF-A2B5-41DE61606A26@fastcrypt.com>

Dave Cramer wrote:
> The easier way to deal with this has already been discussed. Simply  
> bind String to the Oid.Unknown type, and let the server deal with it.
> 
> How about we make this a configuration parameter.

I've implemented this and it seems ok with one exception. This is from 
ServerPreparedStmtTest:

     public void testTypeChange() throws Exception {
         PreparedStatement pstmt = con.prepareStatement("SELECT ?");
         ((PGStatement)pstmt).setUseServerPrepare(true);

         // Prepare with int parameter.
         pstmt.setInt(1, 1);
         ResultSet rs = pstmt.executeQuery();
         assertTrue(rs.next());
         assertEquals(1, rs.getInt(1));
         assertTrue(!rs.next());

         // Change to text parameter, check it still works.
         pstmt.setString(1, "test string");
         rs = pstmt.executeQuery();
         assertTrue(rs.next());
         assertEquals("test string", rs.getString(1));
         assertTrue(!rs.next());
     }

With String bound to OID 0 this fails with:

  ERROR: invalid input syntax for integer: "test string"

What is happening is that the use of setInt() prepares a server-side 
statement with the parameter typed as integer. When setString() is later 
used, it feeds oid 0 down into the query executor which decides to reuse 
the existing statement since it is "compatible enough" as the parameter 
which was previously integer now has an unspecified type. Then the 
server tries to parse the string as an integer and breaks.

IIRC this was originally done to avoid re-preparing the statement when 
setNull(x,Types.OTHER) was done (or equivalently, setObject(x,null), 
which just calls setNull) -- which can pass oid 0 to an existing query 
with resolved types.

I guess that we should tighten the checks in the query executor so that 
it will only consider types "compatible enough" if the new value is both 
of unspecified type *and* null?

(I am also renaming Oid.INVALID to Oid.UNSPECIFIED in the driver to 
reduce confusion -- InvalidOid in the backend is indeed 0 but "invalid" 
is not the protocol-level meaning for oid 0 here, and "unknown" is an 
actual pseudotype that has a non-zero oid).

-O



view thread (20+ messages)  latest in thread

Message-ID: <438548DB.8070407@opencloud.com>
Permalink:  ../438548DB.8070407@opencloud.com/
Also on:    postgresql.org/message-id/438548DB.8070407@opencloud.com

 · 

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: oliver@opencloud.com, pg@fastcrypt.com, mark.lewis@mir3.com, books@ejurka.com, xinxincute@gmail.com
  Subject: Re: Can PostgreSQL do data type automated casting in
  In-Reply-To: <438548DB.8070407@opencloud.com>
  References: <200511211718.26679.xinxincute@gmail.com> <Pine.BSO.4.61.0511211806430.23994@leary.csoft.net> <1132616246.4154.10.camel@archimedes> <6F909ADC-23ED-40CF-A2B5-41DE61606A26@fastcrypt.com> <438548DB.8070407@opencloud.com>

* 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