Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import io.vertx.sqlclient.codec.CommandResponse;
import io.vertx.sqlclient.spi.protocol.ExtendedQueryCommand;

import java.util.regex.Pattern;

public class ExtendedQueryPgCommandMessage<R, C extends ExtendedQueryCommand<R>> extends QueryBasePgCommandMessage<R, C> {

private PgEncoder encoder;

private static final String TABLE_SCHEMA_CHANGE_ERROR_MESSAGE_PATTERN = "bind message has \\d result formats but query has \\d columns";
private static final Pattern TABLE_SCHEMA_CHANGE_ERROR_MESSAGE_PATTERN = Pattern.compile("bind message has \\d+ result formats but query has \\d+ columns");

private PgPreparedStatement ps;

Expand Down Expand Up @@ -100,6 +102,6 @@ public void handleErrorResponse(ErrorResponse errorResponse) {
}

private boolean isTableSchemaErrorMessage(ErrorResponse errorResponse) {
return errorResponse.getMessage().matches(TABLE_SCHEMA_CHANGE_ERROR_MESSAGE_PATTERN) || errorResponse.getMessage().equals("cached plan must not change result type");
return TABLE_SCHEMA_CHANGE_ERROR_MESSAGE_PATTERN.matcher(errorResponse.getMessage()).matches() || errorResponse.getMessage().equals("cached plan must not change result type");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.ext.unit.TestContext;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgConnection;
import io.vertx.pgclient.PgException;
import io.vertx.sqlclient.Tuple;
import org.junit.Test;

Expand Down Expand Up @@ -75,6 +76,40 @@ public void testOneShotPreparedQueryCacheRefreshOnTableSchemaChange(TestContext
}));
}

@Test
public void testPreparedQueryCacheRefreshOnWideTableSchemaChangeAfterReparse(TestContext ctx) {
// A proxy such as RDS Proxy parses the cached statement again on its backend connection, so after a schema change
// the bind fails with "bind message has 10 result formats but query has 11 columns" instead of
// "cached plan must not change result type". Simulate that by re-preparing the statement under the same name.
Async async = ctx.async();
PgConnection.connect(vertx, options()).onComplete(ctx.asyncAssertSuccess(conn -> {
String select = "SELECT * FROM unstable_wide WHERE id=$1";
conn.query("DROP TABLE IF EXISTS unstable_wide").execute()
.compose(v -> conn.query("CREATE TABLE unstable_wide (id integer PRIMARY KEY, c1 integer, c2 integer, c3 integer, " +
"c4 integer, c5 integer, c6 integer, c7 integer, c8 integer, c9 integer)").execute())
.compose(v -> conn.query("INSERT INTO unstable_wide (id) VALUES (1)").execute())
.compose(v -> conn.preparedQuery(select).execute(Tuple.of(1)))
.compose(res1 -> {
ctx.assertEquals(10, res1.columnsNames().size());
return conn.query("ALTER TABLE unstable_wide ADD COLUMN c10 integer").execute();
})
.compose(v -> conn.query("SELECT name FROM pg_prepared_statements WHERE statement = '" + select + "'").execute())
.compose(names -> {
String name = names.iterator().next().getString(0);
return conn.query("DEALLOCATE \"" + name + "\"; PREPARE \"" + name + "\" (int4) AS " + select).execute();
})
.compose(v -> conn.preparedQuery(select).execute(Tuple.of(1)))
.onComplete(ctx.asyncAssertFailure(failure -> {
ctx.assertEquals("bind message has 10 result formats but query has 11 columns", ((PgException) failure).getErrorMessage());
conn.preparedQuery(select).execute(Tuple.of(1)).onComplete(ctx.asyncAssertSuccess(res2 -> {
ctx.assertEquals(11, res2.columnsNames().size());
conn.close();
async.complete();
}));
}));
}));
}

@Test
public void testMaxPreparedStatementEviction(TestContext ctx) {
testPreparedStatements(ctx, options().setCachePreparedStatements(true).setPreparedStatementCacheMaxSize(16), 128, 16);
Expand Down
Loading