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