Skip to content

Commit

Permalink
fix(frontend): allow write in rw txn for CI usage, cherry pick 16012 …
Browse files Browse the repository at this point in the history
…to release-1.8 (#16016)
  • Loading branch information
KeXiangWang authored Mar 29, 2024
1 parent fe8a951 commit 020a36c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
12 changes: 8 additions & 4 deletions e2e_test/batch/transaction/read_only.slt
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ select * from t;
2
3

statement error read-only transaction
statement ok
insert into t values (4);

statement ok
COMMIT;

statement ok
flush;

query I rowsort
select * from t;
----
1
2
3

statement ok
COMMIT;
4

statement ok
drop table t;
Expand Down
30 changes: 12 additions & 18 deletions src/frontend/src/handler/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub async fn handle_begin(
modes: Vec<TransactionMode>,
) -> Result<RwPgResponse> {
let HandlerArgs { session, .. } = handler_args;
let mut notices = vec![];

let mut builder = RwPgResponse::builder(stmt_type);

let access_mode = {
let mut access_mode = None;
Expand All @@ -47,9 +48,9 @@ pub async fn handle_begin(
// Note: This is for compatibility with some external drivers (like postgres_fdw) that
// always start a transaction with an Isolation Level.
const MESSAGE: &str = "\
Transaction with given Isolation Level is not supported yet.\n\
For compatibility, this statement will still proceed with RepeatableRead.";
notices.push(MESSAGE);
Transaction with given Isolation Level is not supported yet.\n\
For compatibility, this statement will proceed with RepeatableRead.";
builder = builder.notice(MESSAGE);
}
}
}
Expand All @@ -58,27 +59,20 @@ pub async fn handle_begin(
Some(TransactionAccessMode::ReadOnly) => AccessMode::ReadOnly,
Some(TransactionAccessMode::ReadWrite) | None => {
// Note: This is for compatibility with some external drivers (like psycopg2) that
// issue `BEGIN` implicitly for users.
// issue `BEGIN` implicitly for users. Not actually starting a transaction is okay
// since `COMMIT` and `ROLLBACK` are no-ops (except for warnings) when there is no
// active transaction.
const MESSAGE: &str = "\
Read-write transaction is not supported yet. Please specify `READ ONLY` to start a read-only transaction.\n\
For compatibility, this statement will still succeed and be executed as Read-only transactions.\n\
The write operations in this transaction will be rejected.";
notices.push(MESSAGE);
AccessMode::ReadOnly
For compatibility, this statement will still succeed but no transaction is actually started.";
builder = builder.notice(MESSAGE);
return Ok(builder.into());
}
}
};

session.txn_begin_explicit(access_mode);
if notices.is_empty() {
Ok(RwPgResponse::empty_result(stmt_type))
} else {
let mut builder = RwPgResponse::builder(stmt_type);
for notice in notices {
builder = builder.notice(notice);
}
Ok(builder.into())
}
Ok(builder.into())
}

#[expect(clippy::unused_async)]
Expand Down

0 comments on commit 020a36c

Please sign in to comment.