pgjdbc: How to resolve the "Tried to send an out-of-range integer as a 2-byte value" error

I’m submitting a question about “Tried to send an out-of-range integer as a 2-byte value”

  • [ jdbc driver] bug report
  • feature request

Introduce

Jdbc execute a long/batch insert sql,  like: ```insert into values(?,?,?),(?,?,?),(?,?,?)...```
but client throws an error: "Tried to send an out-of-range integer as a 2-byte value:" ... 
When I review the source code, and I find the implementation like below:

Background

Maven pom.xml

<dependency>
     <groupId>org.postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>42.2.5</version>
</dependency>

Source Code

 [Line: 1442]  QueryExecutorImpl.java

  // ....
   pgStream.sendInteger2(params.getParameterCount());

 // ....

[Line: 235] PGStream.java

// ....
public void sendInteger2(int val) throws IOException {
    if (val < Short.MIN_VALUE || val > Short.MAX_VALUE) {
      throw new IOException("Tried to send an out-of-range integer as a 2-byte value: " + val);
    }

    _int2buf[0] = (byte) (val >>> 8);
    _int2buf[1] = (byte) val;
    pg_output.write(_int2buf);
  }
....

Describe the issue A clear and concise description of what the issue is.

I don't know why param count should be greater/equals than Short.MIN_VALUE (-32768) 
or less/equals than Short.MAX_VALUE (32767). 
When I execute batch sql (especially build lots of columns ), 
this error must be occurred. I can not guess the check whether  to protect the server side?  
I think the jdbc driver client should not limit the length of statement. 
We can use MAX_ALLOWED_PACKET parameter which is defined in MySQL server in the server side.
Wait for your response asap !!!

Java Version

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

OS Version

Mac OSX

PostgreSQL Version

EnterpriseDB 9.3.17.42 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-55), 64-bit

To Reproduce Steps to reproduce the behaviour:

Expected behaviour A clear and concise description of what you expected to happen. And what actually happens

Logs If possible PostgreSQL logs surrounding the occurrence of the issue Additionally logs from the driver can be obtained adding

loggerLevel=TRACE&loggerFile=pgjdbc-trace.log 

to the connection string

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 31 (20 by maintainers)

Commits related to this issue

Most upvoted comments

So what can I do ?

Did you know that what can I do sounds very close to Russian водка найду (English pronunciation is вот кан ай ду) which means I'll find Vodka ?

@pwagland , please try 42.3.7-SNAPSHOT. I would go for releasing is week.

^^^ seems like a reasonable solution.

I don’t think so.

In practice, con.prepareStatement(...) should fail if we notice more than 32K binds are detected. At the same time, preparedStatement.set...(int, ...) should fail in case user uses index exceeding 32K.

That means the assertion in sendInteger2 will never be hit if we fix the assertion.


It is sad java.sql.DatabaseMetaData provides no API for maximum number of parameters in prepared statement

inserted tuples are not logged in the WAL I’m 1000% sure they are. Otherwise physical replication would not work.