pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Ken Johanson <pg-user@kensystem.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Timestamp without Timezone and differing client / server tzs
Date: Wed, 09 Jul 2008 17:55:56 +1200
Message-ID: <487452EC.1010105@opencloud.com> (raw)
In-Reply-To: <48744BE0.1010304@kensystem.com>
References: <4873154E.6020808@kensystem.com>
	<4873201F.1000700@opencloud.com>
	<487439D8.2050508@kensystem.com>
	<48743B53.4000002@opencloud.com>
	<48743F0C.6020504@kensystem.com>
	<4874419A.6030705@opencloud.com>
	<48744BE0.1010304@kensystem.com>

Ken Johanson wrote:

> 
> The servers and their schema (use-of ts w/o tz are not under my control; adding with-timezone would likely break apps that transmit date values with offset already normalized)

This is the root of your problems, you're trying to use WITHOUT TIMEZONE 
to represent an instant in time which only makes sense when you're using 
the same timezone absolutely everywhere - and you're not. (Basically, 
you've got an implicit extra bit of data floating around - the server 
timezone - that's not correctly tied to your column data)

But if you can't change the schema, you can certainly work around it in 
your application:

> Just to pass-in a timestamp, and which ever database it is sent to, be 
> stored with its real atomic (integer) value (ie string conversion 
> normalized to what db uses).
> 
> Simplified:
> 
> Timestamp NOW = new Timestamp(System.currentTimeMillis());
> for (int i=0; i<SERVERS.length; i++)
> {
> Connection con = ....//SERVERS[i] etc; each server exists in different 
> timezone, and datetime/timestamps cols do not store zone.
> PreparedStatement ps = con.prepareStatement("INSERT INTO tbl 
> (lastModified) VALUES (?)");
> ps.setTimestamp(1, NOW));
> ps.executeUpdate();
> }

Ok, so essentially you want to pass a timestamp-with-timezone value to 
the server, then store it as timestamp-without-timezone, using the 
server's timezone to do the cast, right?

What is biting you here is the server-side inference of the datatype of 
your parameter. To support both WITH TIMEZONE and WITHOUT TIMEZONE via 
setTimestamp(), the driver has to pass the parameter with an unknown 
type and let the server infer the actual type.

For example if a client sets a timestamp of "05:00 +1000" (either via an 
explicit Calendar, or because their default timezone is +1000) to insert 
into a WITHOUT TIMEZONE column, the only sensible result is to insert 
that as "05:00" regardless of the server's timezone setting. The client 
sees the value as 05:00, so the only sensible thing the driver can do is 
to insert it as 05:00.

The driver takes advantage of the fact that literals interpreted as 
WITHOUT TIMEZONE completely ignore the timezone specification in this 
case (and when inserting into a WITHOUT TIMEZONE column, the type of an 
unknown-type parameter is inferred to be WITHOUT TIMEZONE).

If the driver passed that literal as a WITH TIMEZONE type, it'd first 
get converted to a seconds-since-epoch value (respecting the timezone 
specification) and then converted to WITHOUT TIMEZONE using the server's 
timezone. If the server's timezone is not +1000, you get something other 
than "05:00" inserted, which isn't what the client asked for.

So the driver deliberately doesn't do that, and in fact jumps through 
some hoops to make sure it doesn't happen.

However, "interpret as WITH TIMEZONE then cast" is exactly the behaviour 
you want in this case. You can get that behaviour via some explicit 
casting, something like this:

CAST((CAST ? AS TIMESTAMP WITH TIMEZONE) AS TIMESTAMP WITHOUT TIMEZONE)

(modulo syntax errors; not sure if the outermost CAST is needed, off the 
top of my head)

-O



view thread (15+ messages)  latest in thread

Message-ID: <487452EC.1010105@opencloud.com>
Permalink:  ../487452EC.1010105@opencloud.com/
Also on:    postgresql.org/message-id/487452EC.1010105@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-user@kensystem.com
  Subject: Re: Timestamp without Timezone and differing client / server tzs
  In-Reply-To: <487452EC.1010105@opencloud.com>
  References: <4873154E.6020808@kensystem.com> <4873201F.1000700@opencloud.com> <487439D8.2050508@kensystem.com> <48743B53.4000002@opencloud.com> <48743F0C.6020504@kensystem.com> <4874419A.6030705@opencloud.com> <48744BE0.1010304@kensystem.com> <487452EC.1010105@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