diff --git a/e2e_test/batch/transaction/read_only.slt b/e2e_test/batch/transaction/read_only.slt index f0738dcd75045..609b4bf37968e 100644 --- a/e2e_test/batch/transaction/read_only.slt +++ b/e2e_test/batch/transaction/read_only.slt @@ -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; diff --git a/src/frontend/src/handler/transaction.rs b/src/frontend/src/handler/transaction.rs index 7075d0d6b5267..bc6cbbbb9113c 100644 --- a/src/frontend/src/handler/transaction.rs +++ b/src/frontend/src/handler/transaction.rs @@ -34,7 +34,8 @@ pub async fn handle_begin( modes: Vec, ) -> Result { let HandlerArgs { session, .. } = handler_args; - let mut notices = vec![]; + + let mut builder = RwPgResponse::builder(stmt_type); let access_mode = { let mut access_mode = None; @@ -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); } } } @@ -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)]