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