1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-18 12:22:09 +03:00

libpq: Trace all messages received from the server

Not all messages that libpq received from the server would be sent
through our message tracing logic.  This commit tries to fix that by
introducing a new function pqParseDone which make it harder to forget
about doing so.

The messages that we now newly send through our tracing logic are:

- CopyData (received by COPY TO STDOUT)
- Authentication requests
- NegotiateProtocolVersion
- Some ErrorResponse messages during connection startup
- ReadyForQuery when received after a FunctionCall message

Author: Jelte Fennema-Nio <postgres@jeltef.nl>
Discussion: https://postgr.es/m/CAGECzQSoPHtZ4xe0raJ6FYSEiPPS+YWXBhOGo+Y1YecLgknF3g@mail.gmail.com
This commit is contained in:
Alvaro Herrera
2024-08-16 13:23:18 -04:00
parent 6be39d77a7
commit b8b3f861fb
6 changed files with 124 additions and 31 deletions

View File

@@ -280,6 +280,14 @@ pqTraceOutput_CommandComplete(FILE *f, const char *message, int *cursor)
pqTraceOutputString(f, message, cursor, false);
}
static void
pqTraceOutput_CopyData(FILE *f, const char *message, int *cursor, int length,
bool suppress)
{
fprintf(f, "CopyData\t");
pqTraceOutputNchar(f, length - *cursor + 1, message, cursor, suppress);
}
static void
pqTraceOutput_DataRow(FILE *f, const char *message, int *cursor)
{
@@ -472,10 +480,58 @@ pqTraceOutput_Query(FILE *f, const char *message, int *cursor)
}
static void
pqTraceOutput_Authentication(FILE *f, const char *message, int *cursor)
pqTraceOutput_Authentication(FILE *f, const char *message, int *cursor,
int length, bool suppress)
{
fprintf(f, "Authentication\t");
pqTraceOutputInt32(f, message, cursor, false);
int authType = 0;
memcpy(&authType, message + *cursor, 4);
authType = (int) pg_ntoh32(authType);
*cursor += 4;
switch (authType)
{
case AUTH_REQ_OK:
fprintf(f, "AuthenticationOk");
break;
/* AUTH_REQ_KRB4 not supported */
/* AUTH_REQ_KRB5 not supported */
case AUTH_REQ_PASSWORD:
fprintf(f, "AuthenticationCleartextPassword");
break;
/* AUTH_REQ_CRYPT not supported */
case AUTH_REQ_MD5:
fprintf(f, "AuthenticationMD5Password");
break;
case AUTH_REQ_GSS:
fprintf(f, "AuthenticationGSS");
break;
case AUTH_REQ_GSS_CONT:
fprintf(f, "AuthenticationGSSContinue\t");
pqTraceOutputNchar(f, length - *cursor + 1, message, cursor,
suppress);
break;
case AUTH_REQ_SSPI:
fprintf(f, "AuthenticationSSPI");
break;
case AUTH_REQ_SASL:
fprintf(f, "AuthenticationSASL\t");
while (message[*cursor] != '\0')
pqTraceOutputString(f, message, cursor, false);
pqTraceOutputString(f, message, cursor, false);
break;
case AUTH_REQ_SASL_CONT:
fprintf(f, "AuthenticationSASLContinue\t");
pqTraceOutputNchar(f, length - *cursor + 1, message, cursor,
suppress);
break;
case AUTH_REQ_SASL_FIN:
fprintf(f, "AuthenticationSASLFinal\t");
pqTraceOutputNchar(f, length - *cursor + 1, message, cursor,
suppress);
break;
default:
fprintf(f, "Unknown authentication message %d", authType);
}
}
static void
@@ -625,7 +681,8 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
pqTraceOutput_CommandComplete(conn->Pfdebug, message, &logCursor);
break;
case PqMsg_CopyData:
/* Drop COPY data to reduce the overhead of logging. */
pqTraceOutput_CopyData(conn->Pfdebug, message, &logCursor,
length, regress);
break;
case PqMsg_Describe:
/* Describe(F) and DataRow(B) use the same identifier. */
@@ -714,7 +771,8 @@ pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
pqTraceOutput_Query(conn->Pfdebug, message, &logCursor);
break;
case PqMsg_AuthenticationRequest:
pqTraceOutput_Authentication(conn->Pfdebug, message, &logCursor);
pqTraceOutput_Authentication(conn->Pfdebug, message, &logCursor,
length, regress);
break;
case PqMsg_PortalSuspended:
fprintf(conn->Pfdebug, "PortalSuspended");