Skip to main content

AsyncInserter

Struct AsyncInserter 

Source
pub struct AsyncInserter<'conn> { /* private fields */ }
Expand description

An async high-performance bulk data inserter.

AsyncInserter is the async mirror of Inserter. Both produce identical wire output (HyperBinary COPY) and have identical throughput characteristics; the difference is whether the network I/O is blocking (Inserter) or async/await (AsyncInserter).

§Lifetime safety

The inserter borrows the underlying AsyncConnection for 'conn. The borrow checker prevents the connection from being dropped or moved while the inserter is live.

§Single-use

Like the sync Inserter, AsyncInserter is intended for one COPY session per instance. After execute returns, the row counter is zeroed so a stray second call returns Ok(0) rather than corrupting the stream. To insert a second batch, construct a new AsyncInserter.

Implementations§

Source§

impl<'conn> AsyncInserter<'conn>

Source

pub fn new( connection: &'conn AsyncConnection, table_def: &TableDefinition, ) -> Result<Self>

Creates a new async inserter for the given table.

The underlying COPY session is started lazily on the first flush or execute, so construction is lightweight. The connection’s transport is validated eagerly — a gRPC connection returns an error immediately.

§Errors
Source

pub fn add_null(&mut self) -> Result<()>

Adds a NULL value to the next column.

Source

pub fn add_bool(&mut self, value: bool) -> Result<()>

Adds a boolean value.

Source

pub fn add_i16(&mut self, value: i16) -> Result<()>

Adds a 16-bit signed integer.

Source

pub fn add_i32(&mut self, value: i32) -> Result<()>

Adds a 32-bit signed integer.

Source

pub fn add_i64(&mut self, value: i64) -> Result<()>

Adds a 64-bit signed integer.

Source

pub fn add_f32(&mut self, value: f32) -> Result<()>

Adds a 32-bit float.

Source

pub fn add_f64(&mut self, value: f64) -> Result<()>

Adds a 64-bit float.

Source

pub fn add_str(&mut self, value: &str) -> Result<()>

Adds a string (TEXT) value.

Source

pub fn add_bytes(&mut self, value: &[u8]) -> Result<()>

Adds a bytes (BYTEA) value.

Source

pub fn add_date(&mut self, value: Date) -> Result<()>

Adds a Date value.

Source

pub fn add_time(&mut self, value: Time) -> Result<()>

Adds a Time value.

Source

pub fn add_timestamp(&mut self, value: Timestamp) -> Result<()>

Adds a Timestamp value.

Source

pub fn add_offset_timestamp(&mut self, value: OffsetTimestamp) -> Result<()>

Adds an OffsetTimestamp value.

Source

pub fn add_interval(&mut self, value: Interval) -> Result<()>

Adds an Interval value.

Source

pub fn add_numeric(&mut self, value: Numeric) -> Result<()>

Adds a Numeric value (NUMERIC). The encoding (small vs big) is chosen from the table definition’s column precision at this position.

§Errors

Returns an error if the column’s precision cannot be determined from the table definition (NUMERIC columns must be declared with explicit precision/scale).

Source

pub async fn end_row(&mut self) -> Result<()>

Marks the end of the current row. Must be called after every full row. May trigger an automatic flush of the buffered data to the server.

§Errors
  • Returns Error::Other if the column count for the row doesn’t match the table definition.
  • Returns Error::Client / Error::Io on transport failures during the auto-flush.
Source

pub async fn execute(&mut self) -> Result<u64>

Executes the insert and commits all buffered rows.

Sends any remaining buffered data and finishes the COPY operation. Returns the number of rows inserted.

The inserter is single-use: calling execute a second time returns Ok(0). Construct a new AsyncInserter to insert another batch.

§Errors
Source

pub fn cancel(&mut self)

Cancels the insert and discards all buffered rows.

The Drop impl on AsyncCopyInWriter queues a CopyFail on the connection so the server tears the COPY down cleanly on the next connection use.

Trait Implementations§

Source§

impl<'conn> Debug for AsyncInserter<'conn>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'conn> Freeze for AsyncInserter<'conn>

§

impl<'conn> !RefUnwindSafe for AsyncInserter<'conn>

§

impl<'conn> Send for AsyncInserter<'conn>

§

impl<'conn> Sync for AsyncInserter<'conn>

§

impl<'conn> Unpin for AsyncInserter<'conn>

§

impl<'conn> UnsafeUnpin for AsyncInserter<'conn>

§

impl<'conn> !UnwindSafe for AsyncInserter<'conn>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more