pg

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 15 Imported by: 4

Documentation

Index

Constants

View Source
const (
	JobLockStateNone     = ""
	JobLockStateHeld     = "held"
	JobLockStateLost     = "lost"
	JobLockStateReleased = "released"
)

Variables

This section is empty.

Functions

func BigintOrNull

func BigintOrNull(n int64) pgtype.Int8

BigintOrNull returns a pgtype.Int8 for the given value, but will return a Int8 value that represents null in the database if the value is zero.

func Date added in v0.7.2

func Date(t time.Time) pgtype.Date

Date converts a stdlib time.Time to a pgtype.Date.

func IsConstraintError

func IsConstraintError(err error, constraint string) bool

IsConstraintError checks if an error was caused by a specific constraint violation.

func PBool added in v0.11.2

func PBool(b *bool) pgtype.Bool

PBool converts a *bool to a pgtype.Bool.

func PInt2 added in v0.7.2

func PInt2(n *int16) pgtype.Int2

PInt2 returns a pgtype.Int2 for the given value, but will return a Int2 value that represents null in the database if the value is nil.

func PInt32 added in v0.11.6

func PInt32(n *int32) pgtype.Int4

PInt32 converts a *bool to a pgtype.Int4.

func PText added in v0.8.1

func PText(s *string) pgtype.Text

PText converts a *string to a pgtype.Text.

func PTime added in v0.7.2

func PTime(t *time.Time) pgtype.Timestamptz

PTime converts a stdlib *time.Time to a pgtype.Timestamptz.

func PUUID added in v0.11.5

func PUUID(u *uuid.UUID) pgtype.UUID

PUUID converts a *uuid.UUID to a pgtype.UUID.

func Rollback added in v0.15.1

func Rollback(tx pgx.Tx, outErr *error)

Rollback rolls back a transaction and joins the rollback error to the outError if the rollback fails. If the transaction already has been committed/closed it's not treated as an error.

Defer a call to Rollback directly after a transaction has been created. That will give you the guarantee that everything you've done will be rolled back if you return early before committing.

func SafeRollback deprecated

func SafeRollback(
	ctx context.Context, logger *slog.Logger, tx pgx.Tx, txName string,
)

SafeRollback rolls back a transaction and logs if the rollback fails. If the transaction already has been closed it's not treated as an error.

Deprecated: use Rollback() instead.

func SetConnStringVariables

func SetConnStringVariables(conn string, vars url.Values) (string, error)

SetConnStringVariables parses a connection string URI and adds the given query string variables to it.

func Text added in v0.11.5

func Text(s string) pgtype.Text

Text converts a string to a pgtype.Text.

func TextOrNull

func TextOrNull(s string) pgtype.Text

TextOrNull returns a pgtype.Text for the given string, but will return a Text value that represents null in the database if the string is empty.

func Time

func Time(t time.Time) pgtype.Timestamptz

Time converts a stdlib time.Time to a pgtype.Timestamptz.

func TimeOrNull

func TimeOrNull(t time.Time) pgtype.Timestamptz

Time converts a stdlib time.Time to a pgtype.Timestamptz, but will return a Timestamptz that represents a null value in the database if t is zero.

func ToUUIDPointer added in v0.11.5

func ToUUIDPointer(v pgtype.UUID) *uuid.UUID

ToUUIDPointer converts a pgtype.UUID to a *uuid.UUID.

func UUID added in v0.11.5

func UUID(u uuid.UUID) pgtype.UUID

UUID converts a uuid.UUID to a pgtype.UUID.

func WithTX

func WithTX(
	ctx context.Context, pool TransactionBeginner,
	fn func(tx pgx.Tx) error,
) (outErr error)

WithTX starts a transaction and calls the given function with it. If the function returns an error or panics the transaction will be rolled back.

Types

type JobLock

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

JobLock helps separate processes coordinate who should be performing a (background) task through postgres.

func NewJobLock

func NewJobLock(
	db *pgxpool.Pool, logger *slog.Logger, name string,
	opts JobLockOptions,
) (*JobLock, error)

NewJobLock creates a new job lock.

func (*JobLock) Identity added in v0.9.3

func (jl *JobLock) Identity() string

func (*JobLock) RunWithContext

func (jl *JobLock) RunWithContext(
	ctx context.Context,
	fn func(ctx context.Context) error,
) error

RunWithContext runs the provided function once the job lock has been acquired. The context provided to the function will be cancelled if the job lock is lost.

func (*JobLock) Stop

func (jl *JobLock) Stop()

Stop releases the job lock if held and stops all polling.

type JobLockOptions added in v0.7.0

type JobLockOptions struct {
	// PingInterval controls how often the job locked should be
	// pinged/renewed. Defaults to 10s.
	PingInterval time.Duration
	// StaleAfter controls after how long a time a held lock should be
	// considered stale and other clients will start attempting to steal
	// it. Must be longer than the ping interval. Defaults to four times the
	// ping interval.
	StaleAfter time.Duration
	// CheckInterval controls how often clients should check if a held lock
	// has become stale. Defaults to twice the ping interval.
	CheckInterval time.Duration
	// Timeout is the timeout that should be used for all lock
	// operations. Must be shorter than the ping interval. Defaults to half
	// the ping interval.
	Timeout time.Duration
}

JobLockOptions controls how a job lock should behave.

type JobLockState

type JobLockState string

type TransactionBeginner

type TransactionBeginner interface {
	Begin(context.Context) (pgx.Tx, error)
}

TransactionBeginner is the interface for something that can start a pgx transaction for use with WithTX().

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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