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 .