pgxutil

package
v1.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pgxutil centralizes pgx connection configuration so that every Postgres connection (source metadata, sink pool, pgvector pool, state store) is built consistently and is safe to use behind a transaction/statement pooling proxy such as PgBouncer.

PgBouncer in transaction or statement pooling mode multiplexes client sessions onto a small set of backend connections. Server-side prepared statements and the extended query protocol (pgx's default) break in that environment with errors like

ERROR: prepared statement "stmtcache_xxx" already exists

because a cached statement created on one backend may be executed on a different one. To avoid this we switch pgx to the simple/exec query mode and disable the statement and description caches whenever the connection targets a pooler.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPooler = NewPooler()

DefaultPooler is the global connection pooler used by sources and sinks to share backend connections.

Functions

func ApplyPoolerSafety

func ApplyPoolerSafety(cfg *pgx.ConnConfig)

ApplyPoolerSafety configures a pgx connection config for use behind a transaction/statement pooling proxy by disabling the extended protocol's server-side prepared-statement and description caches.

func IsPooledConnString

func IsPooledConnString(connString string) bool

IsPooledConnString reports whether connString declares that it targets a transaction/statement pooling proxy (e.g. PgBouncer) via the custom "pgbouncer=true" or "pool_mode=transaction|statement" markers.

func ParseConfig

func ParseConfig(connString string) (*pgx.ConnConfig, bool, error)

ParseConfig parses connString into a pgx connection config, transparently stripping the custom pooler markers and applying pooler-safe settings when they are present. The second return value reports whether the connection was detected to target a pooler.

func ParsePoolConfig

func ParsePoolConfig(connString string) (*pgxpool.Config, bool, error)

ParsePoolConfig parses connString into a pgxpool config, applying the same pooler-safe settings as ParseConfig when a pooler is detected.

Types

type Pooler

type Pooler struct {
	// contains filtered or unexported fields
}

Pooler caches and reuses pgxpool.Pool instances based on connection strings. It ensures that multiple workflow nodes targeting the same database share the same underlying connection pool, reducing the load on the database or proxy.

func NewPooler

func NewPooler() *Pooler

NewPooler creates a new connection pooler.

func (*Pooler) Close

func (p *Pooler) Close()

Close closes all cached pools and clears the cache.

func (*Pooler) Get

func (p *Pooler) Get(ctx context.Context, connString string) (*pgxpool.Pool, error)

Get returns a shared pool for the given connection string. If no pool exists for the string, a new one is created and cached.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL