Skip to content

Commit 1a1c690

Browse files
authored
Mark RawStatement as Send and document thread safety constraints (#606)
Fixes #366
2 parents 50cea3c + c73c9fa commit 1a1c690

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

crates/duckdb/src/appender/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ use crate::{
88
};
99

1010
/// Appender for fast import data
11+
///
12+
/// # Thread Safety
13+
///
14+
/// `Appender` is neither `Send` nor `Sync`:
15+
/// - Not `Send` because it holds a reference to `Connection`, which is `!Sync`
16+
/// - Not `Sync` because DuckDB appenders don't support concurrent access
17+
///
18+
/// To use an appender in another thread, move the `Connection` to that thread
19+
/// and create the appender there.
20+
///
21+
/// If you need to share an `Appender` across threads, wrap it in a `Mutex`.
22+
///
23+
/// See [DuckDB concurrency documentation](https://duckdb.org/docs/stable/connect/concurrency.html) for more details.
1124
pub struct Appender<'conn> {
1225
conn: &'conn Connection,
1326
app: ffi::duckdb_appender,

crates/duckdb/src/raw_statement.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ use super::{ffi, Result};
1111
use crate::arrow2;
1212
use crate::{error::result_from_duckdb_arrow, Error};
1313

14-
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
15-
// TODO: destroy statement and result
14+
/// Private newtype for DuckDB prepared statements that finalize themselves when dropped.
15+
///
16+
/// # Thread Safety
17+
///
18+
/// `RawStatement` is `Send` but not `Sync`:
19+
/// - `Send` because it owns all its data and can be safely moved between threads
20+
/// - Not `Sync` because DuckDB prepared statements don't support concurrent access
1621
#[derive(Debug)]
1722
pub struct RawStatement {
1823
ptr: ffi::duckdb_prepared_statement,
@@ -325,3 +330,5 @@ impl Drop for RawStatement {
325330
}
326331
}
327332
}
333+
334+
unsafe impl Send for RawStatement {}

crates/duckdb/src/statement.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ use crate::{
1212
};
1313

1414
/// A prepared statement.
15+
///
16+
/// # Thread Safety
17+
///
18+
/// `Statement` is neither `Send` nor `Sync`:
19+
/// - Not `Send` because it holds a reference to `Connection`, which is `!Sync`
20+
/// - Not `Sync` because DuckDB prepared statements don't support concurrent access
21+
///
22+
/// See the [DuckDB concurrency documentation](https://duckdb.org/docs/stable/connect/concurrency.html) for more details.
1523
pub struct Statement<'conn> {
1624
conn: &'conn Connection,
1725
pub(crate) stmt: RawStatement,

0 commit comments

Comments
 (0)