*** src/backend/utils/adt/formatting.c
--- src/backend/utils/adt/formatting.c
***************
*** 1679,1685 **** is_next_separator(FormatNode *n)
  	n++;
  
  	if (n->type == NODE_TYPE_END)
! 		return TRUE;
  
  	if (n->type == NODE_TYPE_ACTION)
  	{
--- 1679,1685 ----
  	n++;
  
  	if (n->type == NODE_TYPE_END)
! 		return FALSE;
  
  	if (n->type == NODE_TYPE_ACTION)
  	{
***************
*** 1811,1820 **** from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
  		 * We need to pull exactly the number of characters given in 'len' out
  		 * of the string, and convert those.
  		 */
! 		char *first;
  		char *last;
  
- 		first = palloc(len + 1);
  		strncpy(first, init, len);
  		first[len] = '\0';
  
--- 1811,1820 ----
  		 * We need to pull exactly the number of characters given in 'len' out
  		 * of the string, and convert those.
  		 */
! 		char first[DCH_MAX_ITEM_SIZ + 1];
  		char *last;
+ 		int used;
  
  		strncpy(first, init, len);
  		first[len] = '\0';
  
***************
*** 1830,1838 **** from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
  							 "using the \"FM\" modifier.")));
  
  		result = strtol(first, &last, 10);
! 		*src += (last - first);
  
! 		pfree(first);
  	}
  
  	if (result == 0 && *src == init)
--- 1830,1848 ----
  							 "using the \"FM\" modifier.")));
  
  		result = strtol(first, &last, 10);
! 		used = last - first;
! 
! 		if (used > 0 && used < len)
! 			ereport(ERROR,
! 					(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
! 					 errmsg("invalid value for \"%s\" in source string", 
! 						 	node->key->name),
! 					 errdetail("Field requires %d characters, but only %d "
! 						 	   "could be parsed.", len, used),
! 					 errhint("If your source string is not fixed-width, try "
! 						 	 "using the \"FM\" modifier.")));
  
! 		*src += used;
  	}
  
  	if (result == 0 && *src == init)
*** src/test/regress/expected/timestamp.out
--- src/test/regress/expected/timestamp.out
***************
*** 1729,1744 **** SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
  ERROR:  source string too short for "MM" formatting field
  DETAIL:  Field requires 2 characters, but only 1 remain.
  HINT:  If your source string is not fixed-width, try using the "FM" modifier.
  -- Value clobbering:
! SELECT '' AS to_timestamp_24, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
  ERROR:  conflicting value for "Mon" field in formatting string
  DETAIL:  This value contradicts a previous setting for the same field type.
  -- Non-numeric input:
! SELECT '' AS to_timestamp_24, to_timestamp('199711xy', 'YYYYMMDD');
  ERROR:  invalid value for "DD" in source string
  DETAIL:  Value must be an integer.
  -- Input that doesn't fit in an int:
! SELECT '' AS to_timestamp_24, to_timestamp('10000000000', 'YYYY');
  ERROR:  value for "YYYY" in source string is out of range
  DETAIL:  Value must be in the range -2147483648 to 2147483647.
  SET DateStyle TO DEFAULT;
--- 1729,1749 ----
  ERROR:  source string too short for "MM" formatting field
  DETAIL:  Field requires 2 characters, but only 1 remain.
  HINT:  If your source string is not fixed-width, try using the "FM" modifier.
+ -- Insufficient digit characters for a single node:
+ SELECT '' AS to_timestamp_24, to_timestamp('19971)24', 'YYYYMMDD');
+ ERROR:  invalid value for "MM" in source string
+ DETAIL:  Field requires 2 characters, but only 1 could be parsed.
+ HINT:  If your source string is not fixed-width, try using the "FM" modifier.
  -- Value clobbering:
! SELECT '' AS to_timestamp_25, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
  ERROR:  conflicting value for "Mon" field in formatting string
  DETAIL:  This value contradicts a previous setting for the same field type.
  -- Non-numeric input:
! SELECT '' AS to_timestamp_26, to_timestamp('199711xy', 'YYYYMMDD');
  ERROR:  invalid value for "DD" in source string
  DETAIL:  Value must be an integer.
  -- Input that doesn't fit in an int:
! SELECT '' AS to_timestamp_27, to_timestamp('10000000000', 'FMYYYY');
  ERROR:  value for "YYYY" in source string is out of range
  DETAIL:  Value must be in the range -2147483648 to 2147483647.
  SET DateStyle TO DEFAULT;
*** src/test/regress/sql/timestamp.sql
--- src/test/regress/sql/timestamp.sql
***************
*** 279,291 **** SELECT '' AS to_timestamp_22, to_timestamp('2005527', 'YYYYIWID');
  -- Insufficient characters in the source string:
  SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
  
  -- Value clobbering:
! SELECT '' AS to_timestamp_24, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
  
  -- Non-numeric input:
! SELECT '' AS to_timestamp_24, to_timestamp('199711xy', 'YYYYMMDD');
  
  -- Input that doesn't fit in an int:
! SELECT '' AS to_timestamp_24, to_timestamp('10000000000', 'YYYY');
  
  SET DateStyle TO DEFAULT;
--- 279,294 ----
  -- Insufficient characters in the source string:
  SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
  
+ -- Insufficient digit characters for a single node:
+ SELECT '' AS to_timestamp_24, to_timestamp('19971)24', 'YYYYMMDD');
+ 
  -- Value clobbering:
! SELECT '' AS to_timestamp_25, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
  
  -- Non-numeric input:
! SELECT '' AS to_timestamp_26, to_timestamp('199711xy', 'YYYYMMDD');
  
  -- Input that doesn't fit in an int:
! SELECT '' AS to_timestamp_27, to_timestamp('10000000000', 'FMYYYY');
  
  SET DateStyle TO DEFAULT;
