From 2d77d835403a20b51e17e59f0343ddc17f431eec Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 13 Sep 2021 09:03:45 +0900 Subject: Refactor the syslogger pipe protocol to use a bitmask for its options The previous protocol expected a set of matching characters to check if a message sent was the last one or not, that changed depending on the destination wanted: - 't' and 'f' tracked the last message of a log sent to stderr. - 'T' and 'F' tracked the last message of a log sent to csvlog. This could be extended with more characters when introducing new destinations, but using a bitmask is much more elegant. This commit changes the protocol so as a bitmask is used in the header of a log chunk message sent to the syslogger, with the following options available for now: - log_destination as stderr. - log_destination as csvlog. - if a message is the last chunk of a message. Sehrope found this issue in a patch set to introduce JSON as an option for log_destination, but his patch made the size of the protocol header larger. This commit keeps the same size as the original, and adapts the protocol as wanted. Thanks also to Andrew Dunstan and Greg Stark for the discussion. Author: Michael Paquier, Sehrope Sarkuni Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com --- src/backend/utils/error/elog.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/backend/utils/error/elog.c') diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 816b071afaa..2af87ee3bd1 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -3250,11 +3250,16 @@ write_pipe_chunks(char *data, int len, int dest) p.proto.nuls[0] = p.proto.nuls[1] = '\0'; p.proto.pid = MyProcPid; + p.proto.flags = 0; + if (dest == LOG_DESTINATION_STDERR) + p.proto.flags |= PIPE_PROTO_DEST_STDERR; + else if (dest == LOG_DESTINATION_CSVLOG) + p.proto.flags |= PIPE_PROTO_DEST_CSVLOG; /* write all but the last chunk */ while (len > PIPE_MAX_PAYLOAD) { - p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'F' : 'f'); + /* no need to set PIPE_PROTO_IS_LAST yet */ p.proto.len = PIPE_MAX_PAYLOAD; memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD); rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD); @@ -3264,7 +3269,7 @@ write_pipe_chunks(char *data, int len, int dest) } /* write the last chunk */ - p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'T' : 't'); + p.proto.flags |= PIPE_PROTO_IS_LAST; p.proto.len = len; memcpy(p.proto.data, data, len); rc = write(fd, &p, PIPE_HEADER_SIZE + len); -- cgit v1.2.3