1 /*-------------------------------------------------------------------------
4 * Functions for accessing the statistics collector data
6 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/pgstatfuncs.c
13 *-------------------------------------------------------------------------
17 #include "access/htup_details.h"
18 #include "catalog/pg_type.h"
21 #include "miscadmin.h"
23 #include "utils/acl.h"
24 #include "utils/builtins.h"
25 #include "utils/inet.h"
26 #include "utils/timestamp.h"
28 /* bogus ... these externs should be in a header file */
29 extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
30 extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
31 extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
32 extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
33 extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
34 extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
35 extern Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS);
36 extern Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS);
37 extern Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS);
38 extern Datum pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS);
39 extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
40 extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
41 extern Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS);
42 extern Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS);
44 extern Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS);
45 extern Datum pg_stat_get_vacuum_count(PG_FUNCTION_ARGS);
46 extern Datum pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_analyze_count(PG_FUNCTION_ARGS);
48 extern Datum pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS);
50 extern Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS);
51 extern Datum pg_stat_get_function_total_time(PG_FUNCTION_ARGS);
52 extern Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS);
54 extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
55 extern Datum pg_stat_get_activity(PG_FUNCTION_ARGS);
56 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
57 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
58 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
59 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
60 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
61 extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS);
62 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
63 extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
64 extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
65 extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
66 extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
67 extern Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
70 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
71 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
74 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
75 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
77 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
78 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
79 extern Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS);
80 extern Datum pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS);
81 extern Datum pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS);
82 extern Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS);
83 extern Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS);
84 extern Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS);
85 extern Datum pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS);
86 extern Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS);
87 extern Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS);
88 extern Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS);
89 extern Datum pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS);
90 extern Datum pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS);
92 extern Datum pg_stat_get_archiver(PG_FUNCTION_ARGS);
94 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
95 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
96 extern Datum pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS);
97 extern Datum pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS);
98 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
99 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
100 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
101 extern Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS);
102 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
103 extern Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS);
104 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
106 extern Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS);
107 extern Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS);
108 extern Datum pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS);
109 extern Datum pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS);
110 extern Datum pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS);
111 extern Datum pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS);
112 extern Datum pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS);
113 extern Datum pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS);
114 extern Datum pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS);
116 extern Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS);
117 extern Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS);
118 extern Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS);
120 extern Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS);
121 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
122 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
123 extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
124 extern Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS);
125 extern Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS);
127 /* Global bgwriter statistics, from bgwriter.c */
128 extern PgStat_MsgBgWriter bgwriterStats;
131 pg_stat_get_numscans(PG_FUNCTION_ARGS)
133 Oid relid = PG_GETARG_OID(0);
135 PgStat_StatTabEntry *tabentry;
137 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
140 result = (int64) (tabentry->numscans);
142 PG_RETURN_INT64(result);
147 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
149 Oid relid = PG_GETARG_OID(0);
151 PgStat_StatTabEntry *tabentry;
153 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
156 result = (int64) (tabentry->tuples_returned);
158 PG_RETURN_INT64(result);
163 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
165 Oid relid = PG_GETARG_OID(0);
167 PgStat_StatTabEntry *tabentry;
169 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
172 result = (int64) (tabentry->tuples_fetched);
174 PG_RETURN_INT64(result);
179 pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
181 Oid relid = PG_GETARG_OID(0);
183 PgStat_StatTabEntry *tabentry;
185 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
188 result = (int64) (tabentry->tuples_inserted);
190 PG_RETURN_INT64(result);
195 pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
197 Oid relid = PG_GETARG_OID(0);
199 PgStat_StatTabEntry *tabentry;
201 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
204 result = (int64) (tabentry->tuples_updated);
206 PG_RETURN_INT64(result);
211 pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
213 Oid relid = PG_GETARG_OID(0);
215 PgStat_StatTabEntry *tabentry;
217 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
220 result = (int64) (tabentry->tuples_deleted);
222 PG_RETURN_INT64(result);
227 pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
229 Oid relid = PG_GETARG_OID(0);
231 PgStat_StatTabEntry *tabentry;
233 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
236 result = (int64) (tabentry->tuples_hot_updated);
238 PG_RETURN_INT64(result);
243 pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
245 Oid relid = PG_GETARG_OID(0);
247 PgStat_StatTabEntry *tabentry;
249 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
252 result = (int64) (tabentry->n_live_tuples);
254 PG_RETURN_INT64(result);
259 pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
261 Oid relid = PG_GETARG_OID(0);
263 PgStat_StatTabEntry *tabentry;
265 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
268 result = (int64) (tabentry->n_dead_tuples);
270 PG_RETURN_INT64(result);
275 pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
277 Oid relid = PG_GETARG_OID(0);
279 PgStat_StatTabEntry *tabentry;
281 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
284 result = (int64) (tabentry->changes_since_analyze);
286 PG_RETURN_INT64(result);
291 pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
293 Oid relid = PG_GETARG_OID(0);
295 PgStat_StatTabEntry *tabentry;
297 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
300 result = (int64) (tabentry->blocks_fetched);
302 PG_RETURN_INT64(result);
307 pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
309 Oid relid = PG_GETARG_OID(0);
311 PgStat_StatTabEntry *tabentry;
313 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
316 result = (int64) (tabentry->blocks_hit);
318 PG_RETURN_INT64(result);
322 pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
324 Oid relid = PG_GETARG_OID(0);
326 PgStat_StatTabEntry *tabentry;
328 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
331 result = tabentry->vacuum_timestamp;
336 PG_RETURN_TIMESTAMPTZ(result);
340 pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
342 Oid relid = PG_GETARG_OID(0);
344 PgStat_StatTabEntry *tabentry;
346 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
349 result = tabentry->autovac_vacuum_timestamp;
354 PG_RETURN_TIMESTAMPTZ(result);
358 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
360 Oid relid = PG_GETARG_OID(0);
362 PgStat_StatTabEntry *tabentry;
364 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
367 result = tabentry->analyze_timestamp;
372 PG_RETURN_TIMESTAMPTZ(result);
376 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
378 Oid relid = PG_GETARG_OID(0);
380 PgStat_StatTabEntry *tabentry;
382 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
385 result = tabentry->autovac_analyze_timestamp;
390 PG_RETURN_TIMESTAMPTZ(result);
394 pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
396 Oid relid = PG_GETARG_OID(0);
398 PgStat_StatTabEntry *tabentry;
400 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
403 result = (int64) (tabentry->vacuum_count);
405 PG_RETURN_INT64(result);
409 pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
411 Oid relid = PG_GETARG_OID(0);
413 PgStat_StatTabEntry *tabentry;
415 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
418 result = (int64) (tabentry->autovac_vacuum_count);
420 PG_RETURN_INT64(result);
424 pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
426 Oid relid = PG_GETARG_OID(0);
428 PgStat_StatTabEntry *tabentry;
430 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
433 result = (int64) (tabentry->analyze_count);
435 PG_RETURN_INT64(result);
439 pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
441 Oid relid = PG_GETARG_OID(0);
443 PgStat_StatTabEntry *tabentry;
445 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
448 result = (int64) (tabentry->autovac_analyze_count);
450 PG_RETURN_INT64(result);
454 pg_stat_get_function_calls(PG_FUNCTION_ARGS)
456 Oid funcid = PG_GETARG_OID(0);
457 PgStat_StatFuncEntry *funcentry;
459 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
461 PG_RETURN_INT64(funcentry->f_numcalls);
465 pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
467 Oid funcid = PG_GETARG_OID(0);
468 PgStat_StatFuncEntry *funcentry;
470 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
472 /* convert counter from microsec to millisec for display */
473 PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
477 pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
479 Oid funcid = PG_GETARG_OID(0);
480 PgStat_StatFuncEntry *funcentry;
482 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
484 /* convert counter from microsec to millisec for display */
485 PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
489 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
491 FuncCallContext *funcctx;
495 /* stuff done only on the first call of the function */
496 if (SRF_IS_FIRSTCALL())
498 /* create a function context for cross-call persistence */
499 funcctx = SRF_FIRSTCALL_INIT();
501 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
503 funcctx->user_fctx = fctx;
506 fctx[1] = pgstat_fetch_stat_numbackends();
509 /* stuff done on every call of the function */
510 funcctx = SRF_PERCALL_SETUP();
511 fctx = funcctx->user_fctx;
516 if (result <= fctx[1])
518 /* do when there is more left to send */
519 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
523 /* do when there is no more left */
524 SRF_RETURN_DONE(funcctx);
529 * Returns command progress information for the named command.
532 pg_stat_get_progress_info(PG_FUNCTION_ARGS)
534 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
535 int num_backends = pgstat_fetch_stat_numbackends();
537 char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
538 ProgressCommandType cmdtype;
540 Tuplestorestate *tupstore;
541 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
542 MemoryContext per_query_ctx;
543 MemoryContext oldcontext;
545 /* check to see if caller supports us returning a tuplestore */
546 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
548 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
549 errmsg("set-valued function called in context that cannot accept a set")));
550 if (!(rsinfo->allowedModes & SFRM_Materialize))
552 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
553 errmsg("materialize mode required, but it is not " \
554 "allowed in this context")));
556 /* Build a tuple descriptor for our result type */
557 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
558 elog(ERROR, "return type must be a row type");
560 /* Translate command name into command type code. */
561 if (pg_strcasecmp(cmd, "VACUUM") == 0)
562 cmdtype = PROGRESS_COMMAND_VACUUM;
565 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
566 errmsg("invalid command name: \"%s\"", cmd)));
568 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
569 oldcontext = MemoryContextSwitchTo(per_query_ctx);
571 tupstore = tuplestore_begin_heap(true, false, work_mem);
572 rsinfo->returnMode = SFRM_Materialize;
573 rsinfo->setResult = tupstore;
574 rsinfo->setDesc = tupdesc;
575 MemoryContextSwitchTo(oldcontext);
578 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
580 LocalPgBackendStatus *local_beentry;
581 PgBackendStatus *beentry;
582 Datum values[PG_STAT_GET_PROGRESS_COLS];
583 bool nulls[PG_STAT_GET_PROGRESS_COLS];
586 MemSet(values, 0, sizeof(values));
587 MemSet(nulls, 0, sizeof(nulls));
589 local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
594 beentry = &local_beentry->backendStatus;
597 * Report values for only those backends which are running the given
600 if (!beentry || beentry->st_progress_command != cmdtype)
603 /* Value available to all callers */
604 values[0] = Int32GetDatum(beentry->st_procpid);
605 values[1] = ObjectIdGetDatum(beentry->st_databaseid);
607 /* show rest of the values including relid only to role members */
608 if (has_privs_of_role(GetUserId(), beentry->st_userid))
610 values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
611 for(i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
612 values[i+3] = UInt32GetDatum(beentry->st_progress_param[i]);
617 for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
621 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
624 /* clean up and return the tuplestore */
625 tuplestore_donestoring(tupstore);
631 * Returns activity of PG backends.
634 pg_stat_get_activity(PG_FUNCTION_ARGS)
636 #define PG_STAT_GET_ACTIVITY_COLS 22
637 int num_backends = pgstat_fetch_stat_numbackends();
639 int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
640 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
642 Tuplestorestate *tupstore;
643 MemoryContext per_query_ctx;
644 MemoryContext oldcontext;
646 /* check to see if caller supports us returning a tuplestore */
647 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
649 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
650 errmsg("set-valued function called in context that cannot accept a set")));
651 if (!(rsinfo->allowedModes & SFRM_Materialize))
653 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
654 errmsg("materialize mode required, but it is not " \
655 "allowed in this context")));
657 /* Build a tuple descriptor for our result type */
658 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
659 elog(ERROR, "return type must be a row type");
661 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
662 oldcontext = MemoryContextSwitchTo(per_query_ctx);
664 tupstore = tuplestore_begin_heap(true, false, work_mem);
665 rsinfo->returnMode = SFRM_Materialize;
666 rsinfo->setResult = tupstore;
667 rsinfo->setDesc = tupdesc;
669 MemoryContextSwitchTo(oldcontext);
672 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
675 Datum values[PG_STAT_GET_ACTIVITY_COLS];
676 bool nulls[PG_STAT_GET_ACTIVITY_COLS];
677 LocalPgBackendStatus *local_beentry;
678 PgBackendStatus *beentry;
680 MemSet(values, 0, sizeof(values));
681 MemSet(nulls, 0, sizeof(nulls));
685 /* Skip any which are not the one we're looking for. */
686 PgBackendStatus *be = pgstat_fetch_stat_beentry(curr_backend);
688 if (!be || be->st_procpid != pid)
693 /* Get the next one in the list */
694 local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
698 beentry = &local_beentry->backendStatus;
703 for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
707 values[5] = CStringGetTextDatum("<backend information not available>");
709 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
713 /* Values available to all callers */
714 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
715 values[1] = Int32GetDatum(beentry->st_procpid);
716 values[2] = ObjectIdGetDatum(beentry->st_userid);
717 if (beentry->st_appname)
718 values[3] = CStringGetTextDatum(beentry->st_appname);
722 if (TransactionIdIsValid(local_beentry->backend_xid))
723 values[14] = TransactionIdGetDatum(local_beentry->backend_xid);
727 if (TransactionIdIsValid(local_beentry->backend_xmin))
728 values[15] = TransactionIdGetDatum(local_beentry->backend_xmin);
734 values[16] = BoolGetDatum(true); /* ssl */
735 values[17] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
736 values[18] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
737 values[19] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
738 values[20] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
739 values[21] = CStringGetTextDatum(beentry->st_sslstatus->ssl_clientdn);
743 values[16] = BoolGetDatum(false); /* ssl */
744 nulls[17] = nulls[18] = nulls[19] = nulls[20] = nulls[21] = true;
747 /* Values only available to role member */
748 if (has_privs_of_role(GetUserId(), beentry->st_userid))
750 SockAddr zero_clientaddr;
752 switch (beentry->st_state)
755 values[4] = CStringGetTextDatum("idle");
758 values[4] = CStringGetTextDatum("active");
760 case STATE_IDLEINTRANSACTION:
761 values[4] = CStringGetTextDatum("idle in transaction");
764 values[4] = CStringGetTextDatum("fastpath function call");
766 case STATE_IDLEINTRANSACTION_ABORTED:
767 values[4] = CStringGetTextDatum("idle in transaction (aborted)");
770 values[4] = CStringGetTextDatum("disabled");
772 case STATE_UNDEFINED:
777 values[5] = CStringGetTextDatum(beentry->st_activity);
778 values[6] = BoolGetDatum(beentry->st_waiting);
780 if (beentry->st_xact_start_timestamp != 0)
781 values[7] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
785 if (beentry->st_activity_start_timestamp != 0)
786 values[8] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
790 if (beentry->st_proc_start_timestamp != 0)
791 values[9] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
795 if (beentry->st_state_start_timestamp != 0)
796 values[10] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
800 /* A zeroed client addr means we don't know */
801 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
802 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
803 sizeof(zero_clientaddr)) == 0)
811 if (beentry->st_clientaddr.addr.ss_family == AF_INET
813 || beentry->st_clientaddr.addr.ss_family == AF_INET6
817 char remote_host[NI_MAXHOST];
818 char remote_port[NI_MAXSERV];
821 remote_host[0] = '\0';
822 remote_port[0] = '\0';
823 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
824 beentry->st_clientaddr.salen,
825 remote_host, sizeof(remote_host),
826 remote_port, sizeof(remote_port),
827 NI_NUMERICHOST | NI_NUMERICSERV);
830 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
831 values[11] = DirectFunctionCall1(inet_in,
832 CStringGetDatum(remote_host));
833 if (beentry->st_clienthostname &&
834 beentry->st_clienthostname[0])
835 values[12] = CStringGetTextDatum(beentry->st_clienthostname);
838 values[13] = Int32GetDatum(atoi(remote_port));
847 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
850 * Unix sockets always reports NULL for host and -1 for
851 * port, so it's possible to tell the difference to
852 * connections we have no permissions to view, or with
857 values[13] = DatumGetInt32(-1);
861 /* Unknown address type, should never happen */
870 /* No permissions to view data about this session */
871 values[5] = CStringGetTextDatum("<insufficient privilege>");
883 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
885 /* If only a single backend was requested, and we found it, break. */
890 /* clean up and return the tuplestore */
891 tuplestore_donestoring(tupstore);
898 pg_backend_pid(PG_FUNCTION_ARGS)
900 PG_RETURN_INT32(MyProcPid);
905 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
907 int32 beid = PG_GETARG_INT32(0);
908 PgBackendStatus *beentry;
910 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
913 PG_RETURN_INT32(beentry->st_procpid);
918 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
920 int32 beid = PG_GETARG_INT32(0);
921 PgBackendStatus *beentry;
923 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
926 PG_RETURN_OID(beentry->st_databaseid);
931 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
933 int32 beid = PG_GETARG_INT32(0);
934 PgBackendStatus *beentry;
936 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
939 PG_RETURN_OID(beentry->st_userid);
944 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
946 int32 beid = PG_GETARG_INT32(0);
947 PgBackendStatus *beentry;
948 const char *activity;
950 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
951 activity = "<backend information not available>";
952 else if (!has_privs_of_role(GetUserId(), beentry->st_userid))
953 activity = "<insufficient privilege>";
954 else if (*(beentry->st_activity) == '\0')
955 activity = "<command string not enabled>";
957 activity = beentry->st_activity;
959 PG_RETURN_TEXT_P(cstring_to_text(activity));
964 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
966 int32 beid = PG_GETARG_INT32(0);
968 PgBackendStatus *beentry;
970 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
973 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
976 result = beentry->st_waiting;
978 PG_RETURN_BOOL(result);
983 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
985 int32 beid = PG_GETARG_INT32(0);
987 PgBackendStatus *beentry;
989 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
992 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
995 result = beentry->st_activity_start_timestamp;
998 * No time recorded for start of current query -- this is the case if the
999 * user hasn't enabled query-level stats collection.
1004 PG_RETURN_TIMESTAMPTZ(result);
1009 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
1011 int32 beid = PG_GETARG_INT32(0);
1013 PgBackendStatus *beentry;
1015 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1018 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1021 result = beentry->st_xact_start_timestamp;
1023 if (result == 0) /* not in a transaction */
1026 PG_RETURN_TIMESTAMPTZ(result);
1031 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
1033 int32 beid = PG_GETARG_INT32(0);
1035 PgBackendStatus *beentry;
1037 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1040 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1043 result = beentry->st_proc_start_timestamp;
1045 if (result == 0) /* probably can't happen? */
1048 PG_RETURN_TIMESTAMPTZ(result);
1053 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
1055 int32 beid = PG_GETARG_INT32(0);
1056 PgBackendStatus *beentry;
1057 SockAddr zero_clientaddr;
1058 char remote_host[NI_MAXHOST];
1061 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1064 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1067 /* A zeroed client addr means we don't know */
1068 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1069 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1070 sizeof(zero_clientaddr)) == 0)
1073 switch (beentry->st_clientaddr.addr.ss_family)
1084 remote_host[0] = '\0';
1085 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1086 beentry->st_clientaddr.salen,
1087 remote_host, sizeof(remote_host),
1089 NI_NUMERICHOST | NI_NUMERICSERV);
1093 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1095 PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
1096 CStringGetDatum(remote_host)));
1100 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
1102 int32 beid = PG_GETARG_INT32(0);
1103 PgBackendStatus *beentry;
1104 SockAddr zero_clientaddr;
1105 char remote_port[NI_MAXSERV];
1108 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1111 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1114 /* A zeroed client addr means we don't know */
1115 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1116 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1117 sizeof(zero_clientaddr)) == 0)
1120 switch (beentry->st_clientaddr.addr.ss_family)
1128 PG_RETURN_INT32(-1);
1133 remote_port[0] = '\0';
1134 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1135 beentry->st_clientaddr.salen,
1137 remote_port, sizeof(remote_port),
1138 NI_NUMERICHOST | NI_NUMERICSERV);
1142 PG_RETURN_DATUM(DirectFunctionCall1(int4in,
1143 CStringGetDatum(remote_port)));
1148 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
1150 Oid dbid = PG_GETARG_OID(0);
1152 int tot_backends = pgstat_fetch_stat_numbackends();
1156 for (beid = 1; beid <= tot_backends; beid++)
1158 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1160 if (beentry && beentry->st_databaseid == dbid)
1164 PG_RETURN_INT32(result);
1169 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
1171 Oid dbid = PG_GETARG_OID(0);
1173 PgStat_StatDBEntry *dbentry;
1175 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1178 result = (int64) (dbentry->n_xact_commit);
1180 PG_RETURN_INT64(result);
1185 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
1187 Oid dbid = PG_GETARG_OID(0);
1189 PgStat_StatDBEntry *dbentry;
1191 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1194 result = (int64) (dbentry->n_xact_rollback);
1196 PG_RETURN_INT64(result);
1201 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
1203 Oid dbid = PG_GETARG_OID(0);
1205 PgStat_StatDBEntry *dbentry;
1207 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1210 result = (int64) (dbentry->n_blocks_fetched);
1212 PG_RETURN_INT64(result);
1217 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
1219 Oid dbid = PG_GETARG_OID(0);
1221 PgStat_StatDBEntry *dbentry;
1223 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1226 result = (int64) (dbentry->n_blocks_hit);
1228 PG_RETURN_INT64(result);
1233 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
1235 Oid dbid = PG_GETARG_OID(0);
1237 PgStat_StatDBEntry *dbentry;
1239 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1242 result = (int64) (dbentry->n_tuples_returned);
1244 PG_RETURN_INT64(result);
1249 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
1251 Oid dbid = PG_GETARG_OID(0);
1253 PgStat_StatDBEntry *dbentry;
1255 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1258 result = (int64) (dbentry->n_tuples_fetched);
1260 PG_RETURN_INT64(result);
1265 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
1267 Oid dbid = PG_GETARG_OID(0);
1269 PgStat_StatDBEntry *dbentry;
1271 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1274 result = (int64) (dbentry->n_tuples_inserted);
1276 PG_RETURN_INT64(result);
1281 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
1283 Oid dbid = PG_GETARG_OID(0);
1285 PgStat_StatDBEntry *dbentry;
1287 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1290 result = (int64) (dbentry->n_tuples_updated);
1292 PG_RETURN_INT64(result);
1297 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
1299 Oid dbid = PG_GETARG_OID(0);
1301 PgStat_StatDBEntry *dbentry;
1303 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1306 result = (int64) (dbentry->n_tuples_deleted);
1308 PG_RETURN_INT64(result);
1312 pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
1314 Oid dbid = PG_GETARG_OID(0);
1316 PgStat_StatDBEntry *dbentry;
1318 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1321 result = dbentry->stat_reset_timestamp;
1326 PG_RETURN_TIMESTAMPTZ(result);
1330 pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
1332 Oid dbid = PG_GETARG_OID(0);
1334 PgStat_StatDBEntry *dbentry;
1336 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1339 result = dbentry->n_temp_files;
1341 PG_RETURN_INT64(result);
1346 pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
1348 Oid dbid = PG_GETARG_OID(0);
1350 PgStat_StatDBEntry *dbentry;
1352 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1355 result = dbentry->n_temp_bytes;
1357 PG_RETURN_INT64(result);
1361 pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
1363 Oid dbid = PG_GETARG_OID(0);
1365 PgStat_StatDBEntry *dbentry;
1367 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1370 result = (int64) (dbentry->n_conflict_tablespace);
1372 PG_RETURN_INT64(result);
1376 pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
1378 Oid dbid = PG_GETARG_OID(0);
1380 PgStat_StatDBEntry *dbentry;
1382 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1385 result = (int64) (dbentry->n_conflict_lock);
1387 PG_RETURN_INT64(result);
1391 pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS)
1393 Oid dbid = PG_GETARG_OID(0);
1395 PgStat_StatDBEntry *dbentry;
1397 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1400 result = (int64) (dbentry->n_conflict_snapshot);
1402 PG_RETURN_INT64(result);
1406 pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
1408 Oid dbid = PG_GETARG_OID(0);
1410 PgStat_StatDBEntry *dbentry;
1412 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1415 result = (int64) (dbentry->n_conflict_bufferpin);
1417 PG_RETURN_INT64(result);
1421 pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
1423 Oid dbid = PG_GETARG_OID(0);
1425 PgStat_StatDBEntry *dbentry;
1427 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1430 result = (int64) (dbentry->n_conflict_startup_deadlock);
1432 PG_RETURN_INT64(result);
1436 pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
1438 Oid dbid = PG_GETARG_OID(0);
1440 PgStat_StatDBEntry *dbentry;
1442 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1446 dbentry->n_conflict_tablespace +
1447 dbentry->n_conflict_lock +
1448 dbentry->n_conflict_snapshot +
1449 dbentry->n_conflict_bufferpin +
1450 dbentry->n_conflict_startup_deadlock);
1452 PG_RETURN_INT64(result);
1456 pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
1458 Oid dbid = PG_GETARG_OID(0);
1460 PgStat_StatDBEntry *dbentry;
1462 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1465 result = (int64) (dbentry->n_deadlocks);
1467 PG_RETURN_INT64(result);
1471 pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
1473 Oid dbid = PG_GETARG_OID(0);
1475 PgStat_StatDBEntry *dbentry;
1477 /* convert counter from microsec to millisec for display */
1478 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1481 result = ((double) dbentry->n_block_read_time) / 1000.0;
1483 PG_RETURN_FLOAT8(result);
1487 pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
1489 Oid dbid = PG_GETARG_OID(0);
1491 PgStat_StatDBEntry *dbentry;
1493 /* convert counter from microsec to millisec for display */
1494 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1497 result = ((double) dbentry->n_block_write_time) / 1000.0;
1499 PG_RETURN_FLOAT8(result);
1503 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1505 PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1509 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1511 PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1515 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1517 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1521 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1523 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1527 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1529 PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1533 pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
1535 /* time is already in msec, just convert to double for presentation */
1536 PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1540 pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
1542 /* time is already in msec, just convert to double for presentation */
1543 PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1547 pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
1549 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1553 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1555 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1559 pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
1561 PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1565 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1567 PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1571 pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
1573 Oid relid = PG_GETARG_OID(0);
1575 PgStat_TableStatus *tabentry;
1577 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1580 result = (int64) (tabentry->t_counts.t_numscans);
1582 PG_RETURN_INT64(result);
1586 pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
1588 Oid relid = PG_GETARG_OID(0);
1590 PgStat_TableStatus *tabentry;
1592 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1595 result = (int64) (tabentry->t_counts.t_tuples_returned);
1597 PG_RETURN_INT64(result);
1601 pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
1603 Oid relid = PG_GETARG_OID(0);
1605 PgStat_TableStatus *tabentry;
1607 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1610 result = (int64) (tabentry->t_counts.t_tuples_fetched);
1612 PG_RETURN_INT64(result);
1616 pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
1618 Oid relid = PG_GETARG_OID(0);
1620 PgStat_TableStatus *tabentry;
1621 PgStat_TableXactStatus *trans;
1623 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1627 result = tabentry->t_counts.t_tuples_inserted;
1628 /* live subtransactions' counts aren't in t_tuples_inserted yet */
1629 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1630 result += trans->tuples_inserted;
1633 PG_RETURN_INT64(result);
1637 pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
1639 Oid relid = PG_GETARG_OID(0);
1641 PgStat_TableStatus *tabentry;
1642 PgStat_TableXactStatus *trans;
1644 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1648 result = tabentry->t_counts.t_tuples_updated;
1649 /* live subtransactions' counts aren't in t_tuples_updated yet */
1650 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1651 result += trans->tuples_updated;
1654 PG_RETURN_INT64(result);
1658 pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
1660 Oid relid = PG_GETARG_OID(0);
1662 PgStat_TableStatus *tabentry;
1663 PgStat_TableXactStatus *trans;
1665 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1669 result = tabentry->t_counts.t_tuples_deleted;
1670 /* live subtransactions' counts aren't in t_tuples_deleted yet */
1671 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1672 result += trans->tuples_deleted;
1675 PG_RETURN_INT64(result);
1679 pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
1681 Oid relid = PG_GETARG_OID(0);
1683 PgStat_TableStatus *tabentry;
1685 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1688 result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
1690 PG_RETURN_INT64(result);
1694 pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
1696 Oid relid = PG_GETARG_OID(0);
1698 PgStat_TableStatus *tabentry;
1700 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1703 result = (int64) (tabentry->t_counts.t_blocks_fetched);
1705 PG_RETURN_INT64(result);
1709 pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
1711 Oid relid = PG_GETARG_OID(0);
1713 PgStat_TableStatus *tabentry;
1715 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1718 result = (int64) (tabentry->t_counts.t_blocks_hit);
1720 PG_RETURN_INT64(result);
1724 pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
1726 Oid funcid = PG_GETARG_OID(0);
1727 PgStat_BackendFunctionEntry *funcentry;
1729 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1731 PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
1735 pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
1737 Oid funcid = PG_GETARG_OID(0);
1738 PgStat_BackendFunctionEntry *funcentry;
1740 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1742 PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_total_time));
1746 pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
1748 Oid funcid = PG_GETARG_OID(0);
1749 PgStat_BackendFunctionEntry *funcentry;
1751 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1753 PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_self_time));
1757 /* Get the timestamp of the current statistics snapshot */
1759 pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
1761 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
1764 /* Discard the active statistics snapshot */
1766 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1768 pgstat_clear_snapshot();
1774 /* Reset all counters for the current database */
1776 pg_stat_reset(PG_FUNCTION_ARGS)
1778 pgstat_reset_counters();
1783 /* Reset some shared cluster-wide counters */
1785 pg_stat_reset_shared(PG_FUNCTION_ARGS)
1787 char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1789 pgstat_reset_shared_counters(target);
1794 /* Reset a single counter in the current database */
1796 pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
1798 Oid taboid = PG_GETARG_OID(0);
1800 pgstat_reset_single_counter(taboid, RESET_TABLE);
1806 pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
1808 Oid funcoid = PG_GETARG_OID(0);
1810 pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
1816 pg_stat_get_archiver(PG_FUNCTION_ARGS)
1821 PgStat_ArchiverStats *archiver_stats;
1823 /* Initialise values and NULL flags arrays */
1824 MemSet(values, 0, sizeof(values));
1825 MemSet(nulls, 0, sizeof(nulls));
1827 /* Initialise attributes information in the tuple descriptor */
1828 tupdesc = CreateTemplateTupleDesc(7, false);
1829 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
1831 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
1833 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
1834 TIMESTAMPTZOID, -1, 0);
1835 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
1837 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
1839 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
1840 TIMESTAMPTZOID, -1, 0);
1841 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
1842 TIMESTAMPTZOID, -1, 0);
1844 BlessTupleDesc(tupdesc);
1846 /* Get statistics about the archiver process */
1847 archiver_stats = pgstat_fetch_stat_archiver();
1849 /* Fill values and NULLs */
1850 values[0] = Int64GetDatum(archiver_stats->archived_count);
1851 if (*(archiver_stats->last_archived_wal) == '\0')
1854 values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
1856 if (archiver_stats->last_archived_timestamp == 0)
1859 values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
1861 values[3] = Int64GetDatum(archiver_stats->failed_count);
1862 if (*(archiver_stats->last_failed_wal) == '\0')
1865 values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
1867 if (archiver_stats->last_failed_timestamp == 0)
1870 values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
1872 if (archiver_stats->stat_reset_timestamp == 0)
1875 values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
1877 /* Returns the record as Datum */
1878 PG_RETURN_DATUM(HeapTupleGetDatum(
1879 heap_form_tuple(tupdesc, values, nulls)));