jackc / jackc/pgx

connection with write queries keeps running forever

Open
#713 41 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

My project need to connect two db instances one for read/write and another as only read-only. I have created two pools as below

```
type DB struct {
reader *pgxpool.Pool
writer *pgxpool.Pool
}

//Conn provide reader or writer connection as per readonly state
func (db *DB) Conn(readonly bool) *pgxpool.Pool {
if readonly {
return db.reader
}
return db.writer
}

//DbConfig is config for disk persistent database (PostgreSQL)
type DbConfig struct {
DbHost string
DbPort uint16
DbName string
DbUser string
DbPwd string
DbTimeout int
DbSSLMode string
}

func InitDbPool(reader, writer DbConfig) *DB {
db := DB{}
if reader.DbHost != "" {
db.reader = initdb(reader, "sql-reader")
}
if writer.DbHost != "" {
db.writer = initdb(writer, "sql-writer")
}
return &db
}

func (db *DB) CloseDbPool() {
if db.reader != nil {
db.reader.Close()
}
if db.writer != nil {
db.writer.Close()
}
}

func initdb(config DbConfig, connName string) *pgxpool.Pool {
var err error
if config.DbPort < 1 {
config.DbPort = 5432
}
connString := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?connect_timeout=%d&sslmode=%s",
config.DbUser, config.DbPwd, config.DbHost, config.DbPort, config.DbName, config.DbTimeout, config.DbSSLMode)

cfg, err := pgxpool.ParseConfig(connString)
if err != nil {
panic(fmt.Sprintf("pgsql connection parse error [%s]: %s", connName, err.Error()))
}
p, err := pgxpool.ConnectConfig(context.Background(), cfg)
if err != nil {
panic(fmt.Sprintf("pgsql connection failed [%s]: %s", connName, err.Error()))
}
return p
}
```

and calling it as below for writer,
```
sql := "delete from tabl1 where id=$1;"
db.Conn(false).Query(ctx, sql, arg0)
```

Few calls working fine after after some calls, it just hang on with connection and keep running forever and reaches context deadline.
If i use context.Background() then it keep running forever and do not raise any error as well.

Is it the issue with pgx or i am doing something wrong ?

Postgres and app both are on different machine, Postgres not restarted.
Delay between two machine is < 1 ms.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the pgxpool.Pool.Query call through the writer pool created by pgxpool.ParseConfig and ConnectConfig. Check how the context deadline and returned resources are handled, then reproduce the hang with the supplied two-pool setup. Done means identifying a reproducible cause and documenting or testing the resulting fix.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.