1 /*-------------------------------------------------------------------------
4 * Functions for accessing the statistics collector data
6 * Portions Copyright (c) 1996-2015, 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);
68 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
70 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
71 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
74 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
75 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
77 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
78 extern Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS);
79 extern Datum pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS);
80 extern Datum pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS);
81 extern Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS);
82 extern Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS);
83 extern Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS);
84 extern Datum pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS);
85 extern Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS);
86 extern Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS);
87 extern Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS);
88 extern Datum pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS);
89 extern Datum pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS);
91 extern Datum pg_stat_get_archiver(PG_FUNCTION_ARGS);
93 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
94 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
95 extern Datum pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS);
96 extern Datum pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS);
97 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
98 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
99 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
100 extern Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS);
101 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
102 extern Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS);
103 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
105 extern Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS);
106 extern Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS);
107 extern Datum pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS);
108 extern Datum pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS);
109 extern Datum pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS);
110 extern Datum pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS);
111 extern Datum pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS);
112 extern Datum pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS);
113 extern Datum pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS);
115 extern Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS);
116 extern Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS);
117 extern Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS);
119 extern Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS);
120 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
121 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
122 extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
123 extern Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS);
124 extern Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS);
126 /* Global bgwriter statistics, from bgwriter.c */
127 extern PgStat_MsgBgWriter bgwriterStats;
130 pg_stat_get_numscans(PG_FUNCTION_ARGS)
132 Oid relid = PG_GETARG_OID(0);
134 PgStat_StatTabEntry *tabentry;
136 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
139 result = (int64) (tabentry->numscans);
141 PG_RETURN_INT64(result);
146 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
148 Oid relid = PG_GETARG_OID(0);
150 PgStat_StatTabEntry *tabentry;
152 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
155 result = (int64) (tabentry->tuples_returned);
157 PG_RETURN_INT64(result);
162 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
164 Oid relid = PG_GETARG_OID(0);
166 PgStat_StatTabEntry *tabentry;
168 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
171 result = (int64) (tabentry->tuples_fetched);
173 PG_RETURN_INT64(result);
178 pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
180 Oid relid = PG_GETARG_OID(0);
182 PgStat_StatTabEntry *tabentry;
184 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
187 result = (int64) (tabentry->tuples_inserted);
189 PG_RETURN_INT64(result);
194 pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
196 Oid relid = PG_GETARG_OID(0);
198 PgStat_StatTabEntry *tabentry;
200 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
203 result = (int64) (tabentry->tuples_updated);
205 PG_RETURN_INT64(result);
210 pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
212 Oid relid = PG_GETARG_OID(0);
214 PgStat_StatTabEntry *tabentry;
216 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
219 result = (int64) (tabentry->tuples_deleted);
221 PG_RETURN_INT64(result);
226 pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
228 Oid relid = PG_GETARG_OID(0);
230 PgStat_StatTabEntry *tabentry;
232 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
235 result = (int64) (tabentry->tuples_hot_updated);
237 PG_RETURN_INT64(result);
242 pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
244 Oid relid = PG_GETARG_OID(0);
246 PgStat_StatTabEntry *tabentry;
248 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
251 result = (int64) (tabentry->n_live_tuples);
253 PG_RETURN_INT64(result);
258 pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
260 Oid relid = PG_GETARG_OID(0);
262 PgStat_StatTabEntry *tabentry;
264 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
267 result = (int64) (tabentry->n_dead_tuples);
269 PG_RETURN_INT64(result);
274 pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
276 Oid relid = PG_GETARG_OID(0);
278 PgStat_StatTabEntry *tabentry;
280 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
283 result = (int64) (tabentry->changes_since_analyze);
285 PG_RETURN_INT64(result);
290 pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
292 Oid relid = PG_GETARG_OID(0);
294 PgStat_StatTabEntry *tabentry;
296 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
299 result = (int64) (tabentry->blocks_fetched);
301 PG_RETURN_INT64(result);
306 pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
308 Oid relid = PG_GETARG_OID(0);
310 PgStat_StatTabEntry *tabentry;
312 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
315 result = (int64) (tabentry->blocks_hit);
317 PG_RETURN_INT64(result);
321 pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
323 Oid relid = PG_GETARG_OID(0);
325 PgStat_StatTabEntry *tabentry;
327 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
330 result = tabentry->vacuum_timestamp;
335 PG_RETURN_TIMESTAMPTZ(result);
339 pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
341 Oid relid = PG_GETARG_OID(0);
343 PgStat_StatTabEntry *tabentry;
345 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
348 result = tabentry->autovac_vacuum_timestamp;
353 PG_RETURN_TIMESTAMPTZ(result);
357 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
359 Oid relid = PG_GETARG_OID(0);
361 PgStat_StatTabEntry *tabentry;
363 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
366 result = tabentry->analyze_timestamp;
371 PG_RETURN_TIMESTAMPTZ(result);
375 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
377 Oid relid = PG_GETARG_OID(0);
379 PgStat_StatTabEntry *tabentry;
381 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
384 result = tabentry->autovac_analyze_timestamp;
389 PG_RETURN_TIMESTAMPTZ(result);
393 pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
395 Oid relid = PG_GETARG_OID(0);
397 PgStat_StatTabEntry *tabentry;
399 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
402 result = (int64) (tabentry->vacuum_count);
404 PG_RETURN_INT64(result);
408 pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
410 Oid relid = PG_GETARG_OID(0);
412 PgStat_StatTabEntry *tabentry;
414 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
417 result = (int64) (tabentry->autovac_vacuum_count);
419 PG_RETURN_INT64(result);
423 pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
425 Oid relid = PG_GETARG_OID(0);
427 PgStat_StatTabEntry *tabentry;
429 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
432 result = (int64) (tabentry->analyze_count);
434 PG_RETURN_INT64(result);
438 pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
440 Oid relid = PG_GETARG_OID(0);
442 PgStat_StatTabEntry *tabentry;
444 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
447 result = (int64) (tabentry->autovac_analyze_count);
449 PG_RETURN_INT64(result);
453 pg_stat_get_function_calls(PG_FUNCTION_ARGS)
455 Oid funcid = PG_GETARG_OID(0);
456 PgStat_StatFuncEntry *funcentry;
458 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
460 PG_RETURN_INT64(funcentry->f_numcalls);
464 pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
466 Oid funcid = PG_GETARG_OID(0);
467 PgStat_StatFuncEntry *funcentry;
469 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
471 /* convert counter from microsec to millisec for display */
472 PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
476 pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
478 Oid funcid = PG_GETARG_OID(0);
479 PgStat_StatFuncEntry *funcentry;
481 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
483 /* convert counter from microsec to millisec for display */
484 PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
488 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
490 FuncCallContext *funcctx;
494 /* stuff done only on the first call of the function */
495 if (SRF_IS_FIRSTCALL())
497 /* create a function context for cross-call persistence */
498 funcctx = SRF_FIRSTCALL_INIT();
500 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
502 funcctx->user_fctx = fctx;
505 fctx[1] = pgstat_fetch_stat_numbackends();
508 /* stuff done on every call of the function */
509 funcctx = SRF_PERCALL_SETUP();
510 fctx = funcctx->user_fctx;
515 if (result <= fctx[1])
517 /* do when there is more left to send */
518 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
522 /* do when there is no more left */
523 SRF_RETURN_DONE(funcctx);
528 * Returns activity of PG backends.
531 pg_stat_get_activity(PG_FUNCTION_ARGS)
533 #define PG_STAT_GET_ACTIVITY_COLS 22
534 int num_backends = pgstat_fetch_stat_numbackends();
536 int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
537 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
539 Tuplestorestate *tupstore;
540 MemoryContext per_query_ctx;
541 MemoryContext oldcontext;
543 /* check to see if caller supports us returning a tuplestore */
544 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
546 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
547 errmsg("set-valued function called in context that cannot accept a set")));
548 if (!(rsinfo->allowedModes & SFRM_Materialize))
550 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
551 errmsg("materialize mode required, but it is not " \
552 "allowed in this context")));
554 /* Build a tuple descriptor for our result type */
555 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
556 elog(ERROR, "return type must be a row type");
558 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
559 oldcontext = MemoryContextSwitchTo(per_query_ctx);
561 tupstore = tuplestore_begin_heap(true, false, work_mem);
562 rsinfo->returnMode = SFRM_Materialize;
563 rsinfo->setResult = tupstore;
564 rsinfo->setDesc = tupdesc;
566 MemoryContextSwitchTo(oldcontext);
569 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
572 Datum values[PG_STAT_GET_ACTIVITY_COLS];
573 bool nulls[PG_STAT_GET_ACTIVITY_COLS];
574 LocalPgBackendStatus *local_beentry;
575 PgBackendStatus *beentry;
577 MemSet(values, 0, sizeof(values));
578 MemSet(nulls, 0, sizeof(nulls));
582 /* Skip any which are not the one we're looking for. */
583 PgBackendStatus *be = pgstat_fetch_stat_beentry(curr_backend);
585 if (!be || be->st_procpid != pid)
590 /* Get the next one in the list */
591 local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
595 beentry = &local_beentry->backendStatus;
600 for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
604 values[5] = CStringGetTextDatum("<backend information not available>");
606 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
610 /* Values available to all callers */
611 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
612 values[1] = Int32GetDatum(beentry->st_procpid);
613 values[2] = ObjectIdGetDatum(beentry->st_userid);
614 if (beentry->st_appname)
615 values[3] = CStringGetTextDatum(beentry->st_appname);
619 if (TransactionIdIsValid(local_beentry->backend_xid))
620 values[14] = TransactionIdGetDatum(local_beentry->backend_xid);
624 if (TransactionIdIsValid(local_beentry->backend_xmin))
625 values[15] = TransactionIdGetDatum(local_beentry->backend_xmin);
631 values[16] = BoolGetDatum(true); /* ssl */
632 values[17] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
633 values[18] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
634 values[19] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
635 values[20] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
636 values[21] = CStringGetTextDatum(beentry->st_sslstatus->ssl_clientdn);
640 values[16] = BoolGetDatum(false); /* ssl */
641 nulls[17] = nulls[18] = nulls[19] = nulls[20] = nulls[21] = true;
644 /* Values only available to role member */
645 if (has_privs_of_role(GetUserId(), beentry->st_userid))
647 SockAddr zero_clientaddr;
649 switch (beentry->st_state)
652 values[4] = CStringGetTextDatum("idle");
655 values[4] = CStringGetTextDatum("active");
657 case STATE_IDLEINTRANSACTION:
658 values[4] = CStringGetTextDatum("idle in transaction");
661 values[4] = CStringGetTextDatum("fastpath function call");
663 case STATE_IDLEINTRANSACTION_ABORTED:
664 values[4] = CStringGetTextDatum("idle in transaction (aborted)");
667 values[4] = CStringGetTextDatum("disabled");
669 case STATE_UNDEFINED:
674 values[5] = CStringGetTextDatum(beentry->st_activity);
675 values[6] = BoolGetDatum(beentry->st_waiting);
677 if (beentry->st_xact_start_timestamp != 0)
678 values[7] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
682 if (beentry->st_activity_start_timestamp != 0)
683 values[8] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
687 if (beentry->st_proc_start_timestamp != 0)
688 values[9] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
692 if (beentry->st_state_start_timestamp != 0)
693 values[10] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
697 /* A zeroed client addr means we don't know */
698 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
699 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
700 sizeof(zero_clientaddr)) == 0)
708 if (beentry->st_clientaddr.addr.ss_family == AF_INET
710 || beentry->st_clientaddr.addr.ss_family == AF_INET6
714 char remote_host[NI_MAXHOST];
715 char remote_port[NI_MAXSERV];
718 remote_host[0] = '\0';
719 remote_port[0] = '\0';
720 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
721 beentry->st_clientaddr.salen,
722 remote_host, sizeof(remote_host),
723 remote_port, sizeof(remote_port),
724 NI_NUMERICHOST | NI_NUMERICSERV);
727 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
728 values[11] = DirectFunctionCall1(inet_in,
729 CStringGetDatum(remote_host));
730 if (beentry->st_clienthostname &&
731 beentry->st_clienthostname[0])
732 values[12] = CStringGetTextDatum(beentry->st_clienthostname);
735 values[13] = Int32GetDatum(atoi(remote_port));
744 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
747 * Unix sockets always reports NULL for host and -1 for
748 * port, so it's possible to tell the difference to
749 * connections we have no permissions to view, or with
754 values[13] = DatumGetInt32(-1);
758 /* Unknown address type, should never happen */
767 /* No permissions to view data about this session */
768 values[5] = CStringGetTextDatum("<insufficient privilege>");
780 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
782 /* If only a single backend was requested, and we found it, break. */
787 /* clean up and return the tuplestore */
788 tuplestore_donestoring(tupstore);
795 pg_backend_pid(PG_FUNCTION_ARGS)
797 PG_RETURN_INT32(MyProcPid);
802 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
804 int32 beid = PG_GETARG_INT32(0);
805 PgBackendStatus *beentry;
807 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
810 PG_RETURN_INT32(beentry->st_procpid);
815 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
817 int32 beid = PG_GETARG_INT32(0);
818 PgBackendStatus *beentry;
820 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
823 PG_RETURN_OID(beentry->st_databaseid);
828 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
830 int32 beid = PG_GETARG_INT32(0);
831 PgBackendStatus *beentry;
833 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
836 PG_RETURN_OID(beentry->st_userid);
841 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
843 int32 beid = PG_GETARG_INT32(0);
844 PgBackendStatus *beentry;
845 const char *activity;
847 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
848 activity = "<backend information not available>";
849 else if (!has_privs_of_role(GetUserId(), beentry->st_userid))
850 activity = "<insufficient privilege>";
851 else if (*(beentry->st_activity) == '\0')
852 activity = "<command string not enabled>";
854 activity = beentry->st_activity;
856 PG_RETURN_TEXT_P(cstring_to_text(activity));
861 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
863 int32 beid = PG_GETARG_INT32(0);
865 PgBackendStatus *beentry;
867 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
870 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
873 result = beentry->st_waiting;
875 PG_RETURN_BOOL(result);
880 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
882 int32 beid = PG_GETARG_INT32(0);
884 PgBackendStatus *beentry;
886 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
889 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
892 result = beentry->st_activity_start_timestamp;
895 * No time recorded for start of current query -- this is the case if the
896 * user hasn't enabled query-level stats collection.
901 PG_RETURN_TIMESTAMPTZ(result);
906 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
908 int32 beid = PG_GETARG_INT32(0);
910 PgBackendStatus *beentry;
912 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
915 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
918 result = beentry->st_xact_start_timestamp;
920 if (result == 0) /* not in a transaction */
923 PG_RETURN_TIMESTAMPTZ(result);
928 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
930 int32 beid = PG_GETARG_INT32(0);
932 PgBackendStatus *beentry;
934 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
937 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
940 result = beentry->st_proc_start_timestamp;
942 if (result == 0) /* probably can't happen? */
945 PG_RETURN_TIMESTAMPTZ(result);
950 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
952 int32 beid = PG_GETARG_INT32(0);
953 PgBackendStatus *beentry;
954 SockAddr zero_clientaddr;
955 char remote_host[NI_MAXHOST];
958 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
961 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
964 /* A zeroed client addr means we don't know */
965 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
966 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
967 sizeof(zero_clientaddr)) == 0)
970 switch (beentry->st_clientaddr.addr.ss_family)
981 remote_host[0] = '\0';
982 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
983 beentry->st_clientaddr.salen,
984 remote_host, sizeof(remote_host),
986 NI_NUMERICHOST | NI_NUMERICSERV);
990 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
992 PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
993 CStringGetDatum(remote_host)));
997 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
999 int32 beid = PG_GETARG_INT32(0);
1000 PgBackendStatus *beentry;
1001 SockAddr zero_clientaddr;
1002 char remote_port[NI_MAXSERV];
1005 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1008 if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1011 /* A zeroed client addr means we don't know */
1012 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1013 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1014 sizeof(zero_clientaddr)) == 0)
1017 switch (beentry->st_clientaddr.addr.ss_family)
1025 PG_RETURN_INT32(-1);
1030 remote_port[0] = '\0';
1031 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1032 beentry->st_clientaddr.salen,
1034 remote_port, sizeof(remote_port),
1035 NI_NUMERICHOST | NI_NUMERICSERV);
1039 PG_RETURN_DATUM(DirectFunctionCall1(int4in,
1040 CStringGetDatum(remote_port)));
1045 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
1047 Oid dbid = PG_GETARG_OID(0);
1049 int tot_backends = pgstat_fetch_stat_numbackends();
1053 for (beid = 1; beid <= tot_backends; beid++)
1055 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1057 if (beentry && beentry->st_databaseid == dbid)
1061 PG_RETURN_INT32(result);
1066 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
1068 Oid dbid = PG_GETARG_OID(0);
1070 PgStat_StatDBEntry *dbentry;
1072 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1075 result = (int64) (dbentry->n_xact_commit);
1077 PG_RETURN_INT64(result);
1082 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
1084 Oid dbid = PG_GETARG_OID(0);
1086 PgStat_StatDBEntry *dbentry;
1088 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1091 result = (int64) (dbentry->n_xact_rollback);
1093 PG_RETURN_INT64(result);
1098 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
1100 Oid dbid = PG_GETARG_OID(0);
1102 PgStat_StatDBEntry *dbentry;
1104 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1107 result = (int64) (dbentry->n_blocks_fetched);
1109 PG_RETURN_INT64(result);
1114 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
1116 Oid dbid = PG_GETARG_OID(0);
1118 PgStat_StatDBEntry *dbentry;
1120 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1123 result = (int64) (dbentry->n_blocks_hit);
1125 PG_RETURN_INT64(result);
1130 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
1132 Oid dbid = PG_GETARG_OID(0);
1134 PgStat_StatDBEntry *dbentry;
1136 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1139 result = (int64) (dbentry->n_tuples_returned);
1141 PG_RETURN_INT64(result);
1146 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
1148 Oid dbid = PG_GETARG_OID(0);
1150 PgStat_StatDBEntry *dbentry;
1152 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1155 result = (int64) (dbentry->n_tuples_fetched);
1157 PG_RETURN_INT64(result);
1162 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
1164 Oid dbid = PG_GETARG_OID(0);
1166 PgStat_StatDBEntry *dbentry;
1168 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1171 result = (int64) (dbentry->n_tuples_inserted);
1173 PG_RETURN_INT64(result);
1178 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
1180 Oid dbid = PG_GETARG_OID(0);
1182 PgStat_StatDBEntry *dbentry;
1184 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1187 result = (int64) (dbentry->n_tuples_updated);
1189 PG_RETURN_INT64(result);
1194 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
1196 Oid dbid = PG_GETARG_OID(0);
1198 PgStat_StatDBEntry *dbentry;
1200 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1203 result = (int64) (dbentry->n_tuples_deleted);
1205 PG_RETURN_INT64(result);
1209 pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
1211 Oid dbid = PG_GETARG_OID(0);
1213 PgStat_StatDBEntry *dbentry;
1215 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1218 result = dbentry->stat_reset_timestamp;
1223 PG_RETURN_TIMESTAMPTZ(result);
1227 pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
1229 Oid dbid = PG_GETARG_OID(0);
1231 PgStat_StatDBEntry *dbentry;
1233 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1236 result = dbentry->n_temp_files;
1238 PG_RETURN_INT64(result);
1243 pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
1245 Oid dbid = PG_GETARG_OID(0);
1247 PgStat_StatDBEntry *dbentry;
1249 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1252 result = dbentry->n_temp_bytes;
1254 PG_RETURN_INT64(result);
1258 pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
1260 Oid dbid = PG_GETARG_OID(0);
1262 PgStat_StatDBEntry *dbentry;
1264 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1267 result = (int64) (dbentry->n_conflict_tablespace);
1269 PG_RETURN_INT64(result);
1273 pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
1275 Oid dbid = PG_GETARG_OID(0);
1277 PgStat_StatDBEntry *dbentry;
1279 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1282 result = (int64) (dbentry->n_conflict_lock);
1284 PG_RETURN_INT64(result);
1288 pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS)
1290 Oid dbid = PG_GETARG_OID(0);
1292 PgStat_StatDBEntry *dbentry;
1294 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1297 result = (int64) (dbentry->n_conflict_snapshot);
1299 PG_RETURN_INT64(result);
1303 pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
1305 Oid dbid = PG_GETARG_OID(0);
1307 PgStat_StatDBEntry *dbentry;
1309 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1312 result = (int64) (dbentry->n_conflict_bufferpin);
1314 PG_RETURN_INT64(result);
1318 pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
1320 Oid dbid = PG_GETARG_OID(0);
1322 PgStat_StatDBEntry *dbentry;
1324 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1327 result = (int64) (dbentry->n_conflict_startup_deadlock);
1329 PG_RETURN_INT64(result);
1333 pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
1335 Oid dbid = PG_GETARG_OID(0);
1337 PgStat_StatDBEntry *dbentry;
1339 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1343 dbentry->n_conflict_tablespace +
1344 dbentry->n_conflict_lock +
1345 dbentry->n_conflict_snapshot +
1346 dbentry->n_conflict_bufferpin +
1347 dbentry->n_conflict_startup_deadlock);
1349 PG_RETURN_INT64(result);
1353 pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
1355 Oid dbid = PG_GETARG_OID(0);
1357 PgStat_StatDBEntry *dbentry;
1359 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1362 result = (int64) (dbentry->n_deadlocks);
1364 PG_RETURN_INT64(result);
1368 pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
1370 Oid dbid = PG_GETARG_OID(0);
1372 PgStat_StatDBEntry *dbentry;
1374 /* convert counter from microsec to millisec for display */
1375 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1378 result = ((double) dbentry->n_block_read_time) / 1000.0;
1380 PG_RETURN_FLOAT8(result);
1384 pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
1386 Oid dbid = PG_GETARG_OID(0);
1388 PgStat_StatDBEntry *dbentry;
1390 /* convert counter from microsec to millisec for display */
1391 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1394 result = ((double) dbentry->n_block_write_time) / 1000.0;
1396 PG_RETURN_FLOAT8(result);
1400 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1402 PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1406 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1408 PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1412 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1414 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1418 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1420 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1424 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1426 PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1430 pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
1432 /* time is already in msec, just convert to double for presentation */
1433 PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1437 pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
1439 /* time is already in msec, just convert to double for presentation */
1440 PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1444 pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
1446 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1450 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1452 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1456 pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
1458 PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1462 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1464 PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1468 pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
1470 Oid relid = PG_GETARG_OID(0);
1472 PgStat_TableStatus *tabentry;
1474 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1477 result = (int64) (tabentry->t_counts.t_numscans);
1479 PG_RETURN_INT64(result);
1483 pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
1485 Oid relid = PG_GETARG_OID(0);
1487 PgStat_TableStatus *tabentry;
1489 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1492 result = (int64) (tabentry->t_counts.t_tuples_returned);
1494 PG_RETURN_INT64(result);
1498 pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
1500 Oid relid = PG_GETARG_OID(0);
1502 PgStat_TableStatus *tabentry;
1504 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1507 result = (int64) (tabentry->t_counts.t_tuples_fetched);
1509 PG_RETURN_INT64(result);
1513 pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
1515 Oid relid = PG_GETARG_OID(0);
1517 PgStat_TableStatus *tabentry;
1518 PgStat_TableXactStatus *trans;
1520 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1524 result = tabentry->t_counts.t_tuples_inserted;
1525 /* live subtransactions' counts aren't in t_tuples_inserted yet */
1526 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1527 result += trans->tuples_inserted;
1530 PG_RETURN_INT64(result);
1534 pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
1536 Oid relid = PG_GETARG_OID(0);
1538 PgStat_TableStatus *tabentry;
1539 PgStat_TableXactStatus *trans;
1541 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1545 result = tabentry->t_counts.t_tuples_updated;
1546 /* live subtransactions' counts aren't in t_tuples_updated yet */
1547 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1548 result += trans->tuples_updated;
1551 PG_RETURN_INT64(result);
1555 pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
1557 Oid relid = PG_GETARG_OID(0);
1559 PgStat_TableStatus *tabentry;
1560 PgStat_TableXactStatus *trans;
1562 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1566 result = tabentry->t_counts.t_tuples_deleted;
1567 /* live subtransactions' counts aren't in t_tuples_deleted yet */
1568 for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1569 result += trans->tuples_deleted;
1572 PG_RETURN_INT64(result);
1576 pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
1578 Oid relid = PG_GETARG_OID(0);
1580 PgStat_TableStatus *tabentry;
1582 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1585 result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
1587 PG_RETURN_INT64(result);
1591 pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
1593 Oid relid = PG_GETARG_OID(0);
1595 PgStat_TableStatus *tabentry;
1597 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1600 result = (int64) (tabentry->t_counts.t_blocks_fetched);
1602 PG_RETURN_INT64(result);
1606 pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
1608 Oid relid = PG_GETARG_OID(0);
1610 PgStat_TableStatus *tabentry;
1612 if ((tabentry = find_tabstat_entry(relid)) == NULL)
1615 result = (int64) (tabentry->t_counts.t_blocks_hit);
1617 PG_RETURN_INT64(result);
1621 pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
1623 Oid funcid = PG_GETARG_OID(0);
1624 PgStat_BackendFunctionEntry *funcentry;
1626 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1628 PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
1632 pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
1634 Oid funcid = PG_GETARG_OID(0);
1635 PgStat_BackendFunctionEntry *funcentry;
1637 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1639 PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_total_time));
1643 pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
1645 Oid funcid = PG_GETARG_OID(0);
1646 PgStat_BackendFunctionEntry *funcentry;
1648 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1650 PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->f_counts.f_self_time));
1654 /* Get the timestamp of the current statistics snapshot */
1656 pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
1658 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
1661 /* Discard the active statistics snapshot */
1663 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1665 pgstat_clear_snapshot();
1671 /* Reset all counters for the current database */
1673 pg_stat_reset(PG_FUNCTION_ARGS)
1675 pgstat_reset_counters();
1680 /* Reset some shared cluster-wide counters */
1682 pg_stat_reset_shared(PG_FUNCTION_ARGS)
1684 char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1686 pgstat_reset_shared_counters(target);
1691 /* Reset a single counter in the current database */
1693 pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
1695 Oid taboid = PG_GETARG_OID(0);
1697 pgstat_reset_single_counter(taboid, RESET_TABLE);
1703 pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
1705 Oid funcoid = PG_GETARG_OID(0);
1707 pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
1713 pg_stat_get_archiver(PG_FUNCTION_ARGS)
1718 PgStat_ArchiverStats *archiver_stats;
1720 /* Initialise values and NULL flags arrays */
1721 MemSet(values, 0, sizeof(values));
1722 MemSet(nulls, 0, sizeof(nulls));
1724 /* Initialise attributes information in the tuple descriptor */
1725 tupdesc = CreateTemplateTupleDesc(7, false);
1726 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
1728 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
1730 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
1731 TIMESTAMPTZOID, -1, 0);
1732 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
1734 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
1736 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
1737 TIMESTAMPTZOID, -1, 0);
1738 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
1739 TIMESTAMPTZOID, -1, 0);
1741 BlessTupleDesc(tupdesc);
1743 /* Get statistics about the archiver process */
1744 archiver_stats = pgstat_fetch_stat_archiver();
1746 /* Fill values and NULLs */
1747 values[0] = Int64GetDatum(archiver_stats->archived_count);
1748 if (*(archiver_stats->last_archived_wal) == '\0')
1751 values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
1753 if (archiver_stats->last_archived_timestamp == 0)
1756 values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
1758 values[3] = Int64GetDatum(archiver_stats->failed_count);
1759 if (*(archiver_stats->last_failed_wal) == '\0')
1762 values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
1764 if (archiver_stats->last_failed_timestamp == 0)
1767 values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
1769 if (archiver_stats->stat_reset_timestamp == 0)
1772 values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
1774 /* Returns the record as Datum */
1775 PG_RETURN_DATUM(HeapTupleGetDatum(
1776 heap_form_tuple(tupdesc, values, nulls)));