Index: heap.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/catalog/heap.c,v
retrieving revision 1.163
diff -r1.163 heap.c
1834a1835,1837
> 			int			j;
> 			bool			success;
> 			List	   *listptr2;
1836c1839,1874
< 			snprintf(ccname, NAMEDATALEN, "$%d", numchecks + 1);
---
> 
> 			/* Loop until we find a non-conflicting constraint name */
> 			/* What happens if this loops forever? */
> 			j = numchecks + 1;
> 			do {
> 				success = true;
> 				snprintf(ccname, NAMEDATALEN, "$%d", j);
> 
> 				/* Check against old constraints */
> 				for (i = 0; i < numoldchecks; i++)
> 				{
> 					if (strcmp(oldchecks[i].ccname, ccname) == 0) {
> 						success = false;
> 						break;
> 					}
> 				}
> 				/* Check against other new constraints, if the check hasn't already failed */
> 				if (success) {
> 					foreach(listptr2, rawConstraints)
> 					{
> 						Constraint *cdef2 = (Constraint *) lfirst(listptr2);
> 		
> 						if (cdef2 == cdef ||
> 							cdef2->contype != CONSTR_CHECK ||
> 							cdef2->raw_expr == NULL ||
> 							cdef2->name == NULL)
> 							continue;
> 						if (strcmp(cdef2->name, ccname) == 0) {
> 							success = false;
> 							break;
> 						}
> 					}
> 				}
> 
> 				++j;
> 			} while (!success);
