Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dvQOV-0002zc-Uv for pgsql-jdbc@arkaria.postgresql.org; Fri, 22 Sep 2017 16:01:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dvQOU-00012J-9N for pgsql-jdbc@arkaria.postgresql.org; Fri, 22 Sep 2017 16:01:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dvQIM-0005Nj-4P for pgsql-jdbc@postgresql.org; Fri, 22 Sep 2017 15:55:14 +0000 Received: from mail.online.ua ([77.120.110.136]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dvQIE-0008Qa-SA for pgsql-jdbc@postgresql.org; Fri, 22 Sep 2017 15:55:13 +0000 Received: from [192.168.21.15] (helo=mail8.online.ua) by mail.online.ua with smtp id 1dvQIC-000GSE-Jl; Fri, 22 Sep 2017 18:55:04 +0300 MIME-Version: 1.0 Message-ID: <6945_1506095704@online.ua> To: =?UTF-8?B?IkRhdmUgQ3JhbWVyIg==?= Subject: =?UTF-8?B?UmU6IFJlOiBbSkRCQ10gVGltZXN0YW1wIHBhcmFtZXRlciBnZXRzIHNlbnQgYXMgZGF0ZQ==?= Date: Fri, 22 Sep 2017 18:55:04 +0300 X-Mailer: Online.ua From: =?UTF-8?B?R2VueWE=?= Cc: =?UTF-8?B?Ikxpc3Qi?= Content-Transfer-Encoding: 8bit Content-Type: text/html; charset=UTF-8 List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org

Updated with Types.TIMESTAMP. Did not help. Though, I did not expect it to, since it's different algorithm branch.

 

 
On 22 September 2017 at 06:43, Genya <genyap@online.ua> wrote:

I'm facing quite weird situation working on pure JDBC Java code connected to Postgres DB.

The SQL used for PreparedStatement is relatively simple:

SELECT * FROM test t INNER JOIN ....

WHERE  t.id = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time <= ?::TIMESTAMP WITHOUT TIME ZONE)

 

t.date is [date NOT NULL]
t.time is [time without time zone NOT NULL]

 

So, Java code to prepare and set parameters is as following:

PreparedStatement statement = connection.prepareStatement(SQL); 

statement.setInt(1, id);

if (date== null) {

you are changing the type of the parameter here when you setNull Types.DATE. use Types.TIMESTAMP. 
 
 

   statement.setNull(2, Types.DATE);
   statement.setNull(3, Types.DATE);
}

else {
   statement.setTimestamp(2, Timestamp.valueOf(date));
   statement.setTimestamp(3, Timestamp.valueOf(date));
}

ResultSet rs = statement.executeQuery();

 

The issue is: though Java code sets date parameters to PreparedStatement correctly (checked with logging) i.e. $1 = '29', $2 = '2017-01-14:13:00', $3 = '2017-01-14:13:00' , but actual query performed by PostgreSQL server (logging) SOMETIMES shows parameters used GMT DETAIL:  parameters: $1 = '29', $2 = '2017-01-14', $3 = '2017-01-14'.  Strangest thing that in 95% cases of same request with different params the output (behaviour and parameters) is correct, i.e. parameters are set and seen as DATE + TIME both from API and PostgreSQL. Moreover, repeteadly passing problematic parameters finally gets right query's behavior and output, sometimes after few tries or Java application restart. Worth mentioning, Java app does not do any "caching" or data modification - it's simple REST API for DB calls.

I'm not sure if it might be something very simple (JDBC/DB configuration) or a bug in postgres JDBC driver - since there is no other sofware to blame. Java code seemingly does everything right but PostgresSQL SOMETIMES receives different parameters (date instead of date + time).

I've tried to enable postgres JDBC driver logging as it's said in documentation but did not manage it to operate with Spring Boot logging.