diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 22e5d87..b9c1130 100644
*** a/src/backend/utils/error/elog.c
--- b/src/backend/utils/error/elog.c
*************** static void write_csvlog(ErrorData *edat
*** 177,183 ****
  static void send_message_to_server_log(ErrorData *edata);
  static void write_pipe_chunks(char *data, int len, int dest);
  static void send_message_to_frontend(ErrorData *edata);
- static char *expand_fmt_string(const char *fmt, ErrorData *edata);
  static const char *error_severity(int elevel);
  static void append_with_tabs(StringInfo buf, const char *str);
  static bool is_log_level_output(int elevel, int log_min_level);
--- 177,182 ----
*************** errcode_for_socket_access(void)
*** 705,717 ****
   */
  #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit)	\
  	{ \
- 		char		   *fmtbuf; \
  		StringInfoData	buf; \
  		/* Internationalize the error format string */ \
  		if ((translateit) && !in_error_recursion_trouble()) \
  			fmt = dgettext((domain), fmt);				  \
- 		/* Expand %m in format string */ \
- 		fmtbuf = expand_fmt_string(fmt, edata); \
  		initStringInfo(&buf); \
  		if ((appendval) && edata->targetfield) { \
  			appendStringInfoString(&buf, edata->targetfield); \
--- 704,713 ----
*************** errcode_for_socket_access(void)
*** 722,736 ****
  		{ \
  			va_list		args; \
  			int			needed; \
  			va_start(args, fmt); \
! 			needed = appendStringInfoVA(&buf, fmtbuf, args); \
  			va_end(args); \
  			if (needed == 0) \
  				break; \
  			enlargeStringInfo(&buf, needed); \
  		} \
- 		/* Done with expanded fmt */ \
- 		pfree(fmtbuf); \
  		/* Save the completed message into the stack item */ \
  		if (edata->targetfield) \
  			pfree(edata->targetfield); \
--- 718,731 ----
  		{ \
  			va_list		args; \
  			int			needed; \
+ 			errno = edata->saved_errno; \
  			va_start(args, fmt); \
! 			needed = appendStringInfoVA(&buf, fmt, args); \
  			va_end(args); \
  			if (needed == 0) \
  				break; \
  			enlargeStringInfo(&buf, needed); \
  		} \
  		/* Save the completed message into the stack item */ \
  		if (edata->targetfield) \
  			pfree(edata->targetfield); \
*************** errcode_for_socket_access(void)
*** 746,760 ****
  #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval)  \
  	{ \
  		const char	   *fmt; \
- 		char		   *fmtbuf; \
  		StringInfoData	buf; \
  		/* Internationalize the error format string */ \
  		if (!in_error_recursion_trouble()) \
  			fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
  		else \
  			fmt = (n == 1 ? fmt_singular : fmt_plural); \
- 		/* Expand %m in format string */ \
- 		fmtbuf = expand_fmt_string(fmt, edata); \
  		initStringInfo(&buf); \
  		if ((appendval) && edata->targetfield) { \
  			appendStringInfoString(&buf, edata->targetfield); \
--- 741,752 ----
*************** errcode_for_socket_access(void)
*** 765,779 ****
  		{ \
  			va_list		args; \
  			int			needed; \
  			va_start(args, n); \
! 			needed = appendStringInfoVA(&buf, fmtbuf, args); \
  			va_end(args); \
  			if (needed == 0) \
  				break; \
  			enlargeStringInfo(&buf, needed); \
  		} \
- 		/* Done with expanded fmt */ \
- 		pfree(fmtbuf); \
  		/* Save the completed message into the stack item */ \
  		if (edata->targetfield) \
  			pfree(edata->targetfield); \
--- 757,770 ----
  		{ \
  			va_list		args; \
  			int			needed; \
+ 			errno = edata->saved_errno; \
  			va_start(args, n); \
! 			needed = appendStringInfoVA(&buf, fmt, args); \
  			va_end(args); \
  			if (needed == 0) \
  				break; \
  			enlargeStringInfo(&buf, needed); \
  		} \
  		/* Save the completed message into the stack item */ \
  		if (edata->targetfield) \
  			pfree(edata->targetfield); \
*************** send_message_to_frontend(ErrorData *edat
*** 3329,3387 ****
  
  
  /*
-  * expand_fmt_string --- process special format codes in a format string
-  *
-  * We must replace %m with the appropriate strerror string, since vsnprintf
-  * won't know what to do with it.
-  *
-  * The result is a palloc'd string.
-  */
- static char *
- expand_fmt_string(const char *fmt, ErrorData *edata)
- {
- 	StringInfoData buf;
- 	const char *cp;
- 
- 	initStringInfo(&buf);
- 
- 	for (cp = fmt; *cp; cp++)
- 	{
- 		if (cp[0] == '%' && cp[1] != '\0')
- 		{
- 			cp++;
- 			if (*cp == 'm')
- 			{
- 				/*
- 				 * Replace %m by system error string.  If there are any %'s in
- 				 * the string, we'd better double them so that vsnprintf won't
- 				 * misinterpret.
- 				 */
- 				const char *cp2;
- 
- 				cp2 = strerror(edata->saved_errno);
- 				for (; *cp2; cp2++)
- 				{
- 					if (*cp2 == '%')
- 						appendStringInfoCharMacro(&buf, '%');
- 					appendStringInfoCharMacro(&buf, *cp2);
- 				}
- 			}
- 			else
- 			{
- 				/* copy % and next char --- this avoids trouble with %%m */
- 				appendStringInfoCharMacro(&buf, '%');
- 				appendStringInfoCharMacro(&buf, *cp);
- 			}
- 		}
- 		else
- 			appendStringInfoCharMacro(&buf, *cp);
- 	}
- 
- 	return buf.data;
- }
- 
- 
- /*
   * error_severity --- get string representing elevel
   *
   * The string is not localized here, but we mark the strings for translation
--- 3320,3325 ----
