pgutil

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pgutil provides helpers for working with pgx (jackc/pgx)

Error checks

IsNoRows reports whether err is or wraps pgx.ErrNoRows (use after QueryRow when a missing row is expected) IsPgErrorCode reports whether err is a PgError with the given SQLSTATE code IsPgUniqueViolation, IsForeignKeyViolation, IsNotNullViolation, and IsCheckViolation are conveniences for common constraint codes (23505, 23503, 23502, 23514). IsSerializationFailure, IsDeadlockDetected, and IsRetryableTxError help classify transaction errors that are commonly retried at the transaction boundary

Timestamp conversion

pgx uses pgtype.Timestamptz for timestamp with time zone. TimestamptzToTime returns *time.Time or nil when invalid TimestamptzToTimeZero returns time.Time or the zero value. TimeToTimestamptz converts *time.Time to pgtype.Timestamptz (invalid if nil) PtrTimeToTime dereferences a *time.Time or returns time.Time{} if nil

Index

Constants

View Source
const (
	CodeUniqueViolation      = "23505"
	CodeForeignKeyViolation  = "23503"
	CodeNotNullViolation     = "23502"
	CodeCheckViolation       = "23514"
	CodeSerializationFailure = "40001"
	CodeDeadlockDetected     = "40P01"
)

Variables

This section is empty.

Functions

func IsCheckViolation added in v1.4.0

func IsCheckViolation(err error) bool

IsCheckViolation reports whether err is a PostgreSQL check constraint violation (SQLSTATE 23514)

func IsDeadlockDetected added in v1.4.0

func IsDeadlockDetected(err error) bool

IsDeadlockDetected reports whether err is a PostgreSQL deadlock detected error (SQLSTATE 40P01)

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a PostgreSQL foreign key violation (SQLSTATE 23503)

func IsNoRows

func IsNoRows(err error) bool

IsNoRows reports whether err is or wraps pgx.ErrNoRows. Use after QueryRow when a missing row is acceptable

func IsNotNullViolation

func IsNotNullViolation(err error) bool

IsNotNullViolation reports whether err is a PostgreSQL not null violation (SQLSTATE 23502)

func IsPgErrorCode

func IsPgErrorCode(err error, code string) bool

IsPgErrorCode reports whether err is or wraps a PgError with the given SQLSTATE code

func IsPgUniqueViolation

func IsPgUniqueViolation(err error) bool

IsPgUniqueViolation reports whether err is a PostgreSQL unique constraint violation (SQLSTATE 23505)

func IsRetryableTxError added in v1.4.0

func IsRetryableTxError(err error) bool

IsRetryableTxError reports whether err is a transaction error commonly safe to retry at the transaction boundary

func IsSerializationFailure added in v1.4.0

func IsSerializationFailure(err error) bool

IsSerializationFailure reports whether err is a PostgreSQL serialization failure (SQLSTATE 40001)

func PgErrorCode

func PgErrorCode(err error) string

PgErrorCode extracts the SQLSTATE code from err, or "" if err is not a PgError

func PtrTimeToTime

func PtrTimeToTime(t *time.Time) time.Time

PtrTimeToTime returns *t, or time.Time{} if t is nil

func TimeToTimestamptz

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

TimeToTimestamptz converts *time.Time to pgtype.Timestamptz. Returns an invalid Timestamptz if t is nil

func TimestamptzToTime

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

TimestamptzToTime returns a pointer to the time, or nil if t.Valid is false

func TimestamptzToTimeZero

func TimestamptzToTimeZero(t pgtype.Timestamptz) time.Time

TimestamptzToTimeZero returns t.Time, or time.Time{} if t.Valid is false

Types

This section is empty.

Jump to

Keyboard shortcuts

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