PostgreSQL Source Code  git master
pgstatfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgstatfuncs.c
4  * Functions for accessing the statistics collector data
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/pgstatfuncs.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/xlog.h"
19 #include "catalog/pg_authid.h"
20 #include "catalog/pg_type.h"
21 #include "common/ip.h"
22 #include "funcapi.h"
23 #include "miscadmin.h"
24 #include "pgstat.h"
26 #include "postmaster/postmaster.h"
27 #include "storage/proc.h"
28 #include "storage/procarray.h"
29 #include "utils/acl.h"
30 #include "utils/builtins.h"
31 #include "utils/inet.h"
32 #include "utils/timestamp.h"
33 
34 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
35 
36 #define HAS_PGSTAT_PERMISSIONS(role) (is_member_of_role(GetUserId(), DEFAULT_ROLE_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
498  ereport(ERROR,
499  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
500  errmsg("invalid command name: \"%s\"", cmd)));
501 
502  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
503  oldcontext = MemoryContextSwitchTo(per_query_ctx);
504 
505  tupstore = tuplestore_begin_heap(true, false, work_mem);
506  rsinfo->returnMode = SFRM_Materialize;
507  rsinfo->setResult = tupstore;
508  rsinfo->setDesc = tupdesc;
509  MemoryContextSwitchTo(oldcontext);
510 
511  /* 1-based index */
512  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
513  {
514  LocalPgBackendStatus *local_beentry;
515  PgBackendStatus *beentry;
517  bool nulls[PG_STAT_GET_PROGRESS_COLS];
518  int i;
519 
520  MemSet(values, 0, sizeof(values));
521  MemSet(nulls, 0, sizeof(nulls));
522 
523  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
524 
525  if (!local_beentry)
526  continue;
527 
528  beentry = &local_beentry->backendStatus;
529 
530  /*
531  * Report values for only those backends which are running the given
532  * command.
533  */
534  if (!beentry || beentry->st_progress_command != cmdtype)
535  continue;
536 
537  /* Value available to all callers */
538  values[0] = Int32GetDatum(beentry->st_procpid);
539  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
540 
541  /* show rest of the values including relid only to role members */
542  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
543  {
544  values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
545  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
546  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
547  }
548  else
549  {
550  nulls[2] = true;
551  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
552  nulls[i + 3] = true;
553  }
554 
555  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
556  }
557 
558  /* clean up and return the tuplestore */
559  tuplestore_donestoring(tupstore);
560 
561  return (Datum) 0;
562 }
563 
564 /*
565  * Returns activity of PG backends.
566  */
567 Datum
569 {
570 #define PG_STAT_GET_ACTIVITY_COLS 30
571  int num_backends = pgstat_fetch_stat_numbackends();
572  int curr_backend;
573  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
574  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
575  TupleDesc tupdesc;
576  Tuplestorestate *tupstore;
577  MemoryContext per_query_ctx;
578  MemoryContext oldcontext;
579 
580  /* check to see if caller supports us returning a tuplestore */
581  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
582  ereport(ERROR,
583  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
584  errmsg("set-valued function called in context that cannot accept a set")));
585  if (!(rsinfo->allowedModes & SFRM_Materialize))
586  ereport(ERROR,
587  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
588  errmsg("materialize mode required, but it is not allowed in this context")));
589 
590  /* Build a tuple descriptor for our result type */
591  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
592  elog(ERROR, "return type must be a row type");
593 
594  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
595  oldcontext = MemoryContextSwitchTo(per_query_ctx);
596 
597  tupstore = tuplestore_begin_heap(true, false, work_mem);
598  rsinfo->returnMode = SFRM_Materialize;
599  rsinfo->setResult = tupstore;
600  rsinfo->setDesc = tupdesc;
601 
602  MemoryContextSwitchTo(oldcontext);
603 
604  /* 1-based index */
605  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
606  {
607  /* for each row */
609  bool nulls[PG_STAT_GET_ACTIVITY_COLS];
610  LocalPgBackendStatus *local_beentry;
611  PgBackendStatus *beentry;
612  PGPROC *proc;
613  const char *wait_event_type = NULL;
614  const char *wait_event = NULL;
615 
616  MemSet(values, 0, sizeof(values));
617  MemSet(nulls, 0, sizeof(nulls));
618 
619  /* Get the next one in the list */
620  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
621  if (!local_beentry)
622  {
623  int i;
624 
625  /* Ignore missing entries if looking for specific PID */
626  if (pid != -1)
627  continue;
628 
629  for (i = 0; i < lengthof(nulls); i++)
630  nulls[i] = true;
631 
632  nulls[5] = false;
633  values[5] = CStringGetTextDatum("<backend information not available>");
634 
635  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
636  continue;
637  }
638 
639  beentry = &local_beentry->backendStatus;
640 
641  /* If looking for specific PID, ignore all the others */
642  if (pid != -1 && beentry->st_procpid != pid)
643  continue;
644 
645  /* Values available to all callers */
646  if (beentry->st_databaseid != InvalidOid)
647  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
648  else
649  nulls[0] = true;
650 
651  values[1] = Int32GetDatum(beentry->st_procpid);
652 
653  if (beentry->st_userid != InvalidOid)
654  values[2] = ObjectIdGetDatum(beentry->st_userid);
655  else
656  nulls[2] = true;
657 
658  if (beentry->st_appname)
659  values[3] = CStringGetTextDatum(beentry->st_appname);
660  else
661  nulls[3] = true;
662 
663  if (TransactionIdIsValid(local_beentry->backend_xid))
664  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
665  else
666  nulls[15] = true;
667 
668  if (TransactionIdIsValid(local_beentry->backend_xmin))
669  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
670  else
671  nulls[16] = true;
672 
673  /* Values only available to role member or pg_read_all_stats */
674  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
675  {
676  SockAddr zero_clientaddr;
677  char *clipped_activity;
678 
679  switch (beentry->st_state)
680  {
681  case STATE_IDLE:
682  values[4] = CStringGetTextDatum("idle");
683  break;
684  case STATE_RUNNING:
685  values[4] = CStringGetTextDatum("active");
686  break;
688  values[4] = CStringGetTextDatum("idle in transaction");
689  break;
690  case STATE_FASTPATH:
691  values[4] = CStringGetTextDatum("fastpath function call");
692  break;
694  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
695  break;
696  case STATE_DISABLED:
697  values[4] = CStringGetTextDatum("disabled");
698  break;
699  case STATE_UNDEFINED:
700  nulls[4] = true;
701  break;
702  }
703 
704  clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
705  values[5] = CStringGetTextDatum(clipped_activity);
706  pfree(clipped_activity);
707 
708  /* leader_pid */
709  nulls[29] = true;
710 
711  proc = BackendPidGetProc(beentry->st_procpid);
712 
713  if (proc == NULL && (beentry->st_backendType != B_BACKEND))
714  {
715  /*
716  * For an auxiliary process, retrieve process info from
717  * AuxiliaryProcs stored in shared-memory.
718  */
719  proc = AuxiliaryPidGetProc(beentry->st_procpid);
720  }
721 
722  /*
723  * If a PGPROC entry was retrieved, display wait events and lock
724  * group leader information if any. To avoid extra overhead, no
725  * extra lock is being held, so there is no guarantee of
726  * consistency across multiple rows.
727  */
728  if (proc != NULL)
729  {
730  uint32 raw_wait_event;
731  PGPROC *leader;
732 
733  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
734  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
735  wait_event = pgstat_get_wait_event(raw_wait_event);
736 
737  leader = proc->lockGroupLeader;
738  if (leader)
739  {
740  values[29] = Int32GetDatum(leader->pid);
741  nulls[29] = false;
742  }
743  }
744 
745  if (wait_event_type)
746  values[6] = CStringGetTextDatum(wait_event_type);
747  else
748  nulls[6] = true;
749 
750  if (wait_event)
751  values[7] = CStringGetTextDatum(wait_event);
752  else
753  nulls[7] = true;
754 
755  /*
756  * Don't expose transaction time for walsenders; it confuses
757  * monitoring, particularly because we don't keep the time up-to-
758  * date.
759  */
760  if (beentry->st_xact_start_timestamp != 0 &&
761  beentry->st_backendType != B_WAL_SENDER)
762  values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
763  else
764  nulls[8] = true;
765 
766  if (beentry->st_activity_start_timestamp != 0)
767  values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
768  else
769  nulls[9] = true;
770 
771  if (beentry->st_proc_start_timestamp != 0)
772  values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
773  else
774  nulls[10] = true;
775 
776  if (beentry->st_state_start_timestamp != 0)
777  values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
778  else
779  nulls[11] = true;
780 
781  /* A zeroed client addr means we don't know */
782  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
783  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
784  sizeof(zero_clientaddr)) == 0)
785  {
786  nulls[12] = true;
787  nulls[13] = true;
788  nulls[14] = true;
789  }
790  else
791  {
792  if (beentry->st_clientaddr.addr.ss_family == AF_INET
793 #ifdef HAVE_IPV6
794  || beentry->st_clientaddr.addr.ss_family == AF_INET6
795 #endif
796  )
797  {
798  char remote_host[NI_MAXHOST];
799  char remote_port[NI_MAXSERV];
800  int ret;
801 
802  remote_host[0] = '\0';
803  remote_port[0] = '\0';
804  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
805  beentry->st_clientaddr.salen,
806  remote_host, sizeof(remote_host),
807  remote_port, sizeof(remote_port),
809  if (ret == 0)
810  {
811  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
812  values[12] = DirectFunctionCall1(inet_in,
813  CStringGetDatum(remote_host));
814  if (beentry->st_clienthostname &&
815  beentry->st_clienthostname[0])
816  values[13] = CStringGetTextDatum(beentry->st_clienthostname);
817  else
818  nulls[13] = true;
819  values[14] = Int32GetDatum(atoi(remote_port));
820  }
821  else
822  {
823  nulls[12] = true;
824  nulls[13] = true;
825  nulls[14] = true;
826  }
827  }
828  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
829  {
830  /*
831  * Unix sockets always reports NULL for host and -1 for
832  * port, so it's possible to tell the difference to
833  * connections we have no permissions to view, or with
834  * errors.
835  */
836  nulls[12] = true;
837  nulls[13] = true;
838  values[14] = Int32GetDatum(-1);
839  }
840  else
841  {
842  /* Unknown address type, should never happen */
843  nulls[12] = true;
844  nulls[13] = true;
845  nulls[14] = true;
846  }
847  }
848  /* Add backend type */
849  if (beentry->st_backendType == B_BG_WORKER)
850  {
851  const char *bgw_type;
852 
853  bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
854  if (bgw_type)
855  values[17] = CStringGetTextDatum(bgw_type);
856  else
857  nulls[17] = true;
858  }
859  else
860  values[17] =
862 
863  /* SSL information */
864  if (beentry->st_ssl)
865  {
866  values[18] = BoolGetDatum(true); /* ssl */
867  values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
868  values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
869  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
870  values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
871 
872  if (beentry->st_sslstatus->ssl_client_dn[0])
873  values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn);
874  else
875  nulls[23] = true;
876 
877  if (beentry->st_sslstatus->ssl_client_serial[0])
878  values[24] = DirectFunctionCall3(numeric_in,
881  Int32GetDatum(-1));
882  else
883  nulls[24] = true;
884 
885  if (beentry->st_sslstatus->ssl_issuer_dn[0])
886  values[25] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn);
887  else
888  nulls[25] = true;
889  }
890  else
891  {
892  values[18] = BoolGetDatum(false); /* ssl */
893  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = nulls[25] = true;
894  }
895 
896  /* GSSAPI information */
897  if (beentry->st_gss)
898  {
899  values[26] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
900  values[27] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ);
901  values[28] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
902  }
903  else
904  {
905  values[26] = BoolGetDatum(false); /* gss_auth */
906  nulls[27] = true; /* No GSS principal */
907  values[28] = BoolGetDatum(false); /* GSS Encryption not in
908  * use */
909  }
910  }
911  else
912  {
913  /* No permissions to view data about this session */
914  values[5] = CStringGetTextDatum("<insufficient privilege>");
915  nulls[4] = true;
916  nulls[6] = true;
917  nulls[7] = true;
918  nulls[8] = true;
919  nulls[9] = true;
920  nulls[10] = true;
921  nulls[11] = true;
922  nulls[12] = true;
923  nulls[13] = true;
924  nulls[14] = true;
925  nulls[17] = true;
926  nulls[18] = true;
927  nulls[19] = true;
928  nulls[20] = true;
929  nulls[21] = true;
930  nulls[22] = true;
931  nulls[23] = true;
932  nulls[24] = true;
933  nulls[25] = true;
934  nulls[26] = true;
935  nulls[27] = true;
936  nulls[28] = true;
937  nulls[29] = true;
938  }
939 
940  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
941 
942  /* If only a single backend was requested, and we found it, break. */
943  if (pid != -1)
944  break;
945  }
946 
947  /* clean up and return the tuplestore */
948  tuplestore_donestoring(tupstore);
949 
950  return (Datum) 0;
951 }
952 
953 
954 Datum
956 {
958 }
959 
960 
961 Datum
963 {
964  int32 beid = PG_GETARG_INT32(0);
965  PgBackendStatus *beentry;
966 
967  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
968  PG_RETURN_NULL();
969 
970  PG_RETURN_INT32(beentry->st_procpid);
971 }
972 
973 
974 Datum
976 {
977  int32 beid = PG_GETARG_INT32(0);
978  PgBackendStatus *beentry;
979 
980  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
981  PG_RETURN_NULL();
982 
983  PG_RETURN_OID(beentry->st_databaseid);
984 }
985 
986 
987 Datum
989 {
990  int32 beid = PG_GETARG_INT32(0);
991  PgBackendStatus *beentry;
992 
993  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
994  PG_RETURN_NULL();
995 
996  PG_RETURN_OID(beentry->st_userid);
997 }
998 
999 
1000 Datum
1002 {
1003  int32 beid = PG_GETARG_INT32(0);
1004  PgBackendStatus *beentry;
1005  const char *activity;
1006  char *clipped_activity;
1007  text *ret;
1008 
1009  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1010  activity = "<backend information not available>";
1011  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1012  activity = "<insufficient privilege>";
1013  else if (*(beentry->st_activity_raw) == '\0')
1014  activity = "<command string not enabled>";
1015  else
1016  activity = beentry->st_activity_raw;
1017 
1018  clipped_activity = pgstat_clip_activity(activity);
1019  ret = cstring_to_text(activity);
1020  pfree(clipped_activity);
1021 
1022  PG_RETURN_TEXT_P(ret);
1023 }
1024 
1025 Datum
1027 {
1028  int32 beid = PG_GETARG_INT32(0);
1029  PgBackendStatus *beentry;
1030  PGPROC *proc;
1031  const char *wait_event_type = NULL;
1032 
1033  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1034  wait_event_type = "<backend information not available>";
1035  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1036  wait_event_type = "<insufficient privilege>";
1037  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1038  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
1039 
1040  if (!wait_event_type)
1041  PG_RETURN_NULL();
1042 
1043  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
1044 }
1045 
1046 Datum
1048 {
1049  int32 beid = PG_GETARG_INT32(0);
1050  PgBackendStatus *beentry;
1051  PGPROC *proc;
1052  const char *wait_event = NULL;
1053 
1054  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1055  wait_event = "<backend information not available>";
1056  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1057  wait_event = "<insufficient privilege>";
1058  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1059  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1060 
1061  if (!wait_event)
1062  PG_RETURN_NULL();
1063 
1064  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1065 }
1066 
1067 
1068 Datum
1070 {
1071  int32 beid = PG_GETARG_INT32(0);
1072  TimestampTz result;
1073  PgBackendStatus *beentry;
1074 
1075  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1076  PG_RETURN_NULL();
1077 
1078  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1079  PG_RETURN_NULL();
1080 
1081  result = beentry->st_activity_start_timestamp;
1082 
1083  /*
1084  * No time recorded for start of current query -- this is the case if the
1085  * user hasn't enabled query-level stats collection.
1086  */
1087  if (result == 0)
1088  PG_RETURN_NULL();
1089 
1090  PG_RETURN_TIMESTAMPTZ(result);
1091 }
1092 
1093 
1094 Datum
1096 {
1097  int32 beid = PG_GETARG_INT32(0);
1098  TimestampTz result;
1099  PgBackendStatus *beentry;
1100 
1101  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1102  PG_RETURN_NULL();
1103 
1104  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1105  PG_RETURN_NULL();
1106 
1107  result = beentry->st_xact_start_timestamp;
1108 
1109  if (result == 0) /* not in a transaction */
1110  PG_RETURN_NULL();
1111 
1112  PG_RETURN_TIMESTAMPTZ(result);
1113 }
1114 
1115 
1116 Datum
1118 {
1119  int32 beid = PG_GETARG_INT32(0);
1120  TimestampTz result;
1121  PgBackendStatus *beentry;
1122 
1123  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1124  PG_RETURN_NULL();
1125 
1126  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1127  PG_RETURN_NULL();
1128 
1129  result = beentry->st_proc_start_timestamp;
1130 
1131  if (result == 0) /* probably can't happen? */
1132  PG_RETURN_NULL();
1133 
1134  PG_RETURN_TIMESTAMPTZ(result);
1135 }
1136 
1137 
1138 Datum
1140 {
1141  int32 beid = PG_GETARG_INT32(0);
1142  PgBackendStatus *beentry;
1143  SockAddr zero_clientaddr;
1144  char remote_host[NI_MAXHOST];
1145  int ret;
1146 
1147  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1148  PG_RETURN_NULL();
1149 
1150  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1151  PG_RETURN_NULL();
1152 
1153  /* A zeroed client addr means we don't know */
1154  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1155  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1156  sizeof(zero_clientaddr)) == 0)
1157  PG_RETURN_NULL();
1158 
1159  switch (beentry->st_clientaddr.addr.ss_family)
1160  {
1161  case AF_INET:
1162 #ifdef HAVE_IPV6
1163  case AF_INET6:
1164 #endif
1165  break;
1166  default:
1167  PG_RETURN_NULL();
1168  }
1169 
1170  remote_host[0] = '\0';
1171  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1172  beentry->st_clientaddr.salen,
1173  remote_host, sizeof(remote_host),
1174  NULL, 0,
1176  if (ret != 0)
1177  PG_RETURN_NULL();
1178 
1179  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1180 
1182  CStringGetDatum(remote_host)));
1183 }
1184 
1185 Datum
1187 {
1188  int32 beid = PG_GETARG_INT32(0);
1189  PgBackendStatus *beentry;
1190  SockAddr zero_clientaddr;
1191  char remote_port[NI_MAXSERV];
1192  int ret;
1193 
1194  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1195  PG_RETURN_NULL();
1196 
1197  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1198  PG_RETURN_NULL();
1199 
1200  /* A zeroed client addr means we don't know */
1201  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1202  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1203  sizeof(zero_clientaddr)) == 0)
1204  PG_RETURN_NULL();
1205 
1206  switch (beentry->st_clientaddr.addr.ss_family)
1207  {
1208  case AF_INET:
1209 #ifdef HAVE_IPV6
1210  case AF_INET6:
1211 #endif
1212  break;
1213  case AF_UNIX:
1214  PG_RETURN_INT32(-1);
1215  default:
1216  PG_RETURN_NULL();
1217  }
1218 
1219  remote_port[0] = '\0';
1220  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1221  beentry->st_clientaddr.salen,
1222  NULL, 0,
1223  remote_port, sizeof(remote_port),
1225  if (ret != 0)
1226  PG_RETURN_NULL();
1227 
1229  CStringGetDatum(remote_port)));
1230 }
1231 
1232 
1233 Datum
1235 {
1236  Oid dbid = PG_GETARG_OID(0);
1237  int32 result;
1238  int tot_backends = pgstat_fetch_stat_numbackends();
1239  int beid;
1240 
1241  result = 0;
1242  for (beid = 1; beid <= tot_backends; beid++)
1243  {
1244  PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1245 
1246  if (beentry && beentry->st_databaseid == dbid)
1247  result++;
1248  }
1249 
1250  PG_RETURN_INT32(result);
1251 }
1252 
1253 
1254 Datum
1256 {
1257  Oid dbid = PG_GETARG_OID(0);
1258  int64 result;
1259  PgStat_StatDBEntry *dbentry;
1260 
1261  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1262  result = 0;
1263  else
1264  result = (int64) (dbentry->n_xact_commit);
1265 
1266  PG_RETURN_INT64(result);
1267 }
1268 
1269 
1270 Datum
1272 {
1273  Oid dbid = PG_GETARG_OID(0);
1274  int64 result;
1275  PgStat_StatDBEntry *dbentry;
1276 
1277  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1278  result = 0;
1279  else
1280  result = (int64) (dbentry->n_xact_rollback);
1281 
1282  PG_RETURN_INT64(result);
1283 }
1284 
1285 
1286 Datum
1288 {
1289  Oid dbid = PG_GETARG_OID(0);
1290  int64 result;
1291  PgStat_StatDBEntry *dbentry;
1292 
1293  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1294  result = 0;
1295  else
1296  result = (int64) (dbentry->n_blocks_fetched);
1297 
1298  PG_RETURN_INT64(result);
1299 }
1300 
1301 
1302 Datum
1304 {
1305  Oid dbid = PG_GETARG_OID(0);
1306  int64 result;
1307  PgStat_StatDBEntry *dbentry;
1308 
1309  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1310  result = 0;
1311  else
1312  result = (int64) (dbentry->n_blocks_hit);
1313 
1314  PG_RETURN_INT64(result);
1315 }
1316 
1317 
1318 Datum
1320 {
1321  Oid dbid = PG_GETARG_OID(0);
1322  int64 result;
1323  PgStat_StatDBEntry *dbentry;
1324 
1325  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1326  result = 0;
1327  else
1328  result = (int64) (dbentry->n_tuples_returned);
1329 
1330  PG_RETURN_INT64(result);
1331 }
1332 
1333 
1334 Datum
1336 {
1337  Oid dbid = PG_GETARG_OID(0);
1338  int64 result;
1339  PgStat_StatDBEntry *dbentry;
1340 
1341  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1342  result = 0;
1343  else
1344  result = (int64) (dbentry->n_tuples_fetched);
1345 
1346  PG_RETURN_INT64(result);
1347 }
1348 
1349 
1350 Datum
1352 {
1353  Oid dbid = PG_GETARG_OID(0);
1354  int64 result;
1355  PgStat_StatDBEntry *dbentry;
1356 
1357  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1358  result = 0;
1359  else
1360  result = (int64) (dbentry->n_tuples_inserted);
1361 
1362  PG_RETURN_INT64(result);
1363 }
1364 
1365 
1366 Datum
1368 {
1369  Oid dbid = PG_GETARG_OID(0);
1370  int64 result;
1371  PgStat_StatDBEntry *dbentry;
1372 
1373  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1374  result = 0;
1375  else
1376  result = (int64) (dbentry->n_tuples_updated);
1377 
1378  PG_RETURN_INT64(result);
1379 }
1380 
1381 
1382 Datum
1384 {
1385  Oid dbid = PG_GETARG_OID(0);
1386  int64 result;
1387  PgStat_StatDBEntry *dbentry;
1388 
1389  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1390  result = 0;
1391  else
1392  result = (int64) (dbentry->n_tuples_deleted);
1393 
1394  PG_RETURN_INT64(result);
1395 }
1396 
1397 Datum
1399 {
1400  Oid dbid = PG_GETARG_OID(0);
1401  TimestampTz result;
1402  PgStat_StatDBEntry *dbentry;
1403 
1404  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1405  result = 0;
1406  else
1407  result = dbentry->stat_reset_timestamp;
1408 
1409  if (result == 0)
1410  PG_RETURN_NULL();
1411  else
1412  PG_RETURN_TIMESTAMPTZ(result);
1413 }
1414 
1415 Datum
1417 {
1418  Oid dbid = PG_GETARG_OID(0);
1419  int64 result;
1420  PgStat_StatDBEntry *dbentry;
1421 
1422  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1423  result = 0;
1424  else
1425  result = dbentry->n_temp_files;
1426 
1427  PG_RETURN_INT64(result);
1428 }
1429 
1430 
1431 Datum
1433 {
1434  Oid dbid = PG_GETARG_OID(0);
1435  int64 result;
1436  PgStat_StatDBEntry *dbentry;
1437 
1438  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1439  result = 0;
1440  else
1441  result = dbentry->n_temp_bytes;
1442 
1443  PG_RETURN_INT64(result);
1444 }
1445 
1446 Datum
1448 {
1449  Oid dbid = PG_GETARG_OID(0);
1450  int64 result;
1451  PgStat_StatDBEntry *dbentry;
1452 
1453  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1454  result = 0;
1455  else
1456  result = (int64) (dbentry->n_conflict_tablespace);
1457 
1458  PG_RETURN_INT64(result);
1459 }
1460 
1461 Datum
1463 {
1464  Oid dbid = PG_GETARG_OID(0);
1465  int64 result;
1466  PgStat_StatDBEntry *dbentry;
1467 
1468  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1469  result = 0;
1470  else
1471  result = (int64) (dbentry->n_conflict_lock);
1472 
1473  PG_RETURN_INT64(result);
1474 }
1475 
1476 Datum
1478 {
1479  Oid dbid = PG_GETARG_OID(0);
1480  int64 result;
1481  PgStat_StatDBEntry *dbentry;
1482 
1483  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1484  result = 0;
1485  else
1486  result = (int64) (dbentry->n_conflict_snapshot);
1487 
1488  PG_RETURN_INT64(result);
1489 }
1490 
1491 Datum
1493 {
1494  Oid dbid = PG_GETARG_OID(0);
1495  int64 result;
1496  PgStat_StatDBEntry *dbentry;
1497 
1498  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1499  result = 0;
1500  else
1501  result = (int64) (dbentry->n_conflict_bufferpin);
1502 
1503  PG_RETURN_INT64(result);
1504 }
1505 
1506 Datum
1508 {
1509  Oid dbid = PG_GETARG_OID(0);
1510  int64 result;
1511  PgStat_StatDBEntry *dbentry;
1512 
1513  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1514  result = 0;
1515  else
1516  result = (int64) (dbentry->n_conflict_startup_deadlock);
1517 
1518  PG_RETURN_INT64(result);
1519 }
1520 
1521 Datum
1523 {
1524  Oid dbid = PG_GETARG_OID(0);
1525  int64 result;
1526  PgStat_StatDBEntry *dbentry;
1527 
1528  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1529  result = 0;
1530  else
1531  result = (int64) (dbentry->n_conflict_tablespace +
1532  dbentry->n_conflict_lock +
1533  dbentry->n_conflict_snapshot +
1534  dbentry->n_conflict_bufferpin +
1535  dbentry->n_conflict_startup_deadlock);
1536 
1537  PG_RETURN_INT64(result);
1538 }
1539 
1540 Datum
1542 {
1543  Oid dbid = PG_GETARG_OID(0);
1544  int64 result;
1545  PgStat_StatDBEntry *dbentry;
1546 
1547  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1548  result = 0;
1549  else
1550  result = (int64) (dbentry->n_deadlocks);
1551 
1552  PG_RETURN_INT64(result);
1553 }
1554 
1555 Datum
1557 {
1558  Oid dbid = PG_GETARG_OID(0);
1559  int64 result;
1560  PgStat_StatDBEntry *dbentry;
1561 
1562  if (!DataChecksumsEnabled())
1563  PG_RETURN_NULL();
1564 
1565  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1566  result = 0;
1567  else
1568  result = (int64) (dbentry->n_checksum_failures);
1569 
1570  PG_RETURN_INT64(result);
1571 }
1572 
1573 Datum
1575 {
1576  Oid dbid = PG_GETARG_OID(0);
1577  TimestampTz result;
1578  PgStat_StatDBEntry *dbentry;
1579 
1580  if (!DataChecksumsEnabled())
1581  PG_RETURN_NULL();
1582 
1583  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1584  result = 0;
1585  else
1586  result = dbentry->last_checksum_failure;
1587 
1588  if (result == 0)
1589  PG_RETURN_NULL();
1590  else
1591  PG_RETURN_TIMESTAMPTZ(result);
1592 }
1593 
1594 Datum
1596 {
1597  Oid dbid = PG_GETARG_OID(0);
1598  double result;
1599  PgStat_StatDBEntry *dbentry;
1600 
1601  /* convert counter from microsec to millisec for display */
1602  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1603  result = 0;
1604  else
1605  result = ((double) dbentry->n_block_read_time) / 1000.0;
1606 
1607  PG_RETURN_FLOAT8(result);
1608 }
1609 
1610 Datum
1612 {
1613  Oid dbid = PG_GETARG_OID(0);
1614  double result;
1615  PgStat_StatDBEntry *dbentry;
1616 
1617  /* convert counter from microsec to millisec for display */
1618  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1619  result = 0;
1620  else
1621  result = ((double) dbentry->n_block_write_time) / 1000.0;
1622 
1623  PG_RETURN_FLOAT8(result);
1624 }
1625 
1626 Datum
1628 {
1629  PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1630 }
1631 
1632 Datum
1634 {
1635  PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1636 }
1637 
1638 Datum
1640 {
1641  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1642 }
1643 
1644 Datum
1646 {
1647  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1648 }
1649 
1650 Datum
1652 {
1653  PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1654 }
1655 
1656 Datum
1658 {
1659  /* time is already in msec, just convert to double for presentation */
1660  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1661 }
1662 
1663 Datum
1665 {
1666  /* time is already in msec, just convert to double for presentation */
1667  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1668 }
1669 
1670 Datum
1672 {
1673  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1674 }
1675 
1676 Datum
1678 {
1679  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1680 }
1681 
1682 Datum
1684 {
1685  PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1686 }
1687 
1688 Datum
1690 {
1691  PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1692 }
1693 
1694 /*
1695  * Returns statistics of SLRU caches.
1696  */
1697 Datum
1699 {
1700 #define PG_STAT_GET_SLRU_COLS 9
1701  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1702  TupleDesc tupdesc;
1703  Tuplestorestate *tupstore;
1704  MemoryContext per_query_ctx;
1705  MemoryContext oldcontext;
1706  int i;
1707  PgStat_SLRUStats *stats;
1708 
1709  /* check to see if caller supports us returning a tuplestore */
1710  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1711  ereport(ERROR,
1712  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1713  errmsg("set-valued function called in context that cannot accept a set")));
1714  if (!(rsinfo->allowedModes & SFRM_Materialize))
1715  ereport(ERROR,
1716  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1717  errmsg("materialize mode required, but it is not allowed in this context")));
1718 
1719  /* Build a tuple descriptor for our result type */
1720  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1721  elog(ERROR, "return type must be a row type");
1722 
1723  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1724  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1725 
1726  tupstore = tuplestore_begin_heap(true, false, work_mem);
1727  rsinfo->returnMode = SFRM_Materialize;
1728  rsinfo->setResult = tupstore;
1729  rsinfo->setDesc = tupdesc;
1730 
1731  MemoryContextSwitchTo(oldcontext);
1732 
1733  /* request SLRU stats from the stat collector */
1734  stats = pgstat_fetch_slru();
1735 
1736  for (i = 0;; i++)
1737  {
1738  /* for each row */
1740  bool nulls[PG_STAT_GET_SLRU_COLS];
1741  PgStat_SLRUStats stat = stats[i];
1742  const char *name;
1743 
1744  name = pgstat_slru_name(i);
1745 
1746  if (!name)
1747  break;
1748 
1749  MemSet(values, 0, sizeof(values));
1750  MemSet(nulls, 0, sizeof(nulls));
1751 
1752  values[0] = PointerGetDatum(cstring_to_text(name));
1753  values[1] = Int64GetDatum(stat.blocks_zeroed);
1754  values[2] = Int64GetDatum(stat.blocks_hit);
1755  values[3] = Int64GetDatum(stat.blocks_read);
1756  values[4] = Int64GetDatum(stat.blocks_written);
1757  values[5] = Int64GetDatum(stat.blocks_exists);
1758  values[6] = Int64GetDatum(stat.flush);
1759  values[7] = Int64GetDatum(stat.truncate);
1760  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1761 
1762  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1763  }
1764 
1765  /* clean up and return the tuplestore */
1766  tuplestore_donestoring(tupstore);
1767 
1768  return (Datum) 0;
1769 }
1770 
1771 Datum
1773 {
1774  Oid relid = PG_GETARG_OID(0);
1775  int64 result;
1776  PgStat_TableStatus *tabentry;
1777 
1778  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1779  result = 0;
1780  else
1781  result = (int64) (tabentry->t_counts.t_numscans);
1782 
1783  PG_RETURN_INT64(result);
1784 }
1785 
1786 Datum
1788 {
1789  Oid relid = PG_GETARG_OID(0);
1790  int64 result;
1791  PgStat_TableStatus *tabentry;
1792 
1793  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1794  result = 0;
1795  else
1796  result = (int64) (tabentry->t_counts.t_tuples_returned);
1797 
1798  PG_RETURN_INT64(result);
1799 }
1800 
1801 Datum
1803 {
1804  Oid relid = PG_GETARG_OID(0);
1805  int64 result;
1806  PgStat_TableStatus *tabentry;
1807 
1808  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1809  result = 0;
1810  else
1811  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1812 
1813  PG_RETURN_INT64(result);
1814 }
1815 
1816 Datum
1818 {
1819  Oid relid = PG_GETARG_OID(0);
1820  int64 result;
1821  PgStat_TableStatus *tabentry;
1823 
1824  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1825  result = 0;
1826  else
1827  {
1828  result = tabentry->t_counts.t_tuples_inserted;
1829  /* live subtransactions' counts aren't in t_tuples_inserted yet */
1830  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1831  result += trans->tuples_inserted;
1832  }
1833 
1834  PG_RETURN_INT64(result);
1835 }
1836 
1837 Datum
1839 {
1840  Oid relid = PG_GETARG_OID(0);
1841  int64 result;
1842  PgStat_TableStatus *tabentry;
1844 
1845  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1846  result = 0;
1847  else
1848  {
1849  result = tabentry->t_counts.t_tuples_updated;
1850  /* live subtransactions' counts aren't in t_tuples_updated yet */
1851  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1852  result += trans->tuples_updated;
1853  }
1854 
1855  PG_RETURN_INT64(result);
1856 }
1857 
1858 Datum
1860 {
1861  Oid relid = PG_GETARG_OID(0);
1862  int64 result;
1863  PgStat_TableStatus *tabentry;
1865 
1866  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1867  result = 0;
1868  else
1869  {
1870  result = tabentry->t_counts.t_tuples_deleted;
1871  /* live subtransactions' counts aren't in t_tuples_deleted yet */
1872  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1873  result += trans->tuples_deleted;
1874  }
1875 
1876  PG_RETURN_INT64(result);
1877 }
1878 
1879 Datum
1881 {
1882  Oid relid = PG_GETARG_OID(0);
1883  int64 result;
1884  PgStat_TableStatus *tabentry;
1885 
1886  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1887  result = 0;
1888  else
1889  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
1890 
1891  PG_RETURN_INT64(result);
1892 }
1893 
1894 Datum
1896 {
1897  Oid relid = PG_GETARG_OID(0);
1898  int64 result;
1899  PgStat_TableStatus *tabentry;
1900 
1901  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1902  result = 0;
1903  else
1904  result = (int64) (tabentry->t_counts.t_blocks_fetched);
1905 
1906  PG_RETURN_INT64(result);
1907 }
1908 
1909 Datum
1911 {
1912  Oid relid = PG_GETARG_OID(0);
1913  int64 result;
1914  PgStat_TableStatus *tabentry;
1915 
1916  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1917  result = 0;
1918  else
1919  result = (int64) (tabentry->t_counts.t_blocks_hit);
1920 
1921  PG_RETURN_INT64(result);
1922 }
1923 
1924 Datum
1926 {
1927  Oid funcid = PG_GETARG_OID(0);
1928  PgStat_BackendFunctionEntry *funcentry;
1929 
1930  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1931  PG_RETURN_NULL();
1932  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
1933 }
1934 
1935 Datum
1937 {
1938  Oid funcid = PG_GETARG_OID(0);
1939  PgStat_BackendFunctionEntry *funcentry;
1940 
1941  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1942  PG_RETURN_NULL();
1944 }
1945 
1946 Datum
1948 {
1949  Oid funcid = PG_GETARG_OID(0);
1950  PgStat_BackendFunctionEntry *funcentry;
1951 
1952  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1953  PG_RETURN_NULL();
1955 }
1956 
1957 
1958 /* Get the timestamp of the current statistics snapshot */
1959 Datum
1961 {
1962  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
1963 }
1964 
1965 /* Discard the active statistics snapshot */
1966 Datum
1968 {
1970 
1971  PG_RETURN_VOID();
1972 }
1973 
1974 
1975 /* Reset all counters for the current database */
1976 Datum
1978 {
1980 
1981  PG_RETURN_VOID();
1982 }
1983 
1984 /* Reset some shared cluster-wide counters */
1985 Datum
1987 {
1988  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1989 
1991 
1992  PG_RETURN_VOID();
1993 }
1994 
1995 /* Reset a single counter in the current database */
1996 Datum
1998 {
1999  Oid taboid = PG_GETARG_OID(0);
2000 
2002 
2003  PG_RETURN_VOID();
2004 }
2005 
2006 Datum
2008 {
2009  Oid funcoid = PG_GETARG_OID(0);
2010 
2012 
2013  PG_RETURN_VOID();
2014 }
2015 
2016 /* Reset SLRU counters (a specific one or all of them). */
2017 Datum
2019 {
2020  char *target = NULL;
2021 
2022  if (!PG_ARGISNULL(0))
2023  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2024 
2025  pgstat_reset_slru_counter(target);
2026 
2027  PG_RETURN_VOID();
2028 }
2029 
2030 Datum
2032 {
2033  TupleDesc tupdesc;
2034  Datum values[7];
2035  bool nulls[7];
2036  PgStat_ArchiverStats *archiver_stats;
2037 
2038  /* Initialise values and NULL flags arrays */
2039  MemSet(values, 0, sizeof(values));
2040  MemSet(nulls, 0, sizeof(nulls));
2041 
2042  /* Initialise attributes information in the tuple descriptor */
2043  tupdesc = CreateTemplateTupleDesc(7);
2044  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2045  INT8OID, -1, 0);
2046  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2047  TEXTOID, -1, 0);
2048  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2049  TIMESTAMPTZOID, -1, 0);
2050  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2051  INT8OID, -1, 0);
2052  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2053  TEXTOID, -1, 0);
2054  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2055  TIMESTAMPTZOID, -1, 0);
2056  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2057  TIMESTAMPTZOID, -1, 0);
2058 
2059  BlessTupleDesc(tupdesc);
2060 
2061  /* Get statistics about the archiver process */
2062  archiver_stats = pgstat_fetch_stat_archiver();
2063 
2064  /* Fill values and NULLs */
2065  values[0] = Int64GetDatum(archiver_stats->archived_count);
2066  if (*(archiver_stats->last_archived_wal) == '\0')
2067  nulls[1] = true;
2068  else
2069  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2070 
2071  if (archiver_stats->last_archived_timestamp == 0)
2072  nulls[2] = true;
2073  else
2074  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
2075 
2076  values[3] = Int64GetDatum(archiver_stats->failed_count);
2077  if (*(archiver_stats->last_failed_wal) == '\0')
2078  nulls[4] = true;
2079  else
2080  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2081 
2082  if (archiver_stats->last_failed_timestamp == 0)
2083  nulls[5] = true;
2084  else
2085  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2086 
2087  if (archiver_stats->stat_reset_timestamp == 0)
2088  nulls[6] = true;
2089  else
2090  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2091 
2092  /* Returns the record as Datum */
2093  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2094 }
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:1477
char ssl_issuer_dn[NAMEDATALEN]
Definition: pgstat.h:1032
Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1367
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1231
char gss_princ[NAMEDATALEN]
Definition: pgstat.h:1046
Datum pg_stat_reset_slru(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2018
Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1255
bool ssl_compression
Definition: pgstat.h:1021
Datum pg_stat_get_db_deadlocks(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1541
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:1522
Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1683
Datum pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1026
void pgstat_reset_slru_counter(const char *name)
Definition: pgstat.c:1419
PgStat_Counter analyze_count
Definition: pgstat.h:692
char ssl_cipher[NAMEDATALEN]
Definition: pgstat.h:1023
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:723
Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:459
instr_time f_self_time
Definition: pgstat.h:494
#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:1234
#define PG_STAT_GET_SLRU_COLS
PgStat_Counter archived_count
Definition: pgstat.h:718
Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2007
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Oid st_progress_command_target
Definition: pgstat.h:1136
Datum pg_stat_get_archiver(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:2031
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
int MyProcPid
Definition: globals.c:40
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
Datum pg_stat_get_checkpoint_sync_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1664
PgStat_Counter tuples_updated
Definition: pgstat.h:171
PgStat_Counter tuples_returned
Definition: pgstat.h:671
Datum pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1838
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:202
uint32 wait_event_info
Definition: proc.h:185
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1925
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: pgstat.c:3513
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:105
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2564
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:1094
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2362
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
PgStat_Counter n_blocks_hit
Definition: pgstat.h:629
bool DataChecksumsEnabled(void)
Definition: xlog.c:4933
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:556
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:639
#define PG_STAT_GET_ACTIVITY_COLS
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:640
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:110
Datum pg_stat_get_db_blk_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1611
#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:693
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
Datum pg_stat_get_checkpoint_write_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1657
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:365
TimestampTz stat_reset_timestamp
Definition: pgstat.h:760
PgStat_Counter n_checksum_failures
Definition: pgstat.h:644
char ssl_client_dn[NAMEDATALEN]
Definition: pgstat.h:1024
PgStat_Counter n_temp_files
Definition: pgstat.h:641
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1362
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:353
Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1936
TimestampTz last_checksum_failure
Definition: pgstat.h:645
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
TransactionId backend_xmin
Definition: pgstat.h:1226
Datum pg_stat_reset(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1977
char ssl_version[NAMEDATALEN]
Definition: pgstat.h:1022
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:630
PgStat_Counter vacuum_count
Definition: pgstat.h:688
SockAddr st_clientaddr
Definition: pgstat.h:1100
Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1117
int errcode(int sqlerrcode)
Definition: elog.c:610
Datum pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1574
#define MemSet(start, val, len)
Definition: c.h:978
Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1960
Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1651
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2481
TimestampTz stat_reset_timestamp
Definition: pgstat.h:649
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:493
Datum pg_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:955
Datum pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1910
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2670
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:1319
Datum pg_stat_get_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:42
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
ProgressCommandType st_progress_command
Definition: pgstat.h:1135
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PgStat_Counter numscans
Definition: pgstat.h:669
#define lengthof(array)
Definition: c.h:675
PgStat_TableCounts t_counts
Definition: pgstat.h:161
PgStat_Counter blocks_exists
Definition: pgstat.h:757
unsigned int Oid
Definition: postgres_ext.h:31
Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1689
char ssl_client_serial[NAMEDATALEN]
Definition: pgstat.h:1030
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:677
PgStat_Counter f_total_time
Definition: pgstat.h:708
Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1432
BackendState st_state
Definition: pgstat.h:1112
PgStat_Counter t_tuples_returned
Definition: pgstat.h:104
Datum pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1047
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
char * st_clienthostname
Definition: pgstat.h:1101
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
PgStat_Counter tuples_inserted
Definition: pgstat.h:674
PgStat_Counter f_numcalls
Definition: pgstat.h:492
Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:381
Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1947
Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1639
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
Definition: pgstat.c:2618
Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1383
signed int int32
Definition: c.h:362
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
PgStat_Counter n_xact_commit
Definition: pgstat.h:626
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
Definition: pgstat.c:2653
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:1928
Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:106
Oid st_databaseid
Definition: pgstat.h:1098
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:1001
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:628
Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1398
PgStat_Counter n_live_tuples
Definition: pgstat.h:679
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:682
Datum pg_stat_get_db_blk_read_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1595
Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:975
void pfree(void *pointer)
Definition: mcxt.c:1056
PgStat_FunctionCounts f_counts
Definition: pgstat.h:504
#define NI_MAXHOST
Definition: getaddrinfo.h:88
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:636
Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1271
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:249
TransactionId backend_xid
Definition: pgstat.h:1220
TimestampTz vacuum_timestamp
Definition: pgstat.h:687
PgStat_Counter t_tuples_updated
Definition: pgstat.h:108
Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1416
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter n_dead_tuples
Definition: pgstat.h:680
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:1186
Datum pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1802
Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1677
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
PgStat_Counter f_numcalls
Definition: pgstat.h:706
Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1787
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2508
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
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:985
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:632
PgStat_Counter tuples_inserted
Definition: pgstat.h:170
Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1967
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1492
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: pgstat.c:3568
Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1633
char * st_activity_raw
Definition: pgstat.h:1124
PgStat_Counter blocks_read
Definition: pgstat.h:755
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum pg_stat_get_db_conflict_lock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1462
#define NI_MAXSERV
Definition: getaddrinfo.h:91
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1727
PgStat_Counter tuples_fetched
Definition: pgstat.h:672
unsigned int uint32
Definition: c.h:374
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:638
Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1069
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:569
Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1001
TimestampTz st_state_start_timestamp
Definition: pgstat.h:1095
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:634
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1701
PgStat_Counter n_conflict_lock
Definition: pgstat.h:637
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
PgStat_Counter blocks_hit
Definition: pgstat.h:754
char * st_appname
Definition: pgstat.h:1115
PgStat_Counter t_blocks_hit
Definition: pgstat.h:118
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
Definition: pgstat.c:1393
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:179
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
PgStat_Counter n_xact_rollback
Definition: pgstat.h:627
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
Datum pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1880
Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1095
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:109
PgStat_Counter blocks_zeroed
Definition: pgstat.h:753
#define stat(a, b)
Definition: win32_port.h:255
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define TransactionIdGetDatum(X)
Definition: postgres.h:521
Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:90
Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1287
PgBackendStatus backendStatus
Definition: pgstat.h:1214
uintptr_t Datum
Definition: postgres.h:367
PgStat_Counter tuples_deleted
Definition: pgstat.h:172
Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:138
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
Datum pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1895
BackendType st_backendType
Definition: pgstat.h:1089
PgStat_Counter blocks_hit
Definition: pgstat.h:685
int work_mem
Definition: globals.c:121
TimestampTz analyze_timestamp
Definition: pgstat.h:691
Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:404
TimestampTz last_failed_timestamp
Definition: pgstat.h:725
PgStat_Counter changes_since_analyze
Definition: pgstat.h:681
PgStat_Counter t_numscans
Definition: pgstat.h:102
#define BoolGetDatum(X)
Definition: postgres.h:402
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
PgStat_MsgBgWriter bgwriterStats
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
int allowedModes
Definition: execnodes.h:305
Datum pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1556
PgStat_Counter n_tuples_updated
Definition: pgstat.h:633
#define PG_RETURN_VOID()
Definition: fmgr.h:348
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1335
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
text * cstring_to_text(const char *s)
Definition: varlena.c:172
Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1671
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2637
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:2687
ProgressCommandType
Definition: pgstat.h:991
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2596
Datum pg_stat_reset_shared(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1986
PgStat_Counter failed_count
Definition: pgstat.h:722
PgStat_Counter n_block_write_time
Definition: pgstat.h:647
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1137
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
PgStat_Counter tuples_deleted
Definition: pgstat.h:676
Datum pg_stat_get_function_total_time(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:392
void pgstat_clear_snapshot(void)
Definition: pgstat.c:5840
PgStat_Counter flush
Definition: pgstat.h:758
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
PgStat_Counter tuples_updated
Definition: pgstat.h:675
Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1139
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:1351
Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1997
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:689
PgStat_Counter n_block_read_time
Definition: pgstat.h:646
const char * name
Definition: encode.c:561
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:160
Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1303
Tuplestorestate * setResult
Definition: execnodes.h:310
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:694
Datum pg_stat_get_activity(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:568
static Datum values[MAXATTR]
Definition: bootstrap.c:167
char * text_to_cstring(const text *t)
Definition: varlena.c:205
ExprContext * econtext
Definition: execnodes.h:303
char * pgstat_clip_activity(const char *raw_activity)
Definition: pgstat.c:6644
#define Int32GetDatum(X)
Definition: postgres.h:479
TimestampTz stat_reset_timestamp
Definition: pgstat.h:726
TimestampTz last_archived_timestamp
Definition: pgstat.h:721
Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:962
void * user_fctx
Definition: funcapi.h:82
TupleDesc setDesc
Definition: execnodes.h:311
Datum pg_stat_get_slru(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1698
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402
int errmsg(const char *fmt,...)
Definition: elog.c:824
PgStat_Counter n_deadlocks
Definition: pgstat.h:643
Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1627
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:1093
#define elog(elevel,...)
Definition: elog.h:214
#define PG_STAT_GET_PROGRESS_COLS
int i
const char * pgstat_slru_name(int slru_idx)
Definition: pgstat.c:6709
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:34
Datum pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1817
Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1772
#define CStringGetTextDatum(s)
Definition: builtins.h:87
Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:218
Definition: c.h:562
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1507
Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:988
#define TransactionIdIsValid(xid)
Definition: transam.h:41
PgStat_Counter f_self_time
Definition: pgstat.h:709
Definition: proc.h:102
PgStat_Counter blocks_fetched
Definition: pgstat.h:684
Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:170
#define PG_RETURN_OID(x)
Definition: fmgr.h:359
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:1092
Datum pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1859
int pid
Definition: proc.h:116
PgStat_Counter truncate
Definition: pgstat.h:759
Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1645
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:1105
int16 AttrNumber
Definition: attnum.h:21
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:107
void pgstat_reset_counters(void)
Definition: pgstat.c:1340
#define PG_RETURN_NULL()
Definition: fmgr.h:344
PgStat_Counter blocks_written
Definition: pgstat.h:756
PGPROC * lockGroupLeader
Definition: proc.h:210
PgBackendGSSStatus * st_gssstatus
Definition: pgstat.h:1109
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:117
PgStat_Counter n_tuples_fetched
Definition: pgstat.h:631
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:719
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:690
Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS)
Definition: pgstatfuncs.c:1447
PgStat_Counter n_temp_bytes
Definition: pgstat.h:642
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295