Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6043E632D25 for ; Mon, 8 Feb 2010 15:34:40 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 54003-06-3 for ; Mon, 8 Feb 2010 19:33:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from leary.csoft.net (leary.csoft.net [205.205.214.5]) by mail.postgresql.org (Postfix) with ESMTP id EEAA0633AE8 for ; Mon, 8 Feb 2010 15:34:16 -0400 (AST) Received: by leary.csoft.net (Postfix, from userid 1112) id BCB4AA381; Mon, 8 Feb 2010 14:34:12 -0500 (EST) Date: Mon, 8 Feb 2010 14:34:12 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: "Jeenicke, Martti" cc: pgsql-jdbc@postgresql.org Subject: Re: Strange behavior after setting timestamp field to null - A bug? In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (BSO 1167 2008-08-23) MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="0-1175040670-1265657652=:21307" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.548 tagged_above=-10 required=5 tests=AWL=0.051, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/13 X-Sequence-Number: 20300 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --0-1175040670-1265657652=:21307 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed On Mon, 8 Feb 2010, Jeenicke, Martti wrote: > Hi there, > > we have noticed an odd problem/bug when working with timestamp fields in > prepared statements. The problem arises when setting the timestamp field > to null. The test class to reproduce the behavior produces the following > output: > > 08.02.2010 16:36:20 > 08.02.2010 16:36:20 > 08.02.2010 16:36:20 > 08.02.2010 17:36:20 I've looked into this a little bit. The problem is how the data gets typed when it is sent to the server. When calling setTimestamp, the driver doesn't know whether the server type that will be used will be with or without a timezone. (The SQL Standard and the JDBC API don't match up well here.) So it sends the data as type "unknown" and lets the server figure out how to deal with it because it has additional type information. When calling setNull, the driver thought it was safe to type it as timestamp with timezone to try and help type inference because NULL values look the same with or without timezones. This is looked OK, but you've caught the case here where it is not. By default, the fifth execution of a PreparedStatement will establish a more permanent execution plan that will then be re-used for later executions. So the fifth execution in your test is a setNull case and that is effectively establishing the types that a later execution will use as well. So later executions fail to pass the data as "unknown" and are instead passing it as "timestamp with tz" which does not match up with your table, so you get the mystery drift. The attached patch fixes things for me in a simple test, but I'd like to look at it a little more before I commit it. Kris Jurka --0-1175040670-1265657652=:21307 Content-Type: TEXT/PLAIN; charset=US-ASCII; name=fix-setnull-unknown.patch Content-Transfer-Encoding: BASE64 Content-ID: Content-Description: Content-Disposition: attachment; filename=fix-setnull-unknown.patch SW5kZXg6IG9yZy9wb3N0Z3Jlc3FsL2pkYmMyL0Fic3RyYWN0SmRiYzJTdGF0 ZW1lbnQuamF2YQ0KPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQ0KUkNTIGZpbGU6 IC9jdnNyb290L2pkYmMvcGdqZGJjL29yZy9wb3N0Z3Jlc3FsL2pkYmMyL0Fi c3RyYWN0SmRiYzJTdGF0ZW1lbnQuamF2YSx2DQpyZXRyaWV2aW5nIHJldmlz aW9uIDEuMTE1DQpkaWZmIC1jIC1yMS4xMTUgQWJzdHJhY3RKZGJjMlN0YXRl bWVudC5qYXZhDQoqKiogb3JnL3Bvc3RncmVzcWwvamRiYzIvQWJzdHJhY3RK ZGJjMlN0YXRlbWVudC5qYXZhCTI2IFNlcCAyMDA5IDE1OjIxOjIxIC0wMDAw CTEuMTE1DQotLS0gb3JnL3Bvc3RncmVzcWwvamRiYzIvQWJzdHJhY3RKZGJj MlN0YXRlbWVudC5qYXZhCTggRmViIDIwMTAgMTk6MzM6MjQgLTAwMDANCioq KioqKioqKioqKioqKg0KKioqIDExMDksMTExOCAqKioqDQogICAgICAgICAg ICAgIG9pZCA9IE9pZC5EQVRFOw0KICAgICAgICAgICAgICBicmVhazsNCiAg ICAgICAgICBjYXNlIFR5cGVzLlRJTUU6DQotICAgICAgICAgICAgIG9pZCA9 IE9pZC5USU1FOw0KLSAgICAgICAgICAgICBicmVhazsNCiAgICAgICAgICBj YXNlIFR5cGVzLlRJTUVTVEFNUDoNCiEgICAgICAgICAgICAgb2lkID0gT2lk LlRJTUVTVEFNUFRaOw0KICAgICAgICAgICAgICBicmVhazsNCiAgICAgICAg ICBjYXNlIFR5cGVzLkJJVDoNCiAgICAgICAgICAgICAgb2lkID0gT2lkLkJP T0w7DQotLS0gMTEwOSwxMTE2IC0tLS0NCiAgICAgICAgICAgICAgb2lkID0g T2lkLkRBVEU7DQogICAgICAgICAgICAgIGJyZWFrOw0KICAgICAgICAgIGNh c2UgVHlwZXMuVElNRToNCiAgICAgICAgICBjYXNlIFR5cGVzLlRJTUVTVEFN UDoNCiEgICAgICAgICAgICAgb2lkID0gT2lkLlVOU1BFQ0lGSUVEOw0KICAg ICAgICAgICAgICBicmVhazsNCiAgICAgICAgICBjYXNlIFR5cGVzLkJJVDoN CiAgICAgICAgICAgICAgb2lkID0gT2lkLkJPT0w7DQo= --0-1175040670-1265657652=:21307--