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.51 2008/05/12 00:00:51 alvherre 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_backend_idset(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_activity(PG_FUNCTION_ARGS);
44 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
45 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
46 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
48 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
49 extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS);
50 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
51 extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
52 extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
53 extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
54 extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
56 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
57 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
58 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
59 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
60 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
61 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
62 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
63 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
64 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
65 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
67 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
68 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
70 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
71 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
75 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
78 /* Global bgwriter statistics, from bgwriter.c */
79 extern PgStat_MsgBgWriter bgwriterStats;
82 pg_stat_get_numscans(PG_FUNCTION_ARGS)
84 Oid relid = PG_GETARG_OID(0);
86 PgStat_StatTabEntry *tabentry;
88 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
91 result = (int64) (tabentry->numscans);
93 PG_RETURN_INT64(result);
98 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
100 Oid relid = PG_GETARG_OID(0);
102 PgStat_StatTabEntry *tabentry;
104 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
107 result = (int64) (tabentry->tuples_returned);
109 PG_RETURN_INT64(result);
114 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
116 Oid relid = PG_GETARG_OID(0);
118 PgStat_StatTabEntry *tabentry;
120 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
123 result = (int64) (tabentry->tuples_fetched);
125 PG_RETURN_INT64(result);
130 pg_stat_get_tuples_inserted(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->tuples_inserted);
141 PG_RETURN_INT64(result);
146 pg_stat_get_tuples_updated(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_updated);
157 PG_RETURN_INT64(result);
162 pg_stat_get_tuples_deleted(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_deleted);
173 PG_RETURN_INT64(result);
178 pg_stat_get_tuples_hot_updated(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_hot_updated);
189 PG_RETURN_INT64(result);
194 pg_stat_get_live_tuples(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->n_live_tuples);
205 PG_RETURN_INT64(result);
210 pg_stat_get_dead_tuples(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->n_dead_tuples);
221 PG_RETURN_INT64(result);
226 pg_stat_get_blocks_fetched(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->blocks_fetched);
237 PG_RETURN_INT64(result);
242 pg_stat_get_blocks_hit(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->blocks_hit);
253 PG_RETURN_INT64(result);
257 pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
259 Oid relid = PG_GETARG_OID(0);
261 PgStat_StatTabEntry *tabentry;
263 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
266 result = tabentry->vacuum_timestamp;
271 PG_RETURN_TIMESTAMPTZ(result);
275 pg_stat_get_last_autovacuum_time(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 = tabentry->autovac_vacuum_timestamp;
289 PG_RETURN_TIMESTAMPTZ(result);
293 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
295 Oid relid = PG_GETARG_OID(0);
297 PgStat_StatTabEntry *tabentry;
299 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
302 result = tabentry->analyze_timestamp;
307 PG_RETURN_TIMESTAMPTZ(result);
311 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
313 Oid relid = PG_GETARG_OID(0);
315 PgStat_StatTabEntry *tabentry;
317 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
320 result = tabentry->autovac_analyze_timestamp;
325 PG_RETURN_TIMESTAMPTZ(result);
329 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
331 FuncCallContext *funcctx;
335 /* stuff done only on the first call of the function */
336 if (SRF_IS_FIRSTCALL())
338 /* create a function context for cross-call persistence */
339 funcctx = SRF_FIRSTCALL_INIT();
341 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
343 funcctx->user_fctx = fctx;
346 fctx[1] = pgstat_fetch_stat_numbackends();
349 /* stuff done on every call of the function */
350 funcctx = SRF_PERCALL_SETUP();
351 fctx = funcctx->user_fctx;
356 if (result <= fctx[1])
358 /* do when there is more left to send */
359 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
363 /* do when there is no more left */
364 SRF_RETURN_DONE(funcctx);
369 pg_stat_get_activity(PG_FUNCTION_ARGS)
371 FuncCallContext *funcctx;
373 if (SRF_IS_FIRSTCALL())
375 MemoryContext oldcontext;
378 funcctx = SRF_FIRSTCALL_INIT();
380 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
382 tupdesc = CreateTemplateTupleDesc(10, false);
383 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
384 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
385 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
386 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "current_query", TEXTOID, -1, 0);
387 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "waiting", BOOLOID, -1, 0);
388 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "act_start", TIMESTAMPTZOID, -1, 0);
389 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "query_start", TIMESTAMPTZOID, -1, 0);
390 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "backend_start", TIMESTAMPTZOID, -1, 0);
391 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "client_addr", INETOID, -1, 0);
392 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_port", INT4OID, -1, 0);
394 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
396 funcctx->user_fctx = palloc0(sizeof(int));
399 /* Get all backends */
400 funcctx->max_calls = pgstat_fetch_stat_numbackends();
405 * Get one backend - locate by pid.
407 * We lookup the backend early, so we can return zero rows if it doesn't
408 * exist, instead of returning a single row full of NULLs.
410 int pid = PG_GETARG_INT32(0);
412 int n = pgstat_fetch_stat_numbackends();
414 for (i = 1; i <= n; i++)
416 PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
419 if (be->st_procpid == pid)
421 *(int *)(funcctx->user_fctx) = i;
427 if (*(int *)(funcctx->user_fctx) == 0)
428 /* Pid not found, return zero rows */
429 funcctx->max_calls = 0;
431 funcctx->max_calls = 1;
434 MemoryContextSwitchTo(oldcontext);
437 /* stuff done on every call of the function */
438 funcctx = SRF_PERCALL_SETUP();
440 if (funcctx->call_cntr < funcctx->max_calls)
446 PgBackendStatus *beentry;
447 SockAddr zero_clientaddr;
449 MemSet(values, 0, sizeof(values));
450 MemSet(nulls, 0, sizeof(nulls));
452 if (*(int *)(funcctx->user_fctx) > 0)
453 /* Get specific pid slot */
454 beentry = pgstat_fetch_stat_beentry(*(int *)(funcctx->user_fctx));
456 /* Get the next one in the list */
457 beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr+1); /* 1-based index */
462 for (i = 0; i < sizeof(nulls)/sizeof(nulls[0]); i++)
466 values[3] = CStringGetTextDatum("<backend information not available>");
468 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
469 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
472 /* Values available to all callers */
473 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
474 values[1] = Int32GetDatum(beentry->st_procpid);
475 values[2] = ObjectIdGetDatum(beentry->st_userid);
477 /* Values only available to same user or superuser */
478 if (superuser() || beentry->st_userid == GetUserId())
480 if (*(beentry->st_activity) == '\0')
482 values[3] = CStringGetTextDatum("<command string not enabled>");
486 values[3] = CStringGetTextDatum(beentry->st_activity);
489 values[4] = BoolGetDatum(beentry->st_waiting);
491 if (beentry->st_xact_start_timestamp != 0)
492 values[5] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
496 if (beentry->st_activity_start_timestamp != 0)
497 values[6] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
501 if (beentry->st_proc_start_timestamp != 0)
502 values[7] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
506 /* A zeroed client addr means we don't know */
507 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
508 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
509 sizeof(zero_clientaddr) == 0))
516 if (beentry->st_clientaddr.addr.ss_family == AF_INET
518 || beentry->st_clientaddr.addr.ss_family == AF_INET6
522 char remote_host[NI_MAXHOST];
523 char remote_port[NI_MAXSERV];
526 remote_host[0] = '\0';
527 remote_port[0] = '\0';
528 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
529 beentry->st_clientaddr.salen,
530 remote_host, sizeof(remote_host),
531 remote_port, sizeof(remote_port),
532 NI_NUMERICHOST | NI_NUMERICSERV);
540 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
541 values[8] = DirectFunctionCall1(inet_in,
542 CStringGetDatum(remote_host));
543 values[9] = Int32GetDatum(atoi(remote_port));
546 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
549 * Unix sockets always reports NULL for host and -1 for port, so it's
550 * possible to tell the difference to connections we have no
551 * permissions to view, or with errors.
554 values[9] = DatumGetInt32(-1);
558 /* Unknown address type, should never happen */
566 /* No permissions to view data about this session */
567 values[3] = CStringGetTextDatum("<insufficient privilege>");
576 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
578 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
583 SRF_RETURN_DONE(funcctx);
589 pg_backend_pid(PG_FUNCTION_ARGS)
591 PG_RETURN_INT32(MyProcPid);
596 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
598 int32 beid = PG_GETARG_INT32(0);
599 PgBackendStatus *beentry;
601 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
604 PG_RETURN_INT32(beentry->st_procpid);
609 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
611 int32 beid = PG_GETARG_INT32(0);
612 PgBackendStatus *beentry;
614 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
617 PG_RETURN_OID(beentry->st_databaseid);
622 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
624 int32 beid = PG_GETARG_INT32(0);
625 PgBackendStatus *beentry;
627 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
630 PG_RETURN_OID(beentry->st_userid);
635 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
637 int32 beid = PG_GETARG_INT32(0);
638 PgBackendStatus *beentry;
639 const char *activity;
641 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
642 activity = "<backend information not available>";
643 else if (!superuser() && beentry->st_userid != GetUserId())
644 activity = "<insufficient privilege>";
645 else if (*(beentry->st_activity) == '\0')
646 activity = "<command string not enabled>";
648 activity = beentry->st_activity;
650 PG_RETURN_TEXT_P(cstring_to_text(activity));
655 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
657 int32 beid = PG_GETARG_INT32(0);
659 PgBackendStatus *beentry;
661 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
664 if (!superuser() && beentry->st_userid != GetUserId())
667 result = beentry->st_waiting;
669 PG_RETURN_BOOL(result);
674 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
676 int32 beid = PG_GETARG_INT32(0);
678 PgBackendStatus *beentry;
680 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
683 if (!superuser() && beentry->st_userid != GetUserId())
686 result = beentry->st_activity_start_timestamp;
689 * No time recorded for start of current query -- this is the case if the
690 * user hasn't enabled query-level stats collection.
695 PG_RETURN_TIMESTAMPTZ(result);
700 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
702 int32 beid = PG_GETARG_INT32(0);
704 PgBackendStatus *beentry;
706 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
709 if (!superuser() && beentry->st_userid != GetUserId())
712 result = beentry->st_xact_start_timestamp;
714 if (result == 0) /* not in a transaction */
717 PG_RETURN_TIMESTAMPTZ(result);
722 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
724 int32 beid = PG_GETARG_INT32(0);
726 PgBackendStatus *beentry;
728 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
731 if (!superuser() && beentry->st_userid != GetUserId())
734 result = beentry->st_proc_start_timestamp;
736 if (result == 0) /* probably can't happen? */
739 PG_RETURN_TIMESTAMPTZ(result);
744 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
746 int32 beid = PG_GETARG_INT32(0);
747 PgBackendStatus *beentry;
748 SockAddr zero_clientaddr;
749 char remote_host[NI_MAXHOST];
752 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
755 if (!superuser() && beentry->st_userid != GetUserId())
758 /* A zeroed client addr means we don't know */
759 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
760 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
761 sizeof(zero_clientaddr) == 0))
764 switch (beentry->st_clientaddr.addr.ss_family)
775 remote_host[0] = '\0';
776 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
777 beentry->st_clientaddr.salen,
778 remote_host, sizeof(remote_host),
780 NI_NUMERICHOST | NI_NUMERICSERV);
784 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
786 PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
787 CStringGetDatum(remote_host)));
791 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
793 int32 beid = PG_GETARG_INT32(0);
794 PgBackendStatus *beentry;
795 SockAddr zero_clientaddr;
796 char remote_port[NI_MAXSERV];
799 if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
802 if (!superuser() && beentry->st_userid != GetUserId())
805 /* A zeroed client addr means we don't know */
806 memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
807 if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
808 sizeof(zero_clientaddr) == 0))
811 switch (beentry->st_clientaddr.addr.ss_family)
824 remote_port[0] = '\0';
825 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
826 beentry->st_clientaddr.salen,
828 remote_port, sizeof(remote_port),
829 NI_NUMERICHOST | NI_NUMERICSERV);
833 PG_RETURN_DATUM(DirectFunctionCall1(int4in,
834 CStringGetDatum(remote_port)));
839 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
841 Oid dbid = PG_GETARG_OID(0);
843 int tot_backends = pgstat_fetch_stat_numbackends();
847 for (beid = 1; beid <= tot_backends; beid++)
849 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
851 if (beentry && beentry->st_databaseid == dbid)
855 PG_RETURN_INT32(result);
860 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
862 Oid dbid = PG_GETARG_OID(0);
864 PgStat_StatDBEntry *dbentry;
866 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
869 result = (int64) (dbentry->n_xact_commit);
871 PG_RETURN_INT64(result);
876 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
878 Oid dbid = PG_GETARG_OID(0);
880 PgStat_StatDBEntry *dbentry;
882 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
885 result = (int64) (dbentry->n_xact_rollback);
887 PG_RETURN_INT64(result);
892 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
894 Oid dbid = PG_GETARG_OID(0);
896 PgStat_StatDBEntry *dbentry;
898 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
901 result = (int64) (dbentry->n_blocks_fetched);
903 PG_RETURN_INT64(result);
908 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
910 Oid dbid = PG_GETARG_OID(0);
912 PgStat_StatDBEntry *dbentry;
914 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
917 result = (int64) (dbentry->n_blocks_hit);
919 PG_RETURN_INT64(result);
924 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
926 Oid dbid = PG_GETARG_OID(0);
928 PgStat_StatDBEntry *dbentry;
930 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
933 result = (int64) (dbentry->n_tuples_returned);
935 PG_RETURN_INT64(result);
940 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
942 Oid dbid = PG_GETARG_OID(0);
944 PgStat_StatDBEntry *dbentry;
946 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
949 result = (int64) (dbentry->n_tuples_fetched);
951 PG_RETURN_INT64(result);
956 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
958 Oid dbid = PG_GETARG_OID(0);
960 PgStat_StatDBEntry *dbentry;
962 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
965 result = (int64) (dbentry->n_tuples_inserted);
967 PG_RETURN_INT64(result);
972 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
974 Oid dbid = PG_GETARG_OID(0);
976 PgStat_StatDBEntry *dbentry;
978 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
981 result = (int64) (dbentry->n_tuples_updated);
983 PG_RETURN_INT64(result);
988 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
990 Oid dbid = PG_GETARG_OID(0);
992 PgStat_StatDBEntry *dbentry;
994 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
997 result = (int64) (dbentry->n_tuples_deleted);
999 PG_RETURN_INT64(result);
1003 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1005 PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1009 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1011 PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1015 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1017 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1021 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1023 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1027 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1029 PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1033 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1035 PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1039 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1041 PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1045 /* Discard the active statistics snapshot */
1047 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1049 pgstat_clear_snapshot();
1055 /* Reset all counters for the current database */
1057 pg_stat_reset(PG_FUNCTION_ARGS)
1059 pgstat_reset_counters();