From 80fd0d249025ae786b7d5ce1e5f445a1f3f39bdf Mon Sep 17 00:00:00 2001 From: Aleksander Alekseev Date: Tue, 16 Jul 2024 15:07:16 +0300 Subject: [PATCH v3 2/2] Always pass PqMsg_* to pq_beginmessage[_reuse] Before this patch the code could be written in one of the following ways: 1. pq_beginmessage(&buf, 'D'); 2. pq_beginmessage(&buf, PqMsg_DataRow); This first option is error-prone and makes finding all the places that form PqMsg_DataRow messages difficult. Refactor the code to always use the second option. Aleksander Alekseev, reviewed by Nathan Bossart Discussion: https://postgr.es/m/CAJ7c6TNTd09AZq8tGaHS3LDyH_CCnpv0oOz2wN1dGe8zekxrdQ@mail.gmail.com --- src/backend/access/common/printtup.c | 5 +++-- src/backend/commands/explain.c | 3 ++- src/backend/replication/walsender.c | 2 +- src/backend/tcop/postgres.c | 3 +-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index f2d5ca14fe..c78cc39308 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -17,6 +17,7 @@ #include "access/printtup.h" #include "libpq/pqformat.h" +#include "libpq/protocol.h" #include "tcop/pquery.h" #include "utils/lsyscache.h" #include "utils/memdebug.h" @@ -170,7 +171,7 @@ SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo, ListCell *tlist_item = list_head(targetlist); /* tuple descriptor message type */ - pq_beginmessage_reuse(buf, 'T'); + pq_beginmessage_reuse(buf, PqMsg_RowDescription); /* # of attrs in tuples */ pq_sendint16(buf, natts); @@ -322,7 +323,7 @@ printtup(TupleTableSlot *slot, DestReceiver *self) /* * Prepare a DataRow message (note buffer is in per-query context) */ - pq_beginmessage_reuse(buf, 'D'); + pq_beginmessage_reuse(buf, PqMsg_DataRow); pq_sendint16(buf, natts); diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 118db12903..5771aabf40 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -21,6 +21,7 @@ #include "foreign/fdwapi.h" #include "jit/jit.h" #include "libpq/pqformat.h" +#include "libpq/protocol.h" #include "nodes/extensible.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" @@ -5497,7 +5498,7 @@ serializeAnalyzeReceive(TupleTableSlot *slot, DestReceiver *self) * Note that we fill a StringInfo buffer the same as printtup() does, so * as to capture the costs of manipulating the strings accurately. */ - pq_beginmessage_reuse(buf, 'D'); + pq_beginmessage_reuse(buf, PqMsg_DataRow); pq_sendint16(buf, natts); diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 2d1a9ec900..ca205594bd 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -695,7 +695,7 @@ UploadManifest(void) ib = CreateIncrementalBackupInfo(mcxt); /* Send a CopyInResponse message */ - pq_beginmessage(&buf, 'G'); + pq_beginmessage(&buf, PqMsg_CopyInResponse); pq_sendbyte(&buf, 0); pq_sendint16(&buf, 0); pq_endmessage_reuse(&buf); diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index e39c6804a7..ea517f4b9b 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -2651,8 +2651,7 @@ exec_describe_statement_message(const char *stmt_name) /* * First describe the parameters... */ - pq_beginmessage_reuse(&row_description_buf, 't'); /* parameter description - * message type */ + pq_beginmessage_reuse(&row_description_buf, PqMsg_ParameterDescription); pq_sendint16(&row_description_buf, psrc->num_params); for (int i = 0; i < psrc->num_params; i++) -- 2.45.2