简体   繁体   中英

golang postgres too many connections error

I am new to Golang/Postgres and I am doing some testing and getting an pq: sorry, too many clients already error . My postgres instance is set to a max of 100 connections and I am getting that error in this code

    for i := 0; i < 10000; i++ {
    profile_id = profile_id+1
    on, err := db.Query("insert into streams (post,profile_id,created_on) values ($1,$2,$3)", post, profile_id, created_on)
    defer on.Close()

    if err != nil {
        fmt.Fprintln(w, "-1")
        log.Fatal(err)
    }
}

I can usually get in about 60 to 70 inserts then I get that error . All the connections are coming from that one sample in for For loop . What can I be doing wrong, here is my full code . As far as I know 1 connection can hold many different queries so I don't know why it is only giving me 60 to 70 inserts then get the error .

func Insert_Stream(w http.ResponseWriter, r *http.Request) {


wg := sync.WaitGroup{}
wg.Add(1)


go func(){
    defer wg.Done()

db, err := sql.Open("postgres", Postgres_Connect)
if err != nil {
    log.Fatal(err)
    println(err)

}
defer db.Close()

r.ParseForm()
post := r.FormValue("post")
profile_id,err := strconv.Atoi(r.FormValue("profile_id"))
created_on := time.Now()
for i := 0; i < 10000; i++ {
    profile_id = profile_id+1
    on, err := db.Query("insert into streams (post,profile_id,created_on) values ($1,$2,$3)", post, profile_id)
    defer on.Close()

    if err != nil {
        fmt.Fprintln(w, "-1")
        log.Fatal(err)
    }
}

fmt.Fprintln(w, "1")

}()
wg.Wait()

}

I am essentially inserting 10,000 records into the database with a different profile ID for testing .

DO NOT use defer in loop. Because it will execute when function returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM