From 7b9d827e5059445945d214388371138f0676b306 Mon Sep 17 00:00:00 2001
From: Sehrope Sarkuni <sehrope@jackdb.com>
Date: Wed, 10 Jul 2019 07:17:58 -0400
Subject: [PATCH 2/2] Refactor PipeProtoHeader.is_last constants

Adds constants for PipeProtoHeader.is_last and refactors the
syslogger pipe sending and receiving code to use them in place
of the hard coded char codes.
---
 src/backend/postmaster/syslogger.c | 13 ++++++++-----
 src/backend/utils/error/elog.c     | 10 ++++++++--
 src/include/postmaster/syslogger.h | 14 ++++++++++++--
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index bafd31d22b..86ad86cf7f 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -892,8 +892,8 @@ process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
 		if (p.nuls[0] == '\0' && p.nuls[1] == '\0' &&
 			p.len > 0 && p.len <= PIPE_MAX_PAYLOAD &&
 			p.pid != 0 &&
-			(p.is_last == 't' || p.is_last == 'f' ||
-			 p.is_last == 'T' || p.is_last == 'F'))
+			(p.is_last == PIPE_DEST_STDERR_LAST || p.is_last == PIPE_DEST_STDERR_PART ||
+			 p.is_last == PIPE_DEST_CSVLOG_LAST || p.is_last == PIPE_DEST_CSVLOG_PART))
 		{
 			List	   *buffer_list;
 			ListCell   *cell;
@@ -907,8 +907,10 @@ process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
 			if (count < chunklen)
 				break;
 
-			dest = (p.is_last == 'T' || p.is_last == 'F') ?
-				LOG_DESTINATION_CSVLOG : LOG_DESTINATION_STDERR;
+			if (p.is_last == PIPE_DEST_CSVLOG_LAST || p.is_last == PIPE_DEST_CSVLOG_PART)
+				dest = LOG_DESTINATION_CSVLOG;
+			else
+				dest = LOG_DESTINATION_STDERR;
 
 			/* Locate any existing buffer for this source pid */
 			buffer_list = buffer_lists[p.pid % NBUFFER_LISTS];
@@ -925,7 +927,8 @@ process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
 					free_slot = buf;
 			}
 
-			if (p.is_last == 'f' || p.is_last == 'F')
+			if (p.is_last == PIPE_DEST_CSVLOG_PART ||
+				p.is_last == PIPE_DEST_STDERR_PART)
 			{
 				/*
 				 * Save a complete non-final chunk in a per-pid buffer
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 8b4720ef3a..16615ed2b8 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -3089,7 +3089,10 @@ write_pipe_chunks(char *data, int len, int dest)
 	/* write all but the last chunk */
 	while (len > PIPE_MAX_PAYLOAD)
 	{
-		p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'F' : 'f');
+		if (dest == LOG_DESTINATION_CSVLOG)
+			p.proto.is_last = PIPE_DEST_CSVLOG_PART;
+		else
+			p.proto.is_last = PIPE_DEST_STDERR_PART;
 		p.proto.len = PIPE_MAX_PAYLOAD;
 		memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD);
 		rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
@@ -3099,7 +3102,10 @@ write_pipe_chunks(char *data, int len, int dest)
 	}
 
 	/* write the last chunk */
-	p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'T' : 't');
+	if (dest == LOG_DESTINATION_CSVLOG)
+		p.proto.is_last = PIPE_DEST_CSVLOG_LAST;
+	else
+		p.proto.is_last = PIPE_DEST_STDERR_LAST;
 	p.proto.len = len;
 	memcpy(p.proto.data, data, len);
 	rc = write(fd, &p, PIPE_HEADER_SIZE + len);
diff --git a/src/include/postmaster/syslogger.h b/src/include/postmaster/syslogger.h
index 3a61104573..df60fdcb96 100644
--- a/src/include/postmaster/syslogger.h
+++ b/src/include/postmaster/syslogger.h
@@ -46,8 +46,7 @@ typedef struct
 	char		nuls[2];		/* always \0\0 */
 	uint16		len;			/* size of this chunk (counts data only) */
 	int32		pid;			/* writer's pid */
-	char		is_last;		/* last chunk of message? 't' or 'f' ('T' or
-								 * 'F' for CSV case) */
+	char		is_last;		/* see PIPE_DEST_ constants */
 	char		data[FLEXIBLE_ARRAY_MEMBER];	/* data payload starts here */
 } PipeProtoHeader;
 
@@ -60,6 +59,17 @@ typedef union
 #define PIPE_HEADER_SIZE  offsetof(PipeProtoHeader, data)
 #define PIPE_MAX_PAYLOAD  ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE))
 
+/*
+ * Possible values for PipeProtoHeader.is_last to identify the log
+ * destination and whether the message is complete.
+ *
+ * Values suffixed by _PART indicate a partial chunk of a message.
+ * Values suffxied by _LAST indicate the last chunk of a message.
+ */
+#define PIPE_DEST_STDERR_PART	'f'
+#define PIPE_DEST_STDERR_LAST	't'
+#define PIPE_DEST_CSVLOG_PART	'F'
+#define PIPE_DEST_CSVLOG_LAST	'T'
 
 /* GUC options */
 extern bool Logging_collector;
-- 
2.17.1

