1 /*-------------------------------------------------------------------------
4 * Functions for accessing the statistics collector data
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.60 2010/02/26 02:01:09 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "miscadmin.h"
20 #include "catalog/pg_type.h"
21 #include "utils/builtins.h"
22 #include "utils/inet.h"
25 /* bogus ... these externs should be in a header file */
26 extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
27 extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
28 extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
29 extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
30 extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
31 extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
32 extern Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS);
33 extern Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS);
34 extern Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS);
35 extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
36 extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
37 extern Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS);
38 extern Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS);
39 extern Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS);
40 extern Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS);
42 extern Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_function_time(PG_FUNCTION_ARGS);
44 extern Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS);
46 extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_activity(PG_FUNCTION_ARGS);
48 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
49 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
50 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
51 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
52 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
53 extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS);
54 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
55 extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
56 extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
57 extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
58 extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
60 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
61 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
62 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
63 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
64 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
65 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
66 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
67 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
68 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
71 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
74 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
75 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
77 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
79 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
80 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
81 extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
82 extern Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS);
83 extern Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS);
85 /* Global bgwriter statistics, from bgwriter.c */
86 extern PgStat_MsgBgWriter bgwriterStats;
89 pg_stat_get_numscans(PG_FUNCTION_ARGS)
91 Oid relid = PG_GETARG_OID(0);
93 PgStat_StatTabEntry *tabentry;
95 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
98 result = (int64) (tabentry->numscans);
100 PG_RETURN_INT64(result);
105 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
107 Oid relid = PG_GETARG_OID(0);
109 PgStat_StatTabEntry *tabentry;
111 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
114 result = (int64) (tabentry->tuples_returned);
116 PG_RETURN_INT64(result);
121 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
123 Oid relid = PG_GETARG_OID(0);
125 PgStat_StatTabEntry *tabentry;
127 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
130 result = (int64) (tabentry->tuples_fetched);
132 PG_RETURN_INT64(result);
137 pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
139 Oid relid = PG_GETARG_OID(0);
141 PgStat_StatTabEntry *tabentry;
143 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
146 result = (int64) (tabentry->tuples_inserted);
148 PG_RETURN_INT64(result);
153 pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
155 Oid relid = PG_GETARG_OID(0);
157 PgStat_StatTabEntry *tabentry;
159 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
162 result = (int64) (tabentry->tuples_updated);
164 PG_RETURN_INT64(result);
169 pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
171 Oid relid = PG_GETARG_OID(0);
173 PgStat_StatTabEntry *tabentry;
175 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
178 result = (int64) (tabentry->tuples_deleted);
180 PG_RETURN_INT64(result);
185 pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
187 Oid relid = PG_GETARG_OID(0);
189 PgStat_StatTabEntry *tabentry;
191 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
194 result = (int64) (tabentry->tuples_hot_updated);
196 PG_RETURN_INT64(result);
201 pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
203 Oid relid = PG_GETARG_OID(0);
205 PgStat_StatTabEntry *tabentry;
207 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
210 result = (int64) (tabentry->n_live_tuples);
212 PG_RETURN_INT64(result);
217 pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
219 Oid relid = PG_GETARG_OID(0);
221 PgStat_StatTabEntry *tabentry;
223 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
226 result = (int64) (tabentry->n_dead_tuples);
228 PG_RETURN_INT64(result);
233 pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
235 Oid relid = PG_GETARG_OID(0);
237 PgStat_StatTabEntry *tabentry;
239 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
242 result = (int64) (tabentry->blocks_fetched);
244 PG_RETURN_INT64(result);
249 pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
251 Oid relid = PG_GETARG_OID(0);
253 PgStat_StatTabEntry *tabentry;
255 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
258 result = (int64) (tabentry->blocks_hit);
260 PG_RETURN_INT64(result);
264 pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
266 Oid relid = PG_GETARG_OID(0);
268 PgStat_StatTabEntry *tabentry;
270 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
273 result = tabentry->vacuum_timestamp;
278 PG_RETURN_TIMESTAMPTZ(result);
282 pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
284 Oid relid = PG_GETARG_OID(0);
286 PgStat_StatTabEntry *tabentry;
288 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
291 result = tabentry->autovac_vacuum_timestamp;
296 PG_RETURN_TIMESTAMPTZ(result);
300 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
302 Oid relid = PG_GETARG_OID(0);
304 PgStat_StatTabEntry *tabentry;
306 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
309 result = tabentry->analyze_timestamp;
314 PG_RETURN_TIMESTAMPTZ(result);
318 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
320 Oid relid = PG_GETARG_OID(0);
322 PgStat_StatTabEntry *tabentry;
324 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
327 result = tabentry->autovac_analyze_timestamp;
332 PG_RETURN_TIMESTAMPTZ(result);
336 pg_stat_get_function_calls(PG_FUNCTION_ARGS)
338 Oid funcid = PG_GETARG_OID(0);
339 PgStat_StatFuncEntry *funcentry;
341 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
343 PG_RETURN_INT64(funcentry->f_numcalls);
347 pg_stat_get_function_time(PG_FUNCTION_ARGS)
349 Oid funcid = PG_GETARG_OID(0);
350 PgStat_StatFuncEntry *funcentry;
352 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
354 PG_RETURN_INT64(funcentry->f_time);
358 pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
360 Oid funcid = PG_GETARG_OID(0);
361 PgStat_StatFuncEntry *funcentry;
363 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
365 PG_RETURN_INT64(funcentry->f_time_self);
369 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
371 FuncCallContext *funcctx;
375 /* stuff done only on the first call of the function */
376 if (SRF_IS_FIRSTCALL())
378 /* create a function context for cross-call persistence */
379 funcctx = SRF_FIRSTCALL_INIT();
381 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
383 funcctx->user_fctx = fctx;
386 fctx[1] = pgstat_fetch_stat_numbackends();
389 /* stuff done on every call of the function */
390 funcctx = SRF_PERCALL_SETUP();
391 fctx = funcctx->user_fctx;
396 if (result <= fctx[1])
398 /* do when there is more left to send */
399 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
403 /* do when there is no more left */
404 SRF_RETURN_DONE(funcctx);
409 pg_stat_get_activity(PG_FUNCTION_ARGS)
411 FuncCallContext *funcctx;
413 if (SRF_IS_FIRSTCALL())
415 MemoryContext oldcontext;
418 funcctx = SRF_FIRSTCALL_INIT();
420 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
422 tupdesc = CreateTemplateTupleDesc(11, false);
423 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
424 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
425 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
426 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "application_name", TEXTOID, -1, 0);
427 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "current_query", TEXTOID, -1, 0);
428 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "waiting", BOOLOID, -1, 0);
429 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "act_start", TIMESTAMPTZOID, -1, 0);
430 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "query_start", TIMESTAMPTZOID, -1, 0);
431 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "backend_start", TIMESTAMPTZOID, -1, 0);
432 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_addr", INETOID, -1, 0);
433 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_port", INT4OID, -1, 0);
435 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
437 funcctx->user_fctx = palloc0(sizeof(int));
440 /* Get all backends */
441 funcctx->max_calls = pgstat_fetch_stat_numbackends();
446 * Get one backend - locate by pid.
448 * We lookup the backend early, so we can return zero rows if it
449 * doesn't exist, instead of returning a single row full of NULLs.
451 int pid = PG_GETARG_INT32(0);
453 int n = pgstat_fetch_stat_numbackends();
455 for (i = 1; i <= n; i++)
457 PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
461 if (be->st_procpid == pid)
463 *(int *) (funcctx->user_fctx) = i;
469 if (*(int *) (funcctx->user_fctx) == 0)
470 /* Pid not found, return zero rows */
471 funcctx->max_calls = 0;
473 funcctx->max_calls = 1;
476 MemoryContextSwitchTo(oldcontext);
479 /* stuff done on every call of the function */
480 funcctx = SRF_PERCALL_SETUP();
482 if (funcctx->call_cntr < funcctx->max_calls)
488 PgBackendStatus *beentry;
489 SockAddr zero_clientaddr;
491 MemSet(values, 0, sizeof(values));
492 MemSet(nulls, 0, sizeof(nulls));
494 if (*(int *) (funcctx->user_fctx) > 0)
496 /* Get specific pid slot */
497 beentry = pgstat_fetch_stat_beentry(*(int *) (funcctx->user_fctx));
501 /* Get the next one in the list */
502 beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr + 1); /* 1-based index */
508 for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
512 values[4] = CStringGetTextDatum("<backend information not available>");
514 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
515 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
518 /* Values available to all callers */
519 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
520 values[1] = Int32GetDatum(beentry->st_procpid);
521 values[2] = ObjectIdGetDatum(beentry->st_userid);
522 if (beentry->st_appname)
523 values[3] = CStringGetTextDatum(beentry->st_appname);
527 /* Values only available to same user or superuser */
528 if (superuser() || beentry->st_userid == GetUserId())
530 if (*(beentry->st_activity) == '\0')
532 values[4] = CStringGetTextDatum("<command string not enabled>");
536 values[4] = CStringGetTextDatum(beentry->st_activity);
539 values[5] = BoolGetDatum(beentry->st_waiting);
541 if (beentry->st_xact_start_timestamp != 0)
542 values[6] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
546 if (beentry->st_activity_start_timestamp != 0)
547 values[7] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
551 if (beentry->st_proc_start_timestamp != 0)
552 values[8] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
556 /* A zeroed client addr means we don't know */
557 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
558 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
559 sizeof(zero_clientaddr) == 0))
566 if (beentry->st_clientaddr.addr.ss_family == AF_INET
568 || beentry->st_clientaddr.addr.ss_family == AF_INET6
572 char remote_host[NI_MAXHOST];
573 char remote_port[NI_MAXSERV];
576 remote_host[0] = '\0';
577 remote_port[0] = '\0';
578 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
579 beentry->st_clientaddr.salen,
580 remote_host, sizeof(remote_host),
581 remote_port, sizeof(remote_port),
582 NI_NUMERICHOST | NI_NUMERICSERV);
590 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
591 values[9] = DirectFunctionCall1(inet_in,
592 CStringGetDatum(remote_host));
593 values[10] = Int32GetDatum(atoi(remote_port));
596 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
599 * Unix sockets always reports NULL for host and -1 for
600 * port, so it's possible to tell the difference to
601 * connections we have no permissions to view, or with
605 values[10] = DatumGetInt32(-1);
609 /* Unknown address type, should never happen */
617 /* No permissions to view data about this session */
618 values[4] = CStringGetTextDatum("<insufficient privilege>");
627 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
629 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
634 SRF_RETURN_DONE(funcctx);
640 pg_backend_pid(PG_FUNCTION_ARGS)
642 PG_RETURN_INT32(MyProcPid);
647 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
649 int32 beid = PG_GETARG_INT32(0);
650 PgBackendStatus *beentry;
652 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
655 PG_RETURN_INT32(beentry->st_procpid);
660 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
662 int32 beid = PG_GETARG_INT32(0);
663 PgBackendStatus *beentry;
665 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
668 PG_RETURN_OID(beentry->st_databaseid);
673 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
675 int32 beid = PG_GETARG_INT32(0);
676 PgBackendStatus *beentry;
678 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
681 PG_RETURN_OID(beentry->st_userid);
686 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
688 int32 beid = PG_GETARG_INT32(0);
689 PgBackendStatus *beentry;
690 const char *activity;
692 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
693 activity = "<backend information not available>";
694 else if (!superuser() && beentry->st_userid != GetUserId())
695 activity = "<insufficient privilege>";
696 else if (*(beentry->st_activity) == '\0')
697 activity = "<command string not enabled>";
699 activity = beentry->st_activity;
701 PG_RETURN_TEXT_P(cstring_to_text(activity));
706 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
708 int32 beid = PG_GETARG_INT32(0);
710 PgBackendStatus *beentry;
712 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
715 if (!superuser() && beentry->st_userid != GetUserId())
718 result = beentry->st_waiting;
720 PG_RETURN_BOOL(result);
725 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
727 int32 beid = PG_GETARG_INT32(0);
729 PgBackendStatus *beentry;
731 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
734 if (!superuser() && beentry->st_userid != GetUserId())
737 result = beentry->st_activity_start_timestamp;
740 * No time recorded for start of current query -- this is the case if the
741 * user hasn't enabled query-level stats collection.
746 PG_RETURN_TIMESTAMPTZ(result);
751 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
753 int32 beid = PG_GETARG_INT32(0);
755 PgBackendStatus *beentry;
757 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
760 if (!superuser() && beentry->st_userid != GetUserId())
763 result = beentry->st_xact_start_timestamp;
765 if (result == 0) /* not in a transaction */
768 PG_RETURN_TIMESTAMPTZ(result);
773 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
775 int32 beid = PG_GETARG_INT32(0);
777 PgBackendStatus *beentry;
779 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
782 if (!superuser() && beentry->st_userid != GetUserId())
785 result = beentry->st_proc_start_timestamp;
787 if (result == 0) /* probably can't happen? */
790 PG_RETURN_TIMESTAMPTZ(result);
795 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
797 int32 beid = PG_GETARG_INT32(0);
798 PgBackendStatus *beentry;
799 SockAddr zero_clientaddr;
800 char remote_host[NI_MAXHOST];
803 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
806 if (!superuser() && beentry->st_userid != GetUserId())
809 /* A zeroed client addr means we don't know */
810 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
811 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
812 sizeof(zero_clientaddr) == 0))
815 switch (beentry->st_clientaddr.addr.ss_family)
826 remote_host[0] = '\0';
827 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
828 beentry->st_clientaddr.salen,
829 remote_host, sizeof(remote_host),
831 NI_NUMERICHOST | NI_NUMERICSERV);
835 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
837 PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
838 CStringGetDatum(remote_host)));
842 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
844 int32 beid = PG_GETARG_INT32(0);
845 PgBackendStatus *beentry;
846 SockAddr zero_clientaddr;
847 char remote_port[NI_MAXSERV];
850 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
853 if (!superuser() && beentry->st_userid != GetUserId())
856 /* A zeroed client addr means we don't know */
857 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
858 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
859 sizeof(zero_clientaddr) == 0))
862 switch (beentry->st_clientaddr.addr.ss_family)
875 remote_port[0] = '\0';
876 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
877 beentry->st_clientaddr.salen,
879 remote_port, sizeof(remote_port),
880 NI_NUMERICHOST | NI_NUMERICSERV);
884 PG_RETURN_DATUM(DirectFunctionCall1(int4in,
885 CStringGetDatum(remote_port)));
890 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
892 Oid dbid = PG_GETARG_OID(0);
894 int tot_backends = pgstat_fetch_stat_numbackends();
898 for (beid = 1; beid <= tot_backends; beid++)
900 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
902 if (beentry && beentry->st_databaseid == dbid)
906 PG_RETURN_INT32(result);
911 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
913 Oid dbid = PG_GETARG_OID(0);
915 PgStat_StatDBEntry *dbentry;
917 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
920 result = (int64) (dbentry->n_xact_commit);
922 PG_RETURN_INT64(result);
927 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
929 Oid dbid = PG_GETARG_OID(0);
931 PgStat_StatDBEntry *dbentry;
933 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
936 result = (int64) (dbentry->n_xact_rollback);
938 PG_RETURN_INT64(result);
943 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
945 Oid dbid = PG_GETARG_OID(0);
947 PgStat_StatDBEntry *dbentry;
949 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
952 result = (int64) (dbentry->n_blocks_fetched);
954 PG_RETURN_INT64(result);
959 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
961 Oid dbid = PG_GETARG_OID(0);
963 PgStat_StatDBEntry *dbentry;
965 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
968 result = (int64) (dbentry->n_blocks_hit);
970 PG_RETURN_INT64(result);
975 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
977 Oid dbid = PG_GETARG_OID(0);
979 PgStat_StatDBEntry *dbentry;
981 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
984 result = (int64) (dbentry->n_tuples_returned);
986 PG_RETURN_INT64(result);
991 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
993 Oid dbid = PG_GETARG_OID(0);
995 PgStat_StatDBEntry *dbentry;
997 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1000 result = (int64) (dbentry->n_tuples_fetched);
1002 PG_RETURN_INT64(result);
1007 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
1009 Oid dbid = PG_GETARG_OID(0);
1011 PgStat_StatDBEntry *dbentry;
1013 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1016 result = (int64) (dbentry->n_tuples_inserted);
1018 PG_RETURN_INT64(result);
1023 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
1025 Oid dbid = PG_GETARG_OID(0);
1027 PgStat_StatDBEntry *dbentry;
1029 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1032 result = (int64) (dbentry->n_tuples_updated);
1034 PG_RETURN_INT64(result);
1039 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
1041 Oid dbid = PG_GETARG_OID(0);
1043 PgStat_StatDBEntry *dbentry;
1045 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1048 result = (int64) (dbentry->n_tuples_deleted);
1050 PG_RETURN_INT64(result);
1054 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1056 PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1060 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1062 PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1066 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1068 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1072 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1074 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1078 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1080 PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1084 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1086 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1090 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1092 PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1096 /* Discard the active statistics snapshot */
1098 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1100 pgstat_clear_snapshot();
1106 /* Reset all counters for the current database */
1108 pg_stat_reset(PG_FUNCTION_ARGS)
1110 pgstat_reset_counters();
1115 /* Reset some shared cluster-wide counters */
1117 pg_stat_reset_shared(PG_FUNCTION_ARGS)
1119 char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1121 pgstat_reset_shared_counters(target);
1126 /* Reset a a single counter in the current database */
1128 pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
1130 Oid taboid = PG_GETARG_OID(0);
1132 pgstat_reset_single_counter(taboid, RESET_TABLE);
1138 pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
1140 Oid funcoid = PG_GETARG_OID(0);
1142 pgstat_reset_single_counter(funcoid, RESET_FUNCTION);