> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SqlxBulkInserter

> SqlxBulkInserter — Kinetica Rust API

[gpudb\_api](https://docs.kinetica.com/content/api/rust/index.md)::<wbr />[sqlx\_support](https://docs.kinetica.com/content/api/rust/sqlx_support/index.md)
[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L947-L949)&#x20;

<pre>`pub struct SqlxBulkInserter { /* private fields */ }`</pre>

Bulk inserter for SQLx that uses Kinetica’s HTTP API for efficient data loading.SQLx uses the PostgreSQL wire protocol which is less efficient for bulk loading than Kinetica’s native HTTP API with Avro encoding. This helper bridges the gap by sending the records through a [`Kinetica`](https://docs.kinetica.com/content/api/rust/gpudb/struct.Kinetica) client, connected and configured (TLS, HA failover and the rest) as for any other HTTP API use.Uses `GenericRecord` for dynamic record creation.

## Example

```rust theme={null}
use gpudb_api::sqlx_support::SqlxBulkInserter;
use gpudb_api::{GenericRecord, Kinetica, KineticaError, KineticaOptions};
use std::sync::Arc;

// The HTTP API client the records are sent through
let mut options = KineticaOptions::new();
options.set_username("admin").set_password("password");
let kinetica = Arc::new(Kinetica::new(vec!["http://localhost:9191".to_string()], options).await?);

let bulk_inserter = SqlxBulkInserter::new(kinetica);

// Fetch type schema and create records
let record_type = Arc::new(bulk_inserter.fetch_type("my_table").await?);

// Setting a column fails on a name the type does not have
let records = (0..10000)
    .map(|i| {
        let mut r = record_type.new_record();
        r.set_by_name("id", i)?;
        r.set_by_name("name", format!("user_{i}"))?;
        Ok(r)
    })
    .collect::<Result<Vec<GenericRecord>, KineticaError>>()?;

// Bulk insert
let stats = bulk_inserter.insert("my_table", records, 10000).await?;
println!("Inserted {} records", stats.records_inserted);
```

## Implementations

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L951-L1107)
impl [SqlxBulkInserter](https://docs.kinetica.com/content/api/rust/sqlx_support/struct.SqlxBulkInserter.md)

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L954-L956)
pub fn [new](#method.new)(kinetica: [Arc](https://doc.rust-lang.org/1.98.1/alloc/sync/struct.Arc.html)\<[Kinetica](https://docs.kinetica.com/content/api/rust/gpudb/struct.Kinetica.md)>) -> Self

Create a bulk inserter that sends records through `kinetica`, a client of Kinetica’s HTTP API.

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L959-L961)
pub fn [kinetica](#method.kinetica)(\&self) -> &[Arc](https://doc.rust-lang.org/1.98.1/alloc/sync/struct.Arc.html)\<[Kinetica](https://docs.kinetica.com/content/api/rust/gpudb/struct.Kinetica.md)>

Get a reference to the underlying Kinetica client.

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L978-L980)
pub async fn [fetch\_type](#method.fetch_type)(\&self, table\_name: &[str](https://doc.rust-lang.org/1.98.1/std/primitive.str.html)) -> [Result](https://doc.rust-lang.org/1.98.1/core/result/enum.Result.html)\<[Type](https://docs.kinetica.com/content/api/rust/generic_record/struct.Type.md), [KineticaError](https://docs.kinetica.com/content/api/rust/errors/enum.KineticaError.md)>

Fetch the Type for creating GenericRecords.Returns a `Type` that can be used to create `GenericRecord` instances.

##### Example

```rust theme={null}
let record_type = Arc::new(inserter.fetch_type("my_table").await?);
let mut record = record_type.new_record();
record.set_by_name("id", 42)?;
```

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L1012-L1019)
pub async fn [insert](#method.insert)( \&self, table\_name: &[str](https://doc.rust-lang.org/1.98.1/std/primitive.str.html), records: [Vec](https://doc.rust-lang.org/1.98.1/alloc/vec/struct.Vec.html)\<[GenericRecord](https://docs.kinetica.com/content/api/rust/generic_record/struct.GenericRecord.md)>, batch\_size: [usize](https://doc.rust-lang.org/1.98.1/std/primitive.usize.html), ) -> [Result](https://doc.rust-lang.org/1.98.1/core/result/enum.Result.html)\<[BulkInsertStats](https://docs.kinetica.com/content/api/rust/generic_record/struct.BulkInsertStats.md), [KineticaError](https://docs.kinetica.com/content/api/rust/errors/enum.KineticaError.md)>

Perform a bulk insert of GenericRecords with shard-aware multi-head ingest.This uses the high-performance `BulkInserter<GenericRecord>` which provides:

<ul>
  <li>Shard-aware routing to worker nodes (multi-head ingest)</li>
  <li>Parallel batch processing</li>
  <li>Avro binary encoding for maximum throughput</li>
</ul>

##### Arguments

<ul>
  <li>`table_name` - The name of the table to insert into</li>
  <li>`records` - Vector of GenericRecords to insert</li>
  <li>`batch_size` - Number of records per batch (recommended: 10000-50000)</li>
</ul>

##### Example

```rust theme={null}
let record_type = Arc::new(inserter.fetch_type("my_table").await?);
// Setting a column fails on a name the type does not have
let records = (0..1000).map(|i| {
    let mut r = record_type.new_record();
    r.set_by_name("id", i)?;
    Ok(r)
}).collect::<Result<Vec<GenericRecord>, KineticaError>>()?;
let stats = inserter.insert("my_table", records, 10000).await?;
```

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L1032-L1091)
pub async fn [insert\_with\_options](#method.insert_with_options)( \&self, table\_name: &[str](https://doc.rust-lang.org/1.98.1/std/primitive.str.html), records: [Vec](https://doc.rust-lang.org/1.98.1/alloc/vec/struct.Vec.html)\<[GenericRecord](https://docs.kinetica.com/content/api/rust/generic_record/struct.GenericRecord.md)>, batch\_size: [usize](https://doc.rust-lang.org/1.98.1/std/primitive.usize.html), num\_workers: [usize](https://doc.rust-lang.org/1.98.1/std/primitive.usize.html), options: [Option](https://doc.rust-lang.org/1.98.1/core/option/enum.Option.html)\<[BulkInserterOptions](https://docs.kinetica.com/content/api/rust/bulk_inserter/struct.BulkInserterOptions.md)>, ) -> [Result](https://doc.rust-lang.org/1.98.1/core/result/enum.Result.html)\<[BulkInsertStats](https://docs.kinetica.com/content/api/rust/generic_record/struct.BulkInsertStats.md), [KineticaError](https://docs.kinetica.com/content/api/rust/errors/enum.KineticaError.md)>

Perform a bulk insert of GenericRecords with custom options and shard-aware routing.Uses `BulkInserter<GenericRecord>` for optimal performance with multi-head ingest.

##### Arguments

<ul>
  <li>`table_name` - The name of the table to insert into</li>
  <li>`records` - Vector of GenericRecords to insert</li>
  <li>`batch_size` - Number of records per batch (recommended: 10000-50000)</li>
  <li>`num_workers` - Number of parallel workers (recommended: 4-16)</li>
  <li>`options` - Optional BulkInserterOptions for controlling insert behavior</li>
</ul>

[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/sqlx_support.rs#L1097-L1106)
pub async fn [upsert](#method.upsert)( \&self, table\_name: &[str](https://doc.rust-lang.org/1.98.1/std/primitive.str.html), records: [Vec](https://doc.rust-lang.org/1.98.1/alloc/vec/struct.Vec.html)\<[GenericRecord](https://docs.kinetica.com/content/api/rust/generic_record/struct.GenericRecord.md)>, batch\_size: [usize](https://doc.rust-lang.org/1.98.1/std/primitive.usize.html), ) -> [Result](https://doc.rust-lang.org/1.98.1/core/result/enum.Result.html)\<[BulkInsertStats](https://docs.kinetica.com/content/api/rust/generic_record/struct.BulkInsertStats.md), [KineticaError](https://docs.kinetica.com/content/api/rust/errors/enum.KineticaError.md)>

Perform a bulk insert with upsert mode (update on existing primary key).Updates existing records if primary key already exists. Uses shard-aware multi-head ingest for optimal performance.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.