postgresql

package
v3.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Frontend (client) messages
	MsgQuery      = 'Q'
	MsgParse      = 'P'
	MsgBind       = 'B'
	MsgExecute    = 'E'
	MsgSync       = 'S'
	MsgTerminate  = 'X'
	MsgDescribe   = 'D'
	MsgClose      = 'C'
	MsgFlush      = 'H'
	MsgSSLRequest = 0 // Special case
	MsgStartup    = 0 // Special case

	// Backend (server) messages
	MsgAuthentication       = 'R'
	MsgParameterStatus      = 'S'
	MsgBackendKeyData       = 'K'
	MsgReadyForQuery        = 'Z'
	MsgRowDescription       = 'T'
	MsgDataRow              = 'D'
	MsgCommandComplete      = 'C'
	MsgErrorResponse        = 'E'
	MsgNoticeResponse       = 'N'
	MsgParseComplete        = '1'
	MsgBindComplete         = '2'
	MsgCloseComplete        = '3'
	MsgNoData               = 'n'
	MsgPortalSuspended      = 's'
	MsgParameterDescription = 't'
)

Message types for Frontend/Backend protocol

View Source
const (
	AuthOK                = 0
	AuthKerberosV5        = 2
	AuthCleartextPassword = 3
	AuthMD5Password       = 5
	AuthSCMCredential     = 6
	AuthGSS               = 7
	AuthGSSContinue       = 8
	AuthSSPI              = 9
	AuthSASL              = 10
)

Authentication types

Variables

View Source
var SSLRequest = []byte{0x00, 0x00, 0x00, 0x08, 0x04, 0xD2, 0x16, 0x2F}

SSLRequest magic number

Functions

func CreateCommandComplete

func CreateCommandComplete(tag string) []byte

CreateCommandComplete creates CommandComplete message

func CreateMessage

func CreateMessage(msgType byte, payload []byte) []byte

CreateMessage creates a message with given type and payload

func CreateReadyForQuery

func CreateReadyForQuery(status byte) []byte

CreateReadyForQuery creates ReadyForQuery message status: 'I' = Idle, 'T' = In transaction, 'E' = Failed transaction

Types

type ColumnInfo

type ColumnInfo struct {
	Field      string `json:"Field"`
	Type       string `json:"Type"`
	Collation  string `json:"Collation"`
	Null       string `json:"Null"`
	Key        string `json:"Key"`
	Default    string `json:"Default"`
	Extra      string `json:"Extra"`
	Privileges string `json:"Privileges"`
	Comment    string `json:"Comment"`
}

type ConnectionState

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

ConnectionState tracks prepared statements and mocked portals per connection This is needed for the extended query protocol where we eavesdrop on Parse but only intercept at Bind/Execute

func NewConnectionState

func NewConnectionState() *ConnectionState

NewConnectionState creates a new connection state

type Message

type Message struct {
	Type    byte
	Length  int32
	Payload []byte
}

Message represents a PostgreSQL protocol message

type MockedPortal

type MockedPortal struct {
	Mock              *types.ExpectStatement
	Query             string  // actual SQL captured at Parse/Bind time
	DescribeForwarded bool    // true if Describe was forwarded to upstream for this stmt
	ResultFormatCodes []int16 // per-column result format codes from Bind (0=text, 1=binary)
}

MockedPortal pairs a matched mock with the actual SQL query from Parse, so sendMockExecuteResponse can use the real SELECT/INSERT/UPDATE text rather than falling back to a synthetic "INSERT INTO <table>" string. DescribeForwarded records whether a Describe was forwarded for this statement before Bind, meaning the upstream already sent RowDescription to the client — the Execute response must therefore omit RowDescription to avoid a duplicate.

type Proxy

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

func NewProxy

func NewProxy(addr, upstreamAddr string, reg *registry.MockRegistry) *Proxy

NewProxy creates a new PostgreSQL proxy

func (*Proxy) ResetConnections

func (p *Proxy) ResetConnections()

ResetConnections closes all active client and upstream connections. Called on registry reload so that the service's connection pool reconnects with fresh state, eliminating stale mock-only prepared statement mappings that would otherwise persist across test boundaries.

func (*Proxy) SetResolver

func (p *Proxy) SetResolver(resolver *interpolate.Resolver)

SetResolver wires an interpolate.Resolver into the payload loader so that ${VAR} tokens in RETURNS payload files are resolved at runtime.

func (*Proxy) Start

func (p *Proxy) Start(ctx context.Context) error

Start starts the proxy server

type ResultHandler

type ResultHandler struct{}

ResultHandler generates PostgreSQL result set messages

func NewResultHandler

func NewResultHandler() *ResultHandler

func (*ResultHandler) SendCommandComplete

func (r *ResultHandler) SendCommandComplete(conn net.Conn, tag string) error

SendCommandComplete sends just CommandComplete for non-SELECT operations

func (*ResultHandler) SendDataRow

func (r *ResultHandler) SendDataRow(conn net.Conn, columns []string, values map[string]interface{}) error

SendDataRow sends a single DataRow message using name-based heuristics to choose binary vs text format per column (legacy behaviour).

func (*ResultHandler) SendDataRowWithFormats

func (r *ResultHandler) SendDataRowWithFormats(conn net.Conn, columns []string, values map[string]interface{}, resultFormatCodes []int16) error

SendDataRowWithFormats sends a DataRow honouring the per-column result format codes that the client supplied in its Bind message (0=text, 1=binary). A slice with a single entry applies that code to every column; an empty/nil slice falls back to name-based heuristics.

func (*ResultHandler) SendEmptyResultSet

func (r *ResultHandler) SendEmptyResultSet(conn net.Conn, columns []string) error

SendEmptyResultSet sends an empty result set

func (*ResultHandler) SendRowDescription

func (r *ResultHandler) SendRowDescription(conn net.Conn, columns []string) error

SendRowDescription sends RowDescription message

func (*ResultHandler) SendRowDescriptionWithHints

func (r *ResultHandler) SendRowDescriptionWithHints(conn net.Conn, columns []string, sampleRow map[string]interface{}) error

SendRowDescriptionWithHints sends a RowDescription, using the provided sample row to refine per-column OID inference. Falls back to SendRowDescription when sampleRow is nil.

type StartupHandler

type StartupHandler struct{}

StartupHandler manages the connection startup phase

func NewStartupHandler

func NewStartupHandler() *StartupHandler

Jump to

Keyboard shortcuts

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