*** ./src/backend/storage/lmgr/proc.c.orig	2013-06-27 20:57:34.357713067 +0200
--- ./src/backend/storage/lmgr/proc.c	2013-06-27 21:19:04.917502553 +0200
***************
*** 179,185 ****
  InitProcGlobal(void)
  {
  	PGPROC	   *procs;
! 	int			i;
  	bool		found;
  
  	/* Create the ProcGlobal shared structure */
--- 179,186 ----
  InitProcGlobal(void)
  {
  	PGPROC	   *procs;
! 	int			i,
! 				 j;
  	bool		found;
  
  	/* Create the ProcGlobal shared structure */
***************
*** 223,228 ****
--- 224,233 ----
  		InitSharedLatch(&procs[i].procLatch);
  		procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
  		ProcGlobal->freeProcs = &procs[i];
+ 
+ 		/* Initialize myProcLocks[] shred memory queues. */
+ 		for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
+ 			SHMQueueInit(&(procs[i].myProcLocks[j]));
  	}
  
  	/*
***************
*** 242,247 ****
--- 247,255 ----
  		InitSharedLatch(&procs[i].procLatch);
  		procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
  		ProcGlobal->autovacFreeProcs = &procs[i];
+ 
+ 		for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
+ 			SHMQueueInit(&(procs[i].myProcLocks[j]));
  	}
  
  	/*
***************
*** 253,258 ****
--- 261,269 ----
  		AuxiliaryProcs[i].pid = 0;		/* marks auxiliary proc as not in use */
  		PGSemaphoreCreate(&(AuxiliaryProcs[i].sem));
  		InitSharedLatch(&AuxiliaryProcs[i].procLatch);
+ 
+ 		for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
+ 			SHMQueueInit(&(procs[i].myProcLocks[j]));
  	}
  
  	/* Create ProcStructLock spinlock, too */
***************
*** 268,274 ****
  {
  	/* use volatile pointer to prevent code rearrangement */
  	volatile PROC_HDR *procglobal = ProcGlobal;
- 	int			i;
  
  	/*
  	 * ProcGlobal should be set up already (if we are a backend, we inherit
--- 279,284 ----
***************
*** 358,365 ****
  	MyProc->lwWaitLink = NULL;
  	MyProc->waitLock = NULL;
  	MyProc->waitProcLock = NULL;
! 	for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
! 		SHMQueueInit(&(MyProc->myProcLocks[i]));
  	MyProc->recoveryConflictPending = false;
  
  	/* Initialize fields for sync rep */
--- 368,393 ----
  	MyProc->lwWaitLink = NULL;
  	MyProc->waitLock = NULL;
  	MyProc->waitProcLock = NULL;
! 
! 	/*
! 	 * Our patch
! 	 *
! 	 * we would to diagnose how much often is proces startup find
! 	 * orphaned PGPROCLOCKS. Don't use ereport, because process is
! 	 * not fully prepared in this moment.
! 	 */
! 	{
! 		int		i;
! 
! 		for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
! 		{
! 			if (!SHMQueueEmpty(&(MyProc->myProcLocks[i])))
! 			{
! 				fprintf(stderr, "InitProcess: found orphaned PGPROCLOCK proc:%p, pid:%d\n", MyProc, MyProcPid);
! 			}
! 		}
! 	}
! 
  	MyProc->recoveryConflictPending = false;
  
  	/* Initialize fields for sync rep */
***************
*** 729,734 ****
--- 757,807 ----
  	SyncRepCleanupAtProcExit();
  
  	/*
+ 	 * Our patch
+ 	 *
+ 	 * we would to diagnose how much often is proces startup find
+ 	 * orphaned PGPROCLOCKS. Don't use ereport, because process is
+ 	 * not fully prepared in this moment.
+ 	 */
+ 	{
+ 		int		i;
+ 		bool			found_orphaned_locks = false;
+ 
+ 		for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
+ 		{
+ 			if (!SHMQueueEmpty(&(MyProc->myProcLocks[i])))
+ 			{
+ 				fprintf(stderr, "ProcKill: found orphaned PGPROCLOCK proc:%p, pid:%d\n", MyProc, MyProcPid);
+ 				found_orphaned_locks = true;
+ 				break;
+ 			}
+ 		}
+ 
+ 		if (found_orphaned_locks)
+ 		{
+ 			LockReleaseAll(DEFAULT_LOCKMETHOD, true);
+ 			/* Release transaction-level advisory locks */
+ 			LockReleaseAll(USER_LOCKMETHOD, false);
+ 			found_orphaned_locks = false;
+ 
+ 			for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
+ 			{
+ 				if (!SHMQueueEmpty(&(MyProc->myProcLocks[i])))
+ 				{
+ 					fprintf(stderr, "ProcKill: found orphaned PGPROCLOCK proc:%p, pid:%d again\n", MyProc, MyProcPid);
+ 					found_orphaned_locks = true;
+ 					break;
+ 				}
+ 			}
+ 
+ 			if (!found_orphaned_locks)
+ 				fprintf(stderr, "ProcKill: orphaned PGPROCLOCK proc:%p, pid:%d was removed\n", MyProc, MyProcPid);
+ 			else
+ 				fprintf(stderr, "ProcKill: orphaned PGPROCLOCK proc:%p, pid:%d was not removed\n", MyProc, MyProcPid);
+ 		}
+ 	}
+ 
+ 	/*
  	 * Release any LW locks I am holding.  There really shouldn't be any, but
  	 * it's cheap to check again before we cut the knees off the LWLock
  	 * facility by releasing our PGPROC ...
*** ./src/include/miscadmin.h.orig	2013-06-27 21:15:39.477104870 +0200
--- ./src/include/miscadmin.h	2013-06-27 21:15:49.679124666 +0200
***************
*** 83,89 ****
--- 83,92 ----
  #define CHECK_FOR_INTERRUPTS() \
  do { \
  	if (InterruptPending) \
+ 	{ \
+ 		fprintf(stderr, "CHECK_FOR_INTERRUPTS: func:%s file:%s line:%d, pid:%d\n", __func__, __FILE__, __LINE__, MyProcPid); \
  		ProcessInterrupts(); \
+ 	} \
  } while(0)
  #else							/* WIN32 */
  
