1 /*-------------------------------------------------------------------------
4 * Functions for accessing the statistics collector data
6 * Portions Copyright (c) 1996-2008, 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.50 2008/05/07 14:41:55 mha Exp $
13 *-------------------------------------------------------------------------
18 #include "miscadmin.h"
20 #include "catalog/pg_type.h"
21 #include "access/heapam.h"
22 #include "utils/builtins.h"
23 #include "utils/inet.h"
26 /* bogus ... these externs should be in a header file */
27 extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
28 extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
29 extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
30 extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
31 extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
32 extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
33 extern Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS);
34 extern Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS);
35 extern Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS);
36 extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
37 extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
38 extern Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS);
39 extern Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS);
40 extern Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS);
41 extern Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
44 extern Datum pg_stat_get_activity(PG_FUNCTION_ARGS);
45 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
46 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
48 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
49 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
50 extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS);
51 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
52 extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
53 extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
54 extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
55 extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
57 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
58 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
59 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
60 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
61 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
62 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
63 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
64 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
65 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
66 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
68 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
70 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
71 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
74 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
77 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
79 /* Global bgwriter statistics, from bgwriter.c */
80 extern PgStat_MsgBgWriter bgwriterStats;
83 pg_stat_get_numscans(PG_FUNCTION_ARGS)
85 Oid relid = PG_GETARG_OID(0);
87 PgStat_StatTabEntry *tabentry;
89 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
92 result = (int64) (tabentry->numscans);
94 PG_RETURN_INT64(result);
99 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
101 Oid relid = PG_GETARG_OID(0);
103 PgStat_StatTabEntry *tabentry;
105 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
108 result = (int64) (tabentry->tuples_returned);
110 PG_RETURN_INT64(result);
115 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
117 Oid relid = PG_GETARG_OID(0);
119 PgStat_StatTabEntry *tabentry;
121 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
124 result = (int64) (tabentry->tuples_fetched);
126 PG_RETURN_INT64(result);
131 pg_stat_get_tuples_inserted(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->tuples_inserted);
142 PG_RETURN_INT64(result);
147 pg_stat_get_tuples_updated(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_updated);
158 PG_RETURN_INT64(result);
163 pg_stat_get_tuples_deleted(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_deleted);
174 PG_RETURN_INT64(result);
179 pg_stat_get_tuples_hot_updated(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_hot_updated);
190 PG_RETURN_INT64(result);
195 pg_stat_get_live_tuples(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->n_live_tuples);
206 PG_RETURN_INT64(result);
211 pg_stat_get_dead_tuples(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->n_dead_tuples);
222 PG_RETURN_INT64(result);
227 pg_stat_get_blocks_fetched(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->blocks_fetched);
238 PG_RETURN_INT64(result);
243 pg_stat_get_blocks_hit(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->blocks_hit);
254 PG_RETURN_INT64(result);
258 pg_stat_get_last_vacuum_time(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 = tabentry->vacuum_timestamp;
272 PG_RETURN_TIMESTAMPTZ(result);
276 pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
278 Oid relid = PG_GETARG_OID(0);
280 PgStat_StatTabEntry *tabentry;
282 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
285 result = tabentry->autovac_vacuum_timestamp;
290 PG_RETURN_TIMESTAMPTZ(result);
294 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
296 Oid relid = PG_GETARG_OID(0);
298 PgStat_StatTabEntry *tabentry;
300 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
303 result = tabentry->analyze_timestamp;
308 PG_RETURN_TIMESTAMPTZ(result);
312 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
314 Oid relid = PG_GETARG_OID(0);
316 PgStat_StatTabEntry *tabentry;
318 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
321 result = tabentry->autovac_analyze_timestamp;
326 PG_RETURN_TIMESTAMPTZ(result);
330 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
332 FuncCallContext *funcctx;
336 /* stuff done only on the first call of the function */
337 if (SRF_IS_FIRSTCALL())
339 /* create a function context for cross-call persistence */
340 funcctx = SRF_FIRSTCALL_INIT();
342 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
344 funcctx->user_fctx = fctx;
347 fctx[1] = pgstat_fetch_stat_numbackends();
350 /* stuff done on every call of the function */
351 funcctx = SRF_PERCALL_SETUP();
352 fctx = funcctx->user_fctx;
357 if (result <= fctx[1])
359 /* do when there is more left to send */
360 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
364 /* do when there is no more left */
365 SRF_RETURN_DONE(funcctx);
370 pg_stat_get_activity(PG_FUNCTION_ARGS)
372 FuncCallContext *funcctx;
374 if (SRF_IS_FIRSTCALL())
376 MemoryContext oldcontext;
379 funcctx = SRF_FIRSTCALL_INIT();
381 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
383 tupdesc = CreateTemplateTupleDesc(10, false);
384 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
385 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
386 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
387 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "current_query", TEXTOID, -1, 0);
388 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "waiting", BOOLOID, -1, 0);
389 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "act_start", TIMESTAMPTZOID, -1, 0);
390 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "query_start", TIMESTAMPTZOID, -1, 0);
391 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "backend_start", TIMESTAMPTZOID, -1, 0);
392 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "client_addr", INETOID, -1, 0);
393 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_port", INT4OID, -1, 0);
395 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
397 funcctx->user_fctx = palloc0(sizeof(int));
400 /* Get all backends */
401 funcctx->max_calls = pgstat_fetch_stat_numbackends();
406 * Get one backend - locate by pid.
408 * We lookup the backend early, so we can return zero rows if it doesn't
409 * exist, instead of returning a single row full of NULLs.
411 int pid = PG_GETARG_INT32(0);
413 int n = pgstat_fetch_stat_numbackends();
415 for (i = 1; i <= n; i++)
417 PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
420 if (be->st_procpid == pid)
422 *(int *)(funcctx->user_fctx) = i;
428 if (*(int *)(funcctx->user_fctx) == 0)
429 /* Pid not found, return zero rows */
430 funcctx->max_calls = 0;
432 funcctx->max_calls = 1;
435 MemoryContextSwitchTo(oldcontext);
438 /* stuff done on every call of the function */
439 funcctx = SRF_PERCALL_SETUP();
441 if (funcctx->call_cntr < funcctx->max_calls)
447 PgBackendStatus *beentry;
448 SockAddr zero_clientaddr;
450 MemSet(values, 0, sizeof(values));
451 MemSet(nulls, 0, sizeof(nulls));
453 if (*(int *)(funcctx->user_fctx) > 0)
454 /* Get specific pid slot */
455 beentry = pgstat_fetch_stat_beentry(*(int *)(funcctx->user_fctx));
457 /* Get the next one in the list */
458 beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr+1); /* 1-based index */
463 for (i = 0; i < sizeof(nulls)/sizeof(nulls[0]); i++)
467 values[3] = CStringGetTextDatum("<backend information not available>");
469 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
470 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
473 /* Values available to all callers */
474 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
475 values[1] = Int32GetDatum(beentry->st_procpid);
476 values[2] = ObjectIdGetDatum(beentry->st_userid);
478 /* Values only available to same user or superuser */
479 if (superuser() || beentry->st_userid == GetUserId())
481 if (*(beentry->st_activity) == '\0')
483 values[3] = CStringGetTextDatum("<command string not enabled>");
487 values[3] = CStringGetTextDatum(beentry->st_activity);
490 values[4] = BoolGetDatum(beentry->st_waiting);
492 if (beentry->st_xact_start_timestamp != 0)
493 values[5] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
497 if (beentry->st_activity_start_timestamp != 0)
498 values[6] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
502 if (beentry->st_proc_start_timestamp != 0)
503 values[7] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
507 /* A zeroed client addr means we don't know */
508 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
509 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
510 sizeof(zero_clientaddr) == 0))
517 if (beentry->st_clientaddr.addr.ss_family == AF_INET
519 || beentry->st_clientaddr.addr.ss_family == AF_INET6
523 char remote_host[NI_MAXHOST];
524 char remote_port[NI_MAXSERV];
527 remote_host[0] = '\0';
528 remote_port[0] = '\0';
529 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
530 beentry->st_clientaddr.salen,
531 remote_host, sizeof(remote_host),
532 remote_port, sizeof(remote_port),
533 NI_NUMERICHOST | NI_NUMERICSERV);
541 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
542 values[8] = DirectFunctionCall1(inet_in,
543 CStringGetDatum(remote_host));
544 values[9] = Int32GetDatum(atoi(remote_port));
547 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
550 * Unix sockets always reports NULL for host and -1 for port, so it's
551 * possible to tell the difference to connections we have no
552 * permissions to view, or with errors.
555 values[9] = DatumGetInt32(-1);
559 /* Unknown address type, should never happen */
567 /* No permissions to view data about this session */
568 values[3] = CStringGetTextDatum("<insufficient privilege>");
577 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
579 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
584 SRF_RETURN_DONE(funcctx);
590 pg_backend_pid(PG_FUNCTION_ARGS)
592 PG_RETURN_INT32(MyProcPid);
597 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
599 int32 beid = PG_GETARG_INT32(0);
600 PgBackendStatus *beentry;
602 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
605 PG_RETURN_INT32(beentry->st_procpid);
610 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
612 int32 beid = PG_GETARG_INT32(0);
613 PgBackendStatus *beentry;
615 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
618 PG_RETURN_OID(beentry->st_databaseid);
623 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
625 int32 beid = PG_GETARG_INT32(0);
626 PgBackendStatus *beentry;
628 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
631 PG_RETURN_OID(beentry->st_userid);
636 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
638 int32 beid = PG_GETARG_INT32(0);
639 PgBackendStatus *beentry;
640 const char *activity;
642 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
643 activity = "<backend information not available>";
644 else if (!superuser() && beentry->st_userid != GetUserId())
645 activity = "<insufficient privilege>";
646 else if (*(beentry->st_activity) == '\0')
647 activity = "<command string not enabled>";
649 activity = beentry->st_activity;
651 PG_RETURN_TEXT_P(cstring_to_text(activity));
656 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
658 int32 beid = PG_GETARG_INT32(0);
660 PgBackendStatus *beentry;
662 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
665 if (!superuser() && beentry->st_userid != GetUserId())
668 result = beentry->st_waiting;
670 PG_RETURN_BOOL(result);
675 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
677 int32 beid = PG_GETARG_INT32(0);
679 PgBackendStatus *beentry;
681 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
684 if (!superuser() && beentry->st_userid != GetUserId())
687 result = beentry->st_activity_start_timestamp;
690 * No time recorded for start of current query -- this is the case if the
691 * user hasn't enabled query-level stats collection.
696 PG_RETURN_TIMESTAMPTZ(result);
701 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
703 int32 beid = PG_GETARG_INT32(0);
705 PgBackendStatus *beentry;
707 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
710 if (!superuser() && beentry->st_userid != GetUserId())
713 result = beentry->st_xact_start_timestamp;
715 if (result == 0) /* not in a transaction */
718 PG_RETURN_TIMESTAMPTZ(result);
723 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
725 int32 beid = PG_GETARG_INT32(0);
727 PgBackendStatus *beentry;
729 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
732 if (!superuser() && beentry->st_userid != GetUserId())
735 result = beentry->st_proc_start_timestamp;
737 if (result == 0) /* probably can't happen? */
740 PG_RETURN_TIMESTAMPTZ(result);
745 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
747 int32 beid = PG_GETARG_INT32(0);
748 PgBackendStatus *beentry;
749 SockAddr zero_clientaddr;
750 char remote_host[NI_MAXHOST];
753 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
756 if (!superuser() && beentry->st_userid != GetUserId())
759 /* A zeroed client addr means we don't know */
760 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
761 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
762 sizeof(zero_clientaddr) == 0))
765 switch (beentry->st_clientaddr.addr.ss_family)
776 remote_host[0] = '\0';
777 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
778 beentry->st_clientaddr.salen,
779 remote_host, sizeof(remote_host),
781 NI_NUMERICHOST | NI_NUMERICSERV);
785 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
787 PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
788 CStringGetDatum(remote_host)));
792 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
794 int32 beid = PG_GETARG_INT32(0);
795 PgBackendStatus *beentry;
796 SockAddr zero_clientaddr;
797 char remote_port[NI_MAXSERV];
800 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
803 if (!superuser() && beentry->st_userid != GetUserId())
806 /* A zeroed client addr means we don't know */
807 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
808 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
809 sizeof(zero_clientaddr) == 0))
812 switch (beentry->st_clientaddr.addr.ss_family)
825 remote_port[0] = '\0';
826 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
827 beentry->st_clientaddr.salen,
829 remote_port, sizeof(remote_port),
830 NI_NUMERICHOST | NI_NUMERICSERV);
834 PG_RETURN_DATUM(DirectFunctionCall1(int4in,
835 CStringGetDatum(remote_port)));
840 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
842 Oid dbid = PG_GETARG_OID(0);
844 int tot_backends = pgstat_fetch_stat_numbackends();
848 for (beid = 1; beid <= tot_backends; beid++)
850 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
852 if (beentry && beentry->st_databaseid == dbid)
856 PG_RETURN_INT32(result);
861 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
863 Oid dbid = PG_GETARG_OID(0);
865 PgStat_StatDBEntry *dbentry;
867 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
870 result = (int64) (dbentry->n_xact_commit);
872 PG_RETURN_INT64(result);
877 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
879 Oid dbid = PG_GETARG_OID(0);
881 PgStat_StatDBEntry *dbentry;
883 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
886 result = (int64) (dbentry->n_xact_rollback);
888 PG_RETURN_INT64(result);
893 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
895 Oid dbid = PG_GETARG_OID(0);
897 PgStat_StatDBEntry *dbentry;
899 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
902 result = (int64) (dbentry->n_blocks_fetched);
904 PG_RETURN_INT64(result);
909 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
911 Oid dbid = PG_GETARG_OID(0);
913 PgStat_StatDBEntry *dbentry;
915 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
918 result = (int64) (dbentry->n_blocks_hit);
920 PG_RETURN_INT64(result);
925 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
927 Oid dbid = PG_GETARG_OID(0);
929 PgStat_StatDBEntry *dbentry;
931 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
934 result = (int64) (dbentry->n_tuples_returned);
936 PG_RETURN_INT64(result);
941 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
943 Oid dbid = PG_GETARG_OID(0);
945 PgStat_StatDBEntry *dbentry;
947 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
950 result = (int64) (dbentry->n_tuples_fetched);
952 PG_RETURN_INT64(result);
957 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
959 Oid dbid = PG_GETARG_OID(0);
961 PgStat_StatDBEntry *dbentry;
963 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
966 result = (int64) (dbentry->n_tuples_inserted);
968 PG_RETURN_INT64(result);
973 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
975 Oid dbid = PG_GETARG_OID(0);
977 PgStat_StatDBEntry *dbentry;
979 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
982 result = (int64) (dbentry->n_tuples_updated);
984 PG_RETURN_INT64(result);
989 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
991 Oid dbid = PG_GETARG_OID(0);
993 PgStat_StatDBEntry *dbentry;
995 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
998 result = (int64) (dbentry->n_tuples_deleted);
1000 PG_RETURN_INT64(result);
1004 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1006 PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1010 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1012 PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1016 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1018 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1022 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1024 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1028 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1030 PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1034 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1036 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1040 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1042 PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1046 /* Discard the active statistics snapshot */
1048 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1050 pgstat_clear_snapshot();
1056 /* Reset all counters for the current database */
1058 pg_stat_reset(PG_FUNCTION_ARGS)
1060 pgstat_reset_counters();