jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pgxpool opens more connections than configured

Coronon opened this issue · comments

Describe the bug
I am using pgxpool to share a connection pool throughout my application. Recently, I have noticed that my PostgreSQL server is out of available connection slots. After a little investigation, I found that my application had more than the configured amount of connections open to my database.

I also tested that my connection string was parsed correctly by first creating a config instance with it and reading the parsed .MaxConns.

To Reproduce
Steps to reproduce the behavior:

If possible, please provide runnable example such as:

package main

import (
	"context"
	"log"
	"os"

	"github.com/jackc/pgx/v5/pgxpool"
)

func main() {
	pool, _ := pgxpool.New(
		context.Background(),
		fmt.Sprintf(
			"postgres://%v:%v@%v:%v/%v?pool_max_conns=%v",
			"user",
			"password",
			"host",
			"port",
			"name",
			15,
		),
	)

        // Use the pool a lot in many concurrent go routines
}

Please run your example with the race detector enabled. For example, go run -race main.go or go test -race.

Expected behavior
I have at most 15 connections coming from my application.

Actual behavior
I have over 30 connections open from my application.

Version

  • Go: $ go version -> go version go1.21.5 linux/amd64
  • PostgreSQL: $ psql --no-psqlrc --tuples-only -c 'select version()' -> PostgreSQL 15.5 (Debian 15.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> github.com/jackc/pgx/v5 v5.5.0

Additional context
The go routines are running inside an errgroup.Group

Are those connections being closed by context cancellation or something like that? The only similar situations I've heard of have to do with pgx closing a connection and reusing the pool slot before PostgreSQL has entirely cleaned up from the closed connections.

If this is the case then you should be able to see the connections go back to normal within a second or two after the connections have been closed.

Another thing to check is the pool stats. That would tell us how many connections pgxpool thought were open.

Thank you for getting back to me :)

I pretty much only use context.Background() with all my queries, so they shouldn't be closed. The increased amount of connections stay active until I forcefully restart the executable (which I do through a health check in docker compose).

How would I go about fetching these pool stats? I might be able to simply periodically print these and get the output once the problem occurs :)