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-2021, 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 "replication/slot.h"
28 #include "storage/proc.h"
29 #include "storage/procarray.h"
30 #include "utils/acl.h"
31 #include "utils/builtins.h"
32 #include "utils/inet.h"
33 #include "utils/timestamp.h"
34 
35 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
36 
37 #define HAS_PGSTAT_PERMISSIONS(role) (is_member_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
38 
39 /* Global bgwriter statistics, from bgwriter.c */
41 
42 Datum
44 {
45  Oid relid = PG_GETARG_OID(0);
46  int64 result;
47  PgStat_StatTabEntry *tabentry;
48 
49  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
50  result = 0;
51  else
52  result = (int64) (tabentry->numscans);
53 
54  PG_RETURN_INT64(result);
55 }
56 
57 
58 Datum
60 {
61  Oid relid = PG_GETARG_OID(0);
62  int64 result;
63  PgStat_StatTabEntry *tabentry;
64 
65  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
66  result = 0;
67  else
68  result = (int64) (tabentry->tuples_returned);
69 
70  PG_RETURN_INT64(result);
71 }
72 
73 
74 Datum
76 {
77  Oid relid = PG_GETARG_OID(0);
78  int64 result;
79  PgStat_StatTabEntry *tabentry;
80 
81  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
82  result = 0;
83  else
84  result = (int64) (tabentry->tuples_fetched);
85 
86  PG_RETURN_INT64(result);
87 }
88 
89 
90 Datum
92 {
93  Oid relid = PG_GETARG_OID(0);
94  int64 result;
95  PgStat_StatTabEntry *tabentry;
96 
97  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
98  result = 0;
99  else
100  result = (int64) (tabentry->tuples_inserted);
101 
102  PG_RETURN_INT64(result);
103 }
104 
105 
106 Datum
108 {
109  Oid relid = PG_GETARG_OID(0);
110  int64 result;
111  PgStat_StatTabEntry *tabentry;
112 
113  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
114  result = 0;
115  else
116  result = (int64) (tabentry->tuples_updated);
117 
118  PG_RETURN_INT64(result);
119 }
120 
121 
122 Datum
124 {
125  Oid relid = PG_GETARG_OID(0);
126  int64 result;
127  PgStat_StatTabEntry *tabentry;
128 
129  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
130  result = 0;
131  else
132  result = (int64) (tabentry->tuples_deleted);
133 
134  PG_RETURN_INT64(result);
135 }
136 
137 
138 Datum
140 {
141  Oid relid = PG_GETARG_OID(0);
142  int64 result;
143  PgStat_StatTabEntry *tabentry;
144 
145  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
146  result = 0;
147  else
148  result = (int64) (tabentry->tuples_hot_updated);
149 
150  PG_RETURN_INT64(result);
151 }
152 
153 
154 Datum
156 {
157  Oid relid = PG_GETARG_OID(0);
158  int64 result;
159  PgStat_StatTabEntry *tabentry;
160 
161  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
162  result = 0;
163  else
164  result = (int64) (tabentry->n_live_tuples);
165 
166  PG_RETURN_INT64(result);
167 }
168 
169 
170 Datum
172 {
173  Oid relid = PG_GETARG_OID(0);
174  int64 result;
175  PgStat_StatTabEntry *tabentry;
176 
177  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
178  result = 0;
179  else
180  result = (int64) (tabentry->n_dead_tuples);
181 
182  PG_RETURN_INT64(result);
183 }
184 
185 
186 Datum
188 {
189  Oid relid = PG_GETARG_OID(0);
190  int64 result;
191  PgStat_StatTabEntry *tabentry;
192 
193  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
194  result = 0;
195  else
196  result = (int64) (tabentry->changes_since_analyze);
197 
198  PG_RETURN_INT64(result);
199 }
200 
201 
202 Datum
204 {
205  Oid relid = PG_GETARG_OID(0);
206  int64 result;
207  PgStat_StatTabEntry *tabentry;
208 
209  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
210  result = 0;
211  else
212  result = (int64) (tabentry->inserts_since_vacuum);
213 
214  PG_RETURN_INT64(result);
215 }
216 
217 
218 Datum
220 {
221  Oid relid = PG_GETARG_OID(0);
222  int64 result;
223  PgStat_StatTabEntry *tabentry;
224 
225  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
226  result = 0;
227  else
228  result = (int64) (tabentry->blocks_fetched);
229 
230  PG_RETURN_INT64(result);
231 }
232 
233 
234 Datum
236 {
237  Oid relid = PG_GETARG_OID(0);
238  int64 result;
239  PgStat_StatTabEntry *tabentry;
240 
241  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
242  result = 0;
243  else
244  result = (int64) (tabentry->blocks_hit);
245 
246  PG_RETURN_INT64(result);
247 }
248 
249 Datum
251 {
252  Oid relid = PG_GETARG_OID(0);
253  TimestampTz result;
254  PgStat_StatTabEntry *tabentry;
255 
256  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
257  result = 0;
258  else
259  result = tabentry->vacuum_timestamp;
260 
261  if (result == 0)
262  PG_RETURN_NULL();
263  else
264  PG_RETURN_TIMESTAMPTZ(result);
265 }
266 
267 Datum
269 {
270  Oid relid = PG_GETARG_OID(0);
271  TimestampTz result;
272  PgStat_StatTabEntry *tabentry;
273 
274  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
275  result = 0;
276  else
277  result = tabentry->autovac_vacuum_timestamp;
278 
279  if (result == 0)
280  PG_RETURN_NULL();
281  else
282  PG_RETURN_TIMESTAMPTZ(result);
283 }
284 
285 Datum
287 {
288  Oid relid = PG_GETARG_OID(0);
289  TimestampTz result;
290  PgStat_StatTabEntry *tabentry;
291 
292  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
293  result = 0;
294  else
295  result = tabentry->analyze_timestamp;
296 
297  if (result == 0)
298  PG_RETURN_NULL();
299  else
300  PG_RETURN_TIMESTAMPTZ(result);
301 }
302 
303 Datum
305 {
306  Oid relid = PG_GETARG_OID(0);
307  TimestampTz result;
308  PgStat_StatTabEntry *tabentry;
309 
310  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
311  result = 0;
312  else
313  result = tabentry->autovac_analyze_timestamp;
314 
315  if (result == 0)
316  PG_RETURN_NULL();
317  else
318  PG_RETURN_TIMESTAMPTZ(result);
319 }
320 
321 Datum
323 {
324  Oid relid = PG_GETARG_OID(0);
325  int64 result;
326  PgStat_StatTabEntry *tabentry;
327 
328  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
329  result = 0;
330  else
331  result = (int64) (tabentry->vacuum_count);
332 
333  PG_RETURN_INT64(result);
334 }
335 
336 Datum
338 {
339  Oid relid = PG_GETARG_OID(0);
340  int64 result;
341  PgStat_StatTabEntry *tabentry;
342 
343  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
344  result = 0;
345  else
346  result = (int64) (tabentry->autovac_vacuum_count);
347 
348  PG_RETURN_INT64(result);
349 }
350 
351 Datum
353 {
354  Oid relid = PG_GETARG_OID(0);
355  int64 result;
356  PgStat_StatTabEntry *tabentry;
357 
358  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
359  result = 0;
360  else
361  result = (int64) (tabentry->analyze_count);
362 
363  PG_RETURN_INT64(result);
364 }
365 
366 Datum
368 {
369  Oid relid = PG_GETARG_OID(0);
370  int64 result;
371  PgStat_StatTabEntry *tabentry;
372 
373  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
374  result = 0;
375  else
376  result = (int64) (tabentry->autovac_analyze_count);
377 
378  PG_RETURN_INT64(result);
379 }
380 
381 Datum
383 {
384  Oid funcid = PG_GETARG_OID(0);
385  PgStat_StatFuncEntry *funcentry;
386 
387  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
388  PG_RETURN_NULL();
389  PG_RETURN_INT64(funcentry->f_numcalls);
390 }
391 
392 Datum
394 {
395  Oid funcid = PG_GETARG_OID(0);
396  PgStat_StatFuncEntry *funcentry;
397 
398  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
399  PG_RETURN_NULL();
400  /* convert counter from microsec to millisec for display */
401  PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
402 }
403 
404 Datum
406 {
407  Oid funcid = PG_GETARG_OID(0);
408  PgStat_StatFuncEntry *funcentry;
409 
410  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
411  PG_RETURN_NULL();
412  /* convert counter from microsec to millisec for display */
413  PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
414 }
415 
416 Datum
418 {
419  FuncCallContext *funcctx;
420  int *fctx;
421  int32 result;
422 
423  /* stuff done only on the first call of the function */
424  if (SRF_IS_FIRSTCALL())
425  {
426  /* create a function context for cross-call persistence */
427  funcctx = SRF_FIRSTCALL_INIT();
428 
430  2 * sizeof(int));
431  funcctx->user_fctx = fctx;
432 
433  fctx[0] = 0;
434  fctx[1] = pgstat_fetch_stat_numbackends();
435  }
436 
437  /* stuff done on every call of the function */
438  funcctx = SRF_PERCALL_SETUP();
439  fctx = funcctx->user_fctx;
440 
441  fctx[0] += 1;
442  result = fctx[0];
443 
444  if (result <= fctx[1])
445  {
446  /* do when there is more left to send */
447  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
448  }
449  else
450  {
451  /* do when there is no more left */
452  SRF_RETURN_DONE(funcctx);
453  }
454 }
455 
456 /*
457  * Returns command progress information for the named command.
458  */
459 Datum
461 {
462 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
463  int num_backends = pgstat_fetch_stat_numbackends();
464  int curr_backend;
465  char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
466  ProgressCommandType cmdtype;
467  TupleDesc tupdesc;
468  Tuplestorestate *tupstore;
469  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
470  MemoryContext per_query_ctx;
471  MemoryContext oldcontext;
472 
473  /* check to see if caller supports us returning a tuplestore */
474  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
475  ereport(ERROR,
476  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
477  errmsg("set-valued function called in context that cannot accept a set")));
478  if (!(rsinfo->allowedModes & SFRM_Materialize))
479  ereport(ERROR,
480  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
481  errmsg("materialize mode required, but it is not allowed in this context")));
482 
483  /* Build a tuple descriptor for our result type */
484  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
485  elog(ERROR, "return type must be a row type");
486 
487  /* Translate command name into command type code. */
488  if (pg_strcasecmp(cmd, "VACUUM") == 0)
489  cmdtype = PROGRESS_COMMAND_VACUUM;
490  else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
491  cmdtype = PROGRESS_COMMAND_ANALYZE;
492  else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
493  cmdtype = PROGRESS_COMMAND_CLUSTER;
494  else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
496  else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
497  cmdtype = PROGRESS_COMMAND_BASEBACKUP;
498  else if (pg_strcasecmp(cmd, "COPY") == 0)
499  cmdtype = PROGRESS_COMMAND_COPY;
500  else
501  ereport(ERROR,
502  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
503  errmsg("invalid command name: \"%s\"", cmd)));
504 
505  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
506  oldcontext = MemoryContextSwitchTo(per_query_ctx);
507 
508  tupstore = tuplestore_begin_heap(true, false, work_mem);
509  rsinfo->returnMode = SFRM_Materialize;
510  rsinfo->setResult = tupstore;
511  rsinfo->setDesc = tupdesc;
512  MemoryContextSwitchTo(oldcontext);
513 
514  /* 1-based index */
515  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
516  {
517  LocalPgBackendStatus *local_beentry;
518  PgBackendStatus *beentry;
520  bool nulls[PG_STAT_GET_PROGRESS_COLS];
521  int i;
522 
523  MemSet(values, 0, sizeof(values));
524  MemSet(nulls, 0, sizeof(nulls));
525 
526  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
527 
528  if (!local_beentry)
529  continue;
530 
531  beentry = &local_beentry->backendStatus;
532 
533  /*
534  * Report values for only those backends which are running the given
535  * command.
536  */
537  if (!beentry || beentry->st_progress_command != cmdtype)
538  continue;
539 
540  /* Value available to all callers */
541  values[0] = Int32GetDatum(beentry->st_procpid);
542  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
543 
544  /* show rest of the values including relid only to role members */
545  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
546  {
547  values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
548  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
549  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
550  }
551  else
552  {
553  nulls[2] = true;
554  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
555  nulls[i + 3] = true;
556  }
557 
558  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
559  }
560 
561  /* clean up and return the tuplestore */
562  tuplestore_donestoring(tupstore);
563 
564  return (Datum) 0;
565 }
566 
567 /*
568  * Returns activity of PG backends.
569  */
570 Datum
572 {
573 #define PG_STAT_GET_ACTIVITY_COLS 30
574  int num_backends = pgstat_fetch_stat_numbackends();
575  int curr_backend;
576  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
577  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
578  TupleDesc tupdesc;
579  Tuplestorestate *tupstore;
580  MemoryContext per_query_ctx;
581  MemoryContext oldcontext;
582 
583  /* check to see if caller supports us returning a tuplestore */
584  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
585  ereport(ERROR,
586  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
587  errmsg("set-valued function called in context that cannot accept a set")));
588  if (!(rsinfo->allowedModes & SFRM_Materialize))
589  ereport(ERROR,
590  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
591  errmsg("materialize mode required, but it is not allowed in this context")));
592 
593  /* Build a tuple descriptor for our result type */
594  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
595  elog(ERROR, "return type must be a row type");
596 
597  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
598  oldcontext = MemoryContextSwitchTo(per_query_ctx);
599 
600  tupstore = tuplestore_begin_heap(true, false, work_mem);
601  rsinfo->returnMode = SFRM_Materialize;
602  rsinfo->setResult = tupstore;
603  rsinfo->setDesc = tupdesc;
604 
605  MemoryContextSwitchTo(oldcontext);
606 
607  /* 1-based index */
608  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
609  {
610  /* for each row */
612  bool nulls[PG_STAT_GET_ACTIVITY_COLS];
613  LocalPgBackendStatus *local_beentry;
614  PgBackendStatus *beentry;
615  PGPROC *proc;
616  const char *wait_event_type = NULL;
617  const char *wait_event = NULL;
618 
619  MemSet(values, 0, sizeof(values));
620  MemSet(nulls, 0, sizeof(nulls));
621 
622  /* Get the next one in the list */
623  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
624  if (!local_beentry)
625  {
626  int i;
627 
628  /* Ignore missing entries if looking for specific PID */
629  if (pid != -1)
630  continue;
631 
632  for (i = 0; i < lengthof(nulls); i++)
633  nulls[i] = true;
634 
635  nulls[5] = false;
636  values[5] = CStringGetTextDatum("<backend information not available>");
637 
638  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
639  continue;
640  }
641 
642  beentry = &local_beentry->backendStatus;
643 
644  /* If looking for specific PID, ignore all the others */
645  if (pid != -1 && beentry->st_procpid != pid)
646  continue;
647 
648  /* Values available to all callers */
649  if (beentry->st_databaseid != InvalidOid)
650  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
651  else
652  nulls[0] = true;
653 
654  values[1] = Int32GetDatum(beentry->st_procpid);
655 
656  if (beentry->st_userid != InvalidOid)
657  values[2] = ObjectIdGetDatum(beentry->st_userid);
658  else
659  nulls[2] = true;
660 
661  if (beentry->st_appname)
662  values[3] = CStringGetTextDatum(beentry->st_appname);
663  else
664  nulls[3] = true;
665 
666  if (TransactionIdIsValid(local_beentry->backend_xid))
667  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
668  else
669  nulls[15] = true;
670 
671  if (TransactionIdIsValid(local_beentry->backend_xmin))
672  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
673  else
674  nulls[16] = true;
675 
676  /* Values only available to role member or pg_read_all_stats */
677  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
678  {
679  SockAddr zero_clientaddr;
680  char *clipped_activity;
681 
682  switch (beentry->st_state)
683  {
684  case STATE_IDLE:
685  values[4] = CStringGetTextDatum("idle");
686  break;
687  case STATE_RUNNING:
688  values[4] = CStringGetTextDatum("active");
689  break;
691  values[4] = CStringGetTextDatum("idle in transaction");
692  break;
693  case STATE_FASTPATH:
694  values[4] = CStringGetTextDatum("fastpath function call");
695  break;
697  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
698  break;
699  case STATE_DISABLED:
700  values[4] = CStringGetTextDatum("disabled");
701  break;
702  case STATE_UNDEFINED:
703  nulls[4] = true;
704  break;
705  }
706 
707  clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
708  values[5] = CStringGetTextDatum(clipped_activity);
709  pfree(clipped_activity);
710 
711  /* leader_pid */
712  nulls[28] = true;
713 
714  proc = BackendPidGetProc(beentry->st_procpid);
715 
716  if (proc == NULL && (beentry->st_backendType != B_BACKEND))
717  {
718  /*
719  * For an auxiliary process, retrieve process info from
720  * AuxiliaryProcs stored in shared-memory.
721  */
722  proc = AuxiliaryPidGetProc(beentry->st_procpid);
723  }
724 
725  /*
726  * If a PGPROC entry was retrieved, display wait events and lock
727  * group leader information if any. To avoid extra overhead, no
728  * extra lock is being held, so there is no guarantee of
729  * consistency across multiple rows.
730  */
731  if (proc != NULL)
732  {
733  uint32 raw_wait_event;
734  PGPROC *leader;
735 
736  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
737  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
738  wait_event = pgstat_get_wait_event(raw_wait_event);
739 
740  leader = proc->lockGroupLeader;
741 
742  /*
743  * Show the leader only for active parallel workers. This
744  * leaves the field as NULL for the leader of a parallel
745  * group.
746  */
747  if (leader && leader->pid != beentry->st_procpid)
748  {
749  values[28] = Int32GetDatum(leader->pid);
750  nulls[28] = false;
751  }
752  }
753 
754  if (wait_event_type)
755  values[6] = CStringGetTextDatum(wait_event_type);
756  else
757  nulls[6] = true;
758 
759  if (wait_event)
760  values[7] = CStringGetTextDatum(wait_event);
761  else
762  nulls[7] = true;
763 
764  /*
765  * Don't expose transaction time for walsenders; it confuses
766  * monitoring, particularly because we don't keep the time up-to-
767  * date.
768  */
769  if (beentry->st_xact_start_timestamp != 0 &&
770  beentry->st_backendType != B_WAL_SENDER)
771  values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
772  else
773  nulls[8] = true;
774 
775  if (beentry->st_activity_start_timestamp != 0)
776  values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
777  else
778  nulls[9] = true;
779 
780  if (beentry->st_proc_start_timestamp != 0)
781  values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
782  else
783  nulls[10] = true;
784 
785  if (beentry->st_state_start_timestamp != 0)
786  values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
787  else
788  nulls[11] = true;
789 
790  /* A zeroed client addr means we don't know */
791  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
792  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
793  sizeof(zero_clientaddr)) == 0)
794  {
795  nulls[12] = true;
796  nulls[13] = true;
797  nulls[14] = true;
798  }
799  else
800  {
801  if (beentry->st_clientaddr.addr.ss_family == AF_INET
802 #ifdef HAVE_IPV6
803  || beentry->st_clientaddr.addr.ss_family == AF_INET6
804 #endif
805  )
806  {
807  char remote_host[NI_MAXHOST];
808  char remote_port[NI_MAXSERV];
809  int ret;
810 
811  remote_host[0] = '\0';
812  remote_port[0] = '\0';
813  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
814  beentry->st_clientaddr.salen,
815  remote_host, sizeof(remote_host),
816  remote_port, sizeof(remote_port),
818  if (ret == 0)
819  {
820  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
821  values[12] = DirectFunctionCall1(inet_in,
822  CStringGetDatum(remote_host));
823  if (beentry->st_clienthostname &&
824  beentry->st_clienthostname[0])
825  values[13] = CStringGetTextDatum(beentry->st_clienthostname);
826  else
827  nulls[13] = true;
828  values[14] = Int32GetDatum(atoi(remote_port));
829  }
830  else
831  {
832  nulls[12] = true;
833  nulls[13] = true;
834  nulls[14] = true;
835  }
836  }
837  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
838  {
839  /*
840  * Unix sockets always reports NULL for host and -1 for
841  * port, so it's possible to tell the difference to
842  * connections we have no permissions to view, or with
843  * errors.
844  */
845  nulls[12] = true;
846  nulls[13] = true;
847  values[14] = Int32GetDatum(-1);
848  }
849  else
850  {
851  /* Unknown address type, should never happen */
852  nulls[12] = true;
853  nulls[13] = true;
854  nulls[14] = true;
855  }
856  }
857  /* Add backend type */
858  if (beentry->st_backendType == B_BG_WORKER)
859  {
860  const char *bgw_type;
861 
862  bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
863  if (bgw_type)
864  values[17] = CStringGetTextDatum(bgw_type);
865  else
866  nulls[17] = true;
867  }
868  else
869  values[17] =
871 
872  /* SSL information */
873  if (beentry->st_ssl)
874  {
875  values[18] = BoolGetDatum(true); /* ssl */
876  values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
877  values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
878  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
879 
880  if (beentry->st_sslstatus->ssl_client_dn[0])
881  values[22] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn);
882  else
883  nulls[22] = true;
884 
885  if (beentry->st_sslstatus->ssl_client_serial[0])
886  values[23] = DirectFunctionCall3(numeric_in,
889  Int32GetDatum(-1));
890  else
891  nulls[23] = true;
892 
893  if (beentry->st_sslstatus->ssl_issuer_dn[0])
894  values[24] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn);
895  else
896  nulls[24] = true;
897  }
898  else
899  {
900  values[18] = BoolGetDatum(false); /* ssl */
901  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = true;
902  }
903 
904  /* GSSAPI information */
905  if (beentry->st_gss)
906  {
907  values[25] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
908  values[26] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ);
909  values[27] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
910  }
911  else
912  {
913  values[25] = BoolGetDatum(false); /* gss_auth */
914  nulls[26] = true; /* No GSS principal */
915  values[27] = BoolGetDatum(false); /* GSS Encryption not in
916  * use */
917  }
918  if (beentry->st_query_id == 0)
919  nulls[29] = true;
920  else
921  values[29] = UInt64GetDatum(beentry->st_query_id);
922  }
923  else
924  {
925  /* No permissions to view data about this session */
926  values[5] = CStringGetTextDatum("<insufficient privilege>");
927  nulls[4] = true;
928  nulls[6] = true;
929  nulls[7] = true;
930  nulls[8] = true;
931  nulls[9] = true;
932  nulls[10] = true;
933  nulls[11] = true;
934  nulls[12] = true;
935  nulls[13] = true;
936  nulls[14] = true;
937  nulls[17] = true;
938  nulls[18] = true;
939  nulls[19] = true;
940  nulls[20] = true;
941  nulls[21] = true;
942  nulls[22] = true;
943  nulls[23] = true;
944  nulls[24] = true;
945  nulls[25] = true;
946  nulls[26] = true;
947  nulls[27] = true;
948  nulls[28] = true;
949  nulls[29] = true;
950  }
951 
952  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
953 
954  /* If only a single backend was requested, and we found it, break. */
955  if (pid != -1)
956  break;
957  }
958 
959  /* clean up and return the tuplestore */
960  tuplestore_donestoring(tupstore);
961 
962  return (Datum) 0;
963 }
964 
965 
966 Datum
968 {
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_INT32(beentry->st_procpid);
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_databaseid);
996 }
997 
998 
999 Datum
1001 {
1002  int32 beid = PG_GETARG_INT32(0);
1003  PgBackendStatus *beentry;
1004 
1005  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1006  PG_RETURN_NULL();
1007 
1008  PG_RETURN_OID(beentry->st_userid);
1009 }
1010 
1011 
1012 Datum
1014 {
1015  int32 beid = PG_GETARG_INT32(0);
1016  PgBackendStatus *beentry;
1017  const char *activity;
1018  char *clipped_activity;
1019  text *ret;
1020 
1021  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1022  activity = "<backend information not available>";
1023  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1024  activity = "<insufficient privilege>";
1025  else if (*(beentry->st_activity_raw) == '\0')
1026  activity = "<command string not enabled>";
1027  else
1028  activity = beentry->st_activity_raw;
1029 
1030  clipped_activity = pgstat_clip_activity(activity);
1031  ret = cstring_to_text(activity);
1032  pfree(clipped_activity);
1033 
1034  PG_RETURN_TEXT_P(ret);
1035 }
1036 
1037 Datum
1039 {
1040  int32 beid = PG_GETARG_INT32(0);
1041  PgBackendStatus *beentry;
1042  PGPROC *proc;
1043  const char *wait_event_type = NULL;
1044 
1045  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1046  wait_event_type = "<backend information not available>";
1047  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1048  wait_event_type = "<insufficient privilege>";
1049  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1050  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
1051 
1052  if (!wait_event_type)
1053  PG_RETURN_NULL();
1054 
1055  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
1056 }
1057 
1058 Datum
1060 {
1061  int32 beid = PG_GETARG_INT32(0);
1062  PgBackendStatus *beentry;
1063  PGPROC *proc;
1064  const char *wait_event = NULL;
1065 
1066  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1067  wait_event = "<backend information not available>";
1068  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1069  wait_event = "<insufficient privilege>";
1070  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1071  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1072 
1073  if (!wait_event)
1074  PG_RETURN_NULL();
1075 
1076  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1077 }
1078 
1079 
1080 Datum
1082 {
1083  int32 beid = PG_GETARG_INT32(0);
1084  TimestampTz result;
1085  PgBackendStatus *beentry;
1086 
1087  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1088  PG_RETURN_NULL();
1089 
1090  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1091  PG_RETURN_NULL();
1092 
1093  result = beentry->st_activity_start_timestamp;
1094 
1095  /*
1096  * No time recorded for start of current query -- this is the case if the
1097  * user hasn't enabled query-level stats collection.
1098  */
1099  if (result == 0)
1100  PG_RETURN_NULL();
1101 
1102  PG_RETURN_TIMESTAMPTZ(result);
1103 }
1104 
1105 
1106 Datum
1108 {
1109  int32 beid = PG_GETARG_INT32(0);
1110  TimestampTz result;
1111  PgBackendStatus *beentry;
1112 
1113  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1114  PG_RETURN_NULL();
1115 
1116  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1117  PG_RETURN_NULL();
1118 
1119  result = beentry->st_xact_start_timestamp;
1120 
1121  if (result == 0) /* not in a transaction */
1122  PG_RETURN_NULL();
1123 
1124  PG_RETURN_TIMESTAMPTZ(result);
1125 }
1126 
1127 
1128 Datum
1130 {
1131  int32 beid = PG_GETARG_INT32(0);
1132  TimestampTz result;
1133  PgBackendStatus *beentry;
1134 
1135  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1136  PG_RETURN_NULL();
1137 
1138  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1139  PG_RETURN_NULL();
1140 
1141  result = beentry->st_proc_start_timestamp;
1142 
1143  if (result == 0) /* probably can't happen? */
1144  PG_RETURN_NULL();
1145 
1146  PG_RETURN_TIMESTAMPTZ(result);
1147 }
1148 
1149 
1150 Datum
1152 {
1153  int32 beid = PG_GETARG_INT32(0);
1154  PgBackendStatus *beentry;
1155  SockAddr zero_clientaddr;
1156  char remote_host[NI_MAXHOST];
1157  int ret;
1158 
1159  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1160  PG_RETURN_NULL();
1161 
1162  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1163  PG_RETURN_NULL();
1164 
1165  /* A zeroed client addr means we don't know */
1166  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1167  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1168  sizeof(zero_clientaddr)) == 0)
1169  PG_RETURN_NULL();
1170 
1171  switch (beentry->st_clientaddr.addr.ss_family)
1172  {
1173  case AF_INET:
1174 #ifdef HAVE_IPV6
1175  case AF_INET6:
1176 #endif
1177  break;
1178  default:
1179  PG_RETURN_NULL();
1180  }
1181 
1182  remote_host[0] = '\0';
1183  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1184  beentry->st_clientaddr.salen,
1185  remote_host, sizeof(remote_host),
1186  NULL, 0,
1188  if (ret != 0)
1189  PG_RETURN_NULL();
1190 
1191  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1192 
1194  CStringGetDatum(remote_host)));
1195 }
1196 
1197 Datum
1199 {
1200  int32 beid = PG_GETARG_INT32(0);
1201  PgBackendStatus *beentry;
1202  SockAddr zero_clientaddr;
1203  char remote_port[NI_MAXSERV];
1204  int ret;
1205 
1206  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1207  PG_RETURN_NULL();
1208 
1209  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1210  PG_RETURN_NULL();
1211 
1212  /* A zeroed client addr means we don't know */
1213  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1214  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1215  sizeof(zero_clientaddr)) == 0)
1216  PG_RETURN_NULL();
1217 
1218  switch (beentry->st_clientaddr.addr.ss_family)
1219  {
1220  case AF_INET:
1221 #ifdef HAVE_IPV6
1222  case AF_INET6:
1223 #endif
1224  break;
1225  case AF_UNIX:
1226  PG_RETURN_INT32(-1);
1227  default:
1228  PG_RETURN_NULL();
1229  }
1230 
1231  remote_port[0] = '\0';
1232  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1233  beentry->st_clientaddr.salen,
1234  NULL, 0,
1235  remote_port, sizeof(remote_port),
1237  if (ret != 0)
1238  PG_RETURN_NULL();
1239 
1241  CStringGetDatum(remote_port)));
1242 }
1243 
1244 
1245 Datum
1247 {
1248  Oid dbid = PG_GETARG_OID(0);
1249  int32 result;
1250  int tot_backends = pgstat_fetch_stat_numbackends();
1251  int beid;
1252 
1253  result = 0;
1254  for (beid = 1; beid <= tot_backends; beid++)
1255  {
1256  PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1257 
1258  if (beentry && beentry->st_databaseid == dbid)
1259  result++;
1260  }
1261 
1262  PG_RETURN_INT32(result);
1263 }
1264 
1265 
1266 Datum
1268 {
1269  Oid dbid = PG_GETARG_OID(0);
1270  int64 result;
1271  PgStat_StatDBEntry *dbentry;
1272 
1273  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1274  result = 0;
1275  else
1276  result = (int64) (dbentry->n_xact_commit);
1277 
1278  PG_RETURN_INT64(result);
1279 }
1280 
1281 
1282 Datum
1284 {
1285  Oid dbid = PG_GETARG_OID(0);
1286  int64 result;
1287  PgStat_StatDBEntry *dbentry;
1288 
1289  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1290  result = 0;
1291  else
1292  result = (int64) (dbentry->n_xact_rollback);
1293 
1294  PG_RETURN_INT64(result);
1295 }
1296 
1297 
1298 Datum
1300 {
1301  Oid dbid = PG_GETARG_OID(0);
1302  int64 result;
1303  PgStat_StatDBEntry *dbentry;
1304 
1305  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1306  result = 0;
1307  else
1308  result = (int64) (dbentry->n_blocks_fetched);
1309 
1310  PG_RETURN_INT64(result);
1311 }
1312 
1313 
1314 Datum
1316 {
1317  Oid dbid = PG_GETARG_OID(0);
1318  int64 result;
1319  PgStat_StatDBEntry *dbentry;
1320 
1321  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1322  result = 0;
1323  else
1324  result = (int64) (dbentry->n_blocks_hit);
1325 
1326  PG_RETURN_INT64(result);
1327 }
1328 
1329 
1330 Datum
1332 {
1333  Oid dbid = PG_GETARG_OID(0);
1334  int64 result;
1335  PgStat_StatDBEntry *dbentry;
1336 
1337  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1338  result = 0;
1339  else
1340  result = (int64) (dbentry->n_tuples_returned);
1341 
1342  PG_RETURN_INT64(result);
1343 }
1344 
1345 
1346 Datum
1348 {
1349  Oid dbid = PG_GETARG_OID(0);
1350  int64 result;
1351  PgStat_StatDBEntry *dbentry;
1352 
1353  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1354  result = 0;
1355  else
1356  result = (int64) (dbentry->n_tuples_fetched);
1357 
1358  PG_RETURN_INT64(result);
1359 }
1360 
1361 
1362 Datum
1364 {
1365  Oid dbid = PG_GETARG_OID(0);
1366  int64 result;
1367  PgStat_StatDBEntry *dbentry;
1368 
1369  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1370  result = 0;
1371  else
1372  result = (int64) (dbentry->n_tuples_inserted);
1373 
1374  PG_RETURN_INT64(result);
1375 }
1376 
1377 
1378 Datum
1380 {
1381  Oid dbid = PG_GETARG_OID(0);
1382  int64 result;
1383  PgStat_StatDBEntry *dbentry;
1384 
1385  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1386  result = 0;
1387  else
1388  result = (int64) (dbentry->n_tuples_updated);
1389 
1390  PG_RETURN_INT64(result);
1391 }
1392 
1393 
1394 Datum
1396 {
1397  Oid dbid = PG_GETARG_OID(0);
1398  int64 result;
1399  PgStat_StatDBEntry *dbentry;
1400 
1401  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1402  result = 0;
1403  else
1404  result = (int64) (dbentry->n_tuples_deleted);
1405 
1406  PG_RETURN_INT64(result);
1407 }
1408 
1409 Datum
1411 {
1412  Oid dbid = PG_GETARG_OID(0);
1413  TimestampTz result;
1414  PgStat_StatDBEntry *dbentry;
1415 
1416  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1417  result = 0;
1418  else
1419  result = dbentry->stat_reset_timestamp;
1420 
1421  if (result == 0)
1422  PG_RETURN_NULL();
1423  else
1424  PG_RETURN_TIMESTAMPTZ(result);
1425 }
1426 
1427 Datum
1429 {
1430  Oid dbid = PG_GETARG_OID(0);
1431  int64 result;
1432  PgStat_StatDBEntry *dbentry;
1433 
1434  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1435  result = 0;
1436  else
1437  result = dbentry->n_temp_files;
1438 
1439  PG_RETURN_INT64(result);
1440 }
1441 
1442 
1443 Datum
1445 {
1446  Oid dbid = PG_GETARG_OID(0);
1447  int64 result;
1448  PgStat_StatDBEntry *dbentry;
1449 
1450  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1451  result = 0;
1452  else
1453  result = dbentry->n_temp_bytes;
1454 
1455  PG_RETURN_INT64(result);
1456 }
1457 
1458 Datum
1460 {
1461  Oid dbid = PG_GETARG_OID(0);
1462  int64 result;
1463  PgStat_StatDBEntry *dbentry;
1464 
1465  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1466  result = 0;
1467  else
1468  result = (int64) (dbentry->n_conflict_tablespace);
1469 
1470  PG_RETURN_INT64(result);
1471 }
1472 
1473 Datum
1475 {
1476  Oid dbid = PG_GETARG_OID(0);
1477  int64 result;
1478  PgStat_StatDBEntry *dbentry;
1479 
1480  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1481  result = 0;
1482  else
1483  result = (int64) (dbentry->n_conflict_lock);
1484 
1485  PG_RETURN_INT64(result);
1486 }
1487 
1488 Datum
1490 {
1491  Oid dbid = PG_GETARG_OID(0);
1492  int64 result;
1493  PgStat_StatDBEntry *dbentry;
1494 
1495  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1496  result = 0;
1497  else
1498  result = (int64) (dbentry->n_conflict_snapshot);
1499 
1500  PG_RETURN_INT64(result);
1501 }
1502 
1503 Datum
1505 {
1506  Oid dbid = PG_GETARG_OID(0);
1507  int64 result;
1508  PgStat_StatDBEntry *dbentry;
1509 
1510  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1511  result = 0;
1512  else
1513  result = (int64) (dbentry->n_conflict_bufferpin);
1514 
1515  PG_RETURN_INT64(result);
1516 }
1517 
1518 Datum
1520 {
1521  Oid dbid = PG_GETARG_OID(0);
1522  int64 result;
1523  PgStat_StatDBEntry *dbentry;
1524 
1525  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1526  result = 0;
1527  else
1528  result = (int64) (dbentry->n_conflict_startup_deadlock);
1529 
1530  PG_RETURN_INT64(result);
1531 }
1532 
1533 Datum
1535 {
1536  Oid dbid = PG_GETARG_OID(0);
1537  int64 result;
1538  PgStat_StatDBEntry *dbentry;
1539 
1540  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1541  result = 0;
1542  else
1543  result = (int64) (dbentry->n_conflict_tablespace +
1544  dbentry->n_conflict_lock +
1545  dbentry->n_conflict_snapshot +
1546  dbentry->n_conflict_bufferpin +
1547  dbentry->n_conflict_startup_deadlock);
1548 
1549  PG_RETURN_INT64(result);
1550 }
1551 
1552 Datum
1554 {
1555  Oid dbid = PG_GETARG_OID(0);
1556  int64 result;
1557  PgStat_StatDBEntry *dbentry;
1558 
1559  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1560  result = 0;
1561  else
1562  result = (int64) (dbentry->n_deadlocks);
1563 
1564  PG_RETURN_INT64(result);
1565 }
1566 
1567 Datum
1569 {
1570  Oid dbid = PG_GETARG_OID(0);
1571  int64 result;
1572  PgStat_StatDBEntry *dbentry;
1573 
1574  if (!DataChecksumsEnabled())
1575  PG_RETURN_NULL();
1576 
1577  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1578  result = 0;
1579  else
1580  result = (int64) (dbentry->n_checksum_failures);
1581 
1582  PG_RETURN_INT64(result);
1583 }
1584 
1585 Datum
1587 {
1588  Oid dbid = PG_GETARG_OID(0);
1589  TimestampTz result;
1590  PgStat_StatDBEntry *dbentry;
1591 
1592  if (!DataChecksumsEnabled())
1593  PG_RETURN_NULL();
1594 
1595  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1596  result = 0;
1597  else
1598  result = dbentry->last_checksum_failure;
1599 
1600  if (result == 0)
1601  PG_RETURN_NULL();
1602  else
1603  PG_RETURN_TIMESTAMPTZ(result);
1604 }
1605 
1606 Datum
1608 {
1609  Oid dbid = PG_GETARG_OID(0);
1610  double result;
1611  PgStat_StatDBEntry *dbentry;
1612 
1613  /* convert counter from microsec to millisec for display */
1614  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1615  result = 0;
1616  else
1617  result = ((double) dbentry->n_block_read_time) / 1000.0;
1618 
1619  PG_RETURN_FLOAT8(result);
1620 }
1621 
1622 Datum
1624 {
1625  Oid dbid = PG_GETARG_OID(0);
1626  double result;
1627  PgStat_StatDBEntry *dbentry;
1628 
1629  /* convert counter from microsec to millisec for display */
1630  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1631  result = 0;
1632  else
1633  result = ((double) dbentry->n_block_write_time) / 1000.0;
1634 
1635  PG_RETURN_FLOAT8(result);
1636 }
1637 
1638 Datum
1640 {
1641  Oid dbid = PG_GETARG_OID(0);
1642  double result = 0.0;
1643  PgStat_StatDBEntry *dbentry;
1644 
1645  /* convert counter from microsec to millisec for display */
1646  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1647  result = ((double) dbentry->total_session_time) / 1000.0;
1648 
1649  PG_RETURN_FLOAT8(result);
1650 }
1651 
1652 Datum
1654 {
1655  Oid dbid = PG_GETARG_OID(0);
1656  double result = 0.0;
1657  PgStat_StatDBEntry *dbentry;
1658 
1659  /* convert counter from microsec to millisec for display */
1660  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1661  result = ((double) dbentry->total_active_time) / 1000.0;
1662 
1663  PG_RETURN_FLOAT8(result);
1664 }
1665 
1666 Datum
1668 {
1669  Oid dbid = PG_GETARG_OID(0);
1670  double result = 0.0;
1671  PgStat_StatDBEntry *dbentry;
1672 
1673  /* convert counter from microsec to millisec for display */
1674  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1675  result = ((double) dbentry->total_idle_in_xact_time) / 1000.0;
1676 
1677  PG_RETURN_FLOAT8(result);
1678 }
1679 
1680 Datum
1682 {
1683  Oid dbid = PG_GETARG_OID(0);
1684  int64 result = 0;
1685  PgStat_StatDBEntry *dbentry;
1686 
1687  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1688  result = (int64) (dbentry->n_sessions);
1689 
1690  PG_RETURN_INT64(result);
1691 }
1692 
1693 Datum
1695 {
1696  Oid dbid = PG_GETARG_OID(0);
1697  int64 result = 0;
1698  PgStat_StatDBEntry *dbentry;
1699 
1700  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1701  result = (int64) (dbentry->n_sessions_abandoned);
1702 
1703  PG_RETURN_INT64(result);
1704 }
1705 
1706 Datum
1708 {
1709  Oid dbid = PG_GETARG_OID(0);
1710  int64 result = 0;
1711  PgStat_StatDBEntry *dbentry;
1712 
1713  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1714  result = (int64) (dbentry->n_sessions_fatal);
1715 
1716  PG_RETURN_INT64(result);
1717 }
1718 
1719 Datum
1721 {
1722  Oid dbid = PG_GETARG_OID(0);
1723  int64 result = 0;
1724  PgStat_StatDBEntry *dbentry;
1725 
1726  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1727  result = (int64) (dbentry->n_sessions_killed);
1728 
1729  PG_RETURN_INT64(result);
1730 }
1731 
1732 Datum
1734 {
1735  PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1736 }
1737 
1738 Datum
1740 {
1741  PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1742 }
1743 
1744 Datum
1746 {
1747  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1748 }
1749 
1750 Datum
1752 {
1753  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1754 }
1755 
1756 Datum
1758 {
1759  PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1760 }
1761 
1762 Datum
1764 {
1765  /* time is already in msec, just convert to double for presentation */
1766  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1767 }
1768 
1769 Datum
1771 {
1772  /* time is already in msec, just convert to double for presentation */
1773  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1774 }
1775 
1776 Datum
1778 {
1779  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1780 }
1781 
1782 Datum
1784 {
1785  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1786 }
1787 
1788 Datum
1790 {
1791  PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1792 }
1793 
1794 Datum
1796 {
1797  PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1798 }
1799 
1800 /*
1801  * Returns statistics of WAL activity
1802  */
1803 Datum
1805 {
1806 #define PG_STAT_GET_WAL_COLS 9
1807  TupleDesc tupdesc;
1809  bool nulls[PG_STAT_GET_WAL_COLS];
1810  char buf[256];
1811  PgStat_WalStats *wal_stats;
1812 
1813  /* Initialise values and NULL flags arrays */
1814  MemSet(values, 0, sizeof(values));
1815  MemSet(nulls, 0, sizeof(nulls));
1816 
1817  /* Initialise attributes information in the tuple descriptor */
1819  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1820  INT8OID, -1, 0);
1821  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1822  INT8OID, -1, 0);
1823  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1824  NUMERICOID, -1, 0);
1825  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1826  INT8OID, -1, 0);
1827  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1828  INT8OID, -1, 0);
1829  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1830  INT8OID, -1, 0);
1831  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1832  FLOAT8OID, -1, 0);
1833  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1834  FLOAT8OID, -1, 0);
1835  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1836  TIMESTAMPTZOID, -1, 0);
1837 
1838  BlessTupleDesc(tupdesc);
1839 
1840  /* Get statistics about WAL activity */
1841  wal_stats = pgstat_fetch_stat_wal();
1842 
1843  /* Fill values and NULLs */
1844  values[0] = Int64GetDatum(wal_stats->wal_records);
1845  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1846 
1847  /* Convert to numeric. */
1848  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1849  values[2] = DirectFunctionCall3(numeric_in,
1850  CStringGetDatum(buf),
1851  ObjectIdGetDatum(0),
1852  Int32GetDatum(-1));
1853 
1854  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1855  values[4] = Int64GetDatum(wal_stats->wal_write);
1856  values[5] = Int64GetDatum(wal_stats->wal_sync);
1857 
1858  /* Convert counters from microsec to millisec for display */
1859  values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1860  values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1861 
1862  values[8] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
1863 
1864  /* Returns the record as Datum */
1865  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
1866 }
1867 
1868 /*
1869  * Returns statistics of SLRU caches.
1870  */
1871 Datum
1873 {
1874 #define PG_STAT_GET_SLRU_COLS 9
1875  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1876  TupleDesc tupdesc;
1877  Tuplestorestate *tupstore;
1878  MemoryContext per_query_ctx;
1879  MemoryContext oldcontext;
1880  int i;
1881  PgStat_SLRUStats *stats;
1882 
1883  /* check to see if caller supports us returning a tuplestore */
1884  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1885  ereport(ERROR,
1886  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1887  errmsg("set-valued function called in context that cannot accept a set")));
1888  if (!(rsinfo->allowedModes & SFRM_Materialize))
1889  ereport(ERROR,
1890  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1891  errmsg("materialize mode required, but it is not allowed in this context")));
1892 
1893  /* Build a tuple descriptor for our result type */
1894  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1895  elog(ERROR, "return type must be a row type");
1896 
1897  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1898  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1899 
1900  tupstore = tuplestore_begin_heap(true, false, work_mem);
1901  rsinfo->returnMode = SFRM_Materialize;
1902  rsinfo->setResult = tupstore;
1903  rsinfo->setDesc = tupdesc;
1904 
1905  MemoryContextSwitchTo(oldcontext);
1906 
1907  /* request SLRU stats from the stat collector */
1908  stats = pgstat_fetch_slru();
1909 
1910  for (i = 0;; i++)
1911  {
1912  /* for each row */
1914  bool nulls[PG_STAT_GET_SLRU_COLS];
1915  PgStat_SLRUStats stat = stats[i];
1916  const char *name;
1917 
1918  name = pgstat_slru_name(i);
1919 
1920  if (!name)
1921  break;
1922 
1923  MemSet(values, 0, sizeof(values));
1924  MemSet(nulls, 0, sizeof(nulls));
1925 
1926  values[0] = PointerGetDatum(cstring_to_text(name));
1927  values[1] = Int64GetDatum(stat.blocks_zeroed);
1928  values[2] = Int64GetDatum(stat.blocks_hit);
1929  values[3] = Int64GetDatum(stat.blocks_read);
1930  values[4] = Int64GetDatum(stat.blocks_written);
1931  values[5] = Int64GetDatum(stat.blocks_exists);
1932  values[6] = Int64GetDatum(stat.flush);
1933  values[7] = Int64GetDatum(stat.truncate);
1934  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1935 
1936  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1937  }
1938 
1939  /* clean up and return the tuplestore */
1940  tuplestore_donestoring(tupstore);
1941 
1942  return (Datum) 0;
1943 }
1944 
1945 Datum
1947 {
1948  Oid relid = PG_GETARG_OID(0);
1949  int64 result;
1950  PgStat_TableStatus *tabentry;
1951 
1952  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1953  result = 0;
1954  else
1955  result = (int64) (tabentry->t_counts.t_numscans);
1956 
1957  PG_RETURN_INT64(result);
1958 }
1959 
1960 Datum
1962 {
1963  Oid relid = PG_GETARG_OID(0);
1964  int64 result;
1965  PgStat_TableStatus *tabentry;
1966 
1967  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1968  result = 0;
1969  else
1970  result = (int64) (tabentry->t_counts.t_tuples_returned);
1971 
1972  PG_RETURN_INT64(result);
1973 }
1974 
1975 Datum
1977 {
1978  Oid relid = PG_GETARG_OID(0);
1979  int64 result;
1980  PgStat_TableStatus *tabentry;
1981 
1982  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1983  result = 0;
1984  else
1985  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1986 
1987  PG_RETURN_INT64(result);
1988 }
1989 
1990 Datum
1992 {
1993  Oid relid = PG_GETARG_OID(0);
1994  int64 result;
1995  PgStat_TableStatus *tabentry;
1997 
1998  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1999  result = 0;
2000  else
2001  {
2002  result = tabentry->t_counts.t_tuples_inserted;
2003  /* live subtransactions' counts aren't in t_tuples_inserted yet */
2004  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2005  result += trans->tuples_inserted;
2006  }
2007 
2008  PG_RETURN_INT64(result);
2009 }
2010 
2011 Datum
2013 {
2014  Oid relid = PG_GETARG_OID(0);
2015  int64 result;
2016  PgStat_TableStatus *tabentry;
2018 
2019  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2020  result = 0;
2021  else
2022  {
2023  result = tabentry->t_counts.t_tuples_updated;
2024  /* live subtransactions' counts aren't in t_tuples_updated yet */
2025  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2026  result += trans->tuples_updated;
2027  }
2028 
2029  PG_RETURN_INT64(result);
2030 }
2031 
2032 Datum
2034 {
2035  Oid relid = PG_GETARG_OID(0);
2036  int64 result;
2037  PgStat_TableStatus *tabentry;
2039 
2040  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2041  result = 0;
2042  else
2043  {
2044  result = tabentry->t_counts.t_tuples_deleted;
2045  /* live subtransactions' counts aren't in t_tuples_deleted yet */
2046  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2047  result += trans->tuples_deleted;
2048  }
2049 
2050  PG_RETURN_INT64(result);
2051 }
2052 
2053 Datum
2055 {
2056  Oid relid = PG_GETARG_OID(0);
2057  int64 result;
2058  PgStat_TableStatus *tabentry;
2059 
2060  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2061  result = 0;
2062  else
2063  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
2064 
2065  PG_RETURN_INT64(result);
2066 }
2067 
2068 Datum
2070 {
2071  Oid relid = PG_GETARG_OID(0);
2072  int64 result;
2073  PgStat_TableStatus *tabentry;
2074 
2075  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2076  result = 0;
2077  else
2078  result = (int64) (tabentry->t_counts.t_blocks_fetched);
2079 
2080  PG_RETURN_INT64(result);
2081 }
2082 
2083 Datum
2085 {
2086  Oid relid = PG_GETARG_OID(0);
2087  int64 result;
2088  PgStat_TableStatus *tabentry;
2089 
2090  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2091  result = 0;
2092  else
2093  result = (int64) (tabentry->t_counts.t_blocks_hit);
2094 
2095  PG_RETURN_INT64(result);
2096 }
2097 
2098 Datum
2100 {
2101  Oid funcid = PG_GETARG_OID(0);
2102  PgStat_BackendFunctionEntry *funcentry;
2103 
2104  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2105  PG_RETURN_NULL();
2106  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
2107 }
2108 
2109 Datum
2111 {
2112  Oid funcid = PG_GETARG_OID(0);
2113  PgStat_BackendFunctionEntry *funcentry;
2114 
2115  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2116  PG_RETURN_NULL();
2118 }
2119 
2120 Datum
2122 {
2123  Oid funcid = PG_GETARG_OID(0);
2124  PgStat_BackendFunctionEntry *funcentry;
2125 
2126  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2127  PG_RETURN_NULL();
2129 }
2130 
2131 
2132 /* Get the timestamp of the current statistics snapshot */
2133 Datum
2135 {
2136  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
2137 }
2138 
2139 /* Discard the active statistics snapshot */
2140 Datum
2142 {
2144 
2145  PG_RETURN_VOID();
2146 }
2147 
2148 
2149 /* Reset all counters for the current database */
2150 Datum
2152 {
2154 
2155  PG_RETURN_VOID();
2156 }
2157 
2158 /* Reset some shared cluster-wide counters */
2159 Datum
2161 {
2162  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2163 
2165 
2166  PG_RETURN_VOID();
2167 }
2168 
2169 /* Reset a single counter in the current database */
2170 Datum
2172 {
2173  Oid taboid = PG_GETARG_OID(0);
2174 
2176 
2177  PG_RETURN_VOID();
2178 }
2179 
2180 Datum
2182 {
2183  Oid funcoid = PG_GETARG_OID(0);
2184 
2186 
2187  PG_RETURN_VOID();
2188 }
2189 
2190 /* Reset SLRU counters (a specific one or all of them). */
2191 Datum
2193 {
2194  char *target = NULL;
2195 
2196  if (!PG_ARGISNULL(0))
2197  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2198 
2199  pgstat_reset_slru_counter(target);
2200 
2201  PG_RETURN_VOID();
2202 }
2203 
2204 /* Reset replication slots stats (a specific one or all of them). */
2205 Datum
2207 {
2208  char *target = NULL;
2209 
2210  if (!PG_ARGISNULL(0))
2211  {
2212  ReplicationSlot *slot;
2213 
2214  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2215 
2216  /*
2217  * Check if the slot exists with the given name. It is possible that
2218  * by the time this message is executed the slot is dropped but at
2219  * least this check will ensure that the given name is for a valid
2220  * slot.
2221  */
2222  slot = SearchNamedReplicationSlot(target, true);
2223 
2224  if (!slot)
2225  ereport(ERROR,
2226  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2227  errmsg("replication slot \"%s\" does not exist",
2228  target)));
2229 
2230  /*
2231  * Nothing to do for physical slots as we collect stats only for
2232  * logical slots.
2233  */
2234  if (SlotIsPhysical(slot))
2235  PG_RETURN_VOID();
2236  }
2237 
2239 
2240  PG_RETURN_VOID();
2241 }
2242 
2243 Datum
2245 {
2246  TupleDesc tupdesc;
2247  Datum values[7];
2248  bool nulls[7];
2249  PgStat_ArchiverStats *archiver_stats;
2250 
2251  /* Initialise values and NULL flags arrays */
2252  MemSet(values, 0, sizeof(values));
2253  MemSet(nulls, 0, sizeof(nulls));
2254 
2255  /* Initialise attributes information in the tuple descriptor */
2256  tupdesc = CreateTemplateTupleDesc(7);
2257  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2258  INT8OID, -1, 0);
2259  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2260  TEXTOID, -1, 0);
2261  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2262  TIMESTAMPTZOID, -1, 0);
2263  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2264  INT8OID, -1, 0);
2265  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2266  TEXTOID, -1, 0);
2267  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2268  TIMESTAMPTZOID, -1, 0);
2269  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2270  TIMESTAMPTZOID, -1, 0);
2271 
2272  BlessTupleDesc(tupdesc);
2273 
2274  /* Get statistics about the archiver process */
2275  archiver_stats = pgstat_fetch_stat_archiver();
2276 
2277  /* Fill values and NULLs */
2278  values[0] = Int64GetDatum(archiver_stats->archived_count);
2279  if (*(archiver_stats->last_archived_wal) == '\0')
2280  nulls[1] = true;
2281  else
2282  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2283 
2284  if (archiver_stats->last_archived_timestamp == 0)
2285  nulls[2] = true;
2286  else
2287  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
2288 
2289  values[3] = Int64GetDatum(archiver_stats->failed_count);
2290  if (*(archiver_stats->last_failed_wal) == '\0')
2291  nulls[4] = true;
2292  else
2293  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2294 
2295  if (archiver_stats->last_failed_timestamp == 0)
2296  nulls[5] = true;
2297  else
2298  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2299 
2300  if (archiver_stats->stat_reset_timestamp == 0)
2301  nulls[6] = true;
2302  else
2303  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2304 
2305  /* Returns the record as Datum */
2306  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2307 }
2308 
2309 /*
2310  * Get the statistics for the replication slot. If the slot statistics is not
2311  * available, return all-zeroes stats.
2312  */
2313 Datum
2315 {
2316 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2317  text *slotname_text = PG_GETARG_TEXT_P(0);
2318  NameData slotname;
2319  TupleDesc tupdesc;
2322  PgStat_StatReplSlotEntry *slotent;
2323  PgStat_StatReplSlotEntry allzero;
2324 
2325  /* Initialise values and NULL flags arrays */
2326  MemSet(values, 0, sizeof(values));
2327  MemSet(nulls, 0, sizeof(nulls));
2328 
2329  /* Initialise attributes information in the tuple descriptor */
2331  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2332  TEXTOID, -1, 0);
2333  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2334  INT8OID, -1, 0);
2335  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2336  INT8OID, -1, 0);
2337  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2338  INT8OID, -1, 0);
2339  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2340  INT8OID, -1, 0);
2341  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2342  INT8OID, -1, 0);
2343  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2344  INT8OID, -1, 0);
2345  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2346  INT8OID, -1, 0);
2347  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2348  INT8OID, -1, 0);
2349  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2350  TIMESTAMPTZOID, -1, 0);
2351  BlessTupleDesc(tupdesc);
2352 
2353  namestrcpy(&slotname, text_to_cstring(slotname_text));
2354  slotent = pgstat_fetch_replslot(slotname);
2355  if (!slotent)
2356  {
2357  /*
2358  * If the slot is not found, initialise its stats. This is possible if
2359  * the create slot message is lost.
2360  */
2361  memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2362  slotent = &allzero;
2363  }
2364 
2365  values[0] = CStringGetTextDatum(NameStr(slotname));
2366  values[1] = Int64GetDatum(slotent->spill_txns);
2367  values[2] = Int64GetDatum(slotent->spill_count);
2368  values[3] = Int64GetDatum(slotent->spill_bytes);
2369  values[4] = Int64GetDatum(slotent->stream_txns);
2370  values[5] = Int64GetDatum(slotent->stream_count);
2371  values[6] = Int64GetDatum(slotent->stream_bytes);
2372  values[7] = Int64GetDatum(slotent->total_txns);
2373  values[8] = Int64GetDatum(slotent->total_bytes);
2374 
2375  if (slotent->stat_reset_timestamp == 0)
2376  nulls[9] = true;
2377  else
2378  values[9] = TimestampTzGetDatum(slotent->stat_reset_timestamp);
2379 
2380  /* Returns the record as Datum */
2381  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2382 }
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:1489
char ssl_issuer_dn[NAMEDATALEN]
Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1379
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1299
char gss_princ[NAMEDATALEN]
Datum pg_stat_reset_slru(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2192
Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1267
Datum pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1553
Datum pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:367
Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1534
Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1789
Datum pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1038
void pgstat_reset_slru_counter(const char *name)
Definition: pgstat.c:1502
PgStat_Counter analyze_count
Definition: pgstat.h:807
char ssl_cipher[NAMEDATALEN]
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:838
Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:460
instr_time f_self_time
Definition: pgstat.h:581
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:348
Datum pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:187
Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1246
#define PG_STAT_GET_SLRU_COLS
PgStat_Counter archived_count
Definition: pgstat.h:833
Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2181
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
char * pgstat_clip_activity(const char *raw_activity)
Oid st_progress_command_target
Datum pg_stat_get_archiver(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2244
PgStat_Counter wal_records
Definition: pgstat.h:868
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
int MyProcPid
Definition: globals.c:43
PgStat_Counter wal_write
Definition: pgstat.h:872
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
#define PG_STAT_GET_REPLICATION_SLOT_COLS
Datum pg_stat_get_db_session_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1639
Datum pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1770
PgStat_Counter tuples_updated
Definition: pgstat.h:186
TimestampTz stat_reset_timestamp
Definition: pgstat.h:876
PgStat_Counter tuples_returned
Definition: pgstat.h:785
Datum pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2012
Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:286
Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:123
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:232
Datum pg_stat_get_db_sessions_abandoned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1694
PgStat_Counter spill_bytes
Definition: pgstat.h:902
uint32 wait_event_info
Definition: proc.h:226
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2099
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:119
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2796
void namestrcpy(Name name, const char *str)
Definition: name.c:233
TimestampTz st_activity_start_timestamp
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3133
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_blocks_hit
Definition: pgstat.h:736
bool DataChecksumsEnabled(void)
Definition: xlog.c:4978
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:600
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:746
#define PG_STAT_GET_ACTIVITY_COLS
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:747
#define UInt64GetDatum(X)
Definition: postgres.h:692
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:124
Datum pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1623
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
Datum pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:352
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:808
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
PgStat_Counter wal_sync_time
Definition: pgstat.h:875
Datum pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1763
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
TimestampTz stat_reset_timestamp
Definition: pgstat.h:891
#define PG_STAT_GET_WAL_COLS
PgStat_Counter n_checksum_failures
Definition: pgstat.h:751
char ssl_client_dn[NAMEDATALEN]
PgStat_Counter n_temp_files
Definition: pgstat.h:748
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1443
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:354
Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2110
TimestampTz last_checksum_failure
Definition: pgstat.h:752
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
TransactionId backend_xmin
Datum pg_stat_reset(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2151
char ssl_version[NAMEDATALEN]
Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:235
Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:59
PgStat_Counter n_tuples_returned
Definition: pgstat.h:737
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat.c:2859
PgStat_Counter vacuum_count
Definition: pgstat.h:803
SockAddr st_clientaddr
Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1129
int errcode(int sqlerrcode)
Definition: elog.c:698
Datum pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1586
#define MemSet(start, val, len)
Definition: c.h:1008
Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2134
Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1757
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
TimestampTz stat_reset_timestamp
Definition: pgstat.h:763
Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:75
Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:417
Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:304
instr_time f_total_time
Definition: pgstat.h:580
Datum pg_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:967
Datum pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2084
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843
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:1331
Datum pg_stat_get_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:43
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
ProgressCommandType st_progress_command
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PgStat_Counter numscans
Definition: pgstat.h:783
#define lengthof(array)
Definition: c.h:734
PgStat_TableCounts t_counts
Definition: pgstat.h:176
PgStat_Counter blocks_exists
Definition: pgstat.h:888
unsigned int Oid
Definition: postgres_ext.h:31
Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1795
char ssl_client_serial[NAMEDATALEN]
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:791
PgStat_Counter f_total_time
Definition: pgstat.h:823
Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1444
BackendState st_state
PgStat_Counter t_tuples_returned
Definition: pgstat.h:118
Datum pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1059
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
char * st_clienthostname
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
PgStat_Counter tuples_inserted
Definition: pgstat.h:788
#define SlotIsPhysical(slot)
Definition: slot.h:168
PgStat_Counter f_numcalls
Definition: pgstat.h:579
Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:382
int pgstat_fetch_stat_numbackends(void)
Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2121
Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1745
Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1395
signed int int32
Definition: c.h:429
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1706
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
PgStat_Counter n_xact_commit
Definition: pgstat.h:733
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
Definition: pgstat.c:2826
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2160
Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:107
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:735
PgStat_Counter n_sessions_killed
Definition: pgstat.h:761
Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1410
PgStat_Counter n_live_tuples
Definition: pgstat.h:793
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:797
Datum pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1607
Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:987
void pfree(void *pointer)
Definition: mcxt.c:1169
PgStat_FunctionCounts f_counts
Definition: pgstat.h:591
#define NI_MAXHOST
Definition: getaddrinfo.h:88
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:743
Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1283
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:250
TransactionId backend_xid
PgStat_Counter wal_write_time
Definition: pgstat.h:874
TimestampTz vacuum_timestamp
Definition: pgstat.h:802
PgStat_Counter t_tuples_updated
Definition: pgstat.h:122
Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1428
PgStat_Counter total_active_time
Definition: pgstat.h:757
Datum pg_stat_get_db_active_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1653
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter n_sessions_abandoned
Definition: pgstat.h:759
PgStat_Counter n_dead_tuples
Definition: pgstat.h:794
Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:268
Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1198
Datum pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1976
Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1783
PgStat_Counter wal_fpi
Definition: pgstat.h:869
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
Definition: c.h:675
PgStat_Counter f_numcalls
Definition: pgstat.h:821
Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1961
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:155
Datum pg_stat_get_ins_since_vacuum(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:203
Datum pg_stat_get_db_idle_in_transaction_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1667
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:992
PgStat_Counter n_sessions_fatal
Definition: pgstat.h:760
static char * buf
Definition: pg_test_fsync.c:68
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:739
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2141
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1504
Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1739
PgStat_Counter total_bytes
Definition: pgstat.h:907
PgStat_Counter blocks_read
Definition: pgstat.h:886
PgStat_Counter total_idle_in_xact_time
Definition: pgstat.h:758
#define CStringGetDatum(X)
Definition: postgres.h:622
Datum pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1474
#define NI_MAXSERV
Definition: getaddrinfo.h:91
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1959
PgStat_Counter tuples_fetched
Definition: pgstat.h:786
unsigned int uint32
Definition: c.h:441
TimestampTz stat_reset_timestamp
Definition: pgstat.h:908
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:745
Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1081
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:625
Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1013
TimestampTz st_state_start_timestamp
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:741
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
PgStat_Counter n_conflict_lock
Definition: pgstat.h:744
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
PgStat_Counter blocks_hit
Definition: pgstat.h:885
PgStat_Counter total_session_time
Definition: pgstat.h:756
PgStat_Counter t_blocks_hit
Definition: pgstat.h:132
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
Definition: pgstat.c:1476
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
PgStat_Counter n_xact_rollback
Definition: pgstat.h:734
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Datum pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2054
Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1107
Datum pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:322
Datum pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:337
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:123
PgStat_Counter blocks_zeroed
Definition: pgstat.h:884
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
ProgressCommandType
Datum pg_stat_get_db_sessions_killed(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1720
#define TransactionIdGetDatum(X)
Definition: postgres.h:565
Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:91
Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1299
PgBackendStatus backendStatus
uintptr_t Datum
Definition: postgres.h:411
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:139
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2069
BackendType st_backendType
PgStat_Counter blocks_hit
Definition: pgstat.h:800
int work_mem
Definition: globals.c:124
TimestampTz analyze_timestamp
Definition: pgstat.h:806
Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:405
TimestampTz last_failed_timestamp
Definition: pgstat.h:840
PgStat_Counter changes_since_analyze
Definition: pgstat.h:795
PgStat_Counter t_numscans
Definition: pgstat.h:116
#define BoolGetDatum(X)
Definition: postgres.h:446
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: wait_event.c:129
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
PgStat_MsgBgWriter bgwriterStats
#define InvalidOid
Definition: postgres_ext.h:36
PgStat_Counter stream_bytes
Definition: pgstat.h:905
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
PgStat_Counter spill_txns
Definition: pgstat.h:900
Datum pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1568
PgStat_Counter n_tuples_updated
Definition: pgstat.h:740
#define PG_RETURN_VOID()
Definition: fmgr.h:349
uint64 wal_bytes
Definition: pgstat.h:870
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1347
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
text * cstring_to_text(const char *s)
Definition: varlena.c:190
Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1777
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:74
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
void pgstat_reset_replslot_counter(const char *name)
Definition: pgstat.c:1526
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
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat.c:2875
PgStat_Counter n_sessions
Definition: pgstat.h:755
Datum pg_stat_reset_shared(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2160
PgStat_Counter failed_count
Definition: pgstat.h:837
PgStat_Counter n_block_write_time
Definition: pgstat.h:754
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
PgStat_Counter tuples_deleted
Definition: pgstat.h:790
Datum pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:393
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4709
PgStat_Counter flush
Definition: pgstat.h:889
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Datum pg_stat_get_wal(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1804
PgStat_Counter tuples_updated
Definition: pgstat.h:789
Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1151
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:1363
Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2171
Datum pg_stat_get_db_sessions(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1681
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:804
PgStat_Counter n_block_read_time
Definition: pgstat.h:753
const char * name
Definition: encode.c:515
PgStat_Counter spill_count
Definition: pgstat.h:901
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1315
Tuplestorestate * setResult
Definition: execnodes.h:310
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:809
Datum pg_stat_get_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:571
static Datum values[MAXATTR]
Definition: bootstrap.c:166
char * text_to_cstring(const text *t)
Definition: varlena.c:223
ExprContext * econtext
Definition: execnodes.h:303
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
Definition: pgstat.c:2891
#define Int32GetDatum(X)
Definition: postgres.h:523
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
TimestampTz stat_reset_timestamp
Definition: pgstat.h:841
TimestampTz last_archived_timestamp
Definition: pgstat.h:836
Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:974
void * user_fctx
Definition: funcapi.h:82
TupleDesc setDesc
Definition: execnodes.h:311
Datum pg_stat_get_slru(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1872
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
int errmsg(const char *fmt,...)
Definition: elog.c:909
PgStat_Counter n_deadlocks
Definition: pgstat.h:750
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:336
Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1733
Datum pg_stat_reset_replication_slot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2206
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
PgStat_Counter stream_count
Definition: pgstat.h:904
TimestampTz st_xact_start_timestamp
#define elog(elevel,...)
Definition: elog.h:232
#define PG_STAT_GET_PROGRESS_COLS
int i
const char * pgstat_slru_name(int slru_idx)
Definition: pgstat.c:5801
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:35
Datum pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1991
#define NameStr(name)
Definition: c.h:681
Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1946
#define CStringGetTextDatum(s)
Definition: builtins.h:82
Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:219
Definition: c.h:621
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Datum pg_stat_get_db_sessions_fatal(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1707
PgStat_Counter total_txns
Definition: pgstat.h:906
Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1519
Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1000
#define TransactionIdIsValid(xid)
Definition: transam.h:41
PgStat_Counter wal_buffers_full
Definition: pgstat.h:871
PgStat_Counter f_self_time
Definition: pgstat.h:824
PgStat_Counter wal_sync
Definition: pgstat.h:873
Definition: proc.h:121
PgStat_Counter blocks_fetched
Definition: pgstat.h:799
Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:171
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define snprintf
Definition: port.h:216
TimestampTz st_proc_start_timestamp
Datum pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2033
int pid
Definition: proc.h:146
PgStat_Counter truncate
Definition: pgstat.h:890
Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1751
PgBackendSSLStatus * st_sslstatus
int16 AttrNumber
Definition: attnum.h:21
#define UINT64_FORMAT
Definition: c.h:484
#define PGSTAT_NUM_PROGRESS_PARAM
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:121
void pgstat_reset_counters(void)
Definition: pgstat.c:1421
Datum pg_stat_get_replication_slot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2314
#define PG_RETURN_NULL()
Definition: fmgr.h:345
PgStat_Counter blocks_written
Definition: pgstat.h:887
PGPROC * lockGroupLeader
Definition: proc.h:251
PgBackendGSSStatus * st_gssstatus
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:131
PgStat_Counter n_tuples_fetched
Definition: pgstat.h:738
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:834
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:805
Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1459
PgStat_Counter stream_txns
Definition: pgstat.h:903
PgStat_Counter n_temp_bytes
Definition: pgstat.h:749
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295