Skip to content

Commit bc3991c

Browse files
committed
Add BackendXidGetPid().
1 parent f57e3f4 commit bc3991c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
*
2525
* IDENTIFICATION
26-
* $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.4 2005/07/31 17:19:18 tgl Exp $
26+
* $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.5 2005/08/20 01:26:36 ishii Exp $
2727
*
2828
*-------------------------------------------------------------------------
2929
*/
@@ -685,6 +685,44 @@ BackendPidGetProc(int pid)
685685
return result;
686686
}
687687

688+
/*
689+
* BackendXidGetPid -- get a backend's pid given its XID
690+
*
691+
* Returns 0 if not found or it's a prepared transaction. Note that
692+
* it is up to the caller to be sure that the question remains
693+
* meaningful for long enough for the answer to be used ...
694+
*
695+
* Only main transaction Ids are considered. This function is mainly
696+
* useful for determining what backend owns a lock.
697+
*/
698+
int
699+
BackendXidGetPid(TransactionId xid)
700+
{
701+
int result = 0;
702+
ProcArrayStruct *arrayP = procArray;
703+
int index;
704+
705+
if (xid == InvalidTransactionId) /* never match invalid xid */
706+
return 0;
707+
708+
LWLockAcquire(ProcArrayLock, LW_SHARED);
709+
710+
for (index = 0; index < arrayP->numProcs; index++)
711+
{
712+
PGPROC *proc = arrayP->procs[index];
713+
714+
if (proc->xid == xid)
715+
{
716+
result = proc->pid;
717+
break;
718+
}
719+
}
720+
721+
LWLockRelease(ProcArrayLock);
722+
723+
return result;
724+
}
725+
688726
/*
689727
* IsBackendPid -- is a given pid a running backend
690728
*/

src/include/storage/procarray.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.3 2005/07/31 17:19:22 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.4 2005/08/20 01:26:29 ishii Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -27,6 +27,7 @@ extern bool TransactionIdIsActive(TransactionId xid);
2727
extern TransactionId GetOldestXmin(bool allDbs);
2828

2929
extern PGPROC *BackendPidGetProc(int pid);
30+
extern int BackendXidGetPid(TransactionId xid);
3031
extern bool IsBackendPid(int pid);
3132
extern bool DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself);
3233

0 commit comments

Comments
 (0)