4848#include "postmaster/autovacuum.h"
4949#include "postmaster/fork_process.h"
5050#include "postmaster/postmaster.h"
51+ #include "storage/proc.h"
5152#include "storage/backendid.h"
5253#include "storage/fd.h"
5354#include "storage/ipc.h"
5455#include "storage/latch.h"
5556#include "storage/pg_shmem.h"
5657#include "storage/procsignal.h"
58+ #include "storage/sinvaladt.h"
5759#include "utils/ascii.h"
5860#include "utils/guc.h"
5961#include "utils/memutils.h"
@@ -213,7 +215,7 @@ typedef struct TwoPhasePgStatRecord
213215 */
214216static MemoryContext pgStatLocalContext = NULL ;
215217static HTAB * pgStatDBHash = NULL ;
216- static PgBackendStatus * localBackendStatusTable = NULL ;
218+ static LocalPgBackendStatus * localBackendStatusTable = NULL ;
217219static int localNumBackends = 0 ;
218220
219221/*
@@ -2303,6 +2305,28 @@ pgstat_fetch_stat_beentry(int beid)
23032305{
23042306 pgstat_read_current_status ();
23052307
2308+ if (beid < 1 || beid > localNumBackends )
2309+ return NULL ;
2310+
2311+ return & localBackendStatusTable [beid - 1 ].backendStatus ;
2312+ }
2313+
2314+
2315+ /* ----------
2316+ * pgstat_fetch_stat_local_beentry() -
2317+ *
2318+ * Like pgstat_fetch_stat_beentry() but with locally computed addtions (like
2319+ * xid and xmin values of the backend)
2320+ *
2321+ * NB: caller is responsible for a check if the user is permitted to see
2322+ * this info (especially the querystring).
2323+ * ----------
2324+ */
2325+ LocalPgBackendStatus *
2326+ pgstat_fetch_stat_local_beentry (int beid )
2327+ {
2328+ pgstat_read_current_status ();
2329+
23062330 if (beid < 1 || beid > localNumBackends )
23072331 return NULL ;
23082332
@@ -2783,8 +2807,8 @@ static void
27832807pgstat_read_current_status (void )
27842808{
27852809 volatile PgBackendStatus * beentry ;
2786- PgBackendStatus * localtable ;
2787- PgBackendStatus * localentry ;
2810+ LocalPgBackendStatus * localtable ;
2811+ LocalPgBackendStatus * localentry ;
27882812 char * localappname ,
27892813 * localactivity ;
27902814 int i ;
@@ -2795,9 +2819,9 @@ pgstat_read_current_status(void)
27952819
27962820 pgstat_setup_memcxt ();
27972821
2798- localtable = (PgBackendStatus * )
2822+ localtable = (LocalPgBackendStatus * )
27992823 MemoryContextAlloc (pgStatLocalContext ,
2800- sizeof (PgBackendStatus ) * MaxBackends );
2824+ sizeof (LocalPgBackendStatus ) * MaxBackends );
28012825 localappname = (char * )
28022826 MemoryContextAlloc (pgStatLocalContext ,
28032827 NAMEDATALEN * MaxBackends );
@@ -2821,19 +2845,19 @@ pgstat_read_current_status(void)
28212845 {
28222846 int save_changecount = beentry -> st_changecount ;
28232847
2824- localentry -> st_procpid = beentry -> st_procpid ;
2825- if (localentry -> st_procpid > 0 )
2848+ localentry -> backendStatus . st_procpid = beentry -> st_procpid ;
2849+ if (localentry -> backendStatus . st_procpid > 0 )
28262850 {
2827- memcpy (localentry , (char * ) beentry , sizeof (PgBackendStatus ));
2851+ memcpy (& localentry -> backendStatus , (char * ) beentry , sizeof (PgBackendStatus ));
28282852
28292853 /*
28302854 * strcpy is safe even if the string is modified concurrently,
28312855 * because there's always a \0 at the end of the buffer.
28322856 */
28332857 strcpy (localappname , (char * ) beentry -> st_appname );
2834- localentry -> st_appname = localappname ;
2858+ localentry -> backendStatus . st_appname = localappname ;
28352859 strcpy (localactivity , (char * ) beentry -> st_activity );
2836- localentry -> st_activity = localactivity ;
2860+ localentry -> backendStatus . st_activity = localactivity ;
28372861 }
28382862
28392863 if (save_changecount == beentry -> st_changecount &&
@@ -2846,8 +2870,12 @@ pgstat_read_current_status(void)
28462870
28472871 beentry ++ ;
28482872 /* Only valid entries get included into the local array */
2849- if (localentry -> st_procpid > 0 )
2873+ if (localentry -> backendStatus . st_procpid > 0 )
28502874 {
2875+ BackendIdGetTransactionIds (i ,
2876+ & localentry -> backend_xid ,
2877+ & localentry -> backend_xmin );
2878+
28512879 localentry ++ ;
28522880 localappname += NAMEDATALEN ;
28532881 localactivity += pgstat_track_activity_query_size ;
0 commit comments