2023-05-12 13:33:40 +01:00
|
|
|
package pgproto3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-08-16 16:10:13 +01:00
|
|
|
"sync"
|
2023-05-12 13:33:40 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tracer traces the messages send to and from a Backend or Frontend. The format it produces roughly mimics the
|
|
|
|
// format produced by the libpq C function PQtrace.
|
|
|
|
type tracer struct {
|
2023-08-16 16:10:13 +01:00
|
|
|
TracerOptions
|
|
|
|
|
|
|
|
mux sync.Mutex
|
2023-05-12 13:33:40 +01:00
|
|
|
w io.Writer
|
|
|
|
buf *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// TracerOptions controls tracing behavior. It is roughly equivalent to the libpq function PQsetTraceFlags.
|
|
|
|
type TracerOptions struct {
|
|
|
|
// SuppressTimestamps prevents printing of timestamps.
|
|
|
|
SuppressTimestamps bool
|
|
|
|
|
|
|
|
// RegressMode redacts fields that may be vary between executions.
|
|
|
|
RegressMode bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceMessage(sender byte, encodedLen int32, msg Message) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *AuthenticationCleartextPassword:
|
|
|
|
t.traceAuthenticationCleartextPassword(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationGSS:
|
|
|
|
t.traceAuthenticationGSS(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationGSSContinue:
|
|
|
|
t.traceAuthenticationGSSContinue(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationMD5Password:
|
|
|
|
t.traceAuthenticationMD5Password(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationOk:
|
|
|
|
t.traceAuthenticationOk(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationSASL:
|
|
|
|
t.traceAuthenticationSASL(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationSASLContinue:
|
|
|
|
t.traceAuthenticationSASLContinue(sender, encodedLen, msg)
|
|
|
|
case *AuthenticationSASLFinal:
|
|
|
|
t.traceAuthenticationSASLFinal(sender, encodedLen, msg)
|
|
|
|
case *BackendKeyData:
|
|
|
|
t.traceBackendKeyData(sender, encodedLen, msg)
|
|
|
|
case *Bind:
|
|
|
|
t.traceBind(sender, encodedLen, msg)
|
|
|
|
case *BindComplete:
|
|
|
|
t.traceBindComplete(sender, encodedLen, msg)
|
|
|
|
case *CancelRequest:
|
|
|
|
t.traceCancelRequest(sender, encodedLen, msg)
|
|
|
|
case *Close:
|
|
|
|
t.traceClose(sender, encodedLen, msg)
|
|
|
|
case *CloseComplete:
|
|
|
|
t.traceCloseComplete(sender, encodedLen, msg)
|
|
|
|
case *CommandComplete:
|
|
|
|
t.traceCommandComplete(sender, encodedLen, msg)
|
|
|
|
case *CopyBothResponse:
|
|
|
|
t.traceCopyBothResponse(sender, encodedLen, msg)
|
|
|
|
case *CopyData:
|
|
|
|
t.traceCopyData(sender, encodedLen, msg)
|
|
|
|
case *CopyDone:
|
|
|
|
t.traceCopyDone(sender, encodedLen, msg)
|
|
|
|
case *CopyFail:
|
|
|
|
t.traceCopyFail(sender, encodedLen, msg)
|
|
|
|
case *CopyInResponse:
|
|
|
|
t.traceCopyInResponse(sender, encodedLen, msg)
|
|
|
|
case *CopyOutResponse:
|
|
|
|
t.traceCopyOutResponse(sender, encodedLen, msg)
|
|
|
|
case *DataRow:
|
|
|
|
t.traceDataRow(sender, encodedLen, msg)
|
|
|
|
case *Describe:
|
|
|
|
t.traceDescribe(sender, encodedLen, msg)
|
|
|
|
case *EmptyQueryResponse:
|
|
|
|
t.traceEmptyQueryResponse(sender, encodedLen, msg)
|
|
|
|
case *ErrorResponse:
|
|
|
|
t.traceErrorResponse(sender, encodedLen, msg)
|
|
|
|
case *Execute:
|
|
|
|
t.TraceQueryute(sender, encodedLen, msg)
|
|
|
|
case *Flush:
|
|
|
|
t.traceFlush(sender, encodedLen, msg)
|
|
|
|
case *FunctionCall:
|
|
|
|
t.traceFunctionCall(sender, encodedLen, msg)
|
|
|
|
case *FunctionCallResponse:
|
|
|
|
t.traceFunctionCallResponse(sender, encodedLen, msg)
|
|
|
|
case *GSSEncRequest:
|
|
|
|
t.traceGSSEncRequest(sender, encodedLen, msg)
|
|
|
|
case *NoData:
|
|
|
|
t.traceNoData(sender, encodedLen, msg)
|
|
|
|
case *NoticeResponse:
|
|
|
|
t.traceNoticeResponse(sender, encodedLen, msg)
|
|
|
|
case *NotificationResponse:
|
|
|
|
t.traceNotificationResponse(sender, encodedLen, msg)
|
|
|
|
case *ParameterDescription:
|
|
|
|
t.traceParameterDescription(sender, encodedLen, msg)
|
|
|
|
case *ParameterStatus:
|
|
|
|
t.traceParameterStatus(sender, encodedLen, msg)
|
|
|
|
case *Parse:
|
|
|
|
t.traceParse(sender, encodedLen, msg)
|
|
|
|
case *ParseComplete:
|
|
|
|
t.traceParseComplete(sender, encodedLen, msg)
|
|
|
|
case *PortalSuspended:
|
|
|
|
t.tracePortalSuspended(sender, encodedLen, msg)
|
|
|
|
case *Query:
|
|
|
|
t.traceQuery(sender, encodedLen, msg)
|
|
|
|
case *ReadyForQuery:
|
|
|
|
t.traceReadyForQuery(sender, encodedLen, msg)
|
|
|
|
case *RowDescription:
|
|
|
|
t.traceRowDescription(sender, encodedLen, msg)
|
|
|
|
case *SSLRequest:
|
|
|
|
t.traceSSLRequest(sender, encodedLen, msg)
|
|
|
|
case *StartupMessage:
|
|
|
|
t.traceStartupMessage(sender, encodedLen, msg)
|
|
|
|
case *Sync:
|
|
|
|
t.traceSync(sender, encodedLen, msg)
|
|
|
|
case *Terminate:
|
|
|
|
t.traceTerminate(sender, encodedLen, msg)
|
|
|
|
default:
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Unknown", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationCleartextPassword(sender byte, encodedLen int32, msg *AuthenticationCleartextPassword) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationCleartextPassword", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationGSS(sender byte, encodedLen int32, msg *AuthenticationGSS) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationGSS", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationGSSContinue(sender byte, encodedLen int32, msg *AuthenticationGSSContinue) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationGSSContinue", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationMD5Password(sender byte, encodedLen int32, msg *AuthenticationMD5Password) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationMD5Password", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationOk(sender byte, encodedLen int32, msg *AuthenticationOk) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationOk", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationSASL(sender byte, encodedLen int32, msg *AuthenticationSASL) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationSASL", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationSASLContinue(sender byte, encodedLen int32, msg *AuthenticationSASLContinue) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationSASLContinue", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceAuthenticationSASLFinal(sender byte, encodedLen int32, msg *AuthenticationSASLFinal) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "AuthenticationSASLFinal", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceBackendKeyData(sender byte, encodedLen int32, msg *BackendKeyData) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "BackendKeyData", func() {
|
|
|
|
if t.RegressMode {
|
|
|
|
t.buf.WriteString("\t NNNN NNNN")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(t.buf, "\t %d %d", msg.ProcessID, msg.SecretKey)
|
|
|
|
}
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceBind(sender byte, encodedLen int32, msg *Bind) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Bind", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s %s %d", traceDoubleQuotedString([]byte(msg.DestinationPortal)), traceDoubleQuotedString([]byte(msg.PreparedStatement)), len(msg.ParameterFormatCodes))
|
|
|
|
for _, fc := range msg.ParameterFormatCodes {
|
|
|
|
fmt.Fprintf(t.buf, " %d", fc)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(t.buf, " %d", len(msg.Parameters))
|
|
|
|
for _, p := range msg.Parameters {
|
|
|
|
fmt.Fprintf(t.buf, " %s", traceSingleQuotedString(p))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(t.buf, " %d", len(msg.ResultFormatCodes))
|
|
|
|
for _, fc := range msg.ResultFormatCodes {
|
|
|
|
fmt.Fprintf(t.buf, " %d", fc)
|
|
|
|
}
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceBindComplete(sender byte, encodedLen int32, msg *BindComplete) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "BindComplete", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCancelRequest(sender byte, encodedLen int32, msg *CancelRequest) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CancelRequest", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceClose(sender byte, encodedLen int32, msg *Close) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Close", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCloseComplete(sender byte, encodedLen int32, msg *CloseComplete) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CloseComplete", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCommandComplete(sender byte, encodedLen int32, msg *CommandComplete) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CommandComplete", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s", traceDoubleQuotedString(msg.CommandTag))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyBothResponse(sender byte, encodedLen int32, msg *CopyBothResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyBothResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyData(sender byte, encodedLen int32, msg *CopyData) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyData", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyDone(sender byte, encodedLen int32, msg *CopyDone) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyDone", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyFail(sender byte, encodedLen int32, msg *CopyFail) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyFail", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s", traceDoubleQuotedString([]byte(msg.Message)))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyInResponse(sender byte, encodedLen int32, msg *CopyInResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyInResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceCopyOutResponse(sender byte, encodedLen int32, msg *CopyOutResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "CopyOutResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceDataRow(sender byte, encodedLen int32, msg *DataRow) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "DataRow", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %d", len(msg.Values))
|
|
|
|
for _, v := range msg.Values {
|
|
|
|
if v == nil {
|
|
|
|
t.buf.WriteString(" -1")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(t.buf, " %d %s", len(v), traceSingleQuotedString(v))
|
|
|
|
}
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
2023-08-16 16:10:13 +01:00
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceDescribe(sender byte, encodedLen int32, msg *Describe) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Describe", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %c %s", msg.ObjectType, traceDoubleQuotedString([]byte(msg.Name)))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceEmptyQueryResponse(sender byte, encodedLen int32, msg *EmptyQueryResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "EmptyQueryResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceErrorResponse(sender byte, encodedLen int32, msg *ErrorResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "ErrorResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) TraceQueryute(sender byte, encodedLen int32, msg *Execute) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Execute", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s %d", traceDoubleQuotedString([]byte(msg.Portal)), msg.MaxRows)
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceFlush(sender byte, encodedLen int32, msg *Flush) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Flush", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceFunctionCall(sender byte, encodedLen int32, msg *FunctionCall) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "FunctionCall", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceFunctionCallResponse(sender byte, encodedLen int32, msg *FunctionCallResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "FunctionCallResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceGSSEncRequest(sender byte, encodedLen int32, msg *GSSEncRequest) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "GSSEncRequest", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceNoData(sender byte, encodedLen int32, msg *NoData) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "NoData", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceNoticeResponse(sender byte, encodedLen int32, msg *NoticeResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "NoticeResponse", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceNotificationResponse(sender byte, encodedLen int32, msg *NotificationResponse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "NotificationResponse", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %d %s %s", msg.PID, traceDoubleQuotedString([]byte(msg.Channel)), traceDoubleQuotedString([]byte(msg.Payload)))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceParameterDescription(sender byte, encodedLen int32, msg *ParameterDescription) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "ParameterDescription", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceParameterStatus(sender byte, encodedLen int32, msg *ParameterStatus) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "ParameterStatus", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s %s", traceDoubleQuotedString([]byte(msg.Name)), traceDoubleQuotedString([]byte(msg.Value)))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceParse(sender byte, encodedLen int32, msg *Parse) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Parse", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s %s %d", traceDoubleQuotedString([]byte(msg.Name)), traceDoubleQuotedString([]byte(msg.Query)), len(msg.ParameterOIDs))
|
|
|
|
for _, oid := range msg.ParameterOIDs {
|
|
|
|
fmt.Fprintf(t.buf, " %d", oid)
|
|
|
|
}
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceParseComplete(sender byte, encodedLen int32, msg *ParseComplete) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "ParseComplete", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) tracePortalSuspended(sender byte, encodedLen int32, msg *PortalSuspended) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "PortalSuspended", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceQuery(sender byte, encodedLen int32, msg *Query) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Query", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %s", traceDoubleQuotedString([]byte(msg.String)))
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceReadyForQuery(sender byte, encodedLen int32, msg *ReadyForQuery) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "ReadyForQuery", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %c", msg.TxStatus)
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceRowDescription(sender byte, encodedLen int32, msg *RowDescription) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "RowDescription", func() {
|
|
|
|
fmt.Fprintf(t.buf, "\t %d", len(msg.Fields))
|
|
|
|
for _, fd := range msg.Fields {
|
|
|
|
fmt.Fprintf(t.buf, ` %s %d %d %d %d %d %d`, traceDoubleQuotedString(fd.Name), fd.TableOID, fd.TableAttributeNumber, fd.DataTypeOID, fd.DataTypeSize, fd.TypeModifier, fd.Format)
|
|
|
|
}
|
|
|
|
})
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceSSLRequest(sender byte, encodedLen int32, msg *SSLRequest) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "SSLRequest", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceStartupMessage(sender byte, encodedLen int32, msg *StartupMessage) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "StartupMessage", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceSync(sender byte, encodedLen int32, msg *Sync) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Sync", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) traceTerminate(sender byte, encodedLen int32, msg *Terminate) {
|
2023-08-16 16:10:13 +01:00
|
|
|
t.writeTrace(sender, encodedLen, "Terminate", nil)
|
2023-05-12 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:10:13 +01:00
|
|
|
func (t *tracer) writeTrace(sender byte, encodedLen int32, msgType string, writeDetails func()) {
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
defer func() {
|
|
|
|
if t.buf.Cap() > 1024 {
|
|
|
|
t.buf = &bytes.Buffer{}
|
|
|
|
} else {
|
|
|
|
t.buf.Reset()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-12 13:33:40 +01:00
|
|
|
if !t.SuppressTimestamps {
|
|
|
|
now := time.Now()
|
|
|
|
t.buf.WriteString(now.Format("2006-01-02 15:04:05.000000"))
|
|
|
|
t.buf.WriteByte('\t')
|
|
|
|
}
|
|
|
|
|
|
|
|
t.buf.WriteByte(sender)
|
|
|
|
t.buf.WriteByte('\t')
|
|
|
|
t.buf.WriteString(msgType)
|
|
|
|
t.buf.WriteByte('\t')
|
|
|
|
t.buf.WriteString(strconv.FormatInt(int64(encodedLen), 10))
|
|
|
|
|
2023-08-16 16:10:13 +01:00
|
|
|
if writeDetails != nil {
|
|
|
|
writeDetails()
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:33:40 +01:00
|
|
|
t.buf.WriteByte('\n')
|
|
|
|
t.buf.WriteTo(t.w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// traceDoubleQuotedString returns t.buf as a double-quoted string without any escaping. It is roughly equivalent to
|
|
|
|
// pqTraceOutputString in libpq.
|
|
|
|
func traceDoubleQuotedString(buf []byte) string {
|
|
|
|
return `"` + string(buf) + `"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// traceSingleQuotedString returns buf as a single-quoted string with non-printable characters hex-escaped. It is
|
|
|
|
// roughly equivalent to pqTraceOutputNchar in libpq.
|
|
|
|
func traceSingleQuotedString(buf []byte) string {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
sb.WriteByte('\'')
|
|
|
|
for _, b := range buf {
|
|
|
|
if b < 32 || b > 126 {
|
|
|
|
fmt.Fprintf(sb, `\x%x`, b)
|
|
|
|
} else {
|
|
|
|
sb.WriteByte(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.WriteByte('\'')
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|