diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2005-12-16 04:03:40 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2005-12-16 04:03:40 +0000 |
commit | fb3dbdf986f0ab56e0a5177966bb0dec1f93444c (patch) | |
tree | e2d938afc400167c0b62d49bf37ed825ae0cbd70 /src/backend/storage/ipc/procarray.c | |
parent | 4ce6be4f5e1e4b0c89c5c8de179e3fa8216a7b54 (diff) |
Rethink prior patch to filter out dead backend entries from the pgstats
file. The original code probed the PGPROC array separately for each PID,
which was not good for large numbers of backends: not only is the runtime
O(N^2) but most of it is spent holding ProcArrayLock. Instead, take the
lock just once and copy the active PIDs into an array, then use qsort
and bsearch so that the lookup time is more like O(N log N).
Diffstat (limited to 'src/backend/storage/ipc/procarray.c')
-rw-r--r-- | src/backend/storage/ipc/procarray.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index cafadeb9054..f6330807480 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.9 2005/12/11 21:02:18 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.10 2005/12/16 04:03:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -733,6 +733,42 @@ IsBackendPid(int pid) } /* + * GetAllBackendPids -- get an array of all current backends' PIDs + * + * The result is a palloc'd array with the number of active backends in + * entry [0], their PIDs in entries [1] .. [n]. The caller must bear in + * mind that the result may already be obsolete when returned. + */ +int * +GetAllBackendPids(void) +{ + int *result; + int npids; + ProcArrayStruct *arrayP = procArray; + int index; + + result = (int *) palloc((MaxBackends + 1) * sizeof(int)); + npids = 0; + + LWLockAcquire(ProcArrayLock, LW_SHARED); + + for (index = 0; index < arrayP->numProcs; index++) + { + PGPROC *proc = arrayP->procs[index]; + + if (proc->pid != 0) /* ignore dummy procs */ + result[++npids] = proc->pid; + } + + LWLockRelease(ProcArrayLock); + + Assert(npids <= MaxBackends); + + result[0] = npids; + return result; +} + +/* * CountActiveBackends --- count backends (other than myself) that are in * active transactions. This is used as a heuristic to decide if * a pre-XLOG-flush delay is worthwhile during commit. |