Documentation
¶
Overview ¶
Package postgres provides several github.com/jackc/pgx related helpers for interacting with postgres.
Index ¶
- func Connect(logger *slog.Logger, dsn string, maxConns int, maxIdleTime string, ...) (*pgxpool.Pool, error)
- func PgxErrorToHTTPError(err error) error
- type DB
- type PgxSyncRow
- type PgxSyncRows
- func (rows *PgxSyncRows) Close()
- func (rows *PgxSyncRows) CommandTag() pgconn.CommandTag
- func (rows *PgxSyncRows) Conn() *pgx.Conn
- func (rows *PgxSyncRows) Err() error
- func (rows *PgxSyncRows) FieldDescriptions() []pgconn.FieldDescription
- func (rows *PgxSyncRows) Next() bool
- func (rows *PgxSyncRows) RawValues() [][]byte
- func (rows *PgxSyncRows) Scan(dest ...any) error
- func (rows *PgxSyncRows) Values() ([]any, error)
- type PgxSyncTx
- func (tx *PgxSyncTx) Begin(ctx context.Context) (pgx.Tx, error)
- func (tx *PgxSyncTx) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
- func (tx *PgxSyncTx) Ping(ctx context.Context) error
- func (tx *PgxSyncTx) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
- func (tx *PgxSyncTx) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
- func (tx *PgxSyncTx) Rollback(ctx context.Context) error
- type SpanDB
- func (db SpanDB) Begin(ctx context.Context) (pgx.Tx, error)
- func (db SpanDB) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
- func (db SpanDB) Ping(ctx context.Context) error
- func (db SpanDB) Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)
- func (db SpanDB) QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect( logger *slog.Logger, dsn string, maxConns int, maxIdleTime string, connectTimeout int, sleepBeforeRetry time.Duration, maxRetryDuration time.Duration, ) (*pgxpool.Pool, error)
Connect connects to postgres and returns a *pgxpool.Pool.
The provided arguments are:
- dsn: the url to reach the database
- maxConns: the maximum amount of open connections
- maxIdleTime: the maximum idle time of an open connection
- connectTimeout: the timeout on connecting to the database in seconds
- sleepBeforeRetry: duration to sleep before trying to connect again
- maxRetryDuration: total amount of time to try and achieve a database connection
func PgxErrorToHTTPError ¶
PgxErrorToHTTPError converts a database error from github.com/jackc/pgx to an appropriate HTTP error.
Types ¶
type DB ¶
type DB interface {
Exec(
ctx context.Context,
sql string,
arguments ...any,
) (pgconn.CommandTag, error)
Query(
ctx context.Context,
sql string,
optionsAndArgs ...any,
) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
Begin(ctx context.Context) (pgx.Tx, error)
Ping(ctx context.Context) error
}
DB provides a uniform interface for the postgres database connection, pools and transactions.
type PgxSyncRow ¶
type PgxSyncRow struct {
// contains filtered or unexported fields
}
PgxSyncRow is a concurrent wrapper for pgx.Row.
func (*PgxSyncRow) Scan ¶
func (row *PgxSyncRow) Scan(dest ...any) error
Scan scans the data of PgxSyncRow into dest.
type PgxSyncRows ¶
type PgxSyncRows struct {
// contains filtered or unexported fields
}
PgxSyncRows is a concurrent wrapper for pgx.Rows.
func (*PgxSyncRows) Close ¶
func (rows *PgxSyncRows) Close()
Close doesn't do anything for PgxSyncRows as these are closed in [Query].
func (*PgxSyncRows) CommandTag ¶
func (rows *PgxSyncRows) CommandTag() pgconn.CommandTag
CommandTag fetches the pgconn.CommandTag.
func (*PgxSyncRows) FieldDescriptions ¶
func (rows *PgxSyncRows) FieldDescriptions() []pgconn.FieldDescription
FieldDescriptions fetches [pgconn.FieldDescription]s.
func (*PgxSyncRows) Next ¶
func (rows *PgxSyncRows) Next() bool
Next continues to the next row of PgxSyncRows if there is one.
func (*PgxSyncRows) RawValues ¶
func (rows *PgxSyncRows) RawValues() [][]byte
RawValues fetches the raw values of the current row.
func (*PgxSyncRows) Scan ¶
func (rows *PgxSyncRows) Scan(dest ...any) error
Scan scans the data of the current row into dest.
func (*PgxSyncRows) Values ¶
func (rows *PgxSyncRows) Values() ([]any, error)
Values fetches the values of the current row.
type PgxSyncTx ¶
type PgxSyncTx struct {
// contains filtered or unexported fields
}
PgxSyncTx uses database.SyncTx to make sure pgx.Tx can be used concurrently.
func CreatePgxSyncTx ¶
CreatePgxSyncTx returns a pgx.Tx which works concurrently.
func (*PgxSyncTx) Begin ¶
Begin is used to wrap pgx.Tx.Begin in a database.SyncTx.
func (*PgxSyncTx) Exec ¶
func (tx *PgxSyncTx) Exec( ctx context.Context, sql string, args ...any, ) (pgconn.CommandTag, error)
Exec is used to wrap pgx.Tx.Exec in a database.SyncTx.
func (*PgxSyncTx) Ping ¶
Ping is used to wrap [pgx.Tx.Conn.Ping] in a database.SyncTx.
func (*PgxSyncTx) Query ¶
Query is used to wrap pgx.Tx.Query in a database.SyncTx.
func (*PgxSyncTx) QueryRow ¶
QueryRow is used to wrap pgx.Tx.QueryRow in a database.SyncTx.
type SpanDB ¶
type SpanDB struct {
DB DB
// contains filtered or unexported fields
}
SpanDB is used to wrap database actions in [sentry.Span]s.
func (SpanDB) Begin ¶
Begin doesn't wrap Begin in a [sentry.Span] as this makes little sense for starting a transaction.
func (SpanDB) Exec ¶
func (db SpanDB) Exec( ctx context.Context, sql string, arguments ...any, ) (pgconn.CommandTag, error)
Exec is used to wrap Exec in a [sentry.Span].
func (SpanDB) Ping ¶
Ping doesn't wrap Ping in a [sentry.Span] as this makes little sense for pinging the db.