PostgreSQL Source Code  git master
pgstatfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xlog.h"
#include "access/xlogprefetcher.h"
#include "catalog/catalog.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_type.h"
#include "common/ip.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/postmaster.h"
#include "replication/logicallauncher.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/inet.h"
#include "utils/timestamp.h"
Include dependency graph for pgstatfuncs.c:

Go to the source code of this file.

Macros

#define UINT32_ACCESS_ONCE(var)   ((uint32)(*((volatile uint32 *)&(var))))
 
#define HAS_PGSTAT_PERMISSIONS(role)   (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
 
#define PG_STAT_GET_RELENTRY_INT64(stat)
 
#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat)
 
#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3
 
#define PG_STAT_GET_ACTIVITY_COLS   31
 
#define PG_STAT_GET_SUBXACT_COLS   2
 
#define PG_STAT_GET_DBENTRY_INT64(stat)
 
#define PG_STAT_GET_DBENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_WAL_COLS   9
 
#define PG_STAT_GET_SLRU_COLS   9
 
#define PG_STAT_GET_XACT_RELENTRY_INT64(stat)
 
#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_REPLICATION_SLOT_COLS   10
 
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS   4
 

Typedefs

typedef enum io_stat_col io_stat_col
 

Enumerations

enum  io_stat_col {
  IO_COL_INVALID = -1 , IO_COL_BACKEND_TYPE , IO_COL_OBJECT , IO_COL_CONTEXT ,
  IO_COL_READS , IO_COL_READ_TIME , IO_COL_WRITES , IO_COL_WRITE_TIME ,
  IO_COL_WRITEBACKS , IO_COL_WRITEBACK_TIME , IO_COL_EXTENDS , IO_COL_EXTEND_TIME ,
  IO_COL_CONVERSION , IO_COL_HITS , IO_COL_EVICTIONS , IO_COL_REUSES ,
  IO_COL_FSYNCS , IO_COL_FSYNC_TIME , IO_COL_RESET_TIME , IO_NUM_COLUMNS
}
 

Functions

Datum pg_stat_get_function_calls (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_idset (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_progress_info (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_activity (PG_FUNCTION_ARGS)
 
Datum pg_backend_pid (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_pid (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_dbid (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_userid (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_subxact (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_activity (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_wait_event_type (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_wait_event (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_activity_start (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_xact_start (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_start (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_client_addr (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_client_port (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_numbackends (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_all (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_checksum_failures (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_checksum_last_failure (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_timed_checkpoints (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_requested_checkpoints (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_buf_written_checkpoints (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_buf_written_clean (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_maxwritten_clean (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpoint_write_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpoint_sync_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_buf_written_backend (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_buf_fsync_backend (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_buf_alloc (PG_FUNCTION_ARGS)
 
static io_stat_col pgstat_get_io_op_index (IOOp io_op)
 
static io_stat_col pgstat_get_io_time_index (IOOp io_op)
 
static double pg_stat_us_to_ms (PgStat_Counter val_ms)
 
Datum pg_stat_get_io (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_wal (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_slru (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_tuples_inserted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_tuples_updated (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_tuples_deleted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_function_calls (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_snapshot_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_stat_clear_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_stat_force_next_flush (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_shared (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_single_table_counters (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_single_function_counters (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_slru (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_subscription_stats (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_archiver (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_subscription_stats (PG_FUNCTION_ARGS)
 
Datum pg_stat_have_stats (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ HAS_PGSTAT_PERMISSIONS

#define HAS_PGSTAT_PERMISSIONS (   role)    (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))

Definition at line 39 of file pgstatfuncs.c.

◆ PG_STAT_GET_ACTIVITY_COLS

#define PG_STAT_GET_ACTIVITY_COLS   31

◆ PG_STAT_GET_DBENTRY_FLOAT8_MS

#define PG_STAT_GET_DBENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid dbid = PG_GETARG_OID(0); \
double result; \
PgStat_StatDBEntry *dbentry; \
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
result = 0; \
else \
result = ((double) dbentry->stat) / 1000.0; \
}
#define CppConcat(x, y)
Definition: c.h:317
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)

Definition at line 1153 of file pgstatfuncs.c.

◆ PG_STAT_GET_DBENTRY_INT64

#define PG_STAT_GET_DBENTRY_INT64 (   stat)
Value:
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid dbid = PG_GETARG_OID(0); \
int64 result; \
PgStat_StatDBEntry *dbentry; \
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
result = 0; \
else \
result = (int64) (dbentry->stat); \
PG_RETURN_INT64(result); \
}
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368

Definition at line 992 of file pgstatfuncs.c.

◆ PG_STAT_GET_FUNCENTRY_FLOAT8_MS

#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_function_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid funcid = PG_GETARG_OID(0); \
double result; \
PgStat_StatFuncEntry *funcentry; \
if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL) \
PG_RETURN_NULL(); \
result = ((double) funcentry->stat) / 1000.0; \
PG_RETURN_FLOAT8(result); \
}
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)

Definition at line 157 of file pgstatfuncs.c.

◆ PG_STAT_GET_PROGRESS_COLS

#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3

◆ PG_STAT_GET_RELENTRY_INT64

#define PG_STAT_GET_RELENTRY_INT64 (   stat)
Value:
{ \
Oid relid = PG_GETARG_OID(0); \
int64 result; \
PgStat_StatTabEntry *tabentry; \
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
result = 0; \
else \
result = (int64) (tabentry->stat); \
PG_RETURN_INT64(result); \
}
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)

Definition at line 41 of file pgstatfuncs.c.

◆ PG_STAT_GET_RELENTRY_TIMESTAMPTZ

#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ (   stat)
Value:
{ \
Oid relid = PG_GETARG_OID(0); \
TimestampTz result; \
PgStat_StatTabEntry *tabentry; \
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
result = 0; \
else \
result = tabentry->stat; \
if (result == 0) \
PG_RETURN_NULL(); \
}
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68

Definition at line 111 of file pgstatfuncs.c.

◆ PG_STAT_GET_REPLICATION_SLOT_COLS

#define PG_STAT_GET_REPLICATION_SLOT_COLS   10

◆ PG_STAT_GET_SLRU_COLS

#define PG_STAT_GET_SLRU_COLS   9

◆ PG_STAT_GET_SUBSCRIPTION_STATS_COLS

#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS   4

◆ PG_STAT_GET_SUBXACT_COLS

#define PG_STAT_GET_SUBXACT_COLS   2

◆ PG_STAT_GET_WAL_COLS

#define PG_STAT_GET_WAL_COLS   9

◆ PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS

#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_xact_function_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid funcid = PG_GETARG_OID(0); \
PgStat_FunctionCounts *funcentry; \
if ((funcentry = find_funcstat_entry(funcid)) == NULL) \
PG_RETURN_NULL(); \
PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->stat)); \
}
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
PgStat_FunctionCounts * find_funcstat_entry(Oid func_id)

Definition at line 1677 of file pgstatfuncs.c.

◆ PG_STAT_GET_XACT_RELENTRY_INT64

#define PG_STAT_GET_XACT_RELENTRY_INT64 (   stat)
Value:
CppConcat(pg_stat_get_xact_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid relid = PG_GETARG_OID(0); \
int64 result; \
PgStat_TableStatus *tabentry; \
if ((tabentry = find_tabstat_entry(relid)) == NULL) \
result = 0; \
else \
result = (int64) (tabentry->counts.stat); \
PG_RETURN_INT64(result); \
}
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)

Definition at line 1566 of file pgstatfuncs.c.

◆ UINT32_ACCESS_ONCE

#define UINT32_ACCESS_ONCE (   var)    ((uint32)(*((volatile uint32 *)&(var))))

Definition at line 37 of file pgstatfuncs.c.

Typedef Documentation

◆ io_stat_col

typedef enum io_stat_col io_stat_col

Enumeration Type Documentation

◆ io_stat_col

Enumerator
IO_COL_INVALID 
IO_COL_BACKEND_TYPE 
IO_COL_OBJECT 
IO_COL_CONTEXT 
IO_COL_READS 
IO_COL_READ_TIME 
IO_COL_WRITES 
IO_COL_WRITE_TIME 
IO_COL_WRITEBACKS 
IO_COL_WRITEBACK_TIME 
IO_COL_EXTENDS 
IO_COL_EXTEND_TIME 
IO_COL_CONVERSION 
IO_COL_HITS 
IO_COL_EVICTIONS 
IO_COL_REUSES 
IO_COL_FSYNCS 
IO_COL_FSYNC_TIME 
IO_COL_RESET_TIME 
IO_NUM_COLUMNS 

Definition at line 1258 of file pgstatfuncs.c.

1259 {
1260  IO_COL_INVALID = -1,
1262  IO_COL_OBJECT,
1264  IO_COL_READS,
1266  IO_COL_WRITES,
1273  IO_COL_HITS,
1275  IO_COL_REUSES,
1276  IO_COL_FSYNCS,
1280 } io_stat_col;
io_stat_col
Definition: pgstatfuncs.c:1259
@ IO_COL_READS
Definition: pgstatfuncs.c:1264
@ IO_NUM_COLUMNS
Definition: pgstatfuncs.c:1279
@ IO_COL_RESET_TIME
Definition: pgstatfuncs.c:1278
@ IO_COL_WRITE_TIME
Definition: pgstatfuncs.c:1267
@ IO_COL_HITS
Definition: pgstatfuncs.c:1273
@ IO_COL_EXTENDS
Definition: pgstatfuncs.c:1270
@ IO_COL_WRITEBACK_TIME
Definition: pgstatfuncs.c:1269
@ IO_COL_REUSES
Definition: pgstatfuncs.c:1275
@ IO_COL_WRITES
Definition: pgstatfuncs.c:1266
@ IO_COL_OBJECT
Definition: pgstatfuncs.c:1262
@ IO_COL_EVICTIONS
Definition: pgstatfuncs.c:1274
@ IO_COL_CONVERSION
Definition: pgstatfuncs.c:1272
@ IO_COL_WRITEBACKS
Definition: pgstatfuncs.c:1268
@ IO_COL_CONTEXT
Definition: pgstatfuncs.c:1263
@ IO_COL_BACKEND_TYPE
Definition: pgstatfuncs.c:1261
@ IO_COL_FSYNC_TIME
Definition: pgstatfuncs.c:1277
@ IO_COL_EXTEND_TIME
Definition: pgstatfuncs.c:1271
@ IO_COL_FSYNCS
Definition: pgstatfuncs.c:1276
@ IO_COL_INVALID
Definition: pgstatfuncs.c:1260
@ IO_COL_READ_TIME
Definition: pgstatfuncs.c:1265

Function Documentation

◆ pg_backend_pid()

Datum pg_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 663 of file pgstatfuncs.c.

664 {
666 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int MyProcPid
Definition: globals.c:44

References MyProcPid, and PG_RETURN_INT32.

◆ pg_stat_clear_snapshot()

Datum pg_stat_clear_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1712 of file pgstatfuncs.c.

1713 {
1715 
1716  PG_RETURN_VOID();
1717 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:785

References PG_RETURN_VOID, and pgstat_clear_snapshot().

◆ pg_stat_force_next_flush()

Datum pg_stat_force_next_flush ( PG_FUNCTION_ARGS  )

Definition at line 1722 of file pgstatfuncs.c.

1723 {
1725 
1726  PG_RETURN_VOID();
1727 }
void pgstat_force_next_flush(void)
Definition: pgstat.c:697

References PG_RETURN_VOID, and pgstat_force_next_flush().

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 305 of file pgstatfuncs.c.

306 {
307 #define PG_STAT_GET_ACTIVITY_COLS 31
308  int num_backends = pgstat_fetch_stat_numbackends();
309  int curr_backend;
310  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
311  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
312 
313  InitMaterializedSRF(fcinfo, 0);
314 
315  /* 1-based index */
316  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
317  {
318  /* for each row */
320  bool nulls[PG_STAT_GET_ACTIVITY_COLS] = {0};
321  LocalPgBackendStatus *local_beentry;
322  PgBackendStatus *beentry;
323  PGPROC *proc;
324  const char *wait_event_type = NULL;
325  const char *wait_event = NULL;
326 
327  /* Get the next one in the list */
328  local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
329  beentry = &local_beentry->backendStatus;
330 
331  /* If looking for specific PID, ignore all the others */
332  if (pid != -1 && beentry->st_procpid != pid)
333  continue;
334 
335  /* Values available to all callers */
336  if (beentry->st_databaseid != InvalidOid)
337  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
338  else
339  nulls[0] = true;
340 
341  values[1] = Int32GetDatum(beentry->st_procpid);
342 
343  if (beentry->st_userid != InvalidOid)
344  values[2] = ObjectIdGetDatum(beentry->st_userid);
345  else
346  nulls[2] = true;
347 
348  if (beentry->st_appname)
349  values[3] = CStringGetTextDatum(beentry->st_appname);
350  else
351  nulls[3] = true;
352 
353  if (TransactionIdIsValid(local_beentry->backend_xid))
354  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
355  else
356  nulls[15] = true;
357 
358  if (TransactionIdIsValid(local_beentry->backend_xmin))
359  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
360  else
361  nulls[16] = true;
362 
363  /* Values only available to role member or pg_read_all_stats */
364  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
365  {
366  SockAddr zero_clientaddr;
367  char *clipped_activity;
368 
369  switch (beentry->st_state)
370  {
371  case STATE_IDLE:
372  values[4] = CStringGetTextDatum("idle");
373  break;
374  case STATE_RUNNING:
375  values[4] = CStringGetTextDatum("active");
376  break;
378  values[4] = CStringGetTextDatum("idle in transaction");
379  break;
380  case STATE_FASTPATH:
381  values[4] = CStringGetTextDatum("fastpath function call");
382  break;
384  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
385  break;
386  case STATE_DISABLED:
387  values[4] = CStringGetTextDatum("disabled");
388  break;
389  case STATE_UNDEFINED:
390  nulls[4] = true;
391  break;
392  }
393 
394  clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
395  values[5] = CStringGetTextDatum(clipped_activity);
396  pfree(clipped_activity);
397 
398  /* leader_pid */
399  nulls[29] = true;
400 
401  proc = BackendPidGetProc(beentry->st_procpid);
402 
403  if (proc == NULL && (beentry->st_backendType != B_BACKEND))
404  {
405  /*
406  * For an auxiliary process, retrieve process info from
407  * AuxiliaryProcs stored in shared-memory.
408  */
409  proc = AuxiliaryPidGetProc(beentry->st_procpid);
410  }
411 
412  /*
413  * If a PGPROC entry was retrieved, display wait events and lock
414  * group leader or apply leader information if any. To avoid
415  * extra overhead, no extra lock is being held, so there is no
416  * guarantee of consistency across multiple rows.
417  */
418  if (proc != NULL)
419  {
420  uint32 raw_wait_event;
421  PGPROC *leader;
422 
423  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
424  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
425  wait_event = pgstat_get_wait_event(raw_wait_event);
426 
427  leader = proc->lockGroupLeader;
428 
429  /*
430  * Show the leader only for active parallel workers. This
431  * leaves the field as NULL for the leader of a parallel group
432  * or the leader of parallel apply workers.
433  */
434  if (leader && leader->pid != beentry->st_procpid)
435  {
436  values[29] = Int32GetDatum(leader->pid);
437  nulls[29] = false;
438  }
439  else if (beentry->st_backendType == B_BG_WORKER)
440  {
441  int leader_pid = GetLeaderApplyWorkerPid(beentry->st_procpid);
442 
443  if (leader_pid != InvalidPid)
444  {
445  values[29] = Int32GetDatum(leader_pid);
446  nulls[29] = false;
447  }
448  }
449  }
450 
451  if (wait_event_type)
452  values[6] = CStringGetTextDatum(wait_event_type);
453  else
454  nulls[6] = true;
455 
456  if (wait_event)
457  values[7] = CStringGetTextDatum(wait_event);
458  else
459  nulls[7] = true;
460 
461  /*
462  * Don't expose transaction time for walsenders; it confuses
463  * monitoring, particularly because we don't keep the time up-to-
464  * date.
465  */
466  if (beentry->st_xact_start_timestamp != 0 &&
467  beentry->st_backendType != B_WAL_SENDER)
469  else
470  nulls[8] = true;
471 
472  if (beentry->st_activity_start_timestamp != 0)
474  else
475  nulls[9] = true;
476 
477  if (beentry->st_proc_start_timestamp != 0)
479  else
480  nulls[10] = true;
481 
482  if (beentry->st_state_start_timestamp != 0)
484  else
485  nulls[11] = true;
486 
487  /* A zeroed client addr means we don't know */
488  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
489  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
490  sizeof(zero_clientaddr)) == 0)
491  {
492  nulls[12] = true;
493  nulls[13] = true;
494  nulls[14] = true;
495  }
496  else
497  {
498  if (beentry->st_clientaddr.addr.ss_family == AF_INET ||
499  beentry->st_clientaddr.addr.ss_family == AF_INET6)
500  {
501  char remote_host[NI_MAXHOST];
502  char remote_port[NI_MAXSERV];
503  int ret;
504 
505  remote_host[0] = '\0';
506  remote_port[0] = '\0';
507  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
508  beentry->st_clientaddr.salen,
509  remote_host, sizeof(remote_host),
510  remote_port, sizeof(remote_port),
511  NI_NUMERICHOST | NI_NUMERICSERV);
512  if (ret == 0)
513  {
514  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
516  CStringGetDatum(remote_host));
517  if (beentry->st_clienthostname &&
518  beentry->st_clienthostname[0])
520  else
521  nulls[13] = true;
522  values[14] = Int32GetDatum(atoi(remote_port));
523  }
524  else
525  {
526  nulls[12] = true;
527  nulls[13] = true;
528  nulls[14] = true;
529  }
530  }
531  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
532  {
533  /*
534  * Unix sockets always reports NULL for host and -1 for
535  * port, so it's possible to tell the difference to
536  * connections we have no permissions to view, or with
537  * errors.
538  */
539  nulls[12] = true;
540  nulls[13] = true;
541  values[14] = Int32GetDatum(-1);
542  }
543  else
544  {
545  /* Unknown address type, should never happen */
546  nulls[12] = true;
547  nulls[13] = true;
548  nulls[14] = true;
549  }
550  }
551  /* Add backend type */
552  if (beentry->st_backendType == B_BG_WORKER)
553  {
554  const char *bgw_type;
555 
556  bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
557  if (bgw_type)
558  values[17] = CStringGetTextDatum(bgw_type);
559  else
560  nulls[17] = true;
561  }
562  else
563  values[17] =
565 
566  /* SSL information */
567  if (beentry->st_ssl)
568  {
569  values[18] = BoolGetDatum(true); /* ssl */
572  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
573 
574  if (beentry->st_sslstatus->ssl_client_dn[0])
576  else
577  nulls[22] = true;
578 
579  if (beentry->st_sslstatus->ssl_client_serial[0])
583  Int32GetDatum(-1));
584  else
585  nulls[23] = true;
586 
587  if (beentry->st_sslstatus->ssl_issuer_dn[0])
589  else
590  nulls[24] = true;
591  }
592  else
593  {
594  values[18] = BoolGetDatum(false); /* ssl */
595  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = true;
596  }
597 
598  /* GSSAPI information */
599  if (beentry->st_gss)
600  {
601  values[25] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
603  values[27] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
604  values[28] = BoolGetDatum(beentry->st_gssstatus->gss_delegation); /* GSS credentials
605  * delegated */
606  }
607  else
608  {
609  values[25] = BoolGetDatum(false); /* gss_auth */
610  nulls[26] = true; /* No GSS principal */
611  values[27] = BoolGetDatum(false); /* GSS Encryption not in
612  * use */
613  values[28] = BoolGetDatum(false); /* GSS credentials not
614  * delegated */
615  }
616  if (beentry->st_query_id == 0)
617  nulls[30] = true;
618  else
619  values[30] = UInt64GetDatum(beentry->st_query_id);
620  }
621  else
622  {
623  /* No permissions to view data about this session */
624  values[5] = CStringGetTextDatum("<insufficient privilege>");
625  nulls[4] = true;
626  nulls[6] = true;
627  nulls[7] = true;
628  nulls[8] = true;
629  nulls[9] = true;
630  nulls[10] = true;
631  nulls[11] = true;
632  nulls[12] = true;
633  nulls[13] = true;
634  nulls[14] = true;
635  nulls[17] = true;
636  nulls[18] = true;
637  nulls[19] = true;
638  nulls[20] = true;
639  nulls[21] = true;
640  nulls[22] = true;
641  nulls[23] = true;
642  nulls[24] = true;
643  nulls[25] = true;
644  nulls[26] = true;
645  nulls[27] = true;
646  nulls[28] = true;
647  nulls[29] = true;
648  nulls[30] = true;
649  }
650 
651  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
652 
653  /* If only a single backend was requested, and we found it, break. */
654  if (pid != -1)
655  break;
656  }
657 
658  return (Datum) 0;
659 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:627
int pgstat_fetch_stat_numbackends(void)
LocalPgBackendStatus * pgstat_get_local_beentry_by_index(int idx)
char * pgstat_clip_activity(const char *raw_activity)
@ STATE_UNDEFINED
@ STATE_IDLEINTRANSACTION_ABORTED
@ STATE_IDLE
@ STATE_IDLEINTRANSACTION
@ STATE_DISABLED
@ STATE_FASTPATH
@ STATE_RUNNING
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1288
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
unsigned int uint32
Definition: c.h:495
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:114
pid_t GetLeaderApplyWorkerPid(pid_t pid)
Definition: launcher.c:1252
void pfree(void *pointer)
Definition: mcxt.c:1456
@ B_WAL_SENDER
Definition: miscadmin.h:340
@ B_BG_WORKER
Definition: miscadmin.h:333
@ B_BACKEND
Definition: miscadmin.h:332
#define InvalidPid
Definition: miscadmin.h:32
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:264
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2095
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:121
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:37
#define PG_STAT_GET_ACTIVITY_COLS
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:39
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
uintptr_t Datum
Definition: postgres.h:64
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:436
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3103
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:965
TransactionId backend_xid
PgBackendStatus backendStatus
TransactionId backend_xmin
Definition: proc.h:162
uint32 wait_event_info
Definition: proc.h:270
int pid
Definition: proc.h:186
PGPROC * lockGroupLeader
Definition: proc.h:295
char gss_princ[NAMEDATALEN]
char ssl_version[NAMEDATALEN]
char ssl_cipher[NAMEDATALEN]
char ssl_client_dn[NAMEDATALEN]
char ssl_client_serial[NAMEDATALEN]
char ssl_issuer_dn[NAMEDATALEN]
BackendType st_backendType
TimestampTz st_state_start_timestamp
TimestampTz st_proc_start_timestamp
PgBackendGSSStatus * st_gssstatus
BackendState st_state
TimestampTz st_activity_start_timestamp
SockAddr st_clientaddr
PgBackendSSLStatus * st_sslstatus
TimestampTz st_xact_start_timestamp
char * st_clienthostname
TupleDesc setDesc
Definition: execnodes.h:334
Tuplestorestate * setResult
Definition: execnodes.h:333
struct sockaddr_storage addr
Definition: pqcomm.h:32
socklen_t salen
Definition: pqcomm.h:33
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: wait_event.c:395
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:340

References SockAddr::addr, AuxiliaryPidGetProc(), B_BACKEND, B_BG_WORKER, B_WAL_SENDER, LocalPgBackendStatus::backend_xid, LocalPgBackendStatus::backend_xmin, BackendPidGetProc(), LocalPgBackendStatus::backendStatus, BoolGetDatum(), clean_ipv6_addr(), CStringGetDatum(), CStringGetTextDatum, DirectFunctionCall1, DirectFunctionCall3, GetBackendTypeDesc(), GetBackgroundWorkerTypeByPid(), GetLeaderApplyWorkerPid(), PgBackendGSSStatus::gss_auth, PgBackendGSSStatus::gss_delegation, PgBackendGSSStatus::gss_enc, PgBackendGSSStatus::gss_princ, HAS_PGSTAT_PERMISSIONS, inet_in(), InitMaterializedSRF(), Int32GetDatum(), InvalidOid, InvalidPid, PGPROC::lockGroupLeader, numeric_in(), ObjectIdGetDatum(), pfree(), PG_ARGISNULL, PG_GETARG_INT32, pg_getnameinfo_all(), PG_STAT_GET_ACTIVITY_COLS, pgstat_clip_activity(), pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), pgstat_get_wait_event(), pgstat_get_wait_event_type(), PGPROC::pid, SockAddr::salen, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, PgBackendSSLStatus::ssl_bits, PgBackendSSLStatus::ssl_cipher, PgBackendSSLStatus::ssl_client_dn, PgBackendSSLStatus::ssl_client_serial, PgBackendSSLStatus::ssl_issuer_dn, PgBackendSSLStatus::ssl_version, PgBackendStatus::st_activity_raw, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_appname, PgBackendStatus::st_backendType, PgBackendStatus::st_clientaddr, PgBackendStatus::st_clienthostname, PgBackendStatus::st_databaseid, PgBackendStatus::st_gss, PgBackendStatus::st_gssstatus, PgBackendStatus::st_proc_start_timestamp, PgBackendStatus::st_procpid, PgBackendStatus::st_query_id, PgBackendStatus::st_ssl, PgBackendStatus::st_sslstatus, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_userid, PgBackendStatus::st_xact_start_timestamp, STATE_DISABLED, STATE_FASTPATH, STATE_IDLE, STATE_IDLEINTRANSACTION, STATE_IDLEINTRANSACTION_ABORTED, STATE_RUNNING, STATE_UNDEFINED, TimestampTzGetDatum(), TransactionIdGetDatum(), TransactionIdIsValid, tuplestore_putvalues(), UINT32_ACCESS_ONCE, UInt64GetDatum(), values, and PGPROC::wait_event_info.

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

Definition at line 1861 of file pgstatfuncs.c.

1862 {
1863  TupleDesc tupdesc;
1864  Datum values[7] = {0};
1865  bool nulls[7] = {0};
1866  PgStat_ArchiverStats *archiver_stats;
1867 
1868  /* Initialise attributes information in the tuple descriptor */
1869  tupdesc = CreateTemplateTupleDesc(7);
1870  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
1871  INT8OID, -1, 0);
1872  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
1873  TEXTOID, -1, 0);
1874  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
1875  TIMESTAMPTZOID, -1, 0);
1876  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
1877  INT8OID, -1, 0);
1878  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
1879  TEXTOID, -1, 0);
1880  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
1881  TIMESTAMPTZOID, -1, 0);
1882  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
1883  TIMESTAMPTZOID, -1, 0);
1884 
1885  BlessTupleDesc(tupdesc);
1886 
1887  /* Get statistics about the archiver process */
1888  archiver_stats = pgstat_fetch_stat_archiver();
1889 
1890  /* Fill values and NULLs */
1891  values[0] = Int64GetDatum(archiver_stats->archived_count);
1892  if (*(archiver_stats->last_archived_wal) == '\0')
1893  nulls[1] = true;
1894  else
1895  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
1896 
1897  if (archiver_stats->last_archived_timestamp == 0)
1898  nulls[2] = true;
1899  else
1900  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
1901 
1902  values[3] = Int64GetDatum(archiver_stats->failed_count);
1903  if (*(archiver_stats->last_failed_wal) == '\0')
1904  nulls[4] = true;
1905  else
1906  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
1907 
1908  if (archiver_stats->last_failed_timestamp == 0)
1909  nulls[5] = true;
1910  else
1911  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
1912 
1913  if (archiver_stats->stat_reset_timestamp == 0)
1914  nulls[6] = true;
1915  else
1916  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
1917 
1918  /* Returns the record as Datum */
1920 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2072
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1790
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
TimestampTz last_failed_timestamp
Definition: pgstat.h:249
TimestampTz stat_reset_timestamp
Definition: pgstat.h:250
TimestampTz last_archived_timestamp
Definition: pgstat.h:245
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:247
PgStat_Counter failed_count
Definition: pgstat.h:246
PgStat_Counter archived_count
Definition: pgstat.h:242
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:243
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

References PgStat_ArchiverStats::archived_count, BlessTupleDesc(), CreateTemplateTupleDesc(), CStringGetTextDatum, PgStat_ArchiverStats::failed_count, heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), PgStat_ArchiverStats::last_archived_timestamp, PgStat_ArchiverStats::last_archived_wal, PgStat_ArchiverStats::last_failed_timestamp, PgStat_ArchiverStats::last_failed_wal, PG_RETURN_DATUM, pgstat_fetch_stat_archiver(), PgStat_ArchiverStats::stat_reset_timestamp, TimestampTzGetDatum(), TupleDescInitEntry(), and values.

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

Definition at line 743 of file pgstatfuncs.c.

744 {
745  int32 beid = PG_GETARG_INT32(0);
746  PgBackendStatus *beentry;
747  const char *activity;
748  char *clipped_activity;
749  text *ret;
750 
751  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
752  activity = "<backend information not available>";
753  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
754  activity = "<insufficient privilege>";
755  else if (*(beentry->st_activity_raw) == '\0')
756  activity = "<command string not enabled>";
757  else
758  activity = beentry->st_activity_raw;
759 
760  clipped_activity = pgstat_clip_activity(activity);
761  ret = cstring_to_text(activity);
762  pfree(clipped_activity);
763 
764  PG_RETURN_TEXT_P(ret);
765 }
PgBackendStatus * pgstat_get_beentry_by_backend_id(BackendId beid)
signed int int32
Definition: c.h:483
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:676
text * cstring_to_text(const char *s)
Definition: varlena.c:182

References cstring_to_text(), HAS_PGSTAT_PERMISSIONS, pfree(), PG_GETARG_INT32, PG_RETURN_TEXT_P, pgstat_clip_activity(), pgstat_get_beentry_by_backend_id(), PgBackendStatus::st_activity_raw, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_activity_start()

Datum pg_stat_get_backend_activity_start ( PG_FUNCTION_ARGS  )

Definition at line 811 of file pgstatfuncs.c.

812 {
813  int32 beid = PG_GETARG_INT32(0);
814  TimestampTz result;
815  PgBackendStatus *beentry;
816 
817  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
818  PG_RETURN_NULL();
819 
820  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
821  PG_RETURN_NULL();
822 
823  result = beentry->st_activity_start_timestamp;
824 
825  /*
826  * No time recorded for start of current query -- this is the case if the
827  * user hasn't enabled query-level stats collection.
828  */
829  if (result == 0)
830  PG_RETURN_NULL();
831 
832  PG_RETURN_TIMESTAMPTZ(result);
833 }
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_NULL()
Definition: fmgr.h:345

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_get_beentry_by_backend_id(), PgBackendStatus::st_activity_start_timestamp, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_client_addr()

Datum pg_stat_get_backend_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 881 of file pgstatfuncs.c.

882 {
883  int32 beid = PG_GETARG_INT32(0);
884  PgBackendStatus *beentry;
885  SockAddr zero_clientaddr;
886  char remote_host[NI_MAXHOST];
887  int ret;
888 
889  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
890  PG_RETURN_NULL();
891 
892  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
893  PG_RETURN_NULL();
894 
895  /* A zeroed client addr means we don't know */
896  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
897  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
898  sizeof(zero_clientaddr)) == 0)
899  PG_RETURN_NULL();
900 
901  switch (beentry->st_clientaddr.addr.ss_family)
902  {
903  case AF_INET:
904  case AF_INET6:
905  break;
906  default:
907  PG_RETURN_NULL();
908  }
909 
910  remote_host[0] = '\0';
911  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
912  beentry->st_clientaddr.salen,
913  remote_host, sizeof(remote_host),
914  NULL, 0,
915  NI_NUMERICHOST | NI_NUMERICSERV);
916  if (ret != 0)
917  PG_RETURN_NULL();
918 
919  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
920 
922  CStringGetDatum(remote_host)));
923 }

References SockAddr::addr, clean_ipv6_addr(), CStringGetDatum(), DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, inet_in(), PG_GETARG_INT32, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_NULL, pgstat_get_beentry_by_backend_id(), SockAddr::salen, PgBackendStatus::st_clientaddr, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_client_port()

Datum pg_stat_get_backend_client_port ( PG_FUNCTION_ARGS  )

Definition at line 926 of file pgstatfuncs.c.

927 {
928  int32 beid = PG_GETARG_INT32(0);
929  PgBackendStatus *beentry;
930  SockAddr zero_clientaddr;
931  char remote_port[NI_MAXSERV];
932  int ret;
933 
934  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
935  PG_RETURN_NULL();
936 
937  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
938  PG_RETURN_NULL();
939 
940  /* A zeroed client addr means we don't know */
941  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
942  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
943  sizeof(zero_clientaddr)) == 0)
944  PG_RETURN_NULL();
945 
946  switch (beentry->st_clientaddr.addr.ss_family)
947  {
948  case AF_INET:
949  case AF_INET6:
950  break;
951  case AF_UNIX:
952  PG_RETURN_INT32(-1);
953  default:
954  PG_RETURN_NULL();
955  }
956 
957  remote_port[0] = '\0';
958  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
959  beentry->st_clientaddr.salen,
960  NULL, 0,
961  remote_port, sizeof(remote_port),
962  NI_NUMERICHOST | NI_NUMERICSERV);
963  if (ret != 0)
964  PG_RETURN_NULL();
965 
967  CStringGetDatum(remote_port)));
968 }
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:287

References SockAddr::addr, CStringGetDatum(), DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, int4in(), PG_GETARG_INT32, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_INT32, PG_RETURN_NULL, pgstat_get_beentry_by_backend_id(), SockAddr::salen, PgBackendStatus::st_clientaddr, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_dbid()

Datum pg_stat_get_backend_dbid ( PG_FUNCTION_ARGS  )

Definition at line 683 of file pgstatfuncs.c.

684 {
685  int32 beid = PG_GETARG_INT32(0);
686  PgBackendStatus *beentry;
687 
688  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
689  PG_RETURN_NULL();
690 
691  PG_RETURN_OID(beentry->st_databaseid);
692 }
#define PG_RETURN_OID(x)
Definition: fmgr.h:360

References PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_OID, pgstat_get_beentry_by_backend_id(), and PgBackendStatus::st_databaseid.

◆ pg_stat_get_backend_idset()

Datum pg_stat_get_backend_idset ( PG_FUNCTION_ARGS  )

Definition at line 178 of file pgstatfuncs.c.

179 {
180  FuncCallContext *funcctx;
181  int *fctx;
182 
183  /* stuff done only on the first call of the function */
184  if (SRF_IS_FIRSTCALL())
185  {
186  /* create a function context for cross-call persistence */
187  funcctx = SRF_FIRSTCALL_INIT();
188 
190  sizeof(int));
191  funcctx->user_fctx = fctx;
192 
193  fctx[0] = 0;
194  }
195 
196  /* stuff done on every call of the function */
197  funcctx = SRF_PERCALL_SETUP();
198  fctx = funcctx->user_fctx;
199 
200  fctx[0] += 1;
201 
202  /*
203  * We recheck pgstat_fetch_stat_numbackends() each time through, just in
204  * case the local status data has been refreshed since we started. It's
205  * plenty cheap enough if not. If a refresh does happen, we'll likely
206  * miss or duplicate some backend IDs, but we're content not to crash.
207  * (Refreshing midway through such a query would be problematic usage
208  * anyway, since the backend IDs we've already returned might no longer
209  * refer to extant sessions.)
210  */
211  if (fctx[0] <= pgstat_fetch_stat_numbackends())
212  {
213  /* do when there is more left to send */
215 
216  SRF_RETURN_NEXT(funcctx, Int32GetDatum(local_beentry->backend_id));
217  }
218  else
219  {
220  /* do when there is no more left */
221  SRF_RETURN_DONE(funcctx);
222  }
223 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101

References LocalPgBackendStatus::backend_id, Int32GetDatum(), MemoryContextAlloc(), FuncCallContext::multi_call_memory_ctx, pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and FuncCallContext::user_fctx.

◆ pg_stat_get_backend_pid()

Datum pg_stat_get_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 670 of file pgstatfuncs.c.

671 {
672  int32 beid = PG_GETARG_INT32(0);
673  PgBackendStatus *beentry;
674 
675  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
676  PG_RETURN_NULL();
677 
678  PG_RETURN_INT32(beentry->st_procpid);
679 }

References PG_GETARG_INT32, PG_RETURN_INT32, PG_RETURN_NULL, pgstat_get_beentry_by_backend_id(), and PgBackendStatus::st_procpid.

◆ pg_stat_get_backend_start()

Datum pg_stat_get_backend_start ( PG_FUNCTION_ARGS  )

Definition at line 859 of file pgstatfuncs.c.

860 {
861  int32 beid = PG_GETARG_INT32(0);
862  TimestampTz result;
863  PgBackendStatus *beentry;
864 
865  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
866  PG_RETURN_NULL();
867 
868  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
869  PG_RETURN_NULL();
870 
871  result = beentry->st_proc_start_timestamp;
872 
873  if (result == 0) /* probably can't happen? */
874  PG_RETURN_NULL();
875 
876  PG_RETURN_TIMESTAMPTZ(result);
877 }

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_get_beentry_by_backend_id(), PgBackendStatus::st_proc_start_timestamp, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_subxact()

Datum pg_stat_get_backend_subxact ( PG_FUNCTION_ARGS  )

Definition at line 708 of file pgstatfuncs.c.

709 {
710 #define PG_STAT_GET_SUBXACT_COLS 2
711  TupleDesc tupdesc;
713  bool nulls[PG_STAT_GET_SUBXACT_COLS] = {0};
714  int32 beid = PG_GETARG_INT32(0);
715  LocalPgBackendStatus *local_beentry;
716 
717  /* Initialise attributes information in the tuple descriptor */
719  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subxact_count",
720  INT4OID, -1, 0);
721  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "subxact_overflow",
722  BOOLOID, -1, 0);
723 
724  BlessTupleDesc(tupdesc);
725 
726  if ((local_beentry = pgstat_get_local_beentry_by_backend_id(beid)) != NULL)
727  {
728  /* Fill values and NULLs */
729  values[0] = Int32GetDatum(local_beentry->backend_subxact_count);
730  values[1] = BoolGetDatum(local_beentry->backend_subxact_overflowed);
731  }
732  else
733  {
734  nulls[0] = true;
735  nulls[1] = true;
736  }
737 
738  /* Returns the record as Datum */
740 }
LocalPgBackendStatus * pgstat_get_local_beentry_by_backend_id(BackendId beid)
#define PG_STAT_GET_SUBXACT_COLS

References LocalPgBackendStatus::backend_subxact_count, LocalPgBackendStatus::backend_subxact_overflowed, BlessTupleDesc(), BoolGetDatum(), CreateTemplateTupleDesc(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), PG_GETARG_INT32, PG_RETURN_DATUM, PG_STAT_GET_SUBXACT_COLS, pgstat_get_local_beentry_by_backend_id(), TupleDescInitEntry(), and values.

◆ pg_stat_get_backend_userid()

Datum pg_stat_get_backend_userid ( PG_FUNCTION_ARGS  )

Definition at line 696 of file pgstatfuncs.c.

697 {
698  int32 beid = PG_GETARG_INT32(0);
699  PgBackendStatus *beentry;
700 
701  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
702  PG_RETURN_NULL();
703 
704  PG_RETURN_OID(beentry->st_userid);
705 }

References PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_OID, pgstat_get_beentry_by_backend_id(), and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_wait_event()

Datum pg_stat_get_backend_wait_event ( PG_FUNCTION_ARGS  )

Definition at line 789 of file pgstatfuncs.c.

790 {
791  int32 beid = PG_GETARG_INT32(0);
792  PgBackendStatus *beentry;
793  PGPROC *proc;
794  const char *wait_event = NULL;
795 
796  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
797  wait_event = "<backend information not available>";
798  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
799  wait_event = "<insufficient privilege>";
800  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
801  wait_event = pgstat_get_wait_event(proc->wait_event_info);
802 
803  if (!wait_event)
804  PG_RETURN_NULL();
805 
806  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
807 }

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_get_beentry_by_backend_id(), pgstat_get_wait_event(), PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and PGPROC::wait_event_info.

◆ pg_stat_get_backend_wait_event_type()

Datum pg_stat_get_backend_wait_event_type ( PG_FUNCTION_ARGS  )

Definition at line 768 of file pgstatfuncs.c.

769 {
770  int32 beid = PG_GETARG_INT32(0);
771  PgBackendStatus *beentry;
772  PGPROC *proc;
773  const char *wait_event_type = NULL;
774 
775  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
776  wait_event_type = "<backend information not available>";
777  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
778  wait_event_type = "<insufficient privilege>";
779  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
780  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
781 
782  if (!wait_event_type)
783  PG_RETURN_NULL();
784 
785  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
786 }

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_get_beentry_by_backend_id(), pgstat_get_wait_event_type(), PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and PGPROC::wait_event_info.

◆ pg_stat_get_backend_xact_start()

Datum pg_stat_get_backend_xact_start ( PG_FUNCTION_ARGS  )

Definition at line 837 of file pgstatfuncs.c.

838 {
839  int32 beid = PG_GETARG_INT32(0);
840  TimestampTz result;
841  PgBackendStatus *beentry;
842 
843  if ((beentry = pgstat_get_beentry_by_backend_id(beid)) == NULL)
844  PG_RETURN_NULL();
845 
846  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
847  PG_RETURN_NULL();
848 
849  result = beentry->st_xact_start_timestamp;
850 
851  if (result == 0) /* not in a transaction */
852  PG_RETURN_NULL();
853 
854  PG_RETURN_TIMESTAMPTZ(result);
855 }

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_get_beentry_by_backend_id(), PgBackendStatus::st_userid, and PgBackendStatus::st_xact_start_timestamp.

◆ pg_stat_get_bgwriter_buf_written_checkpoints()

Datum pg_stat_get_bgwriter_buf_written_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1197 of file pgstatfuncs.c.

1198 {
1199  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_checkpoints);
1200 }
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_bgwriter_buf_written_clean()

Datum pg_stat_get_bgwriter_buf_written_clean ( PG_FUNCTION_ARGS  )

Definition at line 1203 of file pgstatfuncs.c.

1204 {
1205  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean);
1206 }
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_bgwriter_maxwritten_clean()

Datum pg_stat_get_bgwriter_maxwritten_clean ( PG_FUNCTION_ARGS  )

Definition at line 1209 of file pgstatfuncs.c.

1210 {
1211  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean);
1212 }

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_bgwriter_requested_checkpoints()

Datum pg_stat_get_bgwriter_requested_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1191 of file pgstatfuncs.c.

1192 {
1193  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->requested_checkpoints);
1194 }

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_bgwriter_stat_reset_time()

Datum pg_stat_get_bgwriter_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1231 of file pgstatfuncs.c.

1232 {
1233  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp);
1234 }

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_bgwriter_timed_checkpoints()

Datum pg_stat_get_bgwriter_timed_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1185 of file pgstatfuncs.c.

1186 {
1187  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->timed_checkpoints);
1188 }

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1249 of file pgstatfuncs.c.

1250 {
1252 }

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_buf_fsync_backend()

Datum pg_stat_get_buf_fsync_backend ( PG_FUNCTION_ARGS  )

Definition at line 1243 of file pgstatfuncs.c.

1244 {
1245  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_fsync_backend);
1246 }

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_buf_written_backend()

Datum pg_stat_get_buf_written_backend ( PG_FUNCTION_ARGS  )

Definition at line 1237 of file pgstatfuncs.c.

1238 {
1239  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_backend);
1240 }

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpoint_sync_time()

Datum pg_stat_get_checkpoint_sync_time ( PG_FUNCTION_ARGS  )

Definition at line 1223 of file pgstatfuncs.c.

1224 {
1225  /* time is already in msec, just convert to double for presentation */
1226  PG_RETURN_FLOAT8((double)
1227  pgstat_fetch_stat_checkpointer()->checkpoint_sync_time);
1228 }

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpoint_write_time()

Datum pg_stat_get_checkpoint_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1215 of file pgstatfuncs.c.

1216 {
1217  /* time is already in msec, just convert to double for presentation */
1218  PG_RETURN_FLOAT8((double)
1219  pgstat_fetch_stat_checkpointer()->checkpoint_write_time);
1220 }

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_db_checksum_failures()

Datum pg_stat_get_db_checksum_failures ( PG_FUNCTION_ARGS  )

Definition at line 1114 of file pgstatfuncs.c.

1115 {
1116  Oid dbid = PG_GETARG_OID(0);
1117  int64 result;
1118  PgStat_StatDBEntry *dbentry;
1119 
1120  if (!DataChecksumsEnabled())
1121  PG_RETURN_NULL();
1122 
1123  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1124  result = 0;
1125  else
1126  result = (int64) (dbentry->checksum_failures);
1127 
1128  PG_RETURN_INT64(result);
1129 }
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter checksum_failures
Definition: pgstat.h:342
bool DataChecksumsEnabled(void)
Definition: xlog.c:4223

References PgStat_StatDBEntry::checksum_failures, DataChecksumsEnabled(), PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_checksum_last_failure()

Datum pg_stat_get_db_checksum_last_failure ( PG_FUNCTION_ARGS  )

Definition at line 1132 of file pgstatfuncs.c.

1133 {
1134  Oid dbid = PG_GETARG_OID(0);
1135  TimestampTz result;
1136  PgStat_StatDBEntry *dbentry;
1137 
1138  if (!DataChecksumsEnabled())
1139  PG_RETURN_NULL();
1140 
1141  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1142  result = 0;
1143  else
1144  result = dbentry->last_checksum_failure;
1145 
1146  if (result == 0)
1147  PG_RETURN_NULL();
1148  else
1149  PG_RETURN_TIMESTAMPTZ(result);
1150 }
TimestampTz last_checksum_failure
Definition: pgstat.h:343

References DataChecksumsEnabled(), PgStat_StatDBEntry::last_checksum_failure, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_all()

Datum pg_stat_get_db_conflict_all ( PG_FUNCTION_ARGS  )

Definition at line 1094 of file pgstatfuncs.c.

1095 {
1096  Oid dbid = PG_GETARG_OID(0);
1097  int64 result;
1098  PgStat_StatDBEntry *dbentry;
1099 
1100  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1101  result = 0;
1102  else
1103  result = (int64) (dbentry->conflict_tablespace +
1104  dbentry->conflict_lock +
1105  dbentry->conflict_snapshot +
1106  dbentry->conflict_logicalslot +
1107  dbentry->conflict_bufferpin +
1108  dbentry->conflict_startup_deadlock);
1109 
1110  PG_RETURN_INT64(result);
1111 }
PgStat_Counter conflict_startup_deadlock
Definition: pgstat.h:338
PgStat_Counter conflict_lock
Definition: pgstat.h:334
PgStat_Counter conflict_snapshot
Definition: pgstat.h:335
PgStat_Counter conflict_bufferpin
Definition: pgstat.h:337
PgStat_Counter conflict_logicalslot
Definition: pgstat.h:336
PgStat_Counter conflict_tablespace
Definition: pgstat.h:333

References PgStat_StatDBEntry::conflict_bufferpin, PgStat_StatDBEntry::conflict_lock, PgStat_StatDBEntry::conflict_logicalslot, PgStat_StatDBEntry::conflict_snapshot, PgStat_StatDBEntry::conflict_startup_deadlock, PgStat_StatDBEntry::conflict_tablespace, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 972 of file pgstatfuncs.c.

973 {
974  Oid dbid = PG_GETARG_OID(0);
975  int32 result;
976  int tot_backends = pgstat_fetch_stat_numbackends();
977  int idx;
978 
979  result = 0;
980  for (idx = 1; idx <= tot_backends; idx++)
981  {
983 
984  if (local_beentry->backendStatus.st_databaseid == dbid)
985  result++;
986  }
987 
988  PG_RETURN_INT32(result);
989 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259

References LocalPgBackendStatus::backendStatus, idx(), PG_GETARG_OID, PG_RETURN_INT32, pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), and PgBackendStatus::st_databaseid.

◆ pg_stat_get_db_stat_reset_time()

Datum pg_stat_get_db_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1075 of file pgstatfuncs.c.

1076 {
1077  Oid dbid = PG_GETARG_OID(0);
1078  TimestampTz result;
1079  PgStat_StatDBEntry *dbentry;
1080 
1081  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1082  result = 0;
1083  else
1084  result = dbentry->stat_reset_timestamp;
1085 
1086  if (result == 0)
1087  PG_RETURN_NULL();
1088  else
1089  PG_RETURN_TIMESTAMPTZ(result);
1090 }
TimestampTz stat_reset_timestamp
Definition: pgstat.h:354

References PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_fetch_stat_dbentry(), and PgStat_StatDBEntry::stat_reset_timestamp.

◆ pg_stat_get_function_calls()

Datum pg_stat_get_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 146 of file pgstatfuncs.c.

147 {
148  Oid funcid = PG_GETARG_OID(0);
149  PgStat_StatFuncEntry *funcentry;
150 
151  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
152  PG_RETURN_NULL();
153  PG_RETURN_INT64(funcentry->numcalls);
154 }
PgStat_Counter numcalls
Definition: pgstat.h:359

References PgStat_StatFuncEntry::numcalls, PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, and pgstat_fetch_stat_funcentry().

◆ pg_stat_get_io()

Datum pg_stat_get_io ( PG_FUNCTION_ARGS  )

Definition at line 1347 of file pgstatfuncs.c.

1348 {
1349  ReturnSetInfo *rsinfo;
1350  PgStat_IO *backends_io_stats;
1351  Datum reset_time;
1352 
1353  InitMaterializedSRF(fcinfo, 0);
1354  rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1355 
1356  backends_io_stats = pgstat_fetch_stat_io();
1357 
1358  reset_time = TimestampTzGetDatum(backends_io_stats->stat_reset_timestamp);
1359 
1360  for (int bktype = 0; bktype < BACKEND_NUM_TYPES; bktype++)
1361  {
1362  Datum bktype_desc = CStringGetTextDatum(GetBackendTypeDesc(bktype));
1363  PgStat_BktypeIO *bktype_stats = &backends_io_stats->stats[bktype];
1364 
1365  /*
1366  * In Assert builds, we can afford an extra loop through all of the
1367  * counters checking that only expected stats are non-zero, since it
1368  * keeps the non-Assert code cleaner.
1369  */
1370  Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype));
1371 
1372  /*
1373  * For those BackendTypes without IO Operation stats, skip
1374  * representing them in the view altogether.
1375  */
1376  if (!pgstat_tracks_io_bktype(bktype))
1377  continue;
1378 
1379  for (int io_obj = 0; io_obj < IOOBJECT_NUM_TYPES; io_obj++)
1380  {
1381  const char *obj_name = pgstat_get_io_object_name(io_obj);
1382 
1383  for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
1384  {
1385  const char *context_name = pgstat_get_io_context_name(io_context);
1386 
1387  Datum values[IO_NUM_COLUMNS] = {0};
1388  bool nulls[IO_NUM_COLUMNS] = {0};
1389 
1390  /*
1391  * Some combinations of BackendType, IOObject, and IOContext
1392  * are not valid for any type of IOOp. In such cases, omit the
1393  * entire row from the view.
1394  */
1395  if (!pgstat_tracks_io_object(bktype, io_obj, io_context))
1396  continue;
1397 
1398  values[IO_COL_BACKEND_TYPE] = bktype_desc;
1399  values[IO_COL_CONTEXT] = CStringGetTextDatum(context_name);
1402 
1403  /*
1404  * Hard-code this to the value of BLCKSZ for now. Future
1405  * values could include XLOG_BLCKSZ, once WAL IO is tracked,
1406  * and constant multipliers, once non-block-oriented IO (e.g.
1407  * temporary file IO) is tracked.
1408  */
1410 
1411  for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
1412  {
1413  int op_idx = pgstat_get_io_op_index(io_op);
1414  int time_idx = pgstat_get_io_time_index(io_op);
1415 
1416  /*
1417  * Some combinations of BackendType and IOOp, of IOContext
1418  * and IOOp, and of IOObject and IOOp are not tracked. Set
1419  * these cells in the view NULL.
1420  */
1421  if (pgstat_tracks_io_op(bktype, io_obj, io_context, io_op))
1422  {
1423  PgStat_Counter count =
1424  bktype_stats->counts[io_obj][io_context][io_op];
1425 
1426  values[op_idx] = Int64GetDatum(count);
1427  }
1428  else
1429  nulls[op_idx] = true;
1430 
1431  /* not every operation is timed */
1432  if (time_idx == IO_COL_INVALID)
1433  continue;
1434 
1435  if (!nulls[op_idx])
1436  {
1437  PgStat_Counter time =
1438  bktype_stats->times[io_obj][io_context][io_op];
1439 
1440  values[time_idx] = Float8GetDatum(pg_stat_us_to_ms(time));
1441  }
1442  else
1443  nulls[time_idx] = true;
1444  }
1445 
1446  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
1447  values, nulls);
1448  }
1449  }
1450  }
1451 
1452  return (Datum) 0;
1453 }
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1799
Assert(fmt[strlen(fmt) - 1] !='\n')
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:344
#define IOOP_NUM_TYPES
Definition: pgstat.h:306
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:292
int64 PgStat_Counter
Definition: pgstat.h:89
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:282
bool pgstat_tracks_io_bktype(BackendType bktype)
Definition: pgstat_io.c:304
const char * pgstat_get_io_object_name(IOObject io_object)
Definition: pgstat_io.c:226
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
Definition: pgstat_io.c:46
PgStat_IO * pgstat_fetch_stat_io(void)
Definition: pgstat_io.c:143
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:407
const char * pgstat_get_io_context_name(IOContext io_context)
Definition: pgstat_io.c:207
bool pgstat_tracks_io_object(BackendType bktype, IOObject io_object, IOContext io_context)
Definition: pgstat_io.c:342
static double pg_stat_us_to_ms(PgStat_Counter val_ms)
Definition: pgstatfuncs.c:1341
static io_stat_col pgstat_get_io_time_index(IOOp io_op)
Definition: pgstatfuncs.c:1320
static io_stat_col pgstat_get_io_op_index(IOOp io_op)
Definition: pgstatfuncs.c:1287
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:311
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:310
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:317
TimestampTz stat_reset_timestamp
Definition: pgstat.h:316

References Assert(), BACKEND_NUM_TYPES, PgStat_BktypeIO::counts, CStringGetTextDatum, Float8GetDatum(), GetBackendTypeDesc(), InitMaterializedSRF(), Int64GetDatum(), IO_COL_BACKEND_TYPE, IO_COL_CONTEXT, IO_COL_CONVERSION, IO_COL_INVALID, IO_COL_OBJECT, IO_COL_RESET_TIME, IO_NUM_COLUMNS, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, pg_stat_us_to_ms(), pgstat_bktype_io_stats_valid(), pgstat_fetch_stat_io(), pgstat_get_io_context_name(), pgstat_get_io_object_name(), pgstat_get_io_op_index(), pgstat_get_io_time_index(), pgstat_tracks_io_bktype(), pgstat_tracks_io_object(), pgstat_tracks_io_op(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, PgStat_IO::stat_reset_timestamp, PgStat_IO::stats, PgStat_BktypeIO::times, TimestampTzGetDatum(), tuplestore_putvalues(), and values.

◆ pg_stat_get_progress_info()

Datum pg_stat_get_progress_info ( PG_FUNCTION_ARGS  )

Definition at line 229 of file pgstatfuncs.c.

230 {
231 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
232  int num_backends = pgstat_fetch_stat_numbackends();
233  int curr_backend;
234  char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
235  ProgressCommandType cmdtype;
236  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
237 
238  /* Translate command name into command type code. */
239  if (pg_strcasecmp(cmd, "VACUUM") == 0)
240  cmdtype = PROGRESS_COMMAND_VACUUM;
241  else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
242  cmdtype = PROGRESS_COMMAND_ANALYZE;
243  else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
244  cmdtype = PROGRESS_COMMAND_CLUSTER;
245  else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
247  else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
248  cmdtype = PROGRESS_COMMAND_BASEBACKUP;
249  else if (pg_strcasecmp(cmd, "COPY") == 0)
250  cmdtype = PROGRESS_COMMAND_COPY;
251  else
252  ereport(ERROR,
253  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
254  errmsg("invalid command name: \"%s\"", cmd)));
255 
256  InitMaterializedSRF(fcinfo, 0);
257 
258  /* 1-based index */
259  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
260  {
261  LocalPgBackendStatus *local_beentry;
262  PgBackendStatus *beentry;
264  bool nulls[PG_STAT_GET_PROGRESS_COLS] = {0};
265  int i;
266 
267  local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
268  beentry = &local_beentry->backendStatus;
269 
270  /*
271  * Report values for only those backends which are running the given
272  * command.
273  */
274  if (beentry->st_progress_command != cmdtype)
275  continue;
276 
277  /* Value available to all callers */
278  values[0] = Int32GetDatum(beentry->st_procpid);
279  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
280 
281  /* show rest of the values including relid only to role members */
282  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
283  {
285  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
286  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
287  }
288  else
289  {
290  nulls[2] = true;
291  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
292  nulls[i + 3] = true;
293  }
294 
295  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
296  }
297 
298  return (Datum) 0;
299 }
#define PGSTAT_NUM_PROGRESS_PARAM
ProgressCommandType
@ PROGRESS_COMMAND_ANALYZE
@ PROGRESS_COMMAND_CLUSTER
@ PROGRESS_COMMAND_CREATE_INDEX
@ PROGRESS_COMMAND_VACUUM
@ PROGRESS_COMMAND_BASEBACKUP
@ PROGRESS_COMMAND_COPY
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int i
Definition: isn.c:73
#define PG_STAT_GET_PROGRESS_COLS
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
ProgressCommandType st_progress_command
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Oid st_progress_command_target
char * text_to_cstring(const text *t)
Definition: varlena.c:215

References LocalPgBackendStatus::backendStatus, ereport, errcode(), errmsg(), ERROR, HAS_PGSTAT_PERMISSIONS, i, if(), InitMaterializedSRF(), Int32GetDatum(), Int64GetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_STAT_GET_PROGRESS_COLS, pg_strcasecmp(), pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), PGSTAT_NUM_PROGRESS_PARAM, PROGRESS_COMMAND_ANALYZE, PROGRESS_COMMAND_BASEBACKUP, PROGRESS_COMMAND_CLUSTER, PROGRESS_COMMAND_COPY, PROGRESS_COMMAND_CREATE_INDEX, PROGRESS_COMMAND_VACUUM, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, PgBackendStatus::st_databaseid, PgBackendStatus::st_procpid, PgBackendStatus::st_progress_command, PgBackendStatus::st_progress_command_target, PgBackendStatus::st_progress_param, PgBackendStatus::st_userid, text_to_cstring(), tuplestore_putvalues(), and values.

◆ pg_stat_get_replication_slot()

Datum pg_stat_get_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 1927 of file pgstatfuncs.c.

1928 {
1929 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
1930  text *slotname_text = PG_GETARG_TEXT_P(0);
1931  NameData slotname;
1932  TupleDesc tupdesc;
1934  bool nulls[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0};
1935  PgStat_StatReplSlotEntry *slotent;
1936  PgStat_StatReplSlotEntry allzero;
1937 
1938  /* Initialise attributes information in the tuple descriptor */
1940  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
1941  TEXTOID, -1, 0);
1942  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
1943  INT8OID, -1, 0);
1944  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
1945  INT8OID, -1, 0);
1946  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
1947  INT8OID, -1, 0);
1948  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
1949  INT8OID, -1, 0);
1950  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
1951  INT8OID, -1, 0);
1952  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
1953  INT8OID, -1, 0);
1954  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
1955  INT8OID, -1, 0);
1956  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
1957  INT8OID, -1, 0);
1958  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
1959  TIMESTAMPTZOID, -1, 0);
1960  BlessTupleDesc(tupdesc);
1961 
1962  namestrcpy(&slotname, text_to_cstring(slotname_text));
1963  slotent = pgstat_fetch_replslot(slotname);
1964  if (!slotent)
1965  {
1966  /*
1967  * If the slot is not found, initialise its stats. This is possible if
1968  * the create slot message is lost.
1969  */
1970  memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
1971  slotent = &allzero;
1972  }
1973 
1974  values[0] = CStringGetTextDatum(NameStr(slotname));
1975  values[1] = Int64GetDatum(slotent->spill_txns);
1976  values[2] = Int64GetDatum(slotent->spill_count);
1977  values[3] = Int64GetDatum(slotent->spill_bytes);
1978  values[4] = Int64GetDatum(slotent->stream_txns);
1979  values[5] = Int64GetDatum(slotent->stream_count);
1980  values[6] = Int64GetDatum(slotent->stream_bytes);
1981  values[7] = Int64GetDatum(slotent->total_txns);
1982  values[8] = Int64GetDatum(slotent->total_bytes);
1983 
1984  if (slotent->stat_reset_timestamp == 0)
1985  nulls[9] = true;
1986  else
1988 
1989  /* Returns the record as Datum */
1991 }
#define NameStr(name)
Definition: c.h:735
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:336
void namestrcpy(Name name, const char *str)
Definition: name.c:233
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
#define PG_STAT_GET_REPLICATION_SLOT_COLS
TimestampTz stat_reset_timestamp
Definition: pgstat.h:375
PgStat_Counter stream_count
Definition: pgstat.h:371
PgStat_Counter total_txns
Definition: pgstat.h:373
PgStat_Counter total_bytes
Definition: pgstat.h:374
PgStat_Counter spill_txns
Definition: pgstat.h:367
PgStat_Counter stream_txns
Definition: pgstat.h:370
PgStat_Counter spill_count
Definition: pgstat.h:368
PgStat_Counter stream_bytes
Definition: pgstat.h:372
PgStat_Counter spill_bytes
Definition: pgstat.h:369
Definition: c.h:730

References BlessTupleDesc(), CreateTemplateTupleDesc(), CStringGetTextDatum, heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), NameStr, namestrcpy(), PG_GETARG_TEXT_P, PG_RETURN_DATUM, PG_STAT_GET_REPLICATION_SLOT_COLS, pgstat_fetch_replslot(), PgStat_StatReplSlotEntry::spill_bytes, PgStat_StatReplSlotEntry::spill_count, PgStat_StatReplSlotEntry::spill_txns, PgStat_StatReplSlotEntry::stat_reset_timestamp, PgStat_StatReplSlotEntry::stream_bytes, PgStat_StatReplSlotEntry::stream_count, PgStat_StatReplSlotEntry::stream_txns, text_to_cstring(), TimestampTzGetDatum(), PgStat_StatReplSlotEntry::total_bytes, PgStat_StatReplSlotEntry::total_txns, TupleDescInitEntry(), and values.

◆ pg_stat_get_slru()

Datum pg_stat_get_slru ( PG_FUNCTION_ARGS  )

Definition at line 1523 of file pgstatfuncs.c.

1524 {
1525 #define PG_STAT_GET_SLRU_COLS 9
1526  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1527  int i;
1528  PgStat_SLRUStats *stats;
1529 
1530  InitMaterializedSRF(fcinfo, 0);
1531 
1532  /* request SLRU stats from the cumulative stats system */
1533  stats = pgstat_fetch_slru();
1534 
1535  for (i = 0;; i++)
1536  {
1537  /* for each row */
1539  bool nulls[PG_STAT_GET_SLRU_COLS] = {0};
1541  const char *name;
1542 
1544 
1545  if (!name)
1546  break;
1547 
1548  stat = stats[i];
1549 
1551  values[1] = Int64GetDatum(stat.blocks_zeroed);
1552  values[2] = Int64GetDatum(stat.blocks_hit);
1553  values[3] = Int64GetDatum(stat.blocks_read);
1554  values[4] = Int64GetDatum(stat.blocks_written);
1555  values[5] = Int64GetDatum(stat.blocks_exists);
1556  values[6] = Int64GetDatum(stat.flush);
1557  values[7] = Int64GetDatum(stat.truncate);
1558  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1559 
1560  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
1561  }
1562 
1563  return (Datum) 0;
1564 }
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat_slru.c:105
const char * pgstat_get_slru_name(int slru_idx)
Definition: pgstat_slru.c:118
#define PG_STAT_GET_SLRU_COLS
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
const char * name
#define stat
Definition: win32_port.h:284

References cstring_to_text(), i, InitMaterializedSRF(), Int64GetDatum(), name, PG_STAT_GET_SLRU_COLS, pgstat_fetch_slru(), pgstat_get_slru_name(), PointerGetDatum(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, stat, TimestampTzGetDatum(), tuplestore_putvalues(), and values.

◆ pg_stat_get_snapshot_timestamp()

Datum pg_stat_get_snapshot_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1697 of file pgstatfuncs.c.

1698 {
1699  bool have_snapshot;
1700  TimestampTz ts;
1701 
1702  ts = pgstat_get_stat_snapshot_timestamp(&have_snapshot);
1703 
1704  if (!have_snapshot)
1705  PG_RETURN_NULL();
1706 
1708 }
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:908

References PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_get_stat_snapshot_timestamp().

◆ pg_stat_get_subscription_stats()

Datum pg_stat_get_subscription_stats ( PG_FUNCTION_ARGS  )

Definition at line 1998 of file pgstatfuncs.c.

1999 {
2000 #define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 4
2001  Oid subid = PG_GETARG_OID(0);
2002  TupleDesc tupdesc;
2004  bool nulls[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0};
2005  PgStat_StatSubEntry *subentry;
2006  PgStat_StatSubEntry allzero;
2007 
2008  /* Get subscription stats */
2009  subentry = pgstat_fetch_stat_subscription(subid);
2010 
2011  /* Initialise attributes information in the tuple descriptor */
2013  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subid",
2014  OIDOID, -1, 0);
2015  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "apply_error_count",
2016  INT8OID, -1, 0);
2017  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "sync_error_count",
2018  INT8OID, -1, 0);
2019  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "stats_reset",
2020  TIMESTAMPTZOID, -1, 0);
2021  BlessTupleDesc(tupdesc);
2022 
2023  if (!subentry)
2024  {
2025  /* If the subscription is not found, initialise its stats */
2026  memset(&allzero, 0, sizeof(PgStat_StatSubEntry));
2027  subentry = &allzero;
2028  }
2029 
2030  /* subid */
2031  values[0] = ObjectIdGetDatum(subid);
2032 
2033  /* apply_error_count */
2034  values[1] = Int64GetDatum(subentry->apply_error_count);
2035 
2036  /* sync_error_count */
2037  values[2] = Int64GetDatum(subentry->sync_error_count);
2038 
2039  /* stats_reset */
2040  if (subentry->stat_reset_timestamp == 0)
2041  nulls[3] = true;
2042  else
2044 
2045  /* Returns the record as Datum */
2047 }
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS
PgStat_Counter apply_error_count
Definition: pgstat.h:392
PgStat_Counter sync_error_count
Definition: pgstat.h:393
TimestampTz stat_reset_timestamp
Definition: pgstat.h:394

References PgStat_StatSubEntry::apply_error_count, BlessTupleDesc(), CreateTemplateTupleDesc(), heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_DATUM, PG_STAT_GET_SUBSCRIPTION_STATS_COLS, pgstat_fetch_stat_subscription(), PgStat_StatSubEntry::stat_reset_timestamp, PgStat_StatSubEntry::sync_error_count, TimestampTzGetDatum(), TupleDescInitEntry(), and values.

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

Definition at line 1459 of file pgstatfuncs.c.

1460 {
1461 #define PG_STAT_GET_WAL_COLS 9
1462  TupleDesc tupdesc;
1464  bool nulls[PG_STAT_GET_WAL_COLS] = {0};
1465  char buf[256];
1466  PgStat_WalStats *wal_stats;
1467 
1468  /* Initialise attributes information in the tuple descriptor */
1470  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1471  INT8OID, -1, 0);
1472  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1473  INT8OID, -1, 0);
1474  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1475  NUMERICOID, -1, 0);
1476  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1477  INT8OID, -1, 0);
1478  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1479  INT8OID, -1, 0);
1480  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1481  INT8OID, -1, 0);
1482  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1483  FLOAT8OID, -1, 0);
1484  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1485  FLOAT8OID, -1, 0);
1486  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1487  TIMESTAMPTZOID, -1, 0);
1488 
1489  BlessTupleDesc(tupdesc);
1490 
1491  /* Get statistics about WAL activity */
1492  wal_stats = pgstat_fetch_stat_wal();
1493 
1494  /* Fill values and NULLs */
1495  values[0] = Int64GetDatum(wal_stats->wal_records);
1496  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1497 
1498  /* Convert to numeric. */
1499  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1502  ObjectIdGetDatum(0),
1503  Int32GetDatum(-1));
1504 
1505  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1506  values[4] = Int64GetDatum(wal_stats->wal_write);
1507  values[5] = Int64GetDatum(wal_stats->wal_sync);
1508 
1509  /* Convert counters from microsec to millisec for display */
1510  values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1511  values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1512 
1514 
1515  /* Returns the record as Datum */
1517 }
#define UINT64_FORMAT
Definition: c.h:538
static char * buf
Definition: pg_test_fsync.c:67
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:67
#define PG_STAT_GET_WAL_COLS
#define snprintf
Definition: port.h:238
PgStat_Counter wal_write
Definition: pgstat.h:435
PgStat_Counter wal_buffers_full
Definition: pgstat.h:434
PgStat_Counter wal_write_time
Definition: pgstat.h:437
TimestampTz stat_reset_timestamp
Definition: pgstat.h:439
uint64 wal_bytes
Definition: pgstat.h:433
PgStat_Counter wal_sync_time
Definition: pgstat.h:438
PgStat_Counter wal_fpi
Definition: pgstat.h:432
PgStat_Counter wal_sync
Definition: pgstat.h:436
PgStat_Counter wal_records
Definition: pgstat.h:431

References BlessTupleDesc(), buf, CreateTemplateTupleDesc(), CStringGetDatum(), DirectFunctionCall3, Float8GetDatum(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), numeric_in(), ObjectIdGetDatum(), PG_RETURN_DATUM, PG_STAT_GET_WAL_COLS, pgstat_fetch_stat_wal(), snprintf, PgStat_WalStats::stat_reset_timestamp, TimestampTzGetDatum(), TupleDescInitEntry(), UINT64_FORMAT, values, PgStat_WalStats::wal_buffers_full, PgStat_WalStats::wal_bytes, PgStat_WalStats::wal_fpi, PgStat_WalStats::wal_records, PgStat_WalStats::wal_sync, PgStat_WalStats::wal_sync_time, PgStat_WalStats::wal_write, and PgStat_WalStats::wal_write_time.

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 1667 of file pgstatfuncs.c.

1668 {
1669  Oid funcid = PG_GETARG_OID(0);
1670  PgStat_FunctionCounts *funcentry;
1671 
1672  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1673  PG_RETURN_NULL();
1674  PG_RETURN_INT64(funcentry->numcalls);
1675 }
PgStat_Counter numcalls
Definition: pgstat.h:109

References find_funcstat_entry(), PgStat_FunctionCounts::numcalls, PG_GETARG_OID, PG_RETURN_INT64, and PG_RETURN_NULL.

◆ pg_stat_get_xact_tuples_deleted()

Datum pg_stat_get_xact_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 1646 of file pgstatfuncs.c.

1647 {
1648  Oid relid = PG_GETARG_OID(0);
1649  int64 result;
1650  PgStat_TableStatus *tabentry;
1652 
1653  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1654  result = 0;
1655  else
1656  {
1657  result = tabentry->counts.tuples_deleted;
1658  /* live subtransactions' counts aren't in tuples_deleted yet */
1659  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1660  result += trans->tuples_deleted;
1661  }
1662 
1663  PG_RETURN_INT64(result);
1664 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
PgStat_TableCounts counts
Definition: pgstat.h:202
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:201
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402

References PgStat_TableStatus::counts, find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::trans, trans, and PgStat_TableCounts::tuples_deleted.

◆ pg_stat_get_xact_tuples_inserted()

Datum pg_stat_get_xact_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1604 of file pgstatfuncs.c.

1605 {
1606  Oid relid = PG_GETARG_OID(0);
1607  int64 result;
1608  PgStat_TableStatus *tabentry;
1610 
1611  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1612  result = 0;
1613  else
1614  {
1615  result = tabentry->counts.tuples_inserted;
1616  /* live subtransactions' counts aren't in tuples_inserted yet */
1617  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1618  result += trans->tuples_inserted;
1619  }
1620 
1621  PG_RETURN_INT64(result);
1622 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:167

References PgStat_TableStatus::counts, find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::trans, trans, and PgStat_TableCounts::tuples_inserted.

◆ pg_stat_get_xact_tuples_updated()

Datum pg_stat_get_xact_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 1625 of file pgstatfuncs.c.

1626 {
1627  Oid relid = PG_GETARG_OID(0);
1628  int64 result;
1629  PgStat_TableStatus *tabentry;
1631 
1632  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1633  result = 0;
1634  else
1635  {
1636  result = tabentry->counts.tuples_updated;
1637  /* live subtransactions' counts aren't in tuples_updated yet */
1638  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1639  result += trans->tuples_updated;
1640  }
1641 
1642  PG_RETURN_INT64(result);
1643 }
PgStat_Counter tuples_updated
Definition: pgstat.h:168

References PgStat_TableStatus::counts, find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::trans, trans, and PgStat_TableCounts::tuples_updated.

◆ pg_stat_have_stats()

Datum pg_stat_have_stats ( PG_FUNCTION_ARGS  )

Definition at line 2057 of file pgstatfuncs.c.

2058 {
2059  char *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0));
2060  Oid dboid = PG_GETARG_OID(1);
2061  Oid objoid = PG_GETARG_OID(2);
2062  PgStat_Kind kind = pgstat_get_kind_from_str(stats_type);
2063 
2064  PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objoid));
2065 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:925
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1242
PgStat_Kind
Definition: pgstat.h:36

References PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_BOOL, pgstat_get_kind_from_str(), pgstat_have_entry(), and text_to_cstring().

◆ pg_stat_reset()

Datum pg_stat_reset ( PG_FUNCTION_ARGS  )

Definition at line 1732 of file pgstatfuncs.c.

1733 {
1735 
1736  PG_RETURN_VOID();
1737 }
void pgstat_reset_counters(void)
Definition: pgstat.c:718

References PG_RETURN_VOID, and pgstat_reset_counters().

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 1820 of file pgstatfuncs.c.

1821 {
1822  char *target = NULL;
1823 
1824  if (PG_ARGISNULL(0))
1826  else
1827  {
1828  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1829  pgstat_reset_replslot(target);
1830  }
1831 
1832  PG_RETURN_VOID();
1833 }
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:759
@ PGSTAT_KIND_REPLSLOT
Definition: pgstat.h:44
void pgstat_reset_replslot(const char *name)

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_REPLSLOT, pgstat_reset_of_kind(), pgstat_reset_replslot(), and text_to_cstring().

◆ pg_stat_reset_shared()

Datum pg_stat_reset_shared ( PG_FUNCTION_ARGS  )

Definition at line 1746 of file pgstatfuncs.c.

1747 {
1748  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1749 
1750  if (strcmp(target, "archiver") == 0)
1752  else if (strcmp(target, "bgwriter") == 0)
1753  {
1754  /*
1755  * Historically checkpointer was part of bgwriter, continue to reset
1756  * both for now.
1757  */
1760  }
1761  else if (strcmp(target, "io") == 0)
1763  else if (strcmp(target, "recovery_prefetch") == 0)
1765  else if (strcmp(target, "wal") == 0)
1767  else
1768  ereport(ERROR,
1769  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1770  errmsg("unrecognized reset target: \"%s\"", target),
1771  errhint("Target must be \"archiver\", \"bgwriter\", \"io\", \"recovery_prefetch\", or \"wal\".")));
1772 
1773  PG_RETURN_VOID();
1774 }
int errhint(const char *fmt,...)
Definition: elog.c:1316
@ PGSTAT_KIND_ARCHIVER
Definition: pgstat.h:48
@ PGSTAT_KIND_IO
Definition: pgstat.h:51
@ PGSTAT_KIND_BGWRITER
Definition: pgstat.h:49
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
@ PGSTAT_KIND_WAL
Definition: pgstat.h:53
void XLogPrefetchResetStats(void)

References ereport, errcode(), errhint(), errmsg(), ERROR, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_ARCHIVER, PGSTAT_KIND_BGWRITER, PGSTAT_KIND_CHECKPOINTER, PGSTAT_KIND_IO, PGSTAT_KIND_WAL, pgstat_reset_of_kind(), text_to_cstring(), and XLogPrefetchResetStats().

◆ pg_stat_reset_single_function_counters()

Datum pg_stat_reset_single_function_counters ( PG_FUNCTION_ARGS  )

Definition at line 1792 of file pgstatfuncs.c.

1793 {
1794  Oid funcoid = PG_GETARG_OID(0);
1795 
1797 
1798  PG_RETURN_VOID();
1799 }
Oid MyDatabaseId
Definition: globals.c:89
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:737
@ PGSTAT_KIND_FUNCTION
Definition: pgstat.h:43

References MyDatabaseId, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_FUNCTION, and pgstat_reset().

◆ pg_stat_reset_single_table_counters()

Datum pg_stat_reset_single_table_counters ( PG_FUNCTION_ARGS  )

Definition at line 1781 of file pgstatfuncs.c.

1782 {
1783  Oid taboid = PG_GETARG_OID(0);
1784  Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
1785 
1786  pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
1787 
1788  PG_RETURN_VOID();
1789 }
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:245
@ PGSTAT_KIND_RELATION
Definition: pgstat.h:42

References InvalidOid, IsSharedRelation(), MyDatabaseId, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_RELATION, and pgstat_reset().

◆ pg_stat_reset_slru()

Datum pg_stat_reset_slru ( PG_FUNCTION_ARGS  )

Definition at line 1803 of file pgstatfuncs.c.

1804 {
1805  char *target = NULL;
1806 
1807  if (PG_ARGISNULL(0))
1809  else
1810  {
1811  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1812  pgstat_reset_slru(target);
1813  }
1814 
1815  PG_RETURN_VOID();
1816 }
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:52
void pgstat_reset_slru(const char *name)
Definition: pgstat_slru.c:45

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_SLRU, pgstat_reset_of_kind(), pgstat_reset_slru(), and text_to_cstring().

◆ pg_stat_reset_subscription_stats()

Datum pg_stat_reset_subscription_stats ( PG_FUNCTION_ARGS  )

Definition at line 1837 of file pgstatfuncs.c.

1838 {
1839  Oid subid;
1840 
1841  if (PG_ARGISNULL(0))
1842  {
1843  /* Clear all subscription stats */
1845  }
1846  else
1847  {
1848  subid = PG_GETARG_OID(0);
1849 
1850  if (!OidIsValid(subid))
1851  ereport(ERROR,
1852  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1853  errmsg("invalid subscription OID %u", subid)));
1855  }
1856 
1857  PG_RETURN_VOID();
1858 }
#define OidIsValid(objectId)
Definition: c.h:764
@ PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat.h:45

References ereport, errcode(), errmsg(), ERROR, InvalidOid, OidIsValid, PG_ARGISNULL, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_SUBSCRIPTION, pgstat_reset(), and pgstat_reset_of_kind().

◆ pg_stat_us_to_ms()

static double pg_stat_us_to_ms ( PgStat_Counter  val_ms)
inlinestatic

Definition at line 1341 of file pgstatfuncs.c.

1342 {
1343  return val_ms * (double) 0.001;
1344 }

Referenced by pg_stat_get_io().

◆ pgstat_get_io_op_index()

static io_stat_col pgstat_get_io_op_index ( IOOp  io_op)
static

Definition at line 1287 of file pgstatfuncs.c.

1288 {
1289  switch (io_op)
1290  {
1291  case IOOP_EVICT:
1292  return IO_COL_EVICTIONS;
1293  case IOOP_EXTEND:
1294  return IO_COL_EXTENDS;
1295  case IOOP_FSYNC:
1296  return IO_COL_FSYNCS;
1297  case IOOP_HIT:
1298  return IO_COL_HITS;
1299  case IOOP_READ:
1300  return IO_COL_READS;
1301  case IOOP_REUSE:
1302  return IO_COL_REUSES;
1303  case IOOP_WRITE:
1304  return IO_COL_WRITES;
1305  case IOOP_WRITEBACK:
1306  return IO_COL_WRITEBACKS;
1307  }
1308 
1309  elog(ERROR, "unrecognized IOOp value: %d", io_op);
1310  pg_unreachable();
1311 }
#define pg_unreachable()
Definition: c.h:285
@ IOOP_EXTEND
Definition: pgstat.h:297
@ IOOP_FSYNC
Definition: pgstat.h:298
@ IOOP_READ
Definition: pgstat.h:300
@ IOOP_WRITEBACK
Definition: pgstat.h:303
@ IOOP_HIT
Definition: pgstat.h:299
@ IOOP_EVICT
Definition: pgstat.h:296
@ IOOP_REUSE
Definition: pgstat.h:301
@ IOOP_WRITE
Definition: pgstat.h:302

References elog(), ERROR, IO_COL_EVICTIONS, IO_COL_EXTENDS, IO_COL_FSYNCS, IO_COL_HITS, IO_COL_READS, IO_COL_REUSES, IO_COL_WRITEBACKS, IO_COL_WRITES, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, and pg_unreachable.

Referenced by pg_stat_get_io(), and pgstat_get_io_time_index().

◆ pgstat_get_io_time_index()

static io_stat_col pgstat_get_io_time_index ( IOOp  io_op)
static

Definition at line 1320 of file pgstatfuncs.c.

1321 {
1322  switch (io_op)
1323  {
1324  case IOOP_READ:
1325  case IOOP_WRITE:
1326  case IOOP_WRITEBACK:
1327  case IOOP_EXTEND:
1328  case IOOP_FSYNC:
1329  return pgstat_get_io_op_index(io_op) + 1;
1330  case IOOP_EVICT:
1331  case IOOP_HIT:
1332  case IOOP_REUSE:
1333  return IO_COL_INVALID;
1334  }
1335 
1336  elog(ERROR, "unrecognized IOOp value: %d", io_op);
1337  pg_unreachable();
1338 }

References elog(), ERROR, IO_COL_INVALID, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, pg_unreachable, and pgstat_get_io_op_index().

Referenced by pg_stat_get_io().