*** ./src/backend/utils/adt/varlena.c.orig	2010-02-26 03:01:10.000000000 +0100
--- ./src/backend/utils/adt/varlena.c	2010-05-04 15:55:45.677424338 +0200
***************
*** 2985,2990 ****
--- 2985,2992 ----
  	char	   *start_ptr;
  	text	   *result_text;
  	ArrayBuildState *astate = NULL;
+ 	text			*null_sym;
+ 	bool	isnull;
  
  	text_position_setup(inputstring, fldsep, &state);
  
***************
*** 3012,3017 ****
--- 3014,3033 ----
  		PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID,
  										   PointerGetDatum(inputstring), 1));
  	}
+ 	
+ 	/*
+ 	 * Third argument (if exists is text used instead NULL value.
+ 	 */
+ 	if (PG_NARGS() > 2)
+ 	{
+ 		if (PG_ARGISNULL(2))
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ 					 errmsg("text for NULL substitution cannot be NULL")));
+ 		null_sym = PG_GETARG_TEXT_PP(2);
+ 	}
+ 	else
+ 		null_sym = NULL;
  
  	start_posn = 1;
  	/* start_ptr points to the start_posn'th character of inputstring */
***************
*** 3036,3046 ****
  
  		/* must build a temp text datum to pass to accumArrayResult */
  		result_text = cstring_to_text_with_len(start_ptr, chunk_len);
  
  		/* stash away this field */
  		astate = accumArrayResult(astate,
  								  PointerGetDatum(result_text),
! 								  false,
  								  TEXTOID,
  								  CurrentMemoryContext);
  
--- 3052,3073 ----
  
  		/* must build a temp text datum to pass to accumArrayResult */
  		result_text = cstring_to_text_with_len(start_ptr, chunk_len);
+ 		
+ 		/* check if this text is equal to null symbol */
+ 		if (null_sym != NULL)
+ 		{
+ 			
+ 			isnull = DatumGetBool(DirectFunctionCall2(texteq,
+ 										PointerGetDatum(null_sym),
+ 										PointerGetDatum(result_text)));
+ 		}
+ 		else
+ 			isnull = false;
  
  		/* stash away this field */
  		astate = accumArrayResult(astate,
  								  PointerGetDatum(result_text),
! 								  isnull,
  								  TEXTOID,
  								  CurrentMemoryContext);
  
***************
*** 3062,3067 ****
--- 3089,3103 ----
  }
  
  /*
+  * Just wrapper for three params variant string_to_array function
+  */
+ Datum
+ text_to_array_with_nullsym(PG_FUNCTION_ARGS)
+ {
+ 	return text_to_array(fcinfo);
+ }
+ 
+ /*
   * array_to_text
   * concatenate Cstring representation of input array elements
   * using provided field separator
***************
*** 3085,3090 ****
--- 3121,3127 ----
  	int			bitmask;
  	int			i;
  	ArrayMetaState *my_extra;
+ 	char		*null_sym;
  
  	ndims = ARR_NDIM(v);
  	dims = ARR_DIMS(v);
***************
*** 3132,3137 ****
--- 3169,3185 ----
  	bitmap = ARR_NULLBITMAP(v);
  	bitmask = 1;
  
+ 	if (PG_NARGS() > 2)
+ 	{
+ 		if (PG_ARGISNULL(2))
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ 					 errmsg("text for NULL substitution cannot be NULL")));
+ 		null_sym = text_to_cstring(PG_GETARG_TEXT_PP(2));
+ 	}
+ 	else
+ 		null_sym = NULL;
+ 
  	for (i = 0; i < nitems; i++)
  	{
  		Datum		itemvalue;
***************
*** 3140,3146 ****
  		/* Get source element, checking for NULL */
  		if (bitmap && (*bitmap & bitmask) == 0)
  		{
! 			/* we ignore nulls */
  		}
  		else
  		{
--- 3188,3201 ----
  		/* Get source element, checking for NULL */
  		if (bitmap && (*bitmap & bitmask) == 0)
  		{
! 			if (null_sym != NULL)
! 			{
! 				if (printed)
! 					appendStringInfo(&buf, "%s%s", fldsep, null_sym);
! 				else
! 					appendStringInfoString(&buf, null_sym);
! 				printed = true;
! 			}
  		}
  		else
  		{
***************
*** 3173,3178 ****
--- 3228,3243 ----
  	PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len));
  }
  
+ /*
+  * Just only wrapper for three params array_to_string function.
+  */
+ Datum
+ array_to_text_with_nullsym(PG_FUNCTION_ARGS)
+ {
+ 	return array_to_text_with_nullsym(fcinfo);
+ }
+ 
+ 
  #define HEXBASE 16
  /*
   * Convert a int32 to a string containing a base 16 (hex) representation of
