Index: src/backend/utils/adt/ruleutils.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v
retrieving revision 1.306
diff -c -r1.306 ruleutils.c
*** src/backend/utils/adt/ruleutils.c	1 Aug 2009 19:59:41 -0000	1.306
--- src/backend/utils/adt/ruleutils.c	26 Aug 2009 23:09:00 -0000
***************
*** 2649,2659 ****
  {
  	StringInfo	buf = context->buf;
  	char	   *sep;
! 	int			colno;
  	ListCell   *l;
  
  	sep = " ";
  	colno = 0;
  	foreach(l, targetList)
  	{
  		TargetEntry *tle = (TargetEntry *) lfirst(l);
--- 2649,2669 ----
  {
  	StringInfo	buf = context->buf;
  	char	   *sep;
! 	int			colno, linecol;
  	ListCell   *l;
+ 	int         save_len;
+ 	char       *line_start;
+ 
+ 	line_start = strrchr(buf->data,'\n');
+ 	if (line_start == NULL)
+ 		line_start = buf->data;
+ 	else
+ 		line_start++;
+ 
  
  	sep = " ";
  	colno = 0;
+ 	linecol = 1;
  	foreach(l, targetList)
  	{
  		TargetEntry *tle = (TargetEntry *) lfirst(l);
***************
*** 2666,2671 ****
--- 2676,2683 ----
  		appendStringInfoString(buf, sep);
  		sep = ", ";
  		colno++;
+ 		linecol++;
+ 		save_len = buf->len;
  
  		/*
  		 * We special-case Var nodes rather than using get_rule_expr. This is
***************
*** 2703,2708 ****
--- 2715,2748 ----
  			if (attname == NULL || strcmp(attname, colname) != 0)
  				appendStringInfo(buf, " AS %s", quote_identifier(colname));
  		}
+ 
+ 		/* handle line overflow */
+ 		if (strlen(line_start) > 80 && linecol > 1 && PRETTY_INDENT(context))
+ 		{
+ 			StringInfoData thiscol;
+ 			
+ 			initStringInfo(&thiscol);
+ 			
+ 			/* save what we just added */
+ 			appendStringInfoString(&thiscol,buf->data + save_len);
+ 
+ 			/* wipe it out from the buffer */
+ 			buf->len = save_len;
+ 			buf->data[save_len] = '\0';
+ 
+ 			/* add a line break and reindent */
+ 			appendContextKeyword(context, "", -PRETTYINDENT_STD, 
+ 								 PRETTYINDENT_STD, PRETTYINDENT_VAR);
+ 
+ 			/* and now put back what we wiped out */
+ 			appendStringInfoString(buf,thiscol.data);
+ 
+ 			/* reset the counters */
+ 			line_start = strrchr(buf->data,'\n') + 1;
+ 			linecol = 0;
+ 
+ 			pfree(thiscol.data);
+ 		}
  	}
  }
  
