package pgxpool
import "github.com/jackc/pgx/v4/pgxpool"
Package pgxpool is a connection pool for pgx.
pgxpool implements a nearly identical interface to pgx connections.
Establishing a Connection
The primary way of establishing a connection is with `pgxpool.Connect`.
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
The database connection string can be in URL or DSN format. PostgreSQL settings, pgx settings, and pool settings can be specified here. In addition, a config struct can be created by `ParseConfig` and modified before establishing the connection with `ConnectConfig`.
config, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL")) if err != nil { // ... } config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { // do something with every new connection } pool, err := pgxpool.ConnectConfig(context.Background(), config)
Index ¶
- type Config
- type Conn
- func (c *Conn) Begin(ctx context.Context) (pgx.Tx, error)
- func (c *Conn) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
- func (c *Conn) Conn() *pgx.Conn
- func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
- func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
- func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
- func (c *Conn) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
- func (c *Conn) Release()
- func (c *Conn) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
- type Pool
- func Connect(ctx context.Context, connString string) (*Pool, error)
- func ConnectConfig(ctx context.Context, config *Config) (*Pool, error)
- func (p *Pool) Acquire(ctx context.Context) (*Conn, error)
- func (p *Pool) AcquireAllIdle(ctx context.Context) []*Conn
- func (p *Pool) Begin(ctx context.Context) (pgx.Tx, error)
- func (p *Pool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
- func (p *Pool) Close()
- func (p *Pool) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
- func (p *Pool) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
- func (p *Pool) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
- func (p *Pool) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
- func (p *Pool) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
- func (p *Pool) Stat() *Stat
- type Stat
- func (s *Stat) AcquireCount() int64
- func (s *Stat) AcquireDuration() time.Duration
- func (s *Stat) AcquiredConns() int32
- func (s *Stat) CanceledAcquireCount() int64
- func (s *Stat) ConstructingConns() int32
- func (s *Stat) EmptyAcquireCount() int64
- func (s *Stat) IdleConns() int32
- func (s *Stat) MaxConns() int32
- func (s *Stat) TotalConns() int32
- type Tx
- func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error)
- func (tx *Tx) Commit(ctx context.Context) error
- func (tx *Tx) Conn() *pgx.Conn
- func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
- func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
- func (tx *Tx) LargeObjects() pgx.LargeObjects
- func (tx *Tx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)
- func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
- func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
- func (tx *Tx) Rollback(ctx context.Context) error
- func (tx *Tx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
Types ¶
type Config ¶
type Config struct { ConnConfig *pgx.ConnConfig // AfterConnect is called after a connection is established, but before it is added to the pool. AfterConnect func(context.Context, *pgx.Conn) error // BeforeAcquire is called before before a connection is acquired from the pool. It must return true to allow the // acquision or false to indicate that the connection should be destroyed and a different connection should be // acquired. BeforeAcquire func(context.Context, *pgx.Conn) bool // AfterRelease is called after a connection is released, but before it is returned to the pool. It must return true to // return the connection to the pool or false to destroy the connection. AfterRelease func(*pgx.Conn) bool // MaxConnLifetime is the duration after which a connection will be automatically closed. MaxConnLifetime time.Duration // MaxConns is the maximum size of the pool. MaxConns int32 // HealthCheckPeriod is the duration between checks of the health of idle connections. HealthCheckPeriod time.Duration // contains filtered or unexported fields }
Config is the configuration struct for creating a pool. It must be created by ParseConfig and then it can be modified. A manually initialized ConnConfig will cause ConnectConfig to panic.
func ParseConfig ¶
ParseConfig builds a Config from connString. It parses connString with the same behavior as pgx.ParseConfig with the addition of the following variables:
pool_max_conns: integer greater than 0 pool_max_conn_lifetime: duration string pool_health_check_period: duration string
See Config for definitions of these arguments.
# Example DSN user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10 # Example URL postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is an acquired *pgx.Conn from a Pool.
func (*Conn) Begin ¶
func (*Conn) BeginTx ¶
func (*Conn) Conn ¶
func (c *Conn) Conn() *pgx.Conn
func (*Conn) CopyFrom ¶
func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
func (*Conn) Exec ¶
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
func (*Conn) Query ¶
func (*Conn) QueryRow ¶
func (*Conn) Release ¶
func (c *Conn) Release()
Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called. However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
func (*Conn) SendBatch ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
func Connect ¶
Connect creates a new Pool and immediately establishes one connection. ctx can be used to cancel this initial connection. See ParseConfig for information on connString format.
func ConnectConfig ¶
ConnectConfig creates a new Pool and immediately establishes one connection. ctx can be used to cancel this initial connection. config must have been created by ParseConfig.
func (*Pool) Acquire ¶
func (*Pool) AcquireAllIdle ¶
AcquireAllIdle atomically acquires all currently idle connections. Its intended use is for health check and keep-alive functionality. It does not update pool statistics.
func (*Pool) Begin ¶
func (*Pool) BeginTx ¶
func (*Pool) Close ¶
func (p *Pool) Close()
Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned to pool and closed.
func (*Pool) CopyFrom ¶
func (p *Pool) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
func (*Pool) Exec ¶
func (p *Pool) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
func (*Pool) Query ¶
func (*Pool) QueryRow ¶
func (*Pool) SendBatch ¶
func (*Pool) Stat ¶
type Stat ¶
type Stat struct {
// contains filtered or unexported fields
}
func (*Stat) AcquireCount ¶
func (*Stat) AcquireDuration ¶
func (*Stat) AcquiredConns ¶
func (*Stat) CanceledAcquireCount ¶
func (*Stat) ConstructingConns ¶
func (*Stat) EmptyAcquireCount ¶
func (*Stat) IdleConns ¶
func (*Stat) MaxConns ¶
func (*Stat) TotalConns ¶
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
func (*Tx) Begin ¶
func (*Tx) Commit ¶
func (*Tx) Conn ¶
func (tx *Tx) Conn() *pgx.Conn
func (*Tx) CopyFrom ¶
func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
func (*Tx) Exec ¶
func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
func (*Tx) LargeObjects ¶
func (tx *Tx) LargeObjects() pgx.LargeObjects
func (*Tx) Prepare ¶
func (*Tx) Query ¶
func (*Tx) QueryRow ¶
func (*Tx) Rollback ¶
func (*Tx) SendBatch ¶
Source Files ¶
batch_results.go conn.go doc.go pool.go rows.go stat.go tx.go
- Version
- v4.1.0
- Published
- Oct 12, 2019
- Platform
- linux/amd64
- Imports
- 9 packages
- Last checked
- 8 seconds ago –
Tools for package owners.