pgx

package
v1.61.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, connString string, opts ...Option) (*pgx.Conn, error)
Example
ctx := context.TODO()

// The package exposes the same connect functions as the regular pgx.v5 library
// which sets up a tracer and connects as usual.
db, err := pgxtrace.Connect(ctx, "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
if err != nil {
	log.Fatal(err)
}
defer db.Close(ctx)

// Any calls made to the database will be traced as expected.
rows, err := db.Query(ctx, "SELECT name FROM users WHERE age=$1", 27)
if err != nil {
	log.Fatal(err)
}
defer rows.Close()

// This enables you to use PostgresSQL specific functions implemented by pgx.v5.
numbers := []int{1, 2, 3}

copyFromSource := pgx.CopyFromSlice(len(numbers), func(i int) ([]any, error) {
	return []any{numbers[i]}, nil
})

_, err = db.CopyFrom(ctx, []string{"numbers"}, []string{"number"}, copyFromSource)
if err != nil {
	log.Fatal(err)
}
Output:

func ConnectConfig

func ConnectConfig(ctx context.Context, connConfig *pgx.ConnConfig, opts ...Option) (*pgx.Conn, error)

func NewPool

func NewPool(ctx context.Context, connString string, opts ...Option) (*pgxpool.Pool, error)

func NewPoolWithConfig

func NewPoolWithConfig(ctx context.Context, config *pgxpool.Config, opts ...Option) (*pgxpool.Pool, error)

Types

type Batch

type Batch = pgx.Batch

type Option

type Option func(*config)

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service name to use for all spans.

func WithTraceBatch

func WithTraceBatch(enabled bool) Option

WithTraceBatch enables tracing batched operations (i.e. pgx.Batch{}).

func WithTraceConnect

func WithTraceConnect(enabled bool) Option

WithTraceConnect enables tracing calls to Connect and ConnectConfig.

pgx.Connect(ctx, "postgres://user:pass@example.com:5432/dbname", pgx.WithTraceConnect())

func WithTraceCopyFrom

func WithTraceCopyFrom(enabled bool) Option

WithTraceCopyFrom enables tracing pgx.CopyFrom calls.

func WithTracePrepare

func WithTracePrepare(enabled bool) Option

WithTracePrepare enables tracing prepared statements.

conn, err := pgx.Connect(ctx, "postgres://user:pass@example.com:5432/dbname", pgx.WithTraceConnect())
if err != nil {
	// handle err
}
defer conn.Close(ctx)

_, err := conn.Prepare(ctx, "stmt", "select $1::integer")
row, err := conn.QueryRow(ctx, "stmt", 1)

func WithTraceQuery

func WithTraceQuery(enabled bool) Option

WithTraceQuery enables tracing query operations.

Jump to

Keyboard shortcuts

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