Skip to content

Commit 309b060

Browse files
committed
feat: support SHOW DEFAULT_TRANSACTION_ISOLATION for PG databases
The `SHOW DEFAULT_TRANSACTION_ISOLATION` command can be used in PostgreSQL to query the current default isolation level for a connection. This command was not supported by the Spanner JDBC driver and PGAdapter, which made it difficult for customers to check whether their attempt to set a different default isolation level actually worked. Updates GoogleCloudPlatform/pgadapter#3984
1 parent c706c63 commit 309b060

File tree

7 files changed

+647
-225
lines changed

7 files changed

+647
-225
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionStatementExecutor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ StatementResult statementSetPgSessionCharacteristicsTransactionMode(
152152

153153
StatementResult statementShowTransactionIsolationLevel();
154154

155+
StatementResult statementShowDefaultTransactionIsolation();
156+
155157
StatementResult statementSetProtoDescriptors(byte[] protoDescriptors);
156158

157159
StatementResult statementSetProtoDescriptorsFilePath(String filePath);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionStatementExecutorImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_COMMIT_RESPONSE;
6565
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_COMMIT_TIMESTAMP;
6666
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DATA_BOOST_ENABLED;
67+
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DEFAULT_TRANSACTION_ISOLATION;
6768
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE;
6869
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DIRECTED_READ;
6970
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_EXCLUDE_TXN_FROM_CHANGE_STREAMS;
@@ -647,7 +648,19 @@ public StatementResult statementShowReadLockMode() {
647648

648649
@Override
649650
public StatementResult statementShowTransactionIsolationLevel() {
650-
return resultSet("transaction_isolation", "serializable", SHOW_TRANSACTION_ISOLATION_LEVEL);
651+
TransactionOptions.IsolationLevel isolationLevel = getConnection().getDefaultIsolationLevel();
652+
if (getConnection().isInTransaction()) {
653+
isolationLevel = getConnection().getTransactionIsolationLevel();
654+
}
655+
return resultSet("transaction_isolation", isolationLevel, SHOW_TRANSACTION_ISOLATION_LEVEL);
656+
}
657+
658+
@Override
659+
public StatementResult statementShowDefaultTransactionIsolation() {
660+
return resultSet(
661+
"default_transaction_isolation",
662+
getConnection().getDefaultIsolationLevel(),
663+
SHOW_DEFAULT_TRANSACTION_ISOLATION);
651664
}
652665

653666
@Override

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/StatementResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ enum ClientSideStatementType {
9898
SET_RPC_PRIORITY,
9999
SHOW_RPC_PRIORITY,
100100
SHOW_TRANSACTION_ISOLATION_LEVEL,
101+
SHOW_DEFAULT_TRANSACTION_ISOLATION,
101102
SHOW_SAVEPOINT_SUPPORT,
102103
SET_SAVEPOINT_SUPPORT,
103104
SHOW_DATA_BOOST_ENABLED,

google-cloud-spanner/src/main/resources/com/google/cloud/spanner/connection/PG_ClientSideStatements.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@
248248
"method": "statementShowTransactionIsolationLevel",
249249
"exampleStatements": ["show transaction isolation level","show variable transaction isolation level"]
250250
},
251+
{
252+
"name": "SHOW [VARIABLE] DEFAULT_TRANSACTION_ISOLATION",
253+
"executorName": "ClientSideStatementNoParamExecutor",
254+
"resultType": "RESULT_SET",
255+
"statementType": "SHOW_DEFAULT_TRANSACTION_ISOLATION",
256+
"regex": "(?is)\\A\\s*show\\s+(?:variable\\s+)?default_transaction_isolation\\s*\\z",
257+
"method": "statementShowDefaultTransactionIsolation",
258+
"exampleStatements": ["show default_transaction_isolation","show variable default_transaction_isolation"]
259+
},
251260
{
252261
"name": "EXPLAIN <sql>",
253262
"executorName": "ClientSideStatementExplainExecutor",

0 commit comments

Comments
 (0)