Skip to main content

pg_bulk_insert

Function pg_bulk_insert 

Source
pub async fn pg_bulk_insert(
    conn: &mut SqlxPgConnection,
    table: &str,
    columns: &[&str],
    rows: &[Vec<Value>],
) -> Result<u64, DbError>
Expand description

PostgreSQL 批量导入(多行 INSERT,作为 COPY 协议的简化替代)

构建多行 INSERT 语句:INSERT INTO t (c1, c2) VALUES ($1, $2), ($3, $4), ... 使用参数绑定避免 SQL 注入。PostgreSQL 占位符 $N 跨行递增。

当数据量极大(>10 万行)时,建议分批调用或使用 PgExtensions::copy_from_stdin 走 COPY 协议以获得更高吞吐。