diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 915fb7a..139939d 100644
*** a/src/backend/utils/adt/ruleutils.c
--- b/src/backend/utils/adt/ruleutils.c
*************** get_target_list(List *targetList, depars
*** 4479,4520 ****
  		/* Consider line-wrapping if enabled */
  		if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
  		{
! 			int			leading_nl_pos = -1;
! 			char	   *trailing_nl;
! 			int			pos;
  
! 			/* Does the new field start with whitespace plus a new line? */
! 			for (pos = 0; pos < targetbuf.len; pos++)
  			{
! 				if (targetbuf.data[pos] == '\n')
! 				{
! 					leading_nl_pos = pos;
! 					break;
! 				}
! 				if (targetbuf.data[pos] != ' ')
! 					break;
  			}
- 
- 			/* Locate the start of the current line in the output buffer */
- 			trailing_nl = strrchr(buf->data, '\n');
- 			if (trailing_nl == NULL)
- 				trailing_nl = buf->data;
  			else
! 				trailing_nl++;
  
! 			/*
! 			 * If the field we're adding is the first in the list, or it
! 			 * already has a leading newline, don't add anything. Otherwise,
! 			 * add a newline, plus some indentation, if either the new field
! 			 * would cause an overflow or the last field used more than one
! 			 * line.
! 			 */
! 			if (colno > 1 &&
! 				leading_nl_pos == -1 &&
! 				((strlen(trailing_nl) + strlen(targetbuf.data) > context->wrapColumn) ||
! 				 last_was_multiline))
! 				appendContextKeyword(context, "", -PRETTYINDENT_STD,
! 									 PRETTYINDENT_STD, PRETTYINDENT_VAR);
  
  			/* Remember this field's multiline status for next iteration */
  			last_was_multiline =
--- 4479,4521 ----
  		/* Consider line-wrapping if enabled */
  		if (PRETTY_INDENT(context) && context->wrapColumn >= 0)
  		{
! 			int			leading_nl_pos;
  
! 			/* Does the new field start with a new line? */
! 			if (targetbuf.len > 0 && targetbuf.data[0] == '\n')
! 				leading_nl_pos = 0;
! 			else
! 				leading_nl_pos = -1;
! 
! 			/* If so, we shouldn't add anything */
! 			if (leading_nl_pos >= 0)
  			{
! 				/* instead, remove any trailing spaces currently in buf */
! 				while (buf->len > 0 && buf->data[buf->len - 1] == ' ')
! 					buf->data[--(buf->len)] = '\0';
  			}
  			else
! 			{
! 				char	   *trailing_nl;
  
! 				/* Locate the start of the current line in the output buffer */
! 				trailing_nl = strrchr(buf->data, '\n');
! 				if (trailing_nl == NULL)
! 					trailing_nl = buf->data;
! 				else
! 					trailing_nl++;
! 
! 				/*
! 				 * Add a newline, plus some indentation, if the new field is
! 				 * not the first and either the new field would cause an
! 				 * overflow or the last field used more than one line.
! 				 */
! 				if (colno > 1 &&
! 					((strlen(trailing_nl) + strlen(targetbuf.data) > context->wrapColumn) ||
! 					 last_was_multiline))
! 					appendContextKeyword(context, "", -PRETTYINDENT_STD,
! 										 PRETTYINDENT_STD, PRETTYINDENT_VAR);
! 			}
  
  			/* Remember this field's multiline status for next iteration */
  			last_was_multiline =
*************** static void
*** 6236,6256 ****
  appendContextKeyword(deparse_context *context, const char *str,
  					 int indentBefore, int indentAfter, int indentPlus)
  {
  	if (PRETTY_INDENT(context))
  	{
  		context->indentLevel += indentBefore;
  
! 		appendStringInfoChar(context->buf, '\n');
! 		appendStringInfoSpaces(context->buf,
  							   Max(context->indentLevel, 0) + indentPlus);
! 		appendStringInfoString(context->buf, str);
  
  		context->indentLevel += indentAfter;
  		if (context->indentLevel < 0)
  			context->indentLevel = 0;
  	}
  	else
! 		appendStringInfoString(context->buf, str);
  }
  
  /*
--- 6237,6264 ----
  appendContextKeyword(deparse_context *context, const char *str,
  					 int indentBefore, int indentAfter, int indentPlus)
  {
+ 	StringInfo	buf = context->buf;
+ 
  	if (PRETTY_INDENT(context))
  	{
  		context->indentLevel += indentBefore;
  
! 		/* remove any trailing spaces currently in the buffer ... */
! 		while (buf->len > 0 && buf->data[buf->len - 1] == ' ')
! 			buf->data[--(buf->len)] = '\0';
! 		/* ... then add a newline and some spaces */
! 		appendStringInfoChar(buf, '\n');
! 		appendStringInfoSpaces(buf,
  							   Max(context->indentLevel, 0) + indentPlus);
! 
! 		appendStringInfoString(buf, str);
  
  		context->indentLevel += indentAfter;
  		if (context->indentLevel < 0)
  			context->indentLevel = 0;
  	}
  	else
! 		appendStringInfoString(buf, str);
  }
  
  /*
