aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/pgstatfuncs.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2016-03-09 12:08:58 -0500
committerRobert Haas <rhaas@postgresql.org>2016-03-09 12:08:58 -0500
commitb6fb6471f6afaf649e52f38269fd8c5c60647669 (patch)
tree18c0f9e24c9378625f994cdf24d193fadcd42349 /src/backend/utils/adt/pgstatfuncs.c
parent8776c15c85322612b9bf79daf50f74be71c12e05 (diff)
downloadpostgresql-b6fb6471f6afaf649e52f38269fd8c5c60647669.tar.gz
postgresql-b6fb6471f6afaf649e52f38269fd8c5c60647669.zip
Add a generic command progress reporting facility.
Using this facility, any utility command can report the target relation upon which it is operating, if there is one, and up to 10 64-bit counters; the intent of this is that users should be able to figure out what a utility command is doing without having to resort to ugly hacks like attaching strace to a backend. As a demonstration, this adds very crude reporting to lazy vacuum; we just report the target relation and nothing else. A forthcoming patch will make VACUUM report a bunch of additional data that will make this much more interesting. But this gets the basic framework in place. Vinayak Pokale, Rahila Syed, Amit Langote, Robert Haas, reviewed by Kyotaro Horiguchi, Jim Nasby, Thom Brown, Masahiko Sawada, Fujii Masao, and Masanori Oyama.
Diffstat (limited to 'src/backend/utils/adt/pgstatfuncs.c')
-rw-r--r--src/backend/utils/adt/pgstatfuncs.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 1b22fcc3a1a..0c790ff3ec5 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -64,6 +64,7 @@ extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
+extern Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS);
extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
@@ -525,6 +526,108 @@ pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
}
/*
+ * Returns command progress information for the named command.
+ */
+Datum
+pg_stat_get_progress_info(PG_FUNCTION_ARGS)
+{
+#define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
+ int num_backends = pgstat_fetch_stat_numbackends();
+ int curr_backend;
+ char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
+ ProgressCommandType cmdtype;
+ TupleDesc tupdesc;
+ Tuplestorestate *tupstore;
+ ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
+ MemoryContext per_query_ctx;
+ MemoryContext oldcontext;
+
+ /* check to see if caller supports us returning a tuplestore */
+ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("set-valued function called in context that cannot accept a set")));
+ if (!(rsinfo->allowedModes & SFRM_Materialize))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("materialize mode required, but it is not " \
+ "allowed in this context")));
+
+ /* Build a tuple descriptor for our result type */
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ /* Translate command name into command type code. */
+ if (pg_strcasecmp(cmd, "VACUUM") == 0)
+ cmdtype = PROGRESS_COMMAND_VACUUM;
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid command name: \"%s\"", cmd)));
+
+ per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
+ oldcontext = MemoryContextSwitchTo(per_query_ctx);
+
+ tupstore = tuplestore_begin_heap(true, false, work_mem);
+ rsinfo->returnMode = SFRM_Materialize;
+ rsinfo->setResult = tupstore;
+ rsinfo->setDesc = tupdesc;
+ MemoryContextSwitchTo(oldcontext);
+
+ /* 1-based index */
+ for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
+ {
+ LocalPgBackendStatus *local_beentry;
+ PgBackendStatus *beentry;
+ Datum values[PG_STAT_GET_PROGRESS_COLS];
+ bool nulls[PG_STAT_GET_PROGRESS_COLS];
+ int i;
+
+ MemSet(values, 0, sizeof(values));
+ MemSet(nulls, 0, sizeof(nulls));
+
+ local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
+
+ if (!local_beentry)
+ continue;
+
+ beentry = &local_beentry->backendStatus;
+
+ /*
+ * Report values for only those backends which are running the given
+ * command.
+ */
+ if (!beentry || beentry->st_progress_command != cmdtype)
+ continue;
+
+ /* Value available to all callers */
+ values[0] = Int32GetDatum(beentry->st_procpid);
+ values[1] = ObjectIdGetDatum(beentry->st_databaseid);
+
+ /* show rest of the values including relid only to role members */
+ if (has_privs_of_role(GetUserId(), beentry->st_userid))
+ {
+ values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
+ for(i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
+ values[i+3] = UInt32GetDatum(beentry->st_progress_param[i]);
+ }
+ else
+ {
+ nulls[2] = true;
+ for (i = 1; i < PGSTAT_NUM_PROGRESS_PARAM + 1; i++)
+ nulls[i+3] = true;
+ }
+
+ tuplestore_putvalues(tupstore, tupdesc, values, nulls);
+ }
+
+ /* clean up and return the tuplestore */
+ tuplestore_donestoring(tupstore);
+
+ return (Datum) 0;
+}
+
+/*
* Returns activity of PG backends.
*/
Datum