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