diff -dcrpN postgresql.1/src/backend/utils/misc/guc.c postgresql.2/src/backend/utils/misc/guc.c
*** postgresql.1/src/backend/utils/misc/guc.c	2013-01-04 12:43:00.023640391 +0100
--- postgresql.2/src/backend/utils/misc/guc.c	2013-01-04 18:21:01.654437283 +0100
***************
*** 63,68 ****
--- 63,69 ----
  #include "storage/bufmgr.h"
  #include "storage/standby.h"
  #include "storage/fd.h"
+ #include "storage/lwlock.h"
  #include "storage/proc.h"
  #include "storage/predicate.h"
  #include "tcop/tcopprot.h"
*************** static int
*** 6097,6138 ****
  create_conf_lock_file(const char *LockFileName)
  {
  	int			fd;
- 	int			ntries;
  
! 	/*
! 	 * We need a loop here because of race conditions. Retry for 100 times.
! 	 */
! 	for (ntries = 0;; ntries++)
  	{
- 		fd = open(LockFileName, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
- 		if (fd >= 0)
- 			break;
- 
  		/*
! 		 * Couldn't create the lock file. Probably it already exists. If so
! 		 * wait for some time
  		 */
! 		if ((errno != EEXIST && errno != EACCES) || ntries > 100)
  		{
! 			if (errno != EEXIST)
! 			{
! 				ereport(ERROR,
  						(errcode_for_file_access(),
  						 errmsg("could not create lock file \"%s\": %m",
  								LockFileName)));
! 			}
! 			else
! 			{
! 				ereport(ERROR,
  						(errcode_for_file_access(),
  						 errmsg("could not create lock file \"%s\": %m ", LockFileName),
  						 errhint("May be too many concurrent edit into file happening, please wait!! and retry"
  								 "or .lock is file accidently left there please clean the file from config_dir")));
- 			}
- 		}
- 		else
- 		{
- 			pg_usleep(100000);	/* in total wait for 10sec */
  		}
  	}
  
--- 6098,6124 ----
  create_conf_lock_file(const char *LockFileName)
  {
  	int			fd;
  
! 	fd = open(LockFileName, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
! 	if (fd < 0)
  	{
  		/*
! 		 * Couldn't create the lock file. Probably it already exists.
  		 */
! 		if (errno != EEXIST)
  		{
! 			ereport(ERROR,
  						(errcode_for_file_access(),
  						 errmsg("could not create lock file \"%s\": %m",
  								LockFileName)));
! 		}
! 		else
! 		{
! 			ereport(ERROR,
  						(errcode_for_file_access(),
  						 errmsg("could not create lock file \"%s\": %m ", LockFileName),
  						 errhint("May be too many concurrent edit into file happening, please wait!! and retry"
  								 "or .lock is file accidently left there please clean the file from config_dir")));
  		}
  	}
  
*************** set_config_file(VariableSetStmt *setstmt
*** 6325,6330 ****
--- 6311,6318 ----
  	ConfAutoFileName = AbsoluteConfigLocation(AutoConfigFileName, ConfigFileName);
  	LockFileName = AbsoluteConfigLocation(AutoConfigLockFilename, ConfigFileName);
  
+ 	LWLockAcquire(SetPersistentLock, LW_EXCLUSIVE);
+ 
  	lockfd = create_conf_lock_file(LockFileName);
  
  	PG_TRY();
*************** set_config_file(VariableSetStmt *setstmt
*** 6382,6387 ****
--- 6370,6384 ----
  		PG_RE_THROW();
  	}
  	PG_END_TRY();
+ 
+ 	/*
+ 	 * Intentionally wait for 15 seconds here to simulate race conditions.
+ 	 * Remove this line when you've done enough testing.
+ 	 */
+ 	pg_usleep(15000000);
+ 
+ 	LWLockRelease(SetPersistentLock);
+ 
  	return;
  }
  
diff -dcrpN postgresql.1/src/include/storage/lwlock.h postgresql.2/src/include/storage/lwlock.h
*** postgresql.1/src/include/storage/lwlock.h	2013-01-02 09:19:03.906522364 +0100
--- postgresql.2/src/include/storage/lwlock.h	2013-01-04 17:57:51.058854977 +0100
*************** typedef enum LWLockId
*** 79,84 ****
--- 79,85 ----
  	SerializablePredicateLockListLock,
  	OldSerXidLock,
  	SyncRepLock,
+ 	SetPersistentLock,
  	/* Individual lock IDs end here */
  	FirstBufMappingLock,
  	FirstLockMgrLock = FirstBufMappingLock + NUM_BUFFER_PARTITIONS,
