Skip to content

Instantly share code, notes, and snippets.

@chaudum
Created June 14, 2018 13:40
Show Gist options
  • Select an option

  • Save chaudum/ae16ae101d8346839cf7592db5d2f095 to your computer and use it in GitHub Desktop.

Select an option

Save chaudum/ae16ae101d8346839cf7592db5d2f095 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"strconv"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
)
func main() {
pgxConfig := pgx.ConnConfig{
Host: "localhost",
Port: 5432,
Database: "test",
User: "crate",
}
conn, err := pgx.Connect(pgxConfig)
if err != nil {
log.Fatal(err)
}
_, err = conn.Prepare("ps1", "INSERT INTO users (name, children) VALUES ($1, $2)")
if err != nil {
log.Fatal(err)
}
for n := 0; n < 100; n++ {
batch := conn.BeginBatch()
for i := 0; i < 1000; i++ {
username := "user_" + strconv.Itoa(n) + "_" + strconv.Itoa(i)
batch.Queue("ps1",
[]interface{}{username, int64(i % 5)},
[]pgtype.OID{pgtype.VarcharOID, pgtype.Int4OID},
nil,
)
}
err = batch.Send(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
err = batch.Close()
if err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment