aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuo Ishii <ishii@postgresql.org>2005-08-20 01:26:36 +0000
committerTatsuo Ishii <ishii@postgresql.org>2005-08-20 01:26:36 +0000
commitbc3991c1851d4fbbb8881640a8fd7dddf7d7b7ca (patch)
tree096270c2875c722b58e4516c796d196b92986cfb
parentf57e3f4cf36f3fdd89cae8d566479ad747809b2f (diff)
downloadpostgresql-bc3991c1851d4fbbb8881640a8fd7dddf7d7b7ca.tar.gz
postgresql-bc3991c1851d4fbbb8881640a8fd7dddf7d7b7ca.zip
Add BackendXidGetPid().
-rw-r--r--src/backend/storage/ipc/procarray.c40
-rw-r--r--src/include/storage/procarray.h3
2 files changed, 41 insertions, 2 deletions
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 1e2783dea80..87b1228ae6d 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -23,7 +23,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.4 2005/07/31 17:19:18 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.5 2005/08/20 01:26:36 ishii Exp $
*
*-------------------------------------------------------------------------
*/
@@ -686,6 +686,44 @@ BackendPidGetProc(int pid)
}
/*
+ * BackendXidGetPid -- get a backend's pid given its XID
+ *
+ * Returns 0 if not found or it's a prepared transaction. Note that
+ * it is up to the caller to be sure that the question remains
+ * meaningful for long enough for the answer to be used ...
+ *
+ * Only main transaction Ids are considered. This function is mainly
+ * useful for determining what backend owns a lock.
+ */
+int
+BackendXidGetPid(TransactionId xid)
+{
+ int result = 0;
+ ProcArrayStruct *arrayP = procArray;
+ int index;
+
+ if (xid == InvalidTransactionId) /* never match invalid xid */
+ return 0;
+
+ LWLockAcquire(ProcArrayLock, LW_SHARED);
+
+ for (index = 0; index < arrayP->numProcs; index++)
+ {
+ PGPROC *proc = arrayP->procs[index];
+
+ if (proc->xid == xid)
+ {
+ result = proc->pid;
+ break;
+ }
+ }
+
+ LWLockRelease(ProcArrayLock);
+
+ return result;
+}
+
+/*
* IsBackendPid -- is a given pid a running backend
*/
bool
diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h
index aa0adfec881..af1209d2192 100644
--- a/src/include/storage/procarray.h
+++ b/src/include/storage/procarray.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.3 2005/07/31 17:19:22 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.4 2005/08/20 01:26:29 ishii Exp $
*
*-------------------------------------------------------------------------
*/
@@ -27,6 +27,7 @@ extern bool TransactionIdIsActive(TransactionId xid);
extern TransactionId GetOldestXmin(bool allDbs);
extern PGPROC *BackendPidGetProc(int pid);
+extern int BackendXidGetPid(TransactionId xid);
extern bool IsBackendPid(int pid);
extern bool DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself);