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