Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify type autoloading with pgxpool #2048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions pgxpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import (

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/puddle/v2"
)

var defaultMaxConns = int32(4)
var defaultMinConns = int32(0)
var defaultMaxConnLifetime = time.Hour
var defaultMaxConnIdleTime = time.Minute * 30
var defaultHealthCheckPeriod = time.Minute
var (
defaultMaxConns = int32(4)
defaultMinConns = int32(0)
defaultMaxConnLifetime = time.Hour
defaultMaxConnIdleTime = time.Minute * 30
defaultHealthCheckPeriod = time.Minute
)

type connResource struct {
conn *pgx.Conn
Expand Down Expand Up @@ -102,6 +105,48 @@ type Pool struct {
closeChan chan struct{}
}

// LoadTypesAfterConnect is suitable for assigning to the AfterConnect configuration setting.
// It will automatically load the named types for each connection in an efficient manner,
// performing a single query to the database backend. The underlying call to pgx.LoadTypes
// is smart enough to also retrieve any related types required to support the definition of the
// named types.
// If reuseTypeMap is enabled, it is assumed that the OID mapping is stable across all database
// backends in this pool, resulting in only needing to query when creating the initial connection;
// subsequent connections will reuse the same OID type mapping.
// Because it is not always possible for a client to know the database topology in the final usage
// context, PGXPOOL_REUSE_TYPEMAP, when given a value of y or n, will take precedence over this argument.
func LoadTypesAfterConnect(typeNames []string, reuseTypeMap bool) func(context.Context, *pgx.Conn) error {
if reuseTypeMap {
mutex := new(sync.Mutex)
var types []*pgtype.Type
return func(ctx context.Context, conn *pgx.Conn) error {
mutex.Lock()
defer mutex.Unlock()
var err error

if types != nil {
conn.TypeMap().RegisterTypes(types)
return nil
}
types, err = conn.LoadTypes(ctx, typeNames)
if err != nil {
types = nil
return err
}
conn.TypeMap().RegisterTypes(types)
return nil
}
}
return func(ctx context.Context, conn *pgx.Conn) error {
types, err := conn.LoadTypes(ctx, typeNames)
if err != nil {
return err
}
conn.TypeMap().RegisterTypes(types)
return nil
}
}

// Config is the configuration struct for creating a pool. It must be created by [ParseConfig] and then it can be
// modified.
type Config struct {
Expand Down