diff -cpr HEAD/src/backend/storage/lmgr/deadlock.c deadlock/src/backend/storage/lmgr/deadlock.c
*** HEAD/src/backend/storage/lmgr/deadlock.c	Wed Jan  2 04:45:52 2008
--- deadlock/src/backend/storage/lmgr/deadlock.c	Fri Feb 29 12:03:33 2008
***************
*** 28,35 ****
--- 28,38 ----
  #include "miscadmin.h"
  #include "storage/lmgr.h"
  #include "storage/proc.h"
+ #include "storage/procarray.h"
+ #include "utils/builtins.h"
  #include "utils/memutils.h"
  
+ extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
  
  /* One edge in the waits-for graph */
  typedef struct
*************** PrintLockQueue(LOCK *lock, const char *i
*** 872,877 ****
--- 875,886 ----
  }
  #endif
  
+ static void
+ deadlock_error_callback(void *arg)
+ {
+ 	errcontext("%s", (const char *) arg);
+ }
+ 
  /*
   * Report a detected deadlock, with available details.
   */
*************** DeadLockReport(void)
*** 880,894 ****
--- 889,907 ----
  {
  	StringInfoData buf;
  	StringInfoData buf2;
+ 	StringInfoData queries;
+ 	ErrorContextCallback errcontext;
  	int			i;
  
  	initStringInfo(&buf);
  	initStringInfo(&buf2);
+ 	initStringInfo(&queries);
  
  	for (i = 0; i < nDeadlockDetails; i++)
  	{
  		DEADLOCK_INFO *info = &deadlockDetails[i];
  		int			nextpid;
+ 		PGPROC	   *proc;
  
  		/* The last proc waits for the first one... */
  		if (i < nDeadlockDetails - 1)
*************** DeadLockReport(void)
*** 911,917 ****
--- 924,954 ----
  										 info->lockmode),
  						 buf2.data,
  						 nextpid);
+ 
+ #ifdef NOT_USED	/* use to exclude my query */
+ 		if (info->pid != MyProcPid)
+ 			continue;
+ #endif
+ 
+ 		proc = BackendPidGetProc(info->pid);
+ 		if (proc != NULL)
+ 		{
+ 			if (queries.len > 0)
+ 				appendStringInfoChar(&queries, '\n');
+ 
+ 			appendStringInfo(&queries, "Process %d: %s", info->pid,
+ 				DatumGetCString(DirectFunctionCall1(textout,
+ 					DirectFunctionCall1(pg_stat_get_backend_activity,
+ 						proc->backendId))));
+ 		}
  	}
+ 
+ 	/* Set up callback to dump conflicted queries */
+ 	errcontext.callback = deadlock_error_callback;
+ 	errcontext.arg = queries.data;
+ 	errcontext.previous = error_context_stack;
+ 	error_context_stack = &errcontext;
+ 
  	ereport(ERROR,
  			(errcode(ERRCODE_T_R_DEADLOCK_DETECTED),
  			 errmsg("deadlock detected"),
