Documentation
¶
Overview ¶
Package pgx provides the pgx-native PostgreSQL API under both TinyGo and standard Go.
It is the native sibling of database/sql/pgxstdlib, for code that wants pgx itself rather than database/sql: no pool mutex on the query path, no driver.Value boxing per parameter, and connection-oriented features such as Batch, CopyFrom and LISTEN/NOTIFY as first-class calls instead of an escape hatch behind a callback.
conn, err := pgx.Connect(ctx, "postgres://user:pass@localhost:5432/db?sslmode=disable")
if err != nil { ... }
defer conn.Close(ctx)
var n int
err = conn.QueryRow(ctx, "SELECT 1").Scan(&n)
On standard Go every name here is an alias for the upstream github.com/jackc/pgx/v5 type, so a *pgx.Conn from this package is upstream's *pgx.Conn and passes to third-party code unchanged. On TinyGo the same names bind to a vendored copy of pgx v5.10.0 with its TLS use rerouted onto the platform's native stack, because TinyGo ships crypto/tls as a stub that cannot be linked. See database/internal/PATCHES.md. Code written against this package compiles identically on both.
Defaults ¶
ParseConfig and Connect install two defaults on every configuration:
- Query cancellation is performed by sending a CancelRequest on a second connection, never by moving the read deadline. Under TinyGo's netdev a deadline change cannot interrupt a blocked read, so the deadline strategy would silently not cancel at all.
- On the TinyGo build, the dialer returns a connection that carries its own file descriptor, which is what lets sslmode start TLS on the already-connected socket.
Both are plain fields on the returned ConnConfig, so a caller who needs different behavior may overwrite them before ConnectConfig.
sslmode ¶
sslmode is honored on both builds with the same semantics as pgxstdlib.Open. On TinyGo two differences from libpq are deliberate: verify-ca is treated as verify-full, and sslcert/sslkey are rejected rather than ignored, because the native TLS backends cannot offer a client certificate. A platform with no TLS backend refuses any mode but disable; it never falls back to plaintext silently.
TinyGo notes ¶
Build with -scheduler=threads. Under the cooperative scheduler a blocking socket call holds the whole runtime, so background goroutines never run and query cancellation silently stops working.
Import netdev for its side effect, as with any TinyGo program using the network:
import _ "github.com/shibukawa/tinygodriver/netdev"
Unix domain sockets and IPv6 are unavailable there, so connect over TCP to an IPv4 host.
Registering custom pgtype codecs needs the pgtype package itself, which the TinyGo build keeps under internal/ and cannot re-export wholesale; that remains standard-Go-only for now. pgxpool is likewise not yet part of this surface.
Index ¶
- Constants
- Variables
- func AppendRows[T any, S ~[]T](slice S, rows Rows, fn RowToFunc[T]) (S, error)
- func CollectExactlyOneRow[T any](rows Rows, fn RowToFunc[T]) (T, error)
- func CollectOneRow[T any](rows Rows, fn RowToFunc[T]) (T, error)
- func CollectRows[T any](rows Rows, fn RowToFunc[T]) ([]T, error)
- func RowTo[T any](row CollectableRow) (T, error)
- func RowToAddrOf[T any](row CollectableRow) (*T, error)
- func RowToAddrOfStructByName[T any](row CollectableRow) (*T, error)
- func RowToAddrOfStructByNameLax[T any](row CollectableRow) (*T, error)
- func RowToAddrOfStructByPos[T any](row CollectableRow) (*T, error)
- func RowToStructByName[T any](row CollectableRow) (T, error)
- func RowToStructByNameLax[T any](row CollectableRow) (T, error)
- func RowToStructByPos[T any](row CollectableRow) (T, error)
- type Batch
- type BatchResults
- type CollectableRow
- type CommandTag
- type Conn
- type ConnConfig
- type CopyFromSource
- type FieldDescription
- type Identifier
- type LargeObjects
- type NamedArgs
- type Notification
- type PgConn
- type PgError
- type QueryExecMode
- type QueryTracer
- type QueuedQuery
- type Row
- type RowScanner
- type RowToFunc
- type Rows
- type StrictNamedArgs
- type Tx
- type TxAccessMode
- type TxDeferrableMode
- type TxIsoLevel
- type TxOptions
Constants ¶
const ( Serializable = pgx.Serializable RepeatableRead = pgx.RepeatableRead ReadCommitted = pgx.ReadCommitted ReadUncommitted = pgx.ReadUncommitted ReadWrite = pgx.ReadWrite ReadOnly = pgx.ReadOnly Deferrable = pgx.Deferrable NotDeferrable = pgx.NotDeferrable )
Transaction characteristics, forwarded as typed constants.
const ( QueryExecModeCacheStatement = pgx.QueryExecModeCacheStatement QueryExecModeCacheDescribe = pgx.QueryExecModeCacheDescribe QueryExecModeDescribeExec = pgx.QueryExecModeDescribeExec QueryExecModeExec = pgx.QueryExecModeExec QueryExecModeSimpleProtocol = pgx.QueryExecModeSimpleProtocol )
Query execution modes, for ConnConfig.DefaultQueryExecMode or as the first query argument.
Variables ¶
var ( ErrNoRows = pgx.ErrNoRows ErrTooManyRows = pgx.ErrTooManyRows ErrTxClosed = pgx.ErrTxClosed ErrTxCommitRollback = pgx.ErrTxCommitRollback )
Sentinel errors. Vars because Go cannot alias a var; errors.Is works unchanged since these are the same values.
var ( CopyFromRows = pgx.CopyFromRows CopyFromSlice = pgx.CopyFromSlice CopyFromFunc = pgx.CopyFromFunc )
The CopyFrom source constructors, as variables because Go has no alias for a function.
var ( RowToMap = pgx.RowToMap ForEachRow = pgx.ForEachRow )
Functions ¶
func RowTo ¶
func RowTo[T any](row CollectableRow) (T, error)
func RowToAddrOf ¶
func RowToAddrOf[T any](row CollectableRow) (*T, error)
func RowToAddrOfStructByName ¶
func RowToAddrOfStructByName[T any](row CollectableRow) (*T, error)
func RowToAddrOfStructByNameLax ¶
func RowToAddrOfStructByNameLax[T any](row CollectableRow) (*T, error)
func RowToAddrOfStructByPos ¶
func RowToAddrOfStructByPos[T any](row CollectableRow) (*T, error)
func RowToStructByName ¶
func RowToStructByName[T any](row CollectableRow) (T, error)
func RowToStructByNameLax ¶
func RowToStructByNameLax[T any](row CollectableRow) (T, error)
func RowToStructByPos ¶
func RowToStructByPos[T any](row CollectableRow) (T, error)
Types ¶
type Batch ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type BatchResults ¶
type BatchResults = pgx.BatchResults
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type CollectableRow ¶
type CollectableRow = pgx.CollectableRow
The row-collection helpers. Generic functions can be neither aliased nor bound to a variable, so unlike the types above they are one-line forwards.
type CommandTag ¶
type CommandTag = pgconn.CommandTag
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type Conn ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
func Connect ¶
Connect opens a native pgx connection for a libpq-style URL or keyword DSN.
Unlike database/sql handles, the connection is real and singular: it is established eagerly, is not safe for concurrent use, and belongs to the caller until Close. Use one connection per goroutine, or pool above this package.
func ConnectConfig ¶
func ConnectConfig(ctx context.Context, cfg *ConnConfig) (*Conn, error)
ConnectConfig opens a connection from a configuration built by ParseConfig. The config must originate from ParseConfig, which is pgx's own rule.
type ConnConfig ¶
type ConnConfig = pgx.ConnConfig
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
func ParseConfig ¶
func ParseConfig(dsn string) (*ConnConfig, error)
ParseConfig parses a libpq-style URL or keyword DSN and applies this package's defaults; see the package documentation. The result may be adjusted before ConnectConfig.
type CopyFromSource ¶
type CopyFromSource = pgx.CopyFromSource
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type FieldDescription ¶
type FieldDescription = pgconn.FieldDescription
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type Identifier ¶
type Identifier = pgx.Identifier
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type LargeObjects ¶
type LargeObjects = pgx.LargeObjects
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type NamedArgs ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type Notification ¶
type Notification = pgconn.Notification
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type PgConn ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type PgError ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type QueryExecMode ¶
type QueryExecMode = pgx.QueryExecMode
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type QueryTracer ¶
type QueryTracer = pgx.QueryTracer
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type QueuedQuery ¶
type QueuedQuery = pgx.QueuedQuery
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type Row ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type RowScanner ¶
type RowScanner = pgx.RowScanner
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type RowToFunc ¶
The row-collection helpers. Generic functions can be neither aliased nor bound to a variable, so unlike the types above they are one-line forwards.
type Rows ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type StrictNamedArgs ¶
type StrictNamedArgs = pgx.StrictNamedArgs
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type Tx ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type TxAccessMode ¶
type TxAccessMode = pgx.TxAccessMode
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type TxDeferrableMode ¶
type TxDeferrableMode = pgx.TxDeferrableMode
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type TxIsoLevel ¶
type TxIsoLevel = pgx.TxIsoLevel
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.
type TxOptions ¶
The public surface, as aliases so every value is the upstream pgx value itself and satisfies upstream interfaces. The vendored backend defines the same names against its own copy; keeping the two sets identical is what makes code written against this package portable across compilers.