@@ -12,14 +12,17 @@ import (
1212
1313 "github.com/jackc/pgx/v5"
1414 "github.com/jackc/pgx/v5/pgconn"
15+ "github.com/jackc/pgx/v5/pgtype"
1516 "github.com/jackc/puddle/v2"
1617)
1718
18- var defaultMaxConns = int32 (4 )
19- var defaultMinConns = int32 (0 )
20- var defaultMaxConnLifetime = time .Hour
21- var defaultMaxConnIdleTime = time .Minute * 30
22- var defaultHealthCheckPeriod = time .Minute
19+ var (
20+ defaultMaxConns = int32 (4 )
21+ defaultMinConns = int32 (0 )
22+ defaultMaxConnLifetime = time .Hour
23+ defaultMaxConnIdleTime = time .Minute * 30
24+ defaultHealthCheckPeriod = time .Minute
25+ )
2326
2427type connResource struct {
2528 conn * pgx.Conn
@@ -102,6 +105,48 @@ type Pool struct {
102105 closeChan chan struct {}
103106}
104107
108+ // LoadTypesAfterConnect is suitable for assigning to the AfterConnect configuration setting.
109+ // It will automatically load the named types for each connection in an efficient manner,
110+ // performing a single query to the database backend. The underlying call to pgx.LoadTypes
111+ // is smart enough to also retrieve any related types required to support the definition of the
112+ // named types.
113+ // If reuseTypeMap is enabled, it is assumed that the OID mapping is stable across all database
114+ // backends in this pool, resulting in only needing to query when creating the initial connection;
115+ // subsequent connections will reuse the same OID type mapping.
116+ // Because it is not always possible for a client to know the database topology in the final usage
117+ // context, PGXPOOL_REUSE_TYPEMAP, when given a value of y or n, will take precedence over this argument.
118+ func LoadTypesAfterConnect (typeNames []string , reuseTypeMap bool ) func (context.Context , * pgx.Conn ) error {
119+ if reuseTypeMap {
120+ mutex := new (sync.Mutex )
121+ var types []* pgtype.Type
122+ return func (ctx context.Context , conn * pgx.Conn ) error {
123+ mutex .Lock ()
124+ defer mutex .Unlock ()
125+ var err error
126+
127+ if types != nil {
128+ conn .TypeMap ().RegisterTypes (types )
129+ return nil
130+ }
131+ types , err = conn .LoadTypes (ctx , typeNames )
132+ if err != nil {
133+ types = nil
134+ return err
135+ }
136+ conn .TypeMap ().RegisterTypes (types )
137+ return nil
138+ }
139+ }
140+ return func (ctx context.Context , conn * pgx.Conn ) error {
141+ types , err := conn .LoadTypes (ctx , typeNames )
142+ if err != nil {
143+ return err
144+ }
145+ conn .TypeMap ().RegisterTypes (types )
146+ return nil
147+ }
148+ }
149+
105150// Config is the configuration struct for creating a pool. It must be created by [ParseConfig] and then it can be
106151// modified.
107152type Config struct {
0 commit comments