jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should pgxpool be closed after each use during the life time of the program?

Gnyblast opened this issue · comments

I've done a significant amount of research and read the documentation as well but I couldn't find any clear answer to this question:

Let's say you have a webserver and it's a long living application not like a script that runs and dies. Your webserver is doing SQL queries and it uses pgxpool. The question is this: Everytime you want to make a connection to db, should you initialize a pgxpool then acquire as many as connection you need and at the end close all the connections then also close the connection pool? Or you should initialize the pgxpool at the beginning of the program and then just acquire connections from the pool as you need then close just the connections when you done, leave the pgxpool alive through the life time of the application?

Is pgxpool creating connections idling and waiting to be acquired thus pgxpools needs to be closed when they are not going to be used for certain period of time?

Or they are just empty pool that creates connection workers as needed and it's not a must to close them when the application is alive and anytime there might be a need for a DB connection?

In almost all cases you should only have one pool for the entire lifetime of your program execution.

The pool will automatically create and close connections as needed. See https://pkg.go.dev/github.com/jackc/pgx/v5@v5.5.5/pgxpool#Config for options on how to customize this behavior.

In almost all cases you should only have one pool for the entire lifetime of your program execution.

This was the answer I need. Thanks.