CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/golang-github-com-jackc-pgx-v5

pgx is a pure Go driver and toolkit for PostgreSQL providing a native high-performance interface with PostgreSQL-specific features plus a database/sql compatibility adapter.

Pending
Overview
Eval results
Files

wire-protocol.mddocs/advanced/

Wire Protocol (pgproto3)

pgproto3 implements the PostgreSQL frontend/backend wire protocol. It provides message types and read/write implementations for building PostgreSQL clients, servers, and proxies.

Import

import "github.com/jackc/pgx/v5/pgproto3"

Constants

const ProtocolVersionNumber = 196608 // PostgreSQL protocol version 3.0

const (
    TextFormat   = 0
    BinaryFormat = 1
)

// Authentication types
const (
    AuthTypeOk                = 0
    AuthTypeCleartextPassword = 3
    AuthTypeMD5Password       = 5
    AuthTypeSCMCreds          = 6
    AuthTypeGSS               = 7
    AuthTypeGSSCont           = 8
    AuthTypeSASL              = 10
    AuthTypeSASLContinue      = 11
    AuthTypeSASLFinal         = 12
)

Frontend (Client-side)

type Frontend struct{ /* unexported */ }

func NewFrontend(r io.Reader, w io.Writer) *Frontend

Sending Messages

func (f *Frontend) Send(msg FrontendMessage) error
func (f *Frontend) SendUnbufferedEncodedCopyData(data []byte) error

Receiving Messages

func (f *Frontend) Receive() (BackendMessage, error)

Tracing

func (f *Frontend) Trace(w io.Writer, options TracerOptions)
func (f *Frontend) Untrace()

Backend (Server-side)

type Backend struct{ /* unexported */ }

func NewBackend(r io.Reader, w io.Writer) *Backend

Receiving Messages

func (b *Backend) Receive() (FrontendMessage, error)
func (b *Backend) SetAuthType(authType uint32) error

Sending Messages

func (b *Backend) Send(msg BackendMessage) error

Tracing

func (b *Backend) Trace(w io.Writer, options TracerOptions)
func (b *Backend) Untrace()

TracerOptions

type TracerOptions struct {
    SuppressTimestamps bool
}

Message Interfaces

type Message interface {
    // common interface for all messages
}

type FrontendMessage interface {
    // messages sent by client to server
}

type BackendMessage interface {
    // messages sent by server to client
}

Frontend Messages (sent by client)

type StartupMessage struct {
    ProtocolVersion uint32
    Parameters      map[string]string
}

type CancelRequest struct {
    ProcessID uint32
    SecretKey uint32
}

type SSLRequest struct{}
type GSSEncRequest struct{}

type Query struct {
    String string
}

type Parse struct {
    Name          string
    Query         string
    ParameterOIDs []uint32
}

type Bind struct {
    DestinationPortal    string
    PreparedStatement    string
    ParameterFormatCodes []int16
    Parameters           [][]byte
    ResultFormatCodes    []int16
}

type Describe struct {
    ObjectType byte // 'S' for prepared statement, 'P' for portal
    Name       string
}

type Execute struct {
    Portal  string
    MaxRows uint32
}

type Close struct {
    ObjectType byte // 'S' for prepared statement, 'P' for portal
    Name       string
}

type Sync struct{}
type Flush struct{}
type Terminate struct{}

type CopyData struct {
    Data []byte
}

type CopyDone struct{}

type CopyFail struct {
    Message string
}

type PasswordMessage struct {
    Password string
}

type SASLInitialResponse struct {
    AuthMechanism string
    Data          []byte
}

type SASLResponse struct {
    Data []byte
}

type GSSResponse struct {
    Data []byte
}

type FunctionCall struct {
    ObjectID        uint32
    ArgumentFormats []int16
    Arguments       [][]byte
    ResultFormat    int16
}

Backend Messages (sent by server)

type AuthenticationOk struct{}
type AuthenticationCleartextPassword struct{}

type AuthenticationMD5Password struct {
    Salt [4]byte
}

type AuthenticationGSS struct{}

type AuthenticationGSSContinue struct {
    Data []byte
}

type AuthenticationSASL struct {
    AuthMechanisms []string
}

type AuthenticationSASLContinue struct {
    Data []byte
}

type AuthenticationSASLFinal struct {
    Data []byte
}

type AuthenticationResponseMessage interface {
    // marker interface for auth response messages
}

type BackendKeyData struct {
    ProcessID uint32
    SecretKey uint32
}

type BindComplete struct{}
type CloseComplete struct{}
type EmptyQueryResponse struct{}
type NoData struct{}
type ParseComplete struct{}
type PortalSuspended struct{}

type CommandComplete struct {
    CommandTag []byte
}

type DataRow struct {
    Values [][]byte
}

type ErrorResponse struct {
    Severity            string
    SeverityUnlocalized 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
    // ... and UnknownFields map[byte]string
}

type NoticeResponse ErrorResponse  // same structure as ErrorResponse

type FieldDescription struct {
    Name                 string
    TableOID             uint32
    TableAttributeNumber uint16
    DataTypeOID          uint32
    DataTypeSize         int16
    TypeModifier         int32
    Format               int16
}

type RowDescription struct {
    Fields []FieldDescription
}

type ParameterDescription struct {
    ParameterOIDs []uint32
}

type ParameterStatus struct {
    Name  string
    Value string
}

type ReadyForQuery struct {
    TxStatus byte  // 'I'=idle, 'T'=in transaction, 'E'=failed transaction
}

type NotificationResponse struct {
    PID     uint32
    Channel string
    Payload string
}

type CopyInResponse struct {
    OverallFormat    byte
    ColumnFormatCodes []uint16
}

type CopyOutResponse struct {
    OverallFormat    byte
    ColumnFormatCodes []uint16
}

type CopyBothResponse struct {
    OverallFormat    byte
    ColumnFormatCodes []uint16
}

type CopyData struct {
    Data []byte
}

type CopyDone struct{}

type FunctionCallResponse struct {
    Result []byte
}

Error Types

type ExceededMaxBodyLenErr struct {
    MaxBodyLen int
    ActualBodyLen int
}

Utility Types

type BigEndianBuf [8]byte

Methods on BigEndianBuf for encoding integers in big-endian format:

  • (b *BigEndianBuf) Int16(n int16) []byte
  • (b *BigEndianBuf) Int32(n int32) []byte
  • (b *BigEndianBuf) Int64(n int64) []byte
  • (b *BigEndianBuf) Uint16(n uint16) []byte
  • (b *BigEndianBuf) Uint32(n uint32) []byte
  • (b *BigEndianBuf) Uint64(n uint64) []byte

Notes

  • pgproto3 is primarily used internally by pgx and pgconn.
  • Direct use is needed when building PostgreSQL proxies, testing infrastructure, or custom protocol implementations.
  • For debugging wire protocol messages, set a trace writer on the Frontend or Backend.

Install with Tessl CLI

npx tessl i tessl/golang-github-com-jackc-pgx-v5

docs

advanced

pgconn.md

pipeline-mode.md

wire-protocol.md

batch.md

common-patterns.md

connection-pool.md

copy.md

database-sql.md

direct-connection.md

index.md

querying.md

testing.md

tracing.md

transactions.md

tile.json