jackc / jackc/pgx

Queries fail on pool when postgres is out of connection slots

Open
#1,657 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

**Describe the bug**
I'm not sure this is a bug or intended behavior.

I used up all the connection slots on my Postgres server (by accident), and then the queries I tried to run on my pgxpool returned:

`FATAL: remaining connection slots are reserved for non-replication superuser connections (SQLSTATE 53300)`

I expected this error to be hidden if there were still open connections in the pool so that my application can continue running without noticing any problems.

(However, maybe that error should be returned once the context given to Pool.Acquire() gets cancelled, as that would likely be the actual failure reason?)

**To Reproduce**
Steps to reproduce the behavior:

I suspect this will show the failure, but didn't test it.

```go
package main

import (
"context"
"fmt"
"log"
"os"

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

func main() {
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
var connections []*pgx.Conn
for {
// Exhaust all connection slots.
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
break
}
connections = append(connections, conn)
}
_, err := pool.Exec("SELECT 1")
fmt.Print(err)
}

```

**Expected behavior**
I'd like the pool to hide errors creating new connections while we have at least MinConns open connections.

**Actual behavior**
One of the queries on my pool failed with:

`FATAL: remaining connection slots are reserved for non-replication superuser connections (SQLSTATE 53300)`

**Version**
- Go: `go version go1.18.something linux/amd64`
- PostgreSQL: `$ psql --no-psqlrc --tuples-only -c 'select version()'` -> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]
- pgx: `github.com/jackc/pgx/v4 v4.17.2`

Contributor guide

Open the contributing guide

Research direction

Start with the pgxpool behavior exercised by Pool.Acquire and pool.Exec, then run the provided reproduction against PostgreSQL with exhausted connection slots. Determine whether SQLSTATE 53300 should be hidden while usable pool connections remain or surfaced when acquisition is canceled. Done requires a clearly decided behavior and regression coverage for the selected outcome.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.