golang postgres 连接过多错误

huangapple go评论109阅读模式
英文:

golang postgres too many connections error

问题

我是新手使用Golang/Postgres,正在进行一些测试,但是遇到了一个pq: sorry, too many clients already错误。我的Postgres实例设置了最大连接数为100,但是在这段代码中出现了错误:

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)
    }
}

我通常可以插入大约60到70条记录,然后就会出现这个错误。所有的连接都来自于for循环中的这个样本。我可能做错了什么?这是我的完整代码。据我所知,一个连接可以执行多个不同的查询,所以我不知道为什么只能插入60到70条记录就会出现错误。

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()
}

我基本上是为了测试将10,000条记录插入数据库,每条记录都有一个不同的profile ID。

英文:

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 &lt; 10000; i++ {
	profile_id = profile_id+1
	on, err := db.Query(&quot;insert into streams (post,profile_id,created_on) values ($1,$2,$3)&quot;, post, profile_id, created_on)
	defer on.Close()

	if err != nil {
		fmt.Fprintln(w, &quot;-1&quot;)
		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(&quot;postgres&quot;, Postgres_Connect)
if err != nil {
	log.Fatal(err)
	println(err)

}
defer db.Close()

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

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

fmt.Fprintln(w, &quot;1&quot;)

}()
wg.Wait()

}

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

答案1

得分: 2

不要在循环中使用defer。
因为它会在函数返回时执行。

英文:

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

huangapple
  • 本文由 发表于 2016年11月10日 08:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/40518168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定