PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgstatfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgstatfuncs.c
4  * Functions for accessing the statistics collector data
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/pgstatfuncs.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "catalog/pg_authid.h"
19 #include "catalog/pg_type.h"
20 #include "common/ip.h"
21 #include "funcapi.h"
22 #include "miscadmin.h"
23 #include "pgstat.h"
24 #include "postmaster/postmaster.h"
25 #include "storage/proc.h"
26 #include "storage/procarray.h"
27 #include "utils/acl.h"
28 #include "utils/builtins.h"
29 #include "utils/inet.h"
30 #include "utils/timestamp.h"
31 
32 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
33 
34 /* Global bgwriter statistics, from bgwriter.c */
36 
37 Datum
39 {
40  Oid relid = PG_GETARG_OID(0);
41  int64 result;
42  PgStat_StatTabEntry *tabentry;
43 
44  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
45  result = 0;
46  else
47  result = (int64) (tabentry->numscans);
48 
49  PG_RETURN_INT64(result);
50 }
51 
52 
53 Datum
55 {
56  Oid relid = PG_GETARG_OID(0);
57  int64 result;
58  PgStat_StatTabEntry *tabentry;
59 
60  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
61  result = 0;
62  else
63  result = (int64) (tabentry->tuples_returned);
64 
65  PG_RETURN_INT64(result);
66 }
67 
68 
69 Datum
71 {
72  Oid relid = PG_GETARG_OID(0);
73  int64 result;
74  PgStat_StatTabEntry *tabentry;
75 
76  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
77  result = 0;
78  else
79  result = (int64) (tabentry->tuples_fetched);
80 
81  PG_RETURN_INT64(result);
82 }
83 
84 
85 Datum
87 {
88  Oid relid = PG_GETARG_OID(0);
89  int64 result;
90  PgStat_StatTabEntry *tabentry;
91 
92  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
93  result = 0;
94  else
95  result = (int64) (tabentry->tuples_inserted);
96 
97  PG_RETURN_INT64(result);
98 }
99 
100 
101 Datum
103 {
104  Oid relid = PG_GETARG_OID(0);
105  int64 result;
106  PgStat_StatTabEntry *tabentry;
107 
108  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
109  result = 0;
110  else
111  result = (int64) (tabentry->tuples_updated);
112 
113  PG_RETURN_INT64(result);
114 }
115 
116 
117 Datum
119 {
120  Oid relid = PG_GETARG_OID(0);
121  int64 result;
122  PgStat_StatTabEntry *tabentry;
123 
124  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
125  result = 0;
126  else
127  result = (int64) (tabentry->tuples_deleted);
128 
129  PG_RETURN_INT64(result);
130 }
131 
132 
133 Datum
135 {
136  Oid relid = PG_GETARG_OID(0);
137  int64 result;
138  PgStat_StatTabEntry *tabentry;
139 
140  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
141  result = 0;
142  else
143  result = (int64) (tabentry->tuples_hot_updated);
144 
145  PG_RETURN_INT64(result);
146 }
147 
148 
149 Datum
151 {
152  Oid relid = PG_GETARG_OID(0);
153  int64 result;
154  PgStat_StatTabEntry *tabentry;
155 
156  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
157  result = 0;
158  else
159  result = (int64) (tabentry->n_live_tuples);
160 
161  PG_RETURN_INT64(result);
162 }
163 
164 
165 Datum
167 {
168  Oid relid = PG_GETARG_OID(0);
169  int64 result;
170  PgStat_StatTabEntry *tabentry;
171 
172  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
173  result = 0;
174  else
175  result = (int64) (tabentry->n_dead_tuples);
176 
177  PG_RETURN_INT64(result);
178 }
179 
180 
181 Datum
183 {
184  Oid relid = PG_GETARG_OID(0);
185  int64 result;
186  PgStat_StatTabEntry *tabentry;
187 
188  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
189  result = 0;
190  else
191  result = (int64) (tabentry->changes_since_analyze);
192 
193  PG_RETURN_INT64(result);
194 }
195 
196 
197 Datum
199 {
200  Oid relid = PG_GETARG_OID(0);
201  int64 result;
202  PgStat_StatTabEntry *tabentry;
203 
204  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
205  result = 0;
206  else
207  result = (int64) (tabentry->blocks_fetched);
208 
209  PG_RETURN_INT64(result);
210 }
211 
212 
213 Datum
215 {
216  Oid relid = PG_GETARG_OID(0);
217  int64 result;
218  PgStat_StatTabEntry *tabentry;
219 
220  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
221  result = 0;
222  else
223  result = (int64) (tabentry->blocks_hit);
224 
225  PG_RETURN_INT64(result);
226 }
227 
228 Datum
230 {
231  Oid relid = PG_GETARG_OID(0);
233  PgStat_StatTabEntry *tabentry;
234 
235  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
236  result = 0;
237  else
238  result = tabentry->vacuum_timestamp;
239 
240  if (result == 0)
241  PG_RETURN_NULL();
242  else
243  PG_RETURN_TIMESTAMPTZ(result);
244 }
245 
246 Datum
248 {
249  Oid relid = PG_GETARG_OID(0);
251  PgStat_StatTabEntry *tabentry;
252 
253  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
254  result = 0;
255  else
256  result = tabentry->autovac_vacuum_timestamp;
257 
258  if (result == 0)
259  PG_RETURN_NULL();
260  else
261  PG_RETURN_TIMESTAMPTZ(result);
262 }
263 
264 Datum
266 {
267  Oid relid = PG_GETARG_OID(0);
269  PgStat_StatTabEntry *tabentry;
270 
271  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
272  result = 0;
273  else
274  result = tabentry->analyze_timestamp;
275 
276  if (result == 0)
277  PG_RETURN_NULL();
278  else
279  PG_RETURN_TIMESTAMPTZ(result);
280 }
281 
282 Datum
284 {
285  Oid relid = PG_GETARG_OID(0);
287  PgStat_StatTabEntry *tabentry;
288 
289  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
290  result = 0;
291  else
292  result = tabentry->autovac_analyze_timestamp;
293 
294  if (result == 0)
295  PG_RETURN_NULL();
296  else
297  PG_RETURN_TIMESTAMPTZ(result);
298 }
299 
300 Datum
302 {
303  Oid relid = PG_GETARG_OID(0);
304  int64 result;
305  PgStat_StatTabEntry *tabentry;
306 
307  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
308  result = 0;
309  else
310  result = (int64) (tabentry->vacuum_count);
311 
312  PG_RETURN_INT64(result);
313 }
314 
315 Datum
317 {
318  Oid relid = PG_GETARG_OID(0);
319  int64 result;
320  PgStat_StatTabEntry *tabentry;
321 
322  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
323  result = 0;
324  else
325  result = (int64) (tabentry->autovac_vacuum_count);
326 
327  PG_RETURN_INT64(result);
328 }
329 
330 Datum
332 {
333  Oid relid = PG_GETARG_OID(0);
334  int64 result;
335  PgStat_StatTabEntry *tabentry;
336 
337  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
338  result = 0;
339  else
340  result = (int64) (tabentry->analyze_count);
341 
342  PG_RETURN_INT64(result);
343 }
344 
345 Datum
347 {
348  Oid relid = PG_GETARG_OID(0);
349  int64 result;
350  PgStat_StatTabEntry *tabentry;
351 
352  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
353  result = 0;
354  else
355  result = (int64) (tabentry->autovac_analyze_count);
356 
357  PG_RETURN_INT64(result);
358 }
359 
360 Datum
362 {
363  Oid funcid = PG_GETARG_OID(0);
364  PgStat_StatFuncEntry *funcentry;
365 
366  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
367  PG_RETURN_NULL();
368  PG_RETURN_INT64(funcentry->f_numcalls);
369 }
370 
371 Datum
373 {
374  Oid funcid = PG_GETARG_OID(0);
375  PgStat_StatFuncEntry *funcentry;
376 
377  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
378  PG_RETURN_NULL();
379  /* convert counter from microsec to millisec for display */
380  PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
381 }
382 
383 Datum
385 {
386  Oid funcid = PG_GETARG_OID(0);
387  PgStat_StatFuncEntry *funcentry;
388 
389  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
390  PG_RETURN_NULL();
391  /* convert counter from microsec to millisec for display */
392  PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
393 }
394 
395 Datum
397 {
398  FuncCallContext *funcctx;
399  int *fctx;
400  int32 result;
401 
402  /* stuff done only on the first call of the function */
403  if (SRF_IS_FIRSTCALL())
404  {
405  /* create a function context for cross-call persistence */
406  funcctx = SRF_FIRSTCALL_INIT();
407 
409  2 * sizeof(int));
410  funcctx->user_fctx = fctx;
411 
412  fctx[0] = 0;
413  fctx[1] = pgstat_fetch_stat_numbackends();
414  }
415 
416  /* stuff done on every call of the function */
417  funcctx = SRF_PERCALL_SETUP();
418  fctx = funcctx->user_fctx;
419 
420  fctx[0] += 1;
421  result = fctx[0];
422 
423  if (result <= fctx[1])
424  {
425  /* do when there is more left to send */
426  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
427  }
428  else
429  {
430  /* do when there is no more left */
431  SRF_RETURN_DONE(funcctx);
432  }
433 }
434 
435 /*
436  * Returns command progress information for the named command.
437  */
438 Datum
440 {
441 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
442  int num_backends = pgstat_fetch_stat_numbackends();
443  int curr_backend;
444  char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
445  ProgressCommandType cmdtype;
446  TupleDesc tupdesc;
447  Tuplestorestate *tupstore;
448  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
449  MemoryContext per_query_ctx;
450  MemoryContext oldcontext;
451 
452  /* check to see if caller supports us returning a tuplestore */
453  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
454  ereport(ERROR,
455  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
456  errmsg("set-valued function called in context that cannot accept a set")));
457  if (!(rsinfo->allowedModes & SFRM_Materialize))
458  ereport(ERROR,
459  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
460  errmsg("materialize mode required, but it is not " \
461  "allowed in this context")));
462 
463  /* Build a tuple descriptor for our result type */
464  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
465  elog(ERROR, "return type must be a row type");
466 
467  /* Translate command name into command type code. */
468  if (pg_strcasecmp(cmd, "VACUUM") == 0)
469  cmdtype = PROGRESS_COMMAND_VACUUM;
470  else
471  ereport(ERROR,
472  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
473  errmsg("invalid command name: \"%s\"", cmd)));
474 
475  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
476  oldcontext = MemoryContextSwitchTo(per_query_ctx);
477 
478  tupstore = tuplestore_begin_heap(true, false, work_mem);
479  rsinfo->returnMode = SFRM_Materialize;
480  rsinfo->setResult = tupstore;
481  rsinfo->setDesc = tupdesc;
482  MemoryContextSwitchTo(oldcontext);
483 
484  /* 1-based index */
485  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
486  {
487  LocalPgBackendStatus *local_beentry;
488  PgBackendStatus *beentry;
490  bool nulls[PG_STAT_GET_PROGRESS_COLS];
491  int i;
492 
493  MemSet(values, 0, sizeof(values));
494  MemSet(nulls, 0, sizeof(nulls));
495 
496  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
497 
498  if (!local_beentry)
499  continue;
500 
501  beentry = &local_beentry->backendStatus;
502 
503  /*
504  * Report values for only those backends which are running the given
505  * command.
506  */
507  if (!beentry || beentry->st_progress_command != cmdtype)
508  continue;
509 
510  /* Value available to all callers */
511  values[0] = Int32GetDatum(beentry->st_procpid);
512  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
513 
514  /* show rest of the values including relid only to role members */
515  if (has_privs_of_role(GetUserId(), beentry->st_userid))
516  {
517  values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
518  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
519  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
520  }
521  else
522  {
523  nulls[2] = true;
524  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
525  nulls[i + 3] = true;
526  }
527 
528  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
529  }
530 
531  /* clean up and return the tuplestore */
532  tuplestore_donestoring(tupstore);
533 
534  return (Datum) 0;
535 }
536 
537 /*
538  * Returns activity of PG backends.
539  */
540 Datum
542 {
543 #define PG_STAT_GET_ACTIVITY_COLS 24
544  int num_backends = pgstat_fetch_stat_numbackends();
545  int curr_backend;
546  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
547  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
548  TupleDesc tupdesc;
549  Tuplestorestate *tupstore;
550  MemoryContext per_query_ctx;
551  MemoryContext oldcontext;
552 
553  /* check to see if caller supports us returning a tuplestore */
554  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
555  ereport(ERROR,
556  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
557  errmsg("set-valued function called in context that cannot accept a set")));
558  if (!(rsinfo->allowedModes & SFRM_Materialize))
559  ereport(ERROR,
560  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
561  errmsg("materialize mode required, but it is not " \
562  "allowed in this context")));
563 
564  /* Build a tuple descriptor for our result type */
565  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
566  elog(ERROR, "return type must be a row type");
567 
568  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
569  oldcontext = MemoryContextSwitchTo(per_query_ctx);
570 
571  tupstore = tuplestore_begin_heap(true, false, work_mem);
572  rsinfo->returnMode = SFRM_Materialize;
573  rsinfo->setResult = tupstore;
574  rsinfo->setDesc = tupdesc;
575 
576  MemoryContextSwitchTo(oldcontext);
577 
578  /* 1-based index */
579  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
580  {
581  /* for each row */
583  bool nulls[PG_STAT_GET_ACTIVITY_COLS];
584  LocalPgBackendStatus *local_beentry;
585  PgBackendStatus *beentry;
586  PGPROC *proc;
587  const char *wait_event_type = NULL;
588  const char *wait_event = NULL;
589 
590  MemSet(values, 0, sizeof(values));
591  MemSet(nulls, 0, sizeof(nulls));
592 
593  /* Get the next one in the list */
594  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
595  if (!local_beentry)
596  {
597  int i;
598 
599  /* Ignore missing entries if looking for specific PID */
600  if (pid != -1)
601  continue;
602 
603  for (i = 0; i < lengthof(nulls); i++)
604  nulls[i] = true;
605 
606  nulls[5] = false;
607  values[5] = CStringGetTextDatum("<backend information not available>");
608 
609  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
610  continue;
611  }
612 
613  beentry = &local_beentry->backendStatus;
614 
615  /* If looking for specific PID, ignore all the others */
616  if (pid != -1 && beentry->st_procpid != pid)
617  continue;
618 
619  /* Values available to all callers */
620  if (beentry->st_databaseid != InvalidOid)
621  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
622  else
623  nulls[0] = true;
624 
625  values[1] = Int32GetDatum(beentry->st_procpid);
626 
627  if (beentry->st_userid != InvalidOid)
628  values[2] = ObjectIdGetDatum(beentry->st_userid);
629  else
630  nulls[2] = true;
631 
632  if (beentry->st_appname)
633  values[3] = CStringGetTextDatum(beentry->st_appname);
634  else
635  nulls[3] = true;
636 
637  if (TransactionIdIsValid(local_beentry->backend_xid))
638  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
639  else
640  nulls[15] = true;
641 
642  if (TransactionIdIsValid(local_beentry->backend_xmin))
643  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
644  else
645  nulls[16] = true;
646 
647  if (beentry->st_ssl)
648  {
649  values[18] = BoolGetDatum(true); /* ssl */
650  values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
651  values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
652  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
653  values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
654  values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_clientdn);
655  }
656  else
657  {
658  values[18] = BoolGetDatum(false); /* ssl */
659  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = true;
660  }
661 
662  /* Values only available to role member or pg_read_all_stats */
663  if (has_privs_of_role(GetUserId(), beentry->st_userid) ||
665  {
666  SockAddr zero_clientaddr;
667 
668  switch (beentry->st_state)
669  {
670  case STATE_IDLE:
671  values[4] = CStringGetTextDatum("idle");
672  break;
673  case STATE_RUNNING:
674  values[4] = CStringGetTextDatum("active");
675  break;
677  values[4] = CStringGetTextDatum("idle in transaction");
678  break;
679  case STATE_FASTPATH:
680  values[4] = CStringGetTextDatum("fastpath function call");
681  break;
683  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
684  break;
685  case STATE_DISABLED:
686  values[4] = CStringGetTextDatum("disabled");
687  break;
688  case STATE_UNDEFINED:
689  nulls[4] = true;
690  break;
691  }
692 
693  values[5] = CStringGetTextDatum(beentry->st_activity);
694 
695  proc = BackendPidGetProc(beentry->st_procpid);
696  if (proc != NULL)
697  {
698  uint32 raw_wait_event;
699 
700  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
701  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
702  wait_event = pgstat_get_wait_event(raw_wait_event);
703 
704  }
705  else if (beentry->st_backendType != B_BACKEND)
706  {
707  /*
708  * For an auxiliary process, retrieve process info from
709  * AuxiliaryProcs stored in shared-memory.
710  */
711  proc = AuxiliaryPidGetProc(beentry->st_procpid);
712 
713  if (proc != NULL)
714  {
715  uint32 raw_wait_event;
716 
717  raw_wait_event =
719  wait_event_type =
720  pgstat_get_wait_event_type(raw_wait_event);
721  wait_event = pgstat_get_wait_event(raw_wait_event);
722  }
723  }
724 
725  if (wait_event_type)
726  values[6] = CStringGetTextDatum(wait_event_type);
727  else
728  nulls[6] = true;
729 
730  if (wait_event)
731  values[7] = CStringGetTextDatum(wait_event);
732  else
733  nulls[7] = true;
734 
735  if (beentry->st_xact_start_timestamp != 0)
736  values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
737  else
738  nulls[8] = true;
739 
740  if (beentry->st_activity_start_timestamp != 0)
741  values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
742  else
743  nulls[9] = true;
744 
745  if (beentry->st_proc_start_timestamp != 0)
746  values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
747  else
748  nulls[10] = true;
749 
750  if (beentry->st_state_start_timestamp != 0)
751  values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
752  else
753  nulls[11] = true;
754 
755  /* A zeroed client addr means we don't know */
756  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
757  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
758  sizeof(zero_clientaddr)) == 0)
759  {
760  nulls[12] = true;
761  nulls[13] = true;
762  nulls[14] = true;
763  }
764  else
765  {
766  if (beentry->st_clientaddr.addr.ss_family == AF_INET
767 #ifdef HAVE_IPV6
768  || beentry->st_clientaddr.addr.ss_family == AF_INET6
769 #endif
770  )
771  {
772  char remote_host[NI_MAXHOST];
773  char remote_port[NI_MAXSERV];
774  int ret;
775 
776  remote_host[0] = '\0';
777  remote_port[0] = '\0';
778  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
779  beentry->st_clientaddr.salen,
780  remote_host, sizeof(remote_host),
781  remote_port, sizeof(remote_port),
783  if (ret == 0)
784  {
785  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
786  values[12] = DirectFunctionCall1(inet_in,
787  CStringGetDatum(remote_host));
788  if (beentry->st_clienthostname &&
789  beentry->st_clienthostname[0])
790  values[13] = CStringGetTextDatum(beentry->st_clienthostname);
791  else
792  nulls[13] = true;
793  values[14] = Int32GetDatum(atoi(remote_port));
794  }
795  else
796  {
797  nulls[12] = true;
798  nulls[13] = true;
799  nulls[14] = true;
800  }
801  }
802  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
803  {
804  /*
805  * Unix sockets always reports NULL for host and -1 for
806  * port, so it's possible to tell the difference to
807  * connections we have no permissions to view, or with
808  * errors.
809  */
810  nulls[12] = true;
811  nulls[13] = true;
812  values[14] = DatumGetInt32(-1);
813  }
814  else
815  {
816  /* Unknown address type, should never happen */
817  nulls[12] = true;
818  nulls[13] = true;
819  nulls[14] = true;
820  }
821  }
822  /* Add backend type */
823  values[17] =
825  }
826  else
827  {
828  /* No permissions to view data about this session */
829  values[5] = CStringGetTextDatum("<insufficient privilege>");
830  nulls[4] = true;
831  nulls[6] = true;
832  nulls[7] = true;
833  nulls[8] = true;
834  nulls[9] = true;
835  nulls[10] = true;
836  nulls[11] = true;
837  nulls[12] = true;
838  nulls[13] = true;
839  nulls[14] = true;
840  nulls[17] = true;
841  }
842 
843  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
844 
845  /* If only a single backend was requested, and we found it, break. */
846  if (pid != -1)
847  break;
848  }
849 
850  /* clean up and return the tuplestore */
851  tuplestore_donestoring(tupstore);
852 
853  return (Datum) 0;
854 }
855 
856 
857 Datum
859 {
861 }
862 
863 
864 Datum
866 {
867  int32 beid = PG_GETARG_INT32(0);
868  PgBackendStatus *beentry;
869 
870  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
871  PG_RETURN_NULL();
872 
873  PG_RETURN_INT32(beentry->st_procpid);
874 }
875 
876 
877 Datum
879 {
880  int32 beid = PG_GETARG_INT32(0);
881  PgBackendStatus *beentry;
882 
883  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
884  PG_RETURN_NULL();
885 
886  PG_RETURN_OID(beentry->st_databaseid);
887 }
888 
889 
890 Datum
892 {
893  int32 beid = PG_GETARG_INT32(0);
894  PgBackendStatus *beentry;
895 
896  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
897  PG_RETURN_NULL();
898 
899  PG_RETURN_OID(beentry->st_userid);
900 }
901 
902 
903 Datum
905 {
906  int32 beid = PG_GETARG_INT32(0);
907  PgBackendStatus *beentry;
908  const char *activity;
909 
910  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
911  activity = "<backend information not available>";
912  else if (!has_privs_of_role(GetUserId(), beentry->st_userid))
913  activity = "<insufficient privilege>";
914  else if (*(beentry->st_activity) == '\0')
915  activity = "<command string not enabled>";
916  else
917  activity = beentry->st_activity;
918 
920 }
921 
922 Datum
924 {
925  int32 beid = PG_GETARG_INT32(0);
926  PgBackendStatus *beentry;
927  PGPROC *proc;
928  const char *wait_event_type = NULL;
929 
930  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
931  wait_event_type = "<backend information not available>";
932  else if (!has_privs_of_role(GetUserId(), beentry->st_userid))
933  wait_event_type = "<insufficient privilege>";
934  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
935  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
936 
937  if (!wait_event_type)
938  PG_RETURN_NULL();
939 
940  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
941 }
942 
943 Datum
945 {
946  int32 beid = PG_GETARG_INT32(0);
947  PgBackendStatus *beentry;
948  PGPROC *proc;
949  const char *wait_event = NULL;
950 
951  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
952  wait_event = "<backend information not available>";
953  else if (!has_privs_of_role(GetUserId(), beentry->st_userid))
954  wait_event = "<insufficient privilege>";
955  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
956  wait_event = pgstat_get_wait_event(proc->wait_event_info);
957 
958  if (!wait_event)
959  PG_RETURN_NULL();
960 
961  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
962 }
963 
964 
965 Datum
967 {
968  int32 beid = PG_GETARG_INT32(0);
970  PgBackendStatus *beentry;
971 
972  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
973  PG_RETURN_NULL();
974 
975  if (!has_privs_of_role(GetUserId(), beentry->st_userid))
976  PG_RETURN_NULL();
977 
978  result = beentry->st_activity_start_timestamp;
979 
980  /*
981  * No time recorded for start of current query -- this is the case if the
982  * user hasn't enabled query-level stats collection.
983  */
984  if (result == 0)
985  PG_RETURN_NULL();
986 
987  PG_RETURN_TIMESTAMPTZ(result);
988 }
989 
990 
991 Datum
993 {
994  int32 beid = PG_GETARG_INT32(0);
996  PgBackendStatus *beentry;
997 
998  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
999  PG_RETURN_NULL();
1000 
1001  if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1002  PG_RETURN_NULL();
1003 
1004  result = beentry->st_xact_start_timestamp;
1005 
1006  if (result == 0) /* not in a transaction */
1007  PG_RETURN_NULL();
1008 
1009  PG_RETURN_TIMESTAMPTZ(result);
1010 }
1011 
1012 
1013 Datum
1015 {
1016  int32 beid = PG_GETARG_INT32(0);
1018  PgBackendStatus *beentry;
1019 
1020  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1021  PG_RETURN_NULL();
1022 
1023  if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1024  PG_RETURN_NULL();
1025 
1026  result = beentry->st_proc_start_timestamp;
1027 
1028  if (result == 0) /* probably can't happen? */
1029  PG_RETURN_NULL();
1030 
1031  PG_RETURN_TIMESTAMPTZ(result);
1032 }
1033 
1034 
1035 Datum
1037 {
1038  int32 beid = PG_GETARG_INT32(0);
1039  PgBackendStatus *beentry;
1040  SockAddr zero_clientaddr;
1041  char remote_host[NI_MAXHOST];
1042  int ret;
1043 
1044  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1045  PG_RETURN_NULL();
1046 
1047  if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1048  PG_RETURN_NULL();
1049 
1050  /* A zeroed client addr means we don't know */
1051  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1052  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1053  sizeof(zero_clientaddr)) == 0)
1054  PG_RETURN_NULL();
1055 
1056  switch (beentry->st_clientaddr.addr.ss_family)
1057  {
1058  case AF_INET:
1059 #ifdef HAVE_IPV6
1060  case AF_INET6:
1061 #endif
1062  break;
1063  default:
1064  PG_RETURN_NULL();
1065  }
1066 
1067  remote_host[0] = '\0';
1068  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1069  beentry->st_clientaddr.salen,
1070  remote_host, sizeof(remote_host),
1071  NULL, 0,
1073  if (ret != 0)
1074  PG_RETURN_NULL();
1075 
1076  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1077 
1079  CStringGetDatum(remote_host)));
1080 }
1081 
1082 Datum
1084 {
1085  int32 beid = PG_GETARG_INT32(0);
1086  PgBackendStatus *beentry;
1087  SockAddr zero_clientaddr;
1088  char remote_port[NI_MAXSERV];
1089  int ret;
1090 
1091  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1092  PG_RETURN_NULL();
1093 
1094  if (!has_privs_of_role(GetUserId(), beentry->st_userid))
1095  PG_RETURN_NULL();
1096 
1097  /* A zeroed client addr means we don't know */
1098  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1099  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1100  sizeof(zero_clientaddr)) == 0)
1101  PG_RETURN_NULL();
1102 
1103  switch (beentry->st_clientaddr.addr.ss_family)
1104  {
1105  case AF_INET:
1106 #ifdef HAVE_IPV6
1107  case AF_INET6:
1108 #endif
1109  break;
1110  case AF_UNIX:
1111  PG_RETURN_INT32(-1);
1112  default:
1113  PG_RETURN_NULL();
1114  }
1115 
1116  remote_port[0] = '\0';
1117  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1118  beentry->st_clientaddr.salen,
1119  NULL, 0,
1120  remote_port, sizeof(remote_port),
1122  if (ret != 0)
1123  PG_RETURN_NULL();
1124 
1126  CStringGetDatum(remote_port)));
1127 }
1128 
1129 
1130 Datum
1132 {
1133  Oid dbid = PG_GETARG_OID(0);
1134  int32 result;
1135  int tot_backends = pgstat_fetch_stat_numbackends();
1136  int beid;
1137 
1138  result = 0;
1139  for (beid = 1; beid <= tot_backends; beid++)
1140  {
1141  PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1142 
1143  if (beentry && beentry->st_databaseid == dbid)
1144  result++;
1145  }
1146 
1147  PG_RETURN_INT32(result);
1148 }
1149 
1150 
1151 Datum
1153 {
1154  Oid dbid = PG_GETARG_OID(0);
1155  int64 result;
1156  PgStat_StatDBEntry *dbentry;
1157 
1158  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1159  result = 0;
1160  else
1161  result = (int64) (dbentry->n_xact_commit);
1162 
1163  PG_RETURN_INT64(result);
1164 }
1165 
1166 
1167 Datum
1169 {
1170  Oid dbid = PG_GETARG_OID(0);
1171  int64 result;
1172  PgStat_StatDBEntry *dbentry;
1173 
1174  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1175  result = 0;
1176  else
1177  result = (int64) (dbentry->n_xact_rollback);
1178 
1179  PG_RETURN_INT64(result);
1180 }
1181 
1182 
1183 Datum
1185 {
1186  Oid dbid = PG_GETARG_OID(0);
1187  int64 result;
1188  PgStat_StatDBEntry *dbentry;
1189 
1190  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1191  result = 0;
1192  else
1193  result = (int64) (dbentry->n_blocks_fetched);
1194 
1195  PG_RETURN_INT64(result);
1196 }
1197 
1198 
1199 Datum
1201 {
1202  Oid dbid = PG_GETARG_OID(0);
1203  int64 result;
1204  PgStat_StatDBEntry *dbentry;
1205 
1206  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1207  result = 0;
1208  else
1209  result = (int64) (dbentry->n_blocks_hit);
1210 
1211  PG_RETURN_INT64(result);
1212 }
1213 
1214 
1215 Datum
1217 {
1218  Oid dbid = PG_GETARG_OID(0);
1219  int64 result;
1220  PgStat_StatDBEntry *dbentry;
1221 
1222  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1223  result = 0;
1224  else
1225  result = (int64) (dbentry->n_tuples_returned);
1226 
1227  PG_RETURN_INT64(result);
1228 }
1229 
1230 
1231 Datum
1233 {
1234  Oid dbid = PG_GETARG_OID(0);
1235  int64 result;
1236  PgStat_StatDBEntry *dbentry;
1237 
1238  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1239  result = 0;
1240  else
1241  result = (int64) (dbentry->n_tuples_fetched);
1242 
1243  PG_RETURN_INT64(result);
1244 }
1245 
1246 
1247 Datum
1249 {
1250  Oid dbid = PG_GETARG_OID(0);
1251  int64 result;
1252  PgStat_StatDBEntry *dbentry;
1253 
1254  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1255  result = 0;
1256  else
1257  result = (int64) (dbentry->n_tuples_inserted);
1258 
1259  PG_RETURN_INT64(result);
1260 }
1261 
1262 
1263 Datum
1265 {
1266  Oid dbid = PG_GETARG_OID(0);
1267  int64 result;
1268  PgStat_StatDBEntry *dbentry;
1269 
1270  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1271  result = 0;
1272  else
1273  result = (int64) (dbentry->n_tuples_updated);
1274 
1275  PG_RETURN_INT64(result);
1276 }
1277 
1278 
1279 Datum
1281 {
1282  Oid dbid = PG_GETARG_OID(0);
1283  int64 result;
1284  PgStat_StatDBEntry *dbentry;
1285 
1286  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1287  result = 0;
1288  else
1289  result = (int64) (dbentry->n_tuples_deleted);
1290 
1291  PG_RETURN_INT64(result);
1292 }
1293 
1294 Datum
1296 {
1297  Oid dbid = PG_GETARG_OID(0);
1299  PgStat_StatDBEntry *dbentry;
1300 
1301  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1302  result = 0;
1303  else
1304  result = dbentry->stat_reset_timestamp;
1305 
1306  if (result == 0)
1307  PG_RETURN_NULL();
1308  else
1309  PG_RETURN_TIMESTAMPTZ(result);
1310 }
1311 
1312 Datum
1314 {
1315  Oid dbid = PG_GETARG_OID(0);
1316  int64 result;
1317  PgStat_StatDBEntry *dbentry;
1318 
1319  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1320  result = 0;
1321  else
1322  result = dbentry->n_temp_files;
1323 
1324  PG_RETURN_INT64(result);
1325 }
1326 
1327 
1328 Datum
1330 {
1331  Oid dbid = PG_GETARG_OID(0);
1332  int64 result;
1333  PgStat_StatDBEntry *dbentry;
1334 
1335  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1336  result = 0;
1337  else
1338  result = dbentry->n_temp_bytes;
1339 
1340  PG_RETURN_INT64(result);
1341 }
1342 
1343 Datum
1345 {
1346  Oid dbid = PG_GETARG_OID(0);
1347  int64 result;
1348  PgStat_StatDBEntry *dbentry;
1349 
1350  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1351  result = 0;
1352  else
1353  result = (int64) (dbentry->n_conflict_tablespace);
1354 
1355  PG_RETURN_INT64(result);
1356 }
1357 
1358 Datum
1360 {
1361  Oid dbid = PG_GETARG_OID(0);
1362  int64 result;
1363  PgStat_StatDBEntry *dbentry;
1364 
1365  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1366  result = 0;
1367  else
1368  result = (int64) (dbentry->n_conflict_lock);
1369 
1370  PG_RETURN_INT64(result);
1371 }
1372 
1373 Datum
1375 {
1376  Oid dbid = PG_GETARG_OID(0);
1377  int64 result;
1378  PgStat_StatDBEntry *dbentry;
1379 
1380  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1381  result = 0;
1382  else
1383  result = (int64) (dbentry->n_conflict_snapshot);
1384 
1385  PG_RETURN_INT64(result);
1386 }
1387 
1388 Datum
1390 {
1391  Oid dbid = PG_GETARG_OID(0);
1392  int64 result;
1393  PgStat_StatDBEntry *dbentry;
1394 
1395  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1396  result = 0;
1397  else
1398  result = (int64) (dbentry->n_conflict_bufferpin);
1399 
1400  PG_RETURN_INT64(result);
1401 }
1402 
1403 Datum
1405 {
1406  Oid dbid = PG_GETARG_OID(0);
1407  int64 result;
1408  PgStat_StatDBEntry *dbentry;
1409 
1410  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1411  result = 0;
1412  else
1413  result = (int64) (dbentry->n_conflict_startup_deadlock);
1414 
1415  PG_RETURN_INT64(result);
1416 }
1417 
1418 Datum
1420 {
1421  Oid dbid = PG_GETARG_OID(0);
1422  int64 result;
1423  PgStat_StatDBEntry *dbentry;
1424 
1425  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1426  result = 0;
1427  else
1428  result = (int64) (
1429  dbentry->n_conflict_tablespace +
1430  dbentry->n_conflict_lock +
1431  dbentry->n_conflict_snapshot +
1432  dbentry->n_conflict_bufferpin +
1433  dbentry->n_conflict_startup_deadlock);
1434 
1435  PG_RETURN_INT64(result);
1436 }
1437 
1438 Datum
1440 {
1441  Oid dbid = PG_GETARG_OID(0);
1442  int64 result;
1443  PgStat_StatDBEntry *dbentry;
1444 
1445  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1446  result = 0;
1447  else
1448  result = (int64) (dbentry->n_deadlocks);
1449 
1450  PG_RETURN_INT64(result);
1451 }
1452 
1453 Datum
1455 {
1456  Oid dbid = PG_GETARG_OID(0);
1457  double result;
1458  PgStat_StatDBEntry *dbentry;
1459 
1460  /* convert counter from microsec to millisec for display */
1461  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1462  result = 0;
1463  else
1464  result = ((double) dbentry->n_block_read_time) / 1000.0;
1465 
1466  PG_RETURN_FLOAT8(result);
1467 }
1468 
1469 Datum
1471 {
1472  Oid dbid = PG_GETARG_OID(0);
1473  double result;
1474  PgStat_StatDBEntry *dbentry;
1475 
1476  /* convert counter from microsec to millisec for display */
1477  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1478  result = 0;
1479  else
1480  result = ((double) dbentry->n_block_write_time) / 1000.0;
1481 
1482  PG_RETURN_FLOAT8(result);
1483 }
1484 
1485 Datum
1487 {
1488  PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1489 }
1490 
1491 Datum
1493 {
1494  PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1495 }
1496 
1497 Datum
1499 {
1500  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1501 }
1502 
1503 Datum
1505 {
1506  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1507 }
1508 
1509 Datum
1511 {
1512  PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1513 }
1514 
1515 Datum
1517 {
1518  /* time is already in msec, just convert to double for presentation */
1519  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1520 }
1521 
1522 Datum
1524 {
1525  /* time is already in msec, just convert to double for presentation */
1526  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1527 }
1528 
1529 Datum
1531 {
1532  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1533 }
1534 
1535 Datum
1537 {
1538  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1539 }
1540 
1541 Datum
1543 {
1544  PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1545 }
1546 
1547 Datum
1549 {
1550  PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1551 }
1552 
1553 Datum
1555 {
1556  Oid relid = PG_GETARG_OID(0);
1557  int64 result;
1558  PgStat_TableStatus *tabentry;
1559 
1560  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1561  result = 0;
1562  else
1563  result = (int64) (tabentry->t_counts.t_numscans);
1564 
1565  PG_RETURN_INT64(result);
1566 }
1567 
1568 Datum
1570 {
1571  Oid relid = PG_GETARG_OID(0);
1572  int64 result;
1573  PgStat_TableStatus *tabentry;
1574 
1575  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1576  result = 0;
1577  else
1578  result = (int64) (tabentry->t_counts.t_tuples_returned);
1579 
1580  PG_RETURN_INT64(result);
1581 }
1582 
1583 Datum
1585 {
1586  Oid relid = PG_GETARG_OID(0);
1587  int64 result;
1588  PgStat_TableStatus *tabentry;
1589 
1590  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1591  result = 0;
1592  else
1593  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1594 
1595  PG_RETURN_INT64(result);
1596 }
1597 
1598 Datum
1600 {
1601  Oid relid = PG_GETARG_OID(0);
1602  int64 result;
1603  PgStat_TableStatus *tabentry;
1605 
1606  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1607  result = 0;
1608  else
1609  {
1610  result = tabentry->t_counts.t_tuples_inserted;
1611  /* live subtransactions' counts aren't in t_tuples_inserted yet */
1612  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1613  result += trans->tuples_inserted;
1614  }
1615 
1616  PG_RETURN_INT64(result);
1617 }
1618 
1619 Datum
1621 {
1622  Oid relid = PG_GETARG_OID(0);
1623  int64 result;
1624  PgStat_TableStatus *tabentry;
1626 
1627  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1628  result = 0;
1629  else
1630  {
1631  result = tabentry->t_counts.t_tuples_updated;
1632  /* live subtransactions' counts aren't in t_tuples_updated yet */
1633  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1634  result += trans->tuples_updated;
1635  }
1636 
1637  PG_RETURN_INT64(result);
1638 }
1639 
1640 Datum
1642 {
1643  Oid relid = PG_GETARG_OID(0);
1644  int64 result;
1645  PgStat_TableStatus *tabentry;
1647 
1648  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1649  result = 0;
1650  else
1651  {
1652  result = tabentry->t_counts.t_tuples_deleted;
1653  /* live subtransactions' counts aren't in t_tuples_deleted yet */
1654  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1655  result += trans->tuples_deleted;
1656  }
1657 
1658  PG_RETURN_INT64(result);
1659 }
1660 
1661 Datum
1663 {
1664  Oid relid = PG_GETARG_OID(0);
1665  int64 result;
1666  PgStat_TableStatus *tabentry;
1667 
1668  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1669  result = 0;
1670  else
1671  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
1672 
1673  PG_RETURN_INT64(result);
1674 }
1675 
1676 Datum
1678 {
1679  Oid relid = PG_GETARG_OID(0);
1680  int64 result;
1681  PgStat_TableStatus *tabentry;
1682 
1683  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1684  result = 0;
1685  else
1686  result = (int64) (tabentry->t_counts.t_blocks_fetched);
1687 
1688  PG_RETURN_INT64(result);
1689 }
1690 
1691 Datum
1693 {
1694  Oid relid = PG_GETARG_OID(0);
1695  int64 result;
1696  PgStat_TableStatus *tabentry;
1697 
1698  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1699  result = 0;
1700  else
1701  result = (int64) (tabentry->t_counts.t_blocks_hit);
1702 
1703  PG_RETURN_INT64(result);
1704 }
1705 
1706 Datum
1708 {
1709  Oid funcid = PG_GETARG_OID(0);
1710  PgStat_BackendFunctionEntry *funcentry;
1711 
1712  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1713  PG_RETURN_NULL();
1714  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
1715 }
1716 
1717 Datum
1719 {
1720  Oid funcid = PG_GETARG_OID(0);
1721  PgStat_BackendFunctionEntry *funcentry;
1722 
1723  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1724  PG_RETURN_NULL();
1726 }
1727 
1728 Datum
1730 {
1731  Oid funcid = PG_GETARG_OID(0);
1732  PgStat_BackendFunctionEntry *funcentry;
1733 
1734  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1735  PG_RETURN_NULL();
1737 }
1738 
1739 
1740 /* Get the timestamp of the current statistics snapshot */
1741 Datum
1743 {
1744  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
1745 }
1746 
1747 /* Discard the active statistics snapshot */
1748 Datum
1750 {
1752 
1753  PG_RETURN_VOID();
1754 }
1755 
1756 
1757 /* Reset all counters for the current database */
1758 Datum
1760 {
1762 
1763  PG_RETURN_VOID();
1764 }
1765 
1766 /* Reset some shared cluster-wide counters */
1767 Datum
1769 {
1770  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1771 
1773 
1774  PG_RETURN_VOID();
1775 }
1776 
1777 /* Reset a single counter in the current database */
1778 Datum
1780 {
1781  Oid taboid = PG_GETARG_OID(0);
1782 
1784 
1785  PG_RETURN_VOID();
1786 }
1787 
1788 Datum
1790 {
1791  Oid funcoid = PG_GETARG_OID(0);
1792 
1794 
1795  PG_RETURN_VOID();
1796 }
1797 
1798 Datum
1800 {
1801  TupleDesc tupdesc;
1802  Datum values[7];
1803  bool nulls[7];
1804  PgStat_ArchiverStats *archiver_stats;
1805 
1806  /* Initialise values and NULL flags arrays */
1807  MemSet(values, 0, sizeof(values));
1808  MemSet(nulls, 0, sizeof(nulls));
1809 
1810  /* Initialise attributes information in the tuple descriptor */
1811  tupdesc = CreateTemplateTupleDesc(7, false);
1812  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
1813  INT8OID, -1, 0);
1814  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
1815  TEXTOID, -1, 0);
1816  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
1817  TIMESTAMPTZOID, -1, 0);
1818  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
1819  INT8OID, -1, 0);
1820  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
1821  TEXTOID, -1, 0);
1822  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
1823  TIMESTAMPTZOID, -1, 0);
1824  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
1825  TIMESTAMPTZOID, -1, 0);
1826 
1827  BlessTupleDesc(tupdesc);
1828 
1829  /* Get statistics about the archiver process */
1830  archiver_stats = pgstat_fetch_stat_archiver();
1831 
1832  /* Fill values and NULLs */
1833  values[0] = Int64GetDatum(archiver_stats->archived_count);
1834  if (*(archiver_stats->last_archived_wal) == '\0')
1835  nulls[1] = true;
1836  else
1837  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
1838 
1839  if (archiver_stats->last_archived_timestamp == 0)
1840  nulls[2] = true;
1841  else
1842  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
1843 
1844  values[3] = Int64GetDatum(archiver_stats->failed_count);
1845  if (*(archiver_stats->last_failed_wal) == '\0')
1846  nulls[4] = true;
1847  else
1848  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
1849 
1850  if (archiver_stats->last_failed_timestamp == 0)
1851  nulls[5] = true;
1852  else
1853  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
1854 
1855  if (archiver_stats->stat_reset_timestamp == 0)
1856  nulls[6] = true;
1857  else
1858  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
1859 
1860  /* Returns the record as Datum */
1862  heap_form_tuple(tupdesc, values, nulls)));
1863 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
Datum pg_stat_get_db_conflict_snapshot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1374
Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1264
#define TIMESTAMPTZOID
Definition: pg_type.h:525
Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1152
bool ssl_compression
Definition: pgstat.h:936
Datum pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1439
Datum pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:346
Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1419
Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1542
Datum pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:923
PgStat_Counter analyze_count
Definition: pgstat.h:641
char ssl_cipher[NAMEDATALEN]
Definition: pgstat.h:938
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:672
Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:439
instr_time f_self_time
Definition: pgstat.h:463
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Datum pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:182
Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1131
PgStat_Counter archived_count
Definition: pgstat.h:667
Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1789
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Oid st_progress_command_target
Definition: pgstat.h:1015
Datum pg_stat_get_archiver(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1799
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
int MyProcPid
Definition: globals.c:38
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
Datum pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1523
PgStat_Counter tuples_updated
Definition: pgstat.h:168
PgStat_Counter tuples_returned
Definition: pgstat.h:621
Datum pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1620
Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:265
Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:118
uint32 wait_event_info
Definition: proc.h:172
Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1707
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: pgstat.c:3331
#define DatumGetInt32(X)
Definition: postgres.h:478
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:102
Oid GetUserId(void)
Definition: miscinit.c:283
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2470
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:983
#define TEXTOID
Definition: pg_type.h:324
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2342
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
PgStat_Counter n_blocks_hit
Definition: pgstat.h:581
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:591
#define PG_STAT_GET_ACTIVITY_COLS
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:592
const char * pgstat_get_backend_desc(BackendType backendType)
Definition: pgstat.c:4024
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:107
Datum pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1470
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:199
Datum pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:331
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:642
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
Datum pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1516
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4813
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
PgStat_Counter n_temp_files
Definition: pgstat.h:593
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1328
struct sockaddr_storage addr
Definition: pqcomm.h:64
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1718
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
TransactionId backend_xmin
Definition: pgstat.h:1081
Datum pg_stat_reset(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1759
char ssl_version[NAMEDATALEN]
Definition: pgstat.h:937
Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:214
Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:54
PgStat_Counter n_tuples_returned
Definition: pgstat.h:582
PgStat_Counter vacuum_count
Definition: pgstat.h:637
SockAddr st_clientaddr
Definition: pgstat.h:989
Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1014
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1742
Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1510
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2387
TimestampTz stat_reset_timestamp
Definition: pgstat.h:599
return result
Definition: formatting.c:1632
Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:70
Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:396
Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:283
instr_time f_total_time
Definition: pgstat.h:462
Datum pg_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:858
Datum pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1692
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2576
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1216
Datum pg_stat_get_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:38
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
ProgressCommandType st_progress_command
Definition: pgstat.h:1014
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PgStat_Counter numscans
Definition: pgstat.h:619
#define lengthof(array)
Definition: c.h:562
PgStat_TableCounts t_counts
Definition: pgstat.h:158
unsigned int Oid
Definition: postgres_ext.h:31
Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1548
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:627
PgStat_Counter f_total_time
Definition: pgstat.h:657
Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1329
BackendState st_state
Definition: pgstat.h:997
PgStat_Counter t_tuples_returned
Definition: pgstat.h:101
Datum pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:944
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
char * st_clienthostname
Definition: pgstat.h:990
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
PgStat_Counter tuples_inserted
Definition: pgstat.h:624
PgStat_Counter f_numcalls
Definition: pgstat.h:461
Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:361
Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1729
Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1498
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
Definition: pgstat.c:2524
Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1280
signed int int32
Definition: c.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
PgStat_Counter n_xact_commit
Definition: pgstat.h:578
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
Definition: pgstat.c:2559
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:1838
Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:102
Oid st_databaseid
Definition: pgstat.h:987
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:918
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:580
Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1295
PgStat_Counter n_live_tuples
Definition: pgstat.h:629
Datum pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1454
Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:878
PgStat_FunctionCounts f_counts
Definition: pgstat.h:473
#define NI_MAXHOST
Definition: getaddrinfo.h:88
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:588
Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1168
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:229
TransactionId backend_xid
Definition: pgstat.h:1075
TimestampTz vacuum_timestamp
Definition: pgstat.h:636
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1313
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter n_dead_tuples
Definition: pgstat.h:630
Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:247
Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1083
Datum pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1584
Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1536
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
PgStat_Counter f_numcalls
Definition: pgstat.h:655
Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1569
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2414
Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:150
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:951
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:584
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1749
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1389
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: pgstat.c:3386
Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1492
#define CStringGetDatum(X)
Definition: postgres.h:584
Datum pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1359
#define NI_MAXSERV
Definition: getaddrinfo.h:91
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1633
PgStat_Counter tuples_fetched
Definition: pgstat.h:622
unsigned int uint32
Definition: c.h:268
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:590
Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:966
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:904
TimestampTz st_state_start_timestamp
Definition: pgstat.h:984
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:586
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
PgStat_Counter n_conflict_lock
Definition: pgstat.h:589
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:497
char * st_appname
Definition: pgstat.h:1000
#define ereport(elevel, rest)
Definition: elog.h:122
PgStat_Counter t_blocks_hit
Definition: pgstat.h:115
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
Definition: pgstat.c:1359
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter n_xact_rollback
Definition: pgstat.h:579
Datum pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1662
Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:992
Datum pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:301
Datum pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:316
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:106
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define TransactionIdGetDatum(X)
Definition: postgres.h:527
Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:86
Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1184
PgBackendStatus backendStatus
Definition: pgstat.h:1069
uintptr_t Datum
Definition: postgres.h:372
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:134
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1677
BackendType st_backendType
Definition: pgstat.h:978
PgStat_Counter blocks_hit
Definition: pgstat.h:634
int work_mem
Definition: globals.c:112
TimestampTz analyze_timestamp
Definition: pgstat.h:640
Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:384
TimestampTz last_failed_timestamp
Definition: pgstat.h:674
PgStat_Counter changes_since_analyze
Definition: pgstat.h:631
#define DEFAULT_ROLE_READ_ALL_STATS
Definition: pg_authid.h:108
PgStat_Counter t_numscans
Definition: pgstat.h:99
#define BoolGetDatum(X)
Definition: postgres.h:408
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
PgStat_MsgBgWriter bgwriterStats
#define InvalidOid
Definition: postgres_ext.h:36
int allowedModes
Definition: execnodes.h:268
PgStat_Counter n_tuples_updated
Definition: pgstat.h:585
#define INT8OID
Definition: pg_type.h:304
#define PG_RETURN_VOID()
Definition: fmgr.h:309
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4837
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1232
SetFunctionReturnMode returnMode
Definition: execnodes.h:270
text * cstring_to_text(const char *s)
Definition: varlena.c:149
Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1530
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2543
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
ProgressCommandType
Definition: pgstat.h:912
char * st_activity
Definition: pgstat.h:1003
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2502
Datum pg_stat_reset_shared(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1768
PgStat_Counter failed_count
Definition: pgstat.h:671
PgStat_Counter n_block_write_time
Definition: pgstat.h:597
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1016
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:80
PgStat_Counter tuples_deleted
Definition: pgstat.h:626
Datum pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:372
void pgstat_clear_snapshot(void)
Definition: pgstat.c:5519
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
PgStat_Counter tuples_updated
Definition: pgstat.h:625
Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1036
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:1504
Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1248
Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1779
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:202
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:638
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
PgStat_Counter n_block_read_time
Definition: pgstat.h:596
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1200
int st_procpid
Definition: pgstat.h:975
Tuplestorestate * setResult
Definition: execnodes.h:273
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:643
Datum pg_stat_get_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:541
static Datum values[MAXATTR]
Definition: bootstrap.c:163
char * text_to_cstring(const text *t)
Definition: varlena.c:182
ExprContext * econtext
Definition: execnodes.h:266
#define Int32GetDatum(X)
Definition: postgres.h:485
TimestampTz stat_reset_timestamp
Definition: pgstat.h:675
TimestampTz last_archived_timestamp
Definition: pgstat.h:670
Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:865
void * user_fctx
Definition: funcapi.h:90
TupleDesc setDesc
Definition: execnodes.h:274
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:382
int errmsg(const char *fmt,...)
Definition: elog.c:797
PgStat_Counter n_deadlocks
Definition: pgstat.h:595
Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1486
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:982
#define PG_STAT_GET_PROGRESS_COLS
int i
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:32
Datum pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1599
Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1554
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:198
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1404
#define elog
Definition: elog.h:219
Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:891
#define TransactionIdIsValid(xid)
Definition: transam.h:41
PgStat_Counter f_self_time
Definition: pgstat.h:658
Definition: proc.h:94
PgStat_Counter blocks_fetched
Definition: pgstat.h:633
Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:166
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:981
Datum pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1641
Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1504
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:994
int16 AttrNumber
Definition: attnum.h:21
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
void pgstat_reset_counters(void)
Definition: pgstat.c:1306
#define PG_RETURN_NULL()
Definition: fmgr.h:305
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:114
PgStat_Counter n_tuples_fetched
Definition: pgstat.h:583
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:668
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:639
char ssl_clientdn[NAMEDATALEN]
Definition: pgstat.h:939
Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1344
PgStat_Counter n_temp_bytes
Definition: pgstat.h:594
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287