Created
June 14, 2018 13:40
-
-
Save chaudum/ae16ae101d8346839cf7592db5d2f095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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