From fc70a4b0df38bda6a13941f1581f25fbb643c7f3 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Sun, 26 Mar 2017 22:02:22 -0400 Subject: Show more processes in pg_stat_activity. Previously, auxiliary processes and background workers not connected to a database (such as the logical replication launcher) weren't shown. Include them, so that we can see the associated wait state information. Add a new column to identify the processes type, so that people can filter them out easily using SQL if they wish. Before this patch was written, there was discussion about whether we should expose this information in a separate view, so as to avoid contaminating pg_stat_activity with things people might not want to see. But putting everything in pg_stat_activity was a more popular choice, so that's what the patch does. Kuntal Ghosh, reviewed by Amit Langote and Michael Paquier. Some revisions and bug fixes by me. Discussion: http://postgr.es/m/CA+TgmoYES5nhkEGw9nZXU8_FhA8XEm8NTm3-SO+3ML1B81Hkww@mail.gmail.com --- src/backend/utils/adt/pgstatfuncs.c | 60 +++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) (limited to 'src/backend/utils/adt/pgstatfuncs.c') diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index a987d0d6214..dd2b924d0af 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -20,6 +20,7 @@ #include "funcapi.h" #include "miscadmin.h" #include "pgstat.h" +#include "postmaster/postmaster.h" #include "storage/proc.h" #include "storage/procarray.h" #include "utils/acl.h" @@ -538,7 +539,7 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS) Datum pg_stat_get_activity(PG_FUNCTION_ARGS) { -#define PG_STAT_GET_ACTIVITY_COLS 23 +#define PG_STAT_GET_ACTIVITY_COLS 24 int num_backends = pgstat_fetch_stat_numbackends(); int curr_backend; int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0); @@ -582,8 +583,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) LocalPgBackendStatus *local_beentry; PgBackendStatus *beentry; PGPROC *proc; - const char *wait_event_type; - const char *wait_event; + const char *wait_event_type = NULL; + const char *wait_event = NULL; MemSet(values, 0, sizeof(values)); MemSet(nulls, 0, sizeof(nulls)); @@ -615,9 +616,18 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) continue; /* Values available to all callers */ - values[0] = ObjectIdGetDatum(beentry->st_databaseid); + if (beentry->st_databaseid != InvalidOid) + values[0] = ObjectIdGetDatum(beentry->st_databaseid); + else + nulls[0] = true; + values[1] = Int32GetDatum(beentry->st_procpid); - values[2] = ObjectIdGetDatum(beentry->st_userid); + + if (beentry->st_userid != InvalidOid) + values[2] = ObjectIdGetDatum(beentry->st_userid); + else + nulls[2] = true; + if (beentry->st_appname) values[3] = CStringGetTextDatum(beentry->st_appname); else @@ -635,17 +645,17 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) if (beentry->st_ssl) { - values[17] = BoolGetDatum(true); /* ssl */ - values[18] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version); - values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher); - values[20] = Int32GetDatum(beentry->st_sslstatus->ssl_bits); - values[21] = BoolGetDatum(beentry->st_sslstatus->ssl_compression); - values[22] = CStringGetTextDatum(beentry->st_sslstatus->ssl_clientdn); + values[18] = BoolGetDatum(true); /* ssl */ + values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version); + values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher); + values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits); + values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression); + values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_clientdn); } else { - values[17] = BoolGetDatum(false); /* ssl */ - nulls[18] = nulls[19] = nulls[20] = nulls[21] = nulls[22] = true; + values[18] = BoolGetDatum(false); /* ssl */ + nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = true; } /* Values only available to role member */ @@ -690,10 +700,24 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) wait_event = pgstat_get_wait_event(raw_wait_event); } - else + else if (beentry->st_backendType != B_BACKEND) { - wait_event_type = NULL; - wait_event = NULL; + /* + * For an auxiliary process, retrieve process info from + * AuxiliaryProcs stored in shared-memory. + */ + proc = AuxiliaryPidGetProc(beentry->st_procpid); + + if (proc != NULL) + { + uint32 raw_wait_event; + + raw_wait_event = + UINT32_ACCESS_ONCE(proc->wait_event_info); + wait_event_type = + pgstat_get_wait_event_type(raw_wait_event); + wait_event = pgstat_get_wait_event(raw_wait_event); + } } if (wait_event_type) @@ -793,6 +817,9 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) nulls[14] = true; } } + /* Add backend type */ + values[17] = + CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType)); } else { @@ -808,6 +835,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) nulls[12] = true; nulls[13] = true; nulls[14] = true; + nulls[17] = true; } tuplestore_putvalues(tupstore, tupdesc, values, nulls); -- cgit v1.2.3