Skip to content

Commit cc9803f

Browse files
committed
Pg Client: fail cleanly when number of params exceeds limit (#1376)
See 1142 When the number of params exceeds the limit, Postgres successfully prepares the query but returns a number of expected equal to (actual - max - 1). Then the client reports a strange message like: "The number of parameters to execute should be consistent with the expected number of parameters = [64464] but the actual number is [130000]." With this change, an explicit error message is reported. Signed-off-by: Thomas Segismont <[email protected]>
1 parent 98bc029 commit cc9803f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/PgParamDesc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
*/
1717
package io.vertx.pgclient.impl.codec;
1818

19-
import io.vertx.sqlclient.Tuple;
2019
import io.vertx.sqlclient.impl.ErrorMessageFactory;
2120
import io.vertx.sqlclient.impl.ParamDesc;
22-
import io.vertx.pgclient.impl.util.Util;
2321
import io.vertx.sqlclient.impl.TupleInternal;
2422

2523
import java.util.Arrays;
26-
import java.util.stream.Stream;
2724

2825
/**
2926
* @author <a href="mailto:[email protected]">Emad Alblueshi</a>
@@ -43,6 +40,9 @@ DataType[] paramDataTypes() {
4340

4441
public String prepare(TupleInternal values) {
4542
int numberOfParams = values.size();
43+
if (numberOfParams > 65535) {
44+
return "The number of parameters (" + numberOfParams + ") exceeds the maximum of 65535. Use arrays or split the query.";
45+
}
4646
int paramDescLength = paramDataTypes.length;
4747
if (numberOfParams != paramDescLength) {
4848
return ErrorMessageFactory.buildWhenArgumentsLengthNotMatched(paramDescLength, numberOfParams);

vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgPreparedQueryTestBase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.vertx.pgclient.tck;
22

3+
import io.vertx.ext.unit.TestContext;
34
import io.vertx.pgclient.junit.ContainerPgRule;
5+
import io.vertx.sqlclient.Tuple;
46
import io.vertx.sqlclient.tck.PreparedQueryTestBase;
57
import org.junit.ClassRule;
8+
import org.junit.Test;
69

710
public abstract class PgPreparedQueryTestBase extends PreparedQueryTestBase {
811
@ClassRule
@@ -19,4 +22,29 @@ protected String statement(String... parts) {
1922
}
2023
return sb.toString();
2124
}
25+
26+
@Test
27+
public void testMaximumParametersExceeded(TestContext ctx) {
28+
int cnt = 33000;
29+
StringBuilder valuesString = new StringBuilder(3 * 2 * cnt);
30+
Object[] values = new Object[2 * cnt];
31+
for (int i = 0; i < cnt; i++) {
32+
values[2 * i] = i;
33+
values[2 * i + 1] = "value-" + i;
34+
if (i > 0) {
35+
valuesString.append(',');
36+
}
37+
valuesString.append("($").append(2 * i + 1).append(',').append('$').append(2 * (i + 1)).append(')');
38+
}
39+
Tuple tuple = Tuple.wrap(values);
40+
String query = String.format("INSERT INTO mutable (id, val) VALUES %s", valuesString);
41+
connect(ctx.asyncAssertSuccess(conn -> {
42+
conn.preparedQuery(query)
43+
.execute(tuple)
44+
.onComplete(ar -> {
45+
ctx.assertTrue(ar.failed());
46+
ctx.assertTrue(ar.cause().getMessage().contains("exceeds the maximum of 65535"));
47+
});
48+
}));
49+
}
2250
}

0 commit comments

Comments
 (0)