pgx: github.com/jackc/pgx Index | Files | Directories

package pgx

import "github.com/jackc/pgx"

Package pgx is a PostgreSQL database driver.

pgx provides lower level access to PostgreSQL than the standard database/sql. It remains as similar to the database/sql interface as possible while providing better speed and access to PostgreSQL specific features. Import github.com/jackc/pgx/stdlib to use pgx as a database/sql compatible driver.

Query Interface

pgx implements Query and Scan in the familiar database/sql style.

var sum int32

// Send the query to the server. The returned rows MUST be closed
// before conn can be used again.
rows, err := conn.Query("select generate_series(1,$1)", 10)
if err != nil {
    return err
}

// rows.Close is called by rows.Next when all rows are read
// or an error occurs in Next or Scan. So it may optionally be
// omitted if nothing in the rows.Next loop can panic. It is
// safe to close rows multiple times.
defer rows.Close()

// Iterate through the result set
for rows.Next() {
    var n int32
    err = rows.Scan(&n)
    if err != nil {
        return err
    }
    sum += n
}

// Any errors encountered by rows.Next or rows.Scan will be returned here
if rows.Err() != nil {
    return err
}

// No errors found - do something with sum

pgx also implements QueryRow in the same style as database/sql.

var name string
var weight int64
err := conn.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
    return err
}

Use Exec to execute a query that does not return a result set.

commandTag, err := conn.Exec("delete from widgets where id=$1", 42)
if err != nil {
    return err
}
if commandTag.RowsAffected() != 1 {
    return errors.New("No row found to delete")
}

Connection Pool

Connection pool usage is explicit and configurable. In pgx, a connection can be created and managed directly, or a connection pool with a configurable maximum connections can be used. The connection pool offers an after connect hook that allows every connection to be automatically setup before being made available in the connection pool.

It delegates methods such as QueryRow to an automatically checked out and released connection so you can avoid manually acquiring and releasing connections when you do not need that level of control.

var name string
var weight int64
err := pool.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
    return err
}

Base Type Mapping

pgx maps between all common base types directly between Go and PostgreSQL. In particular:

Go           PostgreSQL
-----------------------
string       varchar
             text

// Integers are automatically be converted to any other integer type if
// it can be done without overflow or underflow.
int8
int16        smallint
int32        int
int64        bigint
int
uint8
uint16
uint32
uint64
uint

// Floats are strict and do not automatically convert like integers.
float32      float4
float64      float8

time.Time   date
            timestamp
            timestamptz

[]byte      bytea

Null Mapping

pgx can map nulls in two ways. The first is package pgtype provides types that have a data field and a status field. They work in a similar fashion to database/sql. The second is to use a pointer to a pointer.

var foo pgtype.Varchar
var bar *string
err := conn.QueryRow("select foo, bar from widgets where id=$1", 42).Scan(&a, &b)
if err != nil {
    return err
}

Array Mapping

pgx maps between int16, int32, int64, float32, float64, and string Go slices and the equivalent PostgreSQL array type. Go slices of native types do not support nulls, so if a PostgreSQL array that contains a null is read into a native Go slice an error will occur. The pgtype package includes many more array types for PostgreSQL types that do not directly map to native Go types.

JSON and JSONB Mapping

pgx includes built-in support to marshal and unmarshal between Go types and the PostgreSQL JSON and JSONB.

Inet and CIDR Mapping

pgx encodes from net.IPNet to and from inet and cidr PostgreSQL types. In addition, as a convenience pgx will encode from a net.IP; it will assume a /32 netmask for IPv4 and a /128 for IPv6.

Custom Type Support

pgx includes support for the common data types like integers, floats, strings, dates, and times that have direct mappings between Go and SQL. In addition, pgx uses the github.com/jackc/pgx/pgtype library to support more types. See documention for that library for instructions on how to implement custom types.

See example_custom_type_test.go for an example of a custom type for the PostgreSQL point type.

pgx also includes support for custom types implementing the database/sql.Scanner and database/sql/driver.Valuer interfaces.

Raw Bytes Mapping

[]byte passed as arguments to Query, QueryRow, and Exec are passed unmodified to PostgreSQL.

Transactions

Transactions are started by calling Begin or BeginEx. The BeginEx variant can create a transaction with a specified isolation level.

tx, err := conn.Begin()
if err != nil {
    return err
}
// Rollback is safe to call even if the tx is already closed, so if
// the tx commits successfully, this is a no-op
defer tx.Rollback()

_, err = tx.Exec("insert into foo(id) values (1)")
if err != nil {
    return err
}

err = tx.Commit()
if err != nil {
    return err
}

Copy Protocol

Use CopyFrom to efficiently insert multiple rows at a time using the PostgreSQL copy protocol. CopyFrom accepts a CopyFromSource interface. If the data is already in a [][]interface{} use CopyFromRows to wrap it in a CopyFromSource interface. Or implement CopyFromSource to avoid buffering the entire data set in memory.

rows := [][]interface{}{
    {"John", "Smith", int32(36)},
    {"Jane", "Doe", int32(29)},
}

copyCount, err := conn.CopyFrom(
    pgx.Identifier{"people"},
    []string{"first_name", "last_name", "age"},
    pgx.CopyFromRows(rows),
)

CopyFrom can be faster than an insert with as few as 5 rows.

Listen and Notify

pgx can listen to the PostgreSQL notification system with the WaitForNotification function. It takes a maximum time to wait for a notification.

err := conn.Listen("channelname")
if err != nil {
    return nil
}

if notification, err := conn.WaitForNotification(time.Second); err != nil {
    // do something with notification
}

TLS

The pgx ConnConfig struct has a TLSConfig field. If this field is nil, then TLS will be disabled. If it is present, then it will be used to configure the TLS connection. This allows total configuration of the TLS connection.

Logging

pgx defines a simple logger interface. Connections optionally accept a logger that satisfies this interface. Set LogLevel to control logging verbosity. Adapters for github.com/inconshreveable/log15, github.com/sirupsen/logrus, and the testing log are provided in the log directory.

Index

Package Files

batch.go conn.go conn_pool.go copy_from.go doc.go fastpath.go go_stdlib.go large_objects.go logger.go messages.go pgpass.go query.go replication.go sql.go tx.go values.go

Constants

const (
    LogLevelTrace = 6
    LogLevelDebug = 5
    LogLevelInfo  = 4
    LogLevelWarn  = 3
    LogLevelError = 2
    LogLevelNone  = 1
)

The values for log levels are chosen such that the zero value means that no log level was specified.

const (
    Serializable    = TxIsoLevel("serializable")
    RepeatableRead  = TxIsoLevel("repeatable read")
    ReadCommitted   = TxIsoLevel("read committed")
    ReadUncommitted = TxIsoLevel("read uncommitted")
)

Transaction isolation levels

const (
    ReadWrite = TxAccessMode("read write")
    ReadOnly  = TxAccessMode("read only")
)

Transaction access modes

const (
    Deferrable    = TxDeferrableMode("deferrable")
    NotDeferrable = TxDeferrableMode("not deferrable")
)

Transaction deferrable modes

const (
    TxStatusInProgress      = 0
    TxStatusCommitFailure   = -1
    TxStatusRollbackFailure = -2
    TxStatusCommitSuccess   = 1
    TxStatusRollbackSuccess = 2
)
const (
    TextFormatCode   = 0
    BinaryFormatCode = 1
)

PostgreSQL format codes

Variables

var ErrAcquireTimeout = errors.New("timeout acquiring connection from pool")

ErrAcquireTimeout occurs when an attempt to acquire a connection times out.

var ErrClosedPool = errors.New("cannot acquire from closed pool")

ErrClosedPool occurs on an attempt to acquire a connection from a closed pool.

var ErrConnBusy = errors.New("conn is busy")

ErrConnBusy occurs when the connection is busy (for example, in the middle of reading query results) and another action is attempted.

var ErrDeadConn = errors.New("conn is dead")

ErrDeadConn occurs on an attempt to use a dead connection

var ErrInvalidLogLevel = errors.New("invalid log level")

ErrInvalidLogLevel occurs on attempt to set an invalid log level.

var ErrNoRows = errors.New("no rows in result set")

ErrNoRows occurs when rows are expected but none are returned.

var ErrTLSRefused = errors.New("server refused TLS connection")

ErrTLSRefused occurs when the connection attempt requires TLS and the PostgreSQL server refuses to use TLS

var ErrTxClosed = errors.New("tx is closed")
var ErrTxCommitRollback = errors.New("commit unexpectedly resulted in rollback")

ErrTxCommitRollback occurs when an error has occurred in a transaction and Commit() is called. PostgreSQL accepts COMMIT on aborted transactions, but it is treated as ROLLBACK.

func FormatLSN Uses

func FormatLSN(lsn uint64) string

Format the given 64bit LSN value into the XXX/XXX format, which is the format reported by postgres.

func ParseLSN Uses

func ParseLSN(lsn string) (outputLsn uint64, err error)

Parse the given XXX/XXX format LSN as reported by postgres, into a 64 bit integer as used internally by the wire procotols

type Batch Uses

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

Batch queries are a way of bundling multiple queries together to avoid unnecessary network round trips.

func (*Batch) Close Uses

func (b *Batch) Close() (err error)

Close closes the batch operation. Any error that occured during a batch operation may have made it impossible to resyncronize the connection with the server. In this case the underlying connection will have been closed.

func (*Batch) Conn Uses

func (b *Batch) Conn() *Conn

Conn returns the underlying connection that b will or was performed on.

func (*Batch) ExecResults Uses

func (b *Batch) ExecResults() (CommandTag, error)

ExecResults reads the results from the next query in the batch as if the query has been sent with Exec.

func (*Batch) QueryResults Uses

func (b *Batch) QueryResults() (*Rows, error)

QueryResults reads the results from the next query in the batch as if the query has been sent with Query.

func (*Batch) QueryRowResults Uses

func (b *Batch) QueryRowResults() *Row

QueryRowResults reads the results from the next query in the batch as if the query has been sent with QueryRow.

func (*Batch) Queue Uses

func (b *Batch) Queue(query string, arguments []interface{}, parameterOIDs []pgtype.OID, resultFormatCodes []int16)

Queue queues a query to batch b. parameterOIDs are required if there are parameters and query is not the name of a prepared statement. resultFormatCodes are required if there is a result.

func (*Batch) Send Uses

func (b *Batch) Send(ctx context.Context, txOptions *TxOptions) error

Send sends all queued queries to the server at once. If the batch is created from a conn Object then All queries are wrapped in a transaction. The transaction can optionally be configured with txOptions. The context is in effect until the Batch is closed.

type CommandTag Uses

type CommandTag string

CommandTag is the result of an Exec function

func (CommandTag) RowsAffected Uses

func (ct CommandTag) RowsAffected() int64

RowsAffected returns the number of rows affected. If the CommandTag was not for a row affecting command (such as "CREATE TABLE") then it returns 0

type Conn Uses

type Conn struct {
    RuntimeParams map[string]string // parameters that have been reported by the server

    ConnInfo *pgtype.ConnInfo
    // contains filtered or unexported fields
}

Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use ConnPool to manage access to multiple database connections from multiple goroutines.

func Connect Uses

func Connect(config ConnConfig) (c *Conn, err error)

Connect establishes a connection with a PostgreSQL server using config. config.Host must be specified. config.User will default to the OS user name. Other config fields are optional.

func (*Conn) Begin Uses

func (c *Conn) Begin() (*Tx, error)

Begin starts a transaction with the default transaction mode for the current connection. To use a specific transaction mode see BeginEx.

func (*Conn) BeginBatch Uses

func (c *Conn) BeginBatch() *Batch

BeginBatch returns a *Batch query for c.

func (*Conn) BeginEx Uses

func (c *Conn) BeginEx(ctx context.Context, txOptions *TxOptions) (*Tx, error)

BeginEx starts a transaction with txOptions determining the transaction mode. Unlike database/sql, the context only affects the begin command. i.e. there is no auto-rollback on context cancelation.

func (*Conn) CauseOfDeath Uses

func (c *Conn) CauseOfDeath() error

func (*Conn) Close Uses

func (c *Conn) Close() (err error)

Close closes a connection. It is safe to call Close on a already closed connection.

func (*Conn) CopyFrom Uses

func (c *Conn) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int, error)

CopyFrom uses the PostgreSQL copy protocol to perform bulk data insertion. It returns the number of rows copied and an error.

CopyFrom requires all values use the binary format. Almost all types implemented by pgx use the binary format by default. Types implementing Encoder can only be used if they encode to the binary format.

func (*Conn) Deallocate Uses

func (c *Conn) Deallocate(name string) error

Deallocate released a prepared statement

func (*Conn) Exec Uses

func (c *Conn) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)

Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced positionally from the sql string as $1, $2, etc.

func (*Conn) ExecEx Uses

func (c *Conn) ExecEx(ctx context.Context, sql string, options *QueryExOptions, arguments ...interface{}) (CommandTag, error)

func (*Conn) IsAlive Uses

func (c *Conn) IsAlive() bool

func (*Conn) Listen Uses

func (c *Conn) Listen(channel string) error

Listen establishes a PostgreSQL listen/notify to channel

func (*Conn) PID Uses

func (c *Conn) PID() uint32

PID returns the backend PID for this connection.

func (*Conn) Ping Uses

func (c *Conn) Ping(ctx context.Context) error

func (*Conn) Prepare Uses

func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error)

Prepare creates a prepared statement with name and sql. sql can contain placeholders for bound parameters. These placeholders are referenced positional as $1, $2, etc.

Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same name and sql arguments. This allows a code path to Prepare and Query/Exec without concern for if the statement has already been prepared.

func (*Conn) PrepareEx Uses

func (c *Conn) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (ps *PreparedStatement, err error)

PrepareEx creates a prepared statement with name and sql. sql can contain placeholders for bound parameters. These placeholders are referenced positional as $1, $2, etc. It defers from Prepare as it allows additional options (such as parameter OIDs) to be passed via struct

PrepareEx is idempotent; i.e. it is safe to call PrepareEx multiple times with the same name and sql arguments. This allows a code path to PrepareEx and Query/Exec without concern for if the statement has already been prepared.

func (*Conn) Query Uses

func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error)

Query executes sql with args. If there is an error the returned *Rows will be returned in an error state. So it is allowed to ignore the error returned from Query and handle it in *Rows.

func (*Conn) QueryEx Uses

func (c *Conn) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (rows *Rows, err error)

func (*Conn) QueryRow Uses

func (c *Conn) QueryRow(sql string, args ...interface{}) *Row

QueryRow is a convenience wrapper over Query. Any error that occurs while querying is deferred until calling Scan on the returned *Row. That *Row will error with ErrNoRows if no rows are returned.

func (*Conn) QueryRowEx Uses

func (c *Conn) QueryRowEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) *Row

func (*Conn) SetLogLevel Uses

func (c *Conn) SetLogLevel(lvl int) (int, error)

SetLogLevel replaces the current log level and returns the previous log level.

func (*Conn) SetLogger Uses

func (c *Conn) SetLogger(logger Logger) Logger

SetLogger replaces the current logger and returns the previous logger.

func (*Conn) Unlisten Uses

func (c *Conn) Unlisten(channel string) error

Unlisten unsubscribes from a listen channel

func (*Conn) WaitForNotification Uses

func (c *Conn) WaitForNotification(ctx context.Context) (notification *Notification, err error)

WaitForNotification waits for a PostgreSQL notification.

type ConnConfig Uses

type ConnConfig struct {
    Host              string // host (e.g. localhost) or path to unix domain socket directory (e.g. /private/tmp)
    Port              uint16 // default: 5432
    Database          string
    User              string // default: OS user name
    Password          string
    TLSConfig         *tls.Config // config for TLS connection -- nil disables TLS
    UseFallbackTLS    bool        // Try FallbackTLSConfig if connecting with TLSConfig fails. Used for preferring TLS, but allowing unencrypted, or vice-versa
    FallbackTLSConfig *tls.Config // config for fallback TLS connection (only used if UseFallBackTLS is true)-- nil disables TLS
    Logger            Logger
    LogLevel          int
    Dial              DialFunc
    RuntimeParams     map[string]string // Run-time parameters to set on connection as session default values (e.g. search_path or application_name)
    OnNotice          NoticeHandler     // Callback function called when a notice response is received.
}

ConnConfig contains all the options used to establish a connection.

func ParseConnectionString Uses

func ParseConnectionString(s string) (ConnConfig, error)

ParseConnectionString parses either a URI or a DSN connection string. see ParseURI and ParseDSN for details.

func ParseDSN Uses

func ParseDSN(s string) (ConnConfig, error)

ParseDSN parses a database DSN (data source name) into a ConnConfig

e.g. ParseDSN("user=username password=password host=1.2.3.4 port=5432 dbname=mydb sslmode=disable")

Any options not used by the connection process are parsed into ConnConfig.RuntimeParams.

e.g. ParseDSN("application_name=pgxtest search_path=admin user=username password=password host=1.2.3.4 dbname=mydb")

ParseDSN tries to match libpq behavior with regard to sslmode. See comments for ParseEnvLibpq for more information on the security implications of sslmode options.

func ParseEnvLibpq Uses

func ParseEnvLibpq() (ConnConfig, error)

ParseEnvLibpq parses the environment like libpq does into a ConnConfig

See http://www.postgresql.org/docs/9.4/static/libpq-envars.html for details on the meaning of environment variables.

ParseEnvLibpq currently recognizes the following environment variables: PGHOST PGPORT PGDATABASE PGUSER PGPASSWORD PGSSLMODE PGAPPNAME

Important TLS Security Notes: ParseEnvLibpq tries to match libpq behavior with regard to PGSSLMODE. This includes defaulting to "prefer" behavior if no environment variable is set.

See http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION for details on what level of security each sslmode provides.

"verify-ca" mode currently is treated as "verify-full". e.g. It has stronger security guarantees than it would with libpq. Do not rely on this behavior as it may be possible to match libpq in the future. If you need full security use "verify-full".

Several of the PGSSLMODE options (including the default behavior of "prefer") will set UseFallbackTLS to true and FallbackTLSConfig to a disabled or weakened TLS mode. This means that if ParseEnvLibpq is used, but TLSConfig is later set from a different source that UseFallbackTLS MUST be set false to avoid the possibility of falling back to weaker or disabled security.

func ParseURI Uses

func ParseURI(uri string) (ConnConfig, error)

ParseURI parses a database URI into ConnConfig

Query parameters not used by the connection process are parsed into ConnConfig.RuntimeParams.

func (ConnConfig) Merge Uses

func (old ConnConfig) Merge(other ConnConfig) ConnConfig

Merge returns a new ConnConfig with the attributes of old and other combined. When an attribute is set on both, other takes precedence.

As a security precaution, if the other TLSConfig is nil, all old TLS attributes will be preserved.

type ConnPool Uses

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

func NewConnPool Uses

func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error)

NewConnPool creates a new ConnPool. config.ConnConfig is passed through to Connect directly.

func (*ConnPool) Acquire Uses

func (p *ConnPool) Acquire() (*Conn, error)

Acquire takes exclusive use of a connection until it is released.

func (*ConnPool) Begin Uses

func (p *ConnPool) Begin() (*Tx, error)

Begin acquires a connection and begins a transaction on it. When the transaction is closed the connection will be automatically released.

func (*ConnPool) BeginBatch Uses

func (p *ConnPool) BeginBatch() *Batch

BeginBatch acquires a connection and begins a batch on that connection. When *Batch is finished, the connection is released automatically.

func (*ConnPool) BeginEx Uses

func (p *ConnPool) BeginEx(ctx context.Context, txOptions *TxOptions) (*Tx, error)

BeginEx acquires a connection and starts a transaction with txOptions determining the transaction mode. When the transaction is closed the connection will be automatically released.

func (*ConnPool) Close Uses

func (p *ConnPool) Close()

Close ends the use of a connection pool. It prevents any new connections from being acquired and closes available underlying connections. Any acquired connections will be closed when they are released.

func (*ConnPool) CopyFrom Uses

func (p *ConnPool) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int, error)

CopyFrom acquires a connection, delegates the call to that connection, and releases the connection

func (*ConnPool) Deallocate Uses

func (p *ConnPool) Deallocate(name string) (err error)

Deallocate releases a prepared statement from all connections in the pool.

func (*ConnPool) Exec Uses

func (p *ConnPool) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)

Exec acquires a connection, delegates the call to that connection, and releases the connection

func (*ConnPool) ExecEx Uses

func (p *ConnPool) ExecEx(ctx context.Context, sql string, options *QueryExOptions, arguments ...interface{}) (commandTag CommandTag, err error)

func (*ConnPool) Prepare Uses

func (p *ConnPool) Prepare(name, sql string) (*PreparedStatement, error)

Prepare creates a prepared statement on a connection in the pool to test the statement is valid. If it succeeds all connections accessed through the pool will have the statement available.

Prepare creates a prepared statement with name and sql. sql can contain placeholders for bound parameters. These placeholders are referenced positional as $1, $2, etc.

Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same name and sql arguments. This allows a code path to Prepare and Query/Exec/PrepareEx without concern for if the statement has already been prepared.

func (*ConnPool) PrepareEx Uses

func (p *ConnPool) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (*PreparedStatement, error)

PrepareEx creates a prepared statement on a connection in the pool to test the statement is valid. If it succeeds all connections accessed through the pool will have the statement available.

PrepareEx creates a prepared statement with name and sql. sql can contain placeholders for bound parameters. These placeholders are referenced positional as $1, $2, etc. It defers from Prepare as it allows additional options (such as parameter OIDs) to be passed via struct

PrepareEx is idempotent; i.e. it is safe to call PrepareEx multiple times with the same name and sql arguments. This allows a code path to PrepareEx and Query/Exec/Prepare without concern for if the statement has already been prepared.

func (*ConnPool) Query Uses

func (p *ConnPool) Query(sql string, args ...interface{}) (*Rows, error)

Query acquires a connection and delegates the call to that connection. When *Rows are closed, the connection is released automatically.

func (*ConnPool) QueryEx Uses

func (p *ConnPool) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (*Rows, error)

func (*ConnPool) QueryRow Uses

func (p *ConnPool) QueryRow(sql string, args ...interface{}) *Row

QueryRow acquires a connection and delegates the call to that connection. The connection is released automatically after Scan is called on the returned *Row.

func (*ConnPool) QueryRowEx Uses

func (p *ConnPool) QueryRowEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) *Row

func (*ConnPool) Release Uses

func (p *ConnPool) Release(conn *Conn)

Release gives up use of a connection.

func (*ConnPool) Reset Uses

func (p *ConnPool) Reset()

Reset closes all open connections, but leaves the pool open. It is intended for use when an error is detected that would disrupt all connections (such as a network interruption or a server state change).

It is safe to reset a pool while connections are checked out. Those connections will be closed when they are returned to the pool.

func (*ConnPool) Stat Uses

func (p *ConnPool) Stat() (s ConnPoolStat)

Stat returns connection pool statistics

type ConnPoolConfig Uses

type ConnPoolConfig struct {
    ConnConfig
    MaxConnections int               // max simultaneous connections to use, default 5, must be at least 2
    AfterConnect   func(*Conn) error // function to call on every new connection
    AcquireTimeout time.Duration     // max wait time when all connections are busy (0 means no timeout)
}

type ConnPoolStat Uses

type ConnPoolStat struct {
    MaxConnections       int // max simultaneous connections to use
    CurrentConnections   int // current live connections
    AvailableConnections int // unused live connections
}

type CopyFromSource Uses

type CopyFromSource interface {
    // Next returns true if there is another row and makes the next row data
    // available to Values(). When there are no more rows available or an error
    // has occurred it returns false.
    Next() bool

    // Values returns the values for the current row.
    Values() ([]interface{}, error)

    // Err returns any error that has been encountered by the CopyFromSource. If
    // this is not nil *Conn.CopyFrom will abort the copy.
    Err() error
}

CopyFromSource is the interface used by *Conn.CopyFrom as the source for copy data.

func CopyFromRows Uses

func CopyFromRows(rows [][]interface{}) CopyFromSource

CopyFromRows returns a CopyFromSource interface over the provided rows slice making it usable by *Conn.CopyFrom.

type DialFunc Uses

type DialFunc func(network, addr string) (net.Conn, error)

DialFunc is a function that can be used to connect to a PostgreSQL server

type FieldDescription Uses

type FieldDescription struct {
    Name            string
    Table           pgtype.OID
    AttributeNumber uint16
    DataType        pgtype.OID
    DataTypeSize    int16
    DataTypeName    string
    Modifier        uint32
    FormatCode      int16
}

func (FieldDescription) Length Uses

func (fd FieldDescription) Length() (int64, bool)

func (FieldDescription) PrecisionScale Uses

func (fd FieldDescription) PrecisionScale() (precision, scale int64, ok bool)

func (FieldDescription) Type Uses

func (fd FieldDescription) Type() reflect.Type

type Identifier Uses

type Identifier []string

Identifier a PostgreSQL identifier or name. Identifiers can be composed of multiple parts such as ["schema", "table"] or ["table", "column"].

func (Identifier) Sanitize Uses

func (ident Identifier) Sanitize() string

Sanitize returns a sanitized string safe for SQL interpolation.

type LargeObject Uses

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

A LargeObject is a large object stored on the server. It is only valid within the transaction that it was initialized in. It implements these interfaces:

io.Writer
io.Reader
io.Seeker
io.Closer

func (*LargeObject) Close Uses

func (o *LargeObject) Close() error

Close closees the large object descriptor.

func (*LargeObject) Read Uses

func (o *LargeObject) Read(p []byte) (int, error)

Read reads up to len(p) bytes into p returning the number of bytes read.

func (*LargeObject) Seek Uses

func (o *LargeObject) Seek(offset int64, whence int) (n int64, err error)

Seek moves the current location pointer to the new location specified by offset.

func (*LargeObject) Tell Uses

func (o *LargeObject) Tell() (n int64, err error)

Tell returns the current read or write location of the large object descriptor.

func (*LargeObject) Truncate Uses

func (o *LargeObject) Truncate(size int64) (err error)

Trunctes the large object to size.

func (*LargeObject) Write Uses

func (o *LargeObject) Write(p []byte) (int, error)

Write writes p to the large object and returns the number of bytes written and an error if not all of p was written.

type LargeObjectMode Uses

type LargeObjectMode int32
const (
    LargeObjectModeWrite LargeObjectMode = 0x20000
    LargeObjectModeRead  LargeObjectMode = 0x40000
)

type LargeObjects Uses

type LargeObjects struct {
    // Has64 is true if the server is capable of working with 64-bit numbers
    Has64 bool
    // contains filtered or unexported fields
}

LargeObjects is a structure used to access the large objects API. It is only valid within the transaction where it was created.

For more details see: http://www.postgresql.org/docs/current/static/largeobjects.html

func (*LargeObjects) Create Uses

func (o *LargeObjects) Create(id pgtype.OID) (pgtype.OID, error)

Create creates a new large object. If id is zero, the server assigns an unused OID.

func (*LargeObjects) Open Uses

func (o *LargeObjects) Open(oid pgtype.OID, mode LargeObjectMode) (*LargeObject, error)

Open opens an existing large object with the given mode.

func (o *LargeObjects) Unlink(oid pgtype.OID) error

Unlink removes a large object from the database.

type LogLevel Uses

type LogLevel int

LogLevel represents the pgx logging level. See LogLevel* constants for possible values.

func LogLevelFromString Uses

func LogLevelFromString(s string) (LogLevel, error)

LogLevelFromString converts log level string to constant

Valid levels:

trace
debug
info
warn
error
none

func (LogLevel) String Uses

func (ll LogLevel) String() string

type Logger Uses

type Logger interface {
    // Log a message at the given level with data key/value pairs. data may be nil.
    Log(level LogLevel, msg string, data map[string]interface{})
}

Logger is the interface used to get logging from pgx internals.

type Notice Uses

type Notice PgError

Notice represents a notice response message reported by the PostgreSQL server. Be aware that this is distinct from LISTEN/NOTIFY notification.

type NoticeHandler Uses

type NoticeHandler func(*Conn, *Notice)

NoticeHandler is a function that can handle notices received from the PostgreSQL server. Notices can be received at any time, usually during handling of a query response. The *Conn is provided so the handler is aware of the origin of the notice, but it must not invoke any query method. Be aware that this is distinct from LISTEN/NOTIFY notification.

type Notification Uses

type Notification struct {
    PID     uint32 // backend pid that sent the notification
    Channel string // channel from which notification was received
    Payload string
}

Notification is a message received from the PostgreSQL LISTEN/NOTIFY system

type PgError Uses

type PgError struct {
    Severity         string
    Code             string
    Message          string
    Detail           string
    Hint             string
    Position         int32
    InternalPosition int32
    InternalQuery    string
    Where            string
    SchemaName       string
    TableName        string
    ColumnName       string
    DataTypeName     string
    ConstraintName   string
    File             string
    Line             int32
    Routine          string
}

PgError represents an error reported by the PostgreSQL server. See http://www.postgresql.org/docs/9.3/static/protocol-error-fields.html for detailed field description.

func (PgError) Error Uses

func (pe PgError) Error() string

type PrepareExOptions Uses

type PrepareExOptions struct {
    ParameterOIDs []pgtype.OID
}

PrepareExOptions is an option struct that can be passed to PrepareEx

type PreparedStatement Uses

type PreparedStatement struct {
    Name              string
    SQL               string
    FieldDescriptions []FieldDescription
    ParameterOIDs     []pgtype.OID
}

PreparedStatement is a description of a prepared statement

type ProtocolError Uses

type ProtocolError string

ProtocolError occurs when unexpected data is received from PostgreSQL

func (ProtocolError) Error Uses

func (e ProtocolError) Error() string

type QueryArgs Uses

type QueryArgs []interface{}

QueryArgs is a container for arguments to an SQL query. It is helpful when building SQL statements where the number of arguments is variable.

func (*QueryArgs) Append Uses

func (qa *QueryArgs) Append(v interface{}) string

Append adds a value to qa and returns the placeholder value for the argument. e.g. $1, $2, etc.

type QueryExOptions Uses

type QueryExOptions struct {
    // When ParameterOIDs are present and the query is not a prepared statement,
    // then ParameterOIDs and ResultFormatCodes will be used to avoid an extra
    // network round-trip.
    ParameterOIDs     []pgtype.OID
    ResultFormatCodes []int16

    SimpleProtocol bool
}

type ReplicationConn Uses

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

func ReplicationConnect Uses

func ReplicationConnect(config ConnConfig) (r *ReplicationConn, err error)

func (*ReplicationConn) CauseOfDeath Uses

func (rc *ReplicationConn) CauseOfDeath() error

func (*ReplicationConn) Close Uses

func (rc *ReplicationConn) Close() error

func (*ReplicationConn) CreateReplicationSlot Uses

func (rc *ReplicationConn) CreateReplicationSlot(slotName, outputPlugin string) (err error)

Create the replication slot, using the given name and output plugin.

func (*ReplicationConn) CreateReplicationSlotEx Uses

func (rc *ReplicationConn) CreateReplicationSlotEx(slotName, outputPlugin string) (consistentPoint string, snapshotName string, err error)

Create the replication slot, using the given name and output plugin, and return the consistent_point and snapshot_name values.

func (*ReplicationConn) DropReplicationSlot Uses

func (rc *ReplicationConn) DropReplicationSlot(slotName string) (err error)

Drop the replication slot for the given name

func (*ReplicationConn) IdentifySystem Uses

func (rc *ReplicationConn) IdentifySystem() (r *Rows, err error)

Execute the "IDENTIFY_SYSTEM" command as documented here: https://www.postgresql.org/docs/9.5/static/protocol-replication.html

This will return (if successful) a result set that has a single row that contains the systemid, current timeline, xlogpos and database name.

NOTE: Because this is a replication mode connection, we don't have type names, so the field descriptions in the result will have only OIDs and no DataTypeName values

func (*ReplicationConn) IsAlive Uses

func (rc *ReplicationConn) IsAlive() bool

func (*ReplicationConn) SendStandbyStatus Uses

func (rc *ReplicationConn) SendStandbyStatus(k *StandbyStatus) (err error)

Send standby status to the server, which both acts as a keepalive message to the server, as well as carries the WAL position of the client, which then updates the server's replication slot position.

func (*ReplicationConn) StartReplication Uses

func (rc *ReplicationConn) StartReplication(slotName string, startLsn uint64, timeline int64, pluginArguments ...string) (err error)

Start a replication connection, sending WAL data to the given replication receiver. This function wraps a START_REPLICATION command as documented here: https://www.postgresql.org/docs/9.5/static/protocol-replication.html

Once started, the client needs to invoke WaitForReplicationMessage() in order to fetch the WAL and standby status. Also, it is the responsibility of the caller to periodically send StandbyStatus messages to update the replication slot position.

This function assumes that slotName has already been created. In order to omit the timeline argument pass a -1 for the timeline to get the server default behavior.

func (*ReplicationConn) TimelineHistory Uses

func (rc *ReplicationConn) TimelineHistory(timeline int) (r *Rows, err error)

Execute the "TIMELINE_HISTORY" command as documented here: https://www.postgresql.org/docs/9.5/static/protocol-replication.html

This will return (if successful) a result set that has a single row that contains the filename of the history file and the content of the history file. If called for timeline 1, typically this will generate an error that the timeline history file does not exist.

NOTE: Because this is a replication mode connection, we don't have type names, so the field descriptions in the result will have only OIDs and no DataTypeName values

func (*ReplicationConn) WaitForReplicationMessage Uses

func (rc *ReplicationConn) WaitForReplicationMessage(ctx context.Context) (*ReplicationMessage, error)

Wait for a single replication message.

Properly using this requires some knowledge of the postgres replication mechanisms, as the client can receive both WAL data (the ultimate payload) and server heartbeat updates. The caller also must send standby status updates in order to keep the connection alive and working.

This returns the context error when there is no replication message before the context is canceled.

type ReplicationMessage Uses

type ReplicationMessage struct {
    WalMessage      *WalMessage
    ServerHeartbeat *ServerHeartbeat
}

The replication message wraps all possible messages from the server received during replication. At most one of the wal message or server heartbeat will be non-nil

type Row Uses

type Row Rows

Row is a convenience wrapper over Rows that is returned by QueryRow.

func (*Row) Scan Uses

func (r *Row) Scan(dest ...interface{}) (err error)

Scan works the same as (*Rows Scan) with the following exceptions. If no rows were found it returns ErrNoRows. If multiple rows are returned it ignores all but the first.

type Rows Uses

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

Rows is the result set returned from *Conn.Query. Rows must be closed before the *Conn can be used again. Rows are closed by explicitly calling Close(), calling Next() until it returns false, or when a fatal error occurs.

func (*Rows) Close Uses

func (rows *Rows) Close()

Close closes the rows, making the connection ready for use again. It is safe to call Close after rows is already closed.

func (*Rows) Err Uses

func (rows *Rows) Err() error

func (*Rows) FieldDescriptions Uses

func (rows *Rows) FieldDescriptions() []FieldDescription

func (*Rows) Next Uses

func (rows *Rows) Next() bool

Next prepares the next row for reading. It returns true if there is another row and false if no more rows are available. It automatically closes rows when all rows are read.

func (*Rows) Scan Uses

func (rows *Rows) Scan(dest ...interface{}) (err error)

Scan reads the values from the current row into dest values positionally. dest can include pointers to core types, values implementing the Scanner interface, []byte, and nil. []byte will skip the decoding process and directly copy the raw bytes received from PostgreSQL. nil will skip the value entirely.

func (*Rows) Values Uses

func (rows *Rows) Values() ([]interface{}, error)

Values returns an array of the row values

type SerializationError Uses

type SerializationError string

SerializationError occurs on failure to encode or decode a value

func (SerializationError) Error Uses

func (e SerializationError) Error() string

type ServerHeartbeat Uses

type ServerHeartbeat struct {
    // The current max wal position on the server,
    // used for lag tracking
    ServerWalEnd uint64
    // The server time, in microseconds since jan 1 2000
    ServerTime uint64
    // If 1, the server is requesting a standby status message
    // to be sent immediately.
    ReplyRequested byte
}

The server heartbeat is sent periodically from the server, including server status, and a reply request field

func (*ServerHeartbeat) String Uses

func (s *ServerHeartbeat) String() string

func (*ServerHeartbeat) Time Uses

func (s *ServerHeartbeat) Time() time.Time

type StandbyStatus Uses

type StandbyStatus struct {
    // The WAL position that's been locally written
    WalWritePosition uint64
    // The WAL position that's been locally flushed
    WalFlushPosition uint64
    // The WAL position that's been locally applied
    WalApplyPosition uint64
    // The client time in microseconds since jan 1 2000
    ClientTime uint64
    // If 1, requests the server to immediately send a
    // server heartbeat
    ReplyRequested byte
}

The standby status is the client side heartbeat sent to the postgresql server to track the client wal positions. For practical purposes, all wal positions are typically set to the same value.

func NewStandbyStatus Uses

func NewStandbyStatus(walPositions ...uint64) (status *StandbyStatus, err error)

Create a standby status struct, which sets all the WAL positions to the given wal position, and the client time to the current time. The wal positions are, in order: WalFlushPosition WalApplyPosition WalWritePosition

If only one position is provided, it will be used as the value for all 3 status fields. Note you must provide either 1 wal position, or all 3 in order to initialize the standby status.

type Tx Uses

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

Tx represents a database transaction.

All Tx methods return ErrTxClosed if Commit or Rollback has already been called on the Tx.

func (*Tx) BeginBatch Uses

func (tx *Tx) BeginBatch() *Batch

BeginBatch returns a *Batch query for tx. Since this *Batch is already part of a transaction it will not automatically be wrapped in a transaction.

func (*Tx) Commit Uses

func (tx *Tx) Commit() error

Commit commits the transaction

func (*Tx) CommitEx Uses

func (tx *Tx) CommitEx(ctx context.Context) error

CommitEx commits the transaction with a context.

func (*Tx) CopyFrom Uses

func (tx *Tx) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int, error)

CopyFrom delegates to the underlying *Conn

func (*Tx) Err Uses

func (tx *Tx) Err() error

Err returns the final error state, if any, of calling Commit or Rollback.

func (*Tx) Exec Uses

func (tx *Tx) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)

Exec delegates to the underlying *Conn

func (*Tx) ExecEx Uses

func (tx *Tx) ExecEx(ctx context.Context, sql string, options *QueryExOptions, arguments ...interface{}) (commandTag CommandTag, err error)

ExecEx delegates to the underlying *Conn

func (*Tx) LargeObjects Uses

func (tx *Tx) LargeObjects() (*LargeObjects, error)

LargeObjects returns a LargeObjects instance for the transaction.

func (*Tx) Prepare Uses

func (tx *Tx) Prepare(name, sql string) (*PreparedStatement, error)

Prepare delegates to the underlying *Conn

func (*Tx) PrepareEx Uses

func (tx *Tx) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (*PreparedStatement, error)

PrepareEx delegates to the underlying *Conn

func (*Tx) Query Uses

func (tx *Tx) Query(sql string, args ...interface{}) (*Rows, error)

Query delegates to the underlying *Conn

func (*Tx) QueryEx Uses

func (tx *Tx) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (*Rows, error)

QueryEx delegates to the underlying *Conn

func (*Tx) QueryRow Uses

func (tx *Tx) QueryRow(sql string, args ...interface{}) *Row

QueryRow delegates to the underlying *Conn

func (*Tx) QueryRowEx Uses

func (tx *Tx) QueryRowEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) *Row

QueryRowEx delegates to the underlying *Conn

func (*Tx) Rollback Uses

func (tx *Tx) Rollback() error

Rollback rolls back the transaction. Rollback will return ErrTxClosed if the Tx is already closed, but is otherwise safe to call multiple times. Hence, a defer tx.Rollback() is safe even if tx.Commit() will be called first in a non-error condition.

func (*Tx) RollbackEx Uses

func (tx *Tx) RollbackEx(ctx context.Context) error

RollbackEx is the context version of Rollback

func (*Tx) Status Uses

func (tx *Tx) Status() int8

Status returns the status of the transaction from the set of pgx.TxStatus* constants.

type TxAccessMode Uses

type TxAccessMode string

type TxDeferrableMode Uses

type TxDeferrableMode string

type TxIsoLevel Uses

type TxIsoLevel string

type TxOptions Uses

type TxOptions struct {
    IsoLevel       TxIsoLevel
    AccessMode     TxAccessMode
    DeferrableMode TxDeferrableMode
}

type WalMessage Uses

type WalMessage struct {
    // The WAL start position of this data. This
    // is the WAL position we need to track.
    WalStart uint64
    // The server wal end and server time are
    // documented to track the end position and current
    // time of the server, both of which appear to be
    // unimplemented in pg 9.5.
    ServerWalEnd uint64
    ServerTime   uint64
    // The WAL data is the raw unparsed binary WAL entry.
    // The contents of this are determined by the output
    // logical encoding plugin.
    WalData []byte
}

The WAL message contains WAL payload entry data

func (*WalMessage) ByteLag Uses

func (w *WalMessage) ByteLag() uint64

func (*WalMessage) String Uses

func (w *WalMessage) String() string

func (*WalMessage) Time Uses

func (w *WalMessage) Time() time.Time

Directories

PathSynopsis
chunkreader
internal/sanitize
pgioPackage pgio is a low-level toolkit building messages in the PostgreSQL wire protocol.
pgmock
pgproto3
pgtype
pgtype/ext/satori-uuid
pgtype/ext/shopspring-numeric
pgtype/testutil
stdlibPackage stdlib is the compatibility layer from pgx to database/sql.

Package pgx imports 29 packages (graph) and is imported by 119 packages. Updated 2017-11-19. Refresh now. Tools for package owners.