Permalink
Browse files

fix: "Received resultset tuples, but no field structure for them" whe…

…n bind failure happens on 5th execution of a statement

Current "describe" approach is to set "statement/portal described" flags at "send describe" phase.
It turns out the describe might be skipped (e.g. if bind fails), then query is marked as "described"
however no field information is known.

The fix is to reset "described" flags in "Ready For Query" for the queries in
pendingDescribeStatementQueue / pendingDescribePortalQueue

fixes #811
  • Loading branch information...
vlsi committed Jan 2, 2018
1 parent 0d31d46 commit 082d00941ad5f8abf44a0785a6f086c106b3c746
@@ -53,6 +53,7 @@
import java.sql.Statement;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
@@ -1394,6 +1395,7 @@ private void sendSync() throws IOException {
pgStream.sendChar('S'); // Sync
pgStream.sendInteger4(4); // Length
pgStream.flush();
// Below "add queues" are likely not required at all
pendingExecuteQueue.add(new ExecuteRequest(sync, null, true));
pendingDescribePortalQueue.add(sync);
}
@@ -2331,8 +2333,19 @@ protected void processResults(ResultHandler handler, int flags) throws IOExcepti
}
pendingParseQueue.clear(); // No more ParseComplete messages expected.
pendingDescribeStatementQueue.clear(); // No more ParameterDescription messages expected.
pendingDescribePortalQueue.clear(); // No more RowDescription messages expected.
// Pending "describe" requests might be there in case of error
// If that is the case, reset "described" status, so the statement is properly
// described on next execution
while (!pendingDescribeStatementQueue.isEmpty()) {
DescribeRequest request = pendingDescribeStatementQueue.removeFirst();
LOGGER.log(Level.FINEST, " FE marking setStatementDescribed(false) for query {0}", request.query);
request.query.setStatementDescribed(false);
}
while (!pendingDescribePortalQueue.isEmpty()) {
SimpleQuery describePortalQuery = pendingDescribePortalQueue.removeFirst();
LOGGER.log(Level.FINEST, " FE marking setPortalDescribed(false) for query {0}", describePortalQuery);
describePortalQuery.setPortalDescribed(false);
}
pendingBindQueue.clear(); // No more BindComplete messages expected.
pendingExecuteQueue.clear(); // No more query executions expected.
break;
@@ -51,6 +51,7 @@
NOT_IMPLEMENTED("0A000"),
DATA_ERROR("22000"),
STRING_DATA_RIGHT_TRUNCATION("22001"),
NUMERIC_VALUE_OUT_OF_RANGE("22003"),
BAD_DATETIME_FORMAT("22007"),
DATETIME_OVERFLOW("22008"),
@@ -12,8 +12,10 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.PGStatement;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;
import org.postgresql.util.PSQLState;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -86,7 +88,7 @@ public GeneratedKeysTest(ReturningInQuery returningInQuery, BinaryMode binaryMod
@Override
public void setUp() throws Exception {
super.setUp();
TestUtil.createTempTable(con, "genkeys", "a serial, b text, c int");
TestUtil.createTempTable(con, "genkeys", "a serial, b varchar(5), c int");
}
@Override
@@ -450,5 +452,36 @@ public void selectWithGeneratedKeysViaNonPrepared() throws SQLException {
s.close();
}
@Test
public void breakDescribeOnFirstServerPreparedExecution() throws SQLException {
// Test code is adapted from https://github.com/pgjdbc/pgjdbc/issues/811#issuecomment-352468388
PreparedStatement ps =
con.prepareStatement("insert into genkeys(b) values(?)" + returningClause,
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "TEST");
// The below "prepareThreshold - 1" executions ensure that bind failure would happen
// exactly on prepareThreshold execution (the first one when server flips to server-prepared)
int prepareThreshold = ps.unwrap(PGStatement.class).getPrepareThreshold();
for (int i = 0; i < prepareThreshold - 1; i++) {
ps.executeUpdate();
}
try {
// Send a value that's too long on the 5th request
ps.setString(1, "TESTTESTTEST");
ps.executeUpdate();
} catch (SQLException e) {
// Expected error: org.postgresql.util.PSQLException: ERROR: value
// too long for type character varying(10)
if (!PSQLState.STRING_DATA_RIGHT_TRUNCATION.getState().equals(e.getSQLState())) {
throw e;
}
}
// Send a valid value on the next request
ps.setString(1, "TEST");
ps.executeUpdate();
}
}

0 comments on commit 082d009

Please sign in to comment.