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/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 "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_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3
 
#define PG_STAT_GET_ACTIVITY_COLS   30
 
#define PG_STAT_GET_WAL_COLS   9
 
#define PG_STAT_GET_SLRU_COLS   9
 
#define PG_STAT_GET_REPLICATION_SLOT_COLS   10
 
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS   4
 

Functions

Datum pg_stat_get_numscans (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_lastscan (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_returned (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_fetched (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_inserted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_updated (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_deleted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_tuples_hot_updated (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_live_tuples (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_dead_tuples (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_mod_since_analyze (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_ins_since_vacuum (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_blocks_fetched (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_blocks_hit (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_last_vacuum_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_last_autovacuum_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_last_analyze_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_last_autoanalyze_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_vacuum_count (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_autovacuum_count (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_analyze_count (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_autoanalyze_count (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_function_calls (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_function_total_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_function_self_time (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_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_xact_commit (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_xact_rollback (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_blocks_fetched (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_blocks_hit (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_tuples_returned (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_tuples_fetched (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_tuples_inserted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_tuples_updated (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_tuples_deleted (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_temp_files (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_temp_bytes (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_tablespace (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_lock (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_bufferpin (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_startup_deadlock (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_all (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_deadlocks (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_db_blk_read_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_blk_write_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_session_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_active_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_idle_in_transaction_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_sessions (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_sessions_abandoned (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_sessions_fatal (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_sessions_killed (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)
 
Datum pg_stat_get_wal (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_slru (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_numscans (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_tuples_returned (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_tuples_fetched (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_tuples_hot_updated (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_blocks_fetched (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_blocks_hit (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_function_calls (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_function_total_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_function_self_time (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 37 of file pgstatfuncs.c.

◆ PG_STAT_GET_ACTIVITY_COLS

#define PG_STAT_GET_ACTIVITY_COLS   30

◆ PG_STAT_GET_PROGRESS_COLS

#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3

◆ 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_WAL_COLS

#define PG_STAT_GET_WAL_COLS   9

◆ UINT32_ACCESS_ONCE

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

Definition at line 35 of file pgstatfuncs.c.

Function Documentation

◆ pg_backend_pid()

Datum pg_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 903 of file pgstatfuncs.c.

904 {
906 }
#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 2049 of file pgstatfuncs.c.

2050 {
2052 
2053  PG_RETURN_VOID();
2054 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:752

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 2059 of file pgstatfuncs.c.

2060 {
2062 
2063  PG_RETURN_VOID();
2064 }
void pgstat_force_next_flush(void)
Definition: pgstat.c:665

References PG_RETURN_VOID, and pgstat_force_next_flush().

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 560 of file pgstatfuncs.c.

561 {
562 #define PG_STAT_GET_ACTIVITY_COLS 30
563  int num_backends = pgstat_fetch_stat_numbackends();
564  int curr_backend;
565  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
566  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
567 
568  InitMaterializedSRF(fcinfo, 0);
569 
570  /* 1-based index */
571  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
572  {
573  /* for each row */
575  bool nulls[PG_STAT_GET_ACTIVITY_COLS] = {0};
576  LocalPgBackendStatus *local_beentry;
577  PgBackendStatus *beentry;
578  PGPROC *proc;
579  const char *wait_event_type = NULL;
580  const char *wait_event = NULL;
581 
582  /* Get the next one in the list */
583  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
584  beentry = &local_beentry->backendStatus;
585 
586  /* If looking for specific PID, ignore all the others */
587  if (pid != -1 && beentry->st_procpid != pid)
588  continue;
589 
590  /* Values available to all callers */
591  if (beentry->st_databaseid != InvalidOid)
592  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
593  else
594  nulls[0] = true;
595 
596  values[1] = Int32GetDatum(beentry->st_procpid);
597 
598  if (beentry->st_userid != InvalidOid)
599  values[2] = ObjectIdGetDatum(beentry->st_userid);
600  else
601  nulls[2] = true;
602 
603  if (beentry->st_appname)
604  values[3] = CStringGetTextDatum(beentry->st_appname);
605  else
606  nulls[3] = true;
607 
608  if (TransactionIdIsValid(local_beentry->backend_xid))
609  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
610  else
611  nulls[15] = true;
612 
613  if (TransactionIdIsValid(local_beentry->backend_xmin))
614  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
615  else
616  nulls[16] = true;
617 
618  /* Values only available to role member or pg_read_all_stats */
619  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
620  {
621  SockAddr zero_clientaddr;
622  char *clipped_activity;
623 
624  switch (beentry->st_state)
625  {
626  case STATE_IDLE:
627  values[4] = CStringGetTextDatum("idle");
628  break;
629  case STATE_RUNNING:
630  values[4] = CStringGetTextDatum("active");
631  break;
633  values[4] = CStringGetTextDatum("idle in transaction");
634  break;
635  case STATE_FASTPATH:
636  values[4] = CStringGetTextDatum("fastpath function call");
637  break;
639  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
640  break;
641  case STATE_DISABLED:
642  values[4] = CStringGetTextDatum("disabled");
643  break;
644  case STATE_UNDEFINED:
645  nulls[4] = true;
646  break;
647  }
648 
649  clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
650  values[5] = CStringGetTextDatum(clipped_activity);
651  pfree(clipped_activity);
652 
653  /* leader_pid */
654  nulls[28] = true;
655 
656  proc = BackendPidGetProc(beentry->st_procpid);
657 
658  if (proc == NULL && (beentry->st_backendType != B_BACKEND))
659  {
660  /*
661  * For an auxiliary process, retrieve process info from
662  * AuxiliaryProcs stored in shared-memory.
663  */
664  proc = AuxiliaryPidGetProc(beentry->st_procpid);
665  }
666 
667  /*
668  * If a PGPROC entry was retrieved, display wait events and lock
669  * group leader information if any. To avoid extra overhead, no
670  * extra lock is being held, so there is no guarantee of
671  * consistency across multiple rows.
672  */
673  if (proc != NULL)
674  {
675  uint32 raw_wait_event;
676  PGPROC *leader;
677 
678  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
679  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
680  wait_event = pgstat_get_wait_event(raw_wait_event);
681 
682  leader = proc->lockGroupLeader;
683 
684  /*
685  * Show the leader only for active parallel workers. This
686  * leaves the field as NULL for the leader of a parallel
687  * group.
688  */
689  if (leader && leader->pid != beentry->st_procpid)
690  {
691  values[28] = Int32GetDatum(leader->pid);
692  nulls[28] = false;
693  }
694  }
695 
696  if (wait_event_type)
697  values[6] = CStringGetTextDatum(wait_event_type);
698  else
699  nulls[6] = true;
700 
701  if (wait_event)
702  values[7] = CStringGetTextDatum(wait_event);
703  else
704  nulls[7] = true;
705 
706  /*
707  * Don't expose transaction time for walsenders; it confuses
708  * monitoring, particularly because we don't keep the time up-to-
709  * date.
710  */
711  if (beentry->st_xact_start_timestamp != 0 &&
712  beentry->st_backendType != B_WAL_SENDER)
714  else
715  nulls[8] = true;
716 
717  if (beentry->st_activity_start_timestamp != 0)
719  else
720  nulls[9] = true;
721 
722  if (beentry->st_proc_start_timestamp != 0)
724  else
725  nulls[10] = true;
726 
727  if (beentry->st_state_start_timestamp != 0)
729  else
730  nulls[11] = true;
731 
732  /* A zeroed client addr means we don't know */
733  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
734  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
735  sizeof(zero_clientaddr)) == 0)
736  {
737  nulls[12] = true;
738  nulls[13] = true;
739  nulls[14] = true;
740  }
741  else
742  {
743  if (beentry->st_clientaddr.addr.ss_family == AF_INET ||
744  beentry->st_clientaddr.addr.ss_family == AF_INET6)
745  {
746  char remote_host[NI_MAXHOST];
747  char remote_port[NI_MAXSERV];
748  int ret;
749 
750  remote_host[0] = '\0';
751  remote_port[0] = '\0';
752  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
753  beentry->st_clientaddr.salen,
754  remote_host, sizeof(remote_host),
755  remote_port, sizeof(remote_port),
756  NI_NUMERICHOST | NI_NUMERICSERV);
757  if (ret == 0)
758  {
759  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
761  CStringGetDatum(remote_host));
762  if (beentry->st_clienthostname &&
763  beentry->st_clienthostname[0])
765  else
766  nulls[13] = true;
767  values[14] = Int32GetDatum(atoi(remote_port));
768  }
769  else
770  {
771  nulls[12] = true;
772  nulls[13] = true;
773  nulls[14] = true;
774  }
775  }
776  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
777  {
778  /*
779  * Unix sockets always reports NULL for host and -1 for
780  * port, so it's possible to tell the difference to
781  * connections we have no permissions to view, or with
782  * errors.
783  */
784  nulls[12] = true;
785  nulls[13] = true;
786  values[14] = Int32GetDatum(-1);
787  }
788  else
789  {
790  /* Unknown address type, should never happen */
791  nulls[12] = true;
792  nulls[13] = true;
793  nulls[14] = true;
794  }
795  }
796  /* Add backend type */
797  if (beentry->st_backendType == B_BG_WORKER)
798  {
799  const char *bgw_type;
800 
801  bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
802  if (bgw_type)
803  values[17] = CStringGetTextDatum(bgw_type);
804  else
805  nulls[17] = true;
806  }
807  else
808  values[17] =
810 
811  /* SSL information */
812  if (beentry->st_ssl)
813  {
814  values[18] = BoolGetDatum(true); /* ssl */
817  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
818 
819  if (beentry->st_sslstatus->ssl_client_dn[0])
821  else
822  nulls[22] = true;
823 
824  if (beentry->st_sslstatus->ssl_client_serial[0])
828  Int32GetDatum(-1));
829  else
830  nulls[23] = true;
831 
832  if (beentry->st_sslstatus->ssl_issuer_dn[0])
834  else
835  nulls[24] = true;
836  }
837  else
838  {
839  values[18] = BoolGetDatum(false); /* ssl */
840  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = true;
841  }
842 
843  /* GSSAPI information */
844  if (beentry->st_gss)
845  {
846  values[25] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
848  values[27] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
849  }
850  else
851  {
852  values[25] = BoolGetDatum(false); /* gss_auth */
853  nulls[26] = true; /* No GSS principal */
854  values[27] = BoolGetDatum(false); /* GSS Encryption not in
855  * use */
856  }
857  if (beentry->st_query_id == 0)
858  nulls[29] = true;
859  else
860  values[29] = UInt64GetDatum(beentry->st_query_id);
861  }
862  else
863  {
864  /* No permissions to view data about this session */
865  values[5] = CStringGetTextDatum("<insufficient privilege>");
866  nulls[4] = true;
867  nulls[6] = true;
868  nulls[7] = true;
869  nulls[8] = true;
870  nulls[9] = true;
871  nulls[10] = true;
872  nulls[11] = true;
873  nulls[12] = true;
874  nulls[13] = true;
875  nulls[14] = true;
876  nulls[17] = true;
877  nulls[18] = true;
878  nulls[19] = true;
879  nulls[20] = true;
880  nulls[21] = true;
881  nulls[22] = true;
882  nulls[23] = true;
883  nulls[24] = true;
884  nulls[25] = true;
885  nulls[26] = true;
886  nulls[27] = true;
887  nulls[28] = true;
888  nulls[29] = true;
889  }
890 
891  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
892 
893  /* If only a single backend was requested, and we found it, break. */
894  if (pid != -1)
895  break;
896  }
897 
898  return (Datum) 0;
899 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:617
int pgstat_fetch_stat_numbackends(void)
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
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:1282
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
unsigned int uint32
Definition: c.h:442
#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
void pfree(void *pointer)
Definition: mcxt.c:1306
@ B_WAL_SENDER
Definition: miscadmin.h:330
@ B_BG_WORKER
Definition: miscadmin.h:323
@ B_BACKEND
Definition: miscadmin.h:322
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:251
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:35
#define PG_STAT_GET_ACTIVITY_COLS
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:620
uintptr_t Datum
Definition: postgres.h:412
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:784
static Datum BoolGetDatum(bool X)
Definition: postgres.h:450
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:698
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
#define InvalidOid
Definition: postgres_ext.h:36
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3151
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:968
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:332
Tuplestorestate * setResult
Definition: execnodes.h:331
struct sockaddr_storage addr
Definition: pqcomm.h:26
socklen_t salen
Definition: pqcomm.h:27
#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:129
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:74

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(), PgBackendGSSStatus::gss_auth, PgBackendGSSStatus::gss_enc, PgBackendGSSStatus::gss_princ, HAS_PGSTAT_PERMISSIONS, inet_in(), InitMaterializedSRF(), Int32GetDatum(), InvalidOid, 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_local_beentry(), pgstat_fetch_stat_numbackends(), 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_analyze_count()

Datum pg_stat_get_analyze_count ( PG_FUNCTION_ARGS  )

Definition at line 368 of file pgstatfuncs.c.

369 {
370  Oid relid = PG_GETARG_OID(0);
371  int64 result;
372  PgStat_StatTabEntry *tabentry;
373 
374  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
375  result = 0;
376  else
377  result = (int64) (tabentry->analyze_count);
378 
379  PG_RETURN_INT64(result);
380 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter analyze_count
Definition: pgstat.h:380

References PgStat_StatTabEntry::analyze_count, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

Definition at line 2187 of file pgstatfuncs.c.

2188 {
2189  TupleDesc tupdesc;
2190  Datum values[7] = {0};
2191  bool nulls[7] = {0};
2192  PgStat_ArchiverStats *archiver_stats;
2193 
2194  /* Initialise attributes information in the tuple descriptor */
2195  tupdesc = CreateTemplateTupleDesc(7);
2196  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2197  INT8OID, -1, 0);
2198  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2199  TEXTOID, -1, 0);
2200  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2201  TIMESTAMPTZOID, -1, 0);
2202  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2203  INT8OID, -1, 0);
2204  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2205  TEXTOID, -1, 0);
2206  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2207  TIMESTAMPTZOID, -1, 0);
2208  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2209  TIMESTAMPTZOID, -1, 0);
2210 
2211  BlessTupleDesc(tupdesc);
2212 
2213  /* Get statistics about the archiver process */
2214  archiver_stats = pgstat_fetch_stat_archiver();
2215 
2216  /* Fill values and NULLs */
2217  values[0] = Int64GetDatum(archiver_stats->archived_count);
2218  if (*(archiver_stats->last_archived_wal) == '\0')
2219  nulls[1] = true;
2220  else
2221  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2222 
2223  if (archiver_stats->last_archived_timestamp == 0)
2224  nulls[2] = true;
2225  else
2226  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
2227 
2228  values[3] = Int64GetDatum(archiver_stats->failed_count);
2229  if (*(archiver_stats->last_failed_wal) == '\0')
2230  nulls[4] = true;
2231  else
2232  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2233 
2234  if (archiver_stats->last_failed_timestamp == 0)
2235  nulls[5] = true;
2236  else
2237  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2238 
2239  if (archiver_stats->stat_reset_timestamp == 0)
2240  nulls[6] = true;
2241  else
2242  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2243 
2244  /* Returns the record as Datum */
2246 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#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:1020
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
TimestampTz last_failed_timestamp
Definition: pgstat.h:256
TimestampTz stat_reset_timestamp
Definition: pgstat.h:257
TimestampTz last_archived_timestamp
Definition: pgstat.h:252
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:254
PgStat_Counter failed_count
Definition: pgstat.h:253
PgStat_Counter archived_count
Definition: pgstat.h:249
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:250
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_autoanalyze_count()

Datum pg_stat_get_autoanalyze_count ( PG_FUNCTION_ARGS  )

Definition at line 383 of file pgstatfuncs.c.

384 {
385  Oid relid = PG_GETARG_OID(0);
386  int64 result;
387  PgStat_StatTabEntry *tabentry;
388 
389  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
390  result = 0;
391  else
392  result = (int64) (tabentry->autovac_analyze_count);
393 
394  PG_RETURN_INT64(result);
395 }
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:382

References PgStat_StatTabEntry::autovac_analyze_count, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_autovacuum_count()

Datum pg_stat_get_autovacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 353 of file pgstatfuncs.c.

354 {
355  Oid relid = PG_GETARG_OID(0);
356  int64 result;
357  PgStat_StatTabEntry *tabentry;
358 
359  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
360  result = 0;
361  else
362  result = (int64) (tabentry->autovac_vacuum_count);
363 
364  PG_RETURN_INT64(result);
365 }
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:378

References PgStat_StatTabEntry::autovac_vacuum_count, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

Definition at line 949 of file pgstatfuncs.c.

950 {
951  int32 beid = PG_GETARG_INT32(0);
952  PgBackendStatus *beentry;
953  const char *activity;
954  char *clipped_activity;
955  text *ret;
956 
957  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
958  activity = "<backend information not available>";
959  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
960  activity = "<insufficient privilege>";
961  else if (*(beentry->st_activity_raw) == '\0')
962  activity = "<command string not enabled>";
963  else
964  activity = beentry->st_activity_raw;
965 
966  clipped_activity = pgstat_clip_activity(activity);
967  ret = cstring_to_text(activity);
968  pfree(clipped_activity);
969 
970  PG_RETURN_TEXT_P(ret);
971 }
PgBackendStatus * pgstat_fetch_stat_beentry(BackendId beid)
signed int int32
Definition: c.h:430
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:623
text * cstring_to_text(const char *s)
Definition: varlena.c:189

References cstring_to_text(), HAS_PGSTAT_PERMISSIONS, pfree(), PG_GETARG_INT32, PG_RETURN_TEXT_P, pgstat_clip_activity(), pgstat_fetch_stat_beentry(), 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 1017 of file pgstatfuncs.c.

1018 {
1019  int32 beid = PG_GETARG_INT32(0);
1020  TimestampTz result;
1021  PgBackendStatus *beentry;
1022 
1023  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1024  PG_RETURN_NULL();
1025 
1026  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1027  PG_RETURN_NULL();
1028 
1029  result = beentry->st_activity_start_timestamp;
1030 
1031  /*
1032  * No time recorded for start of current query -- this is the case if the
1033  * user hasn't enabled query-level stats collection.
1034  */
1035  if (result == 0)
1036  PG_RETURN_NULL();
1037 
1038  PG_RETURN_TIMESTAMPTZ(result);
1039 }
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_fetch_stat_beentry(), 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 1087 of file pgstatfuncs.c.

1088 {
1089  int32 beid = PG_GETARG_INT32(0);
1090  PgBackendStatus *beentry;
1091  SockAddr zero_clientaddr;
1092  char remote_host[NI_MAXHOST];
1093  int ret;
1094 
1095  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1096  PG_RETURN_NULL();
1097 
1098  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1099  PG_RETURN_NULL();
1100 
1101  /* A zeroed client addr means we don't know */
1102  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1103  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1104  sizeof(zero_clientaddr)) == 0)
1105  PG_RETURN_NULL();
1106 
1107  switch (beentry->st_clientaddr.addr.ss_family)
1108  {
1109  case AF_INET:
1110  case AF_INET6:
1111  break;
1112  default:
1113  PG_RETURN_NULL();
1114  }
1115 
1116  remote_host[0] = '\0';
1117  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1118  beentry->st_clientaddr.salen,
1119  remote_host, sizeof(remote_host),
1120  NULL, 0,
1121  NI_NUMERICHOST | NI_NUMERICSERV);
1122  if (ret != 0)
1123  PG_RETURN_NULL();
1124 
1125  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1126 
1128  CStringGetDatum(remote_host)));
1129 }

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_fetch_stat_beentry(), 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 1132 of file pgstatfuncs.c.

1133 {
1134  int32 beid = PG_GETARG_INT32(0);
1135  PgBackendStatus *beentry;
1136  SockAddr zero_clientaddr;
1137  char remote_port[NI_MAXSERV];
1138  int ret;
1139 
1140  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1141  PG_RETURN_NULL();
1142 
1143  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1144  PG_RETURN_NULL();
1145 
1146  /* A zeroed client addr means we don't know */
1147  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1148  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1149  sizeof(zero_clientaddr)) == 0)
1150  PG_RETURN_NULL();
1151 
1152  switch (beentry->st_clientaddr.addr.ss_family)
1153  {
1154  case AF_INET:
1155  case AF_INET6:
1156  break;
1157  case AF_UNIX:
1158  PG_RETURN_INT32(-1);
1159  default:
1160  PG_RETURN_NULL();
1161  }
1162 
1163  remote_port[0] = '\0';
1164  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1165  beentry->st_clientaddr.salen,
1166  NULL, 0,
1167  remote_port, sizeof(remote_port),
1168  NI_NUMERICHOST | NI_NUMERICSERV);
1169  if (ret != 0)
1170  PG_RETURN_NULL();
1171 
1173  CStringGetDatum(remote_port)));
1174 }
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:290

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_fetch_stat_beentry(), 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 923 of file pgstatfuncs.c.

924 {
925  int32 beid = PG_GETARG_INT32(0);
926  PgBackendStatus *beentry;
927 
928  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
929  PG_RETURN_NULL();
930 
931  PG_RETURN_OID(beentry->st_databaseid);
932 }
#define PG_RETURN_OID(x)
Definition: fmgr.h:360

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

◆ pg_stat_get_backend_idset()

Datum pg_stat_get_backend_idset ( PG_FUNCTION_ARGS  )

Definition at line 433 of file pgstatfuncs.c.

434 {
435  FuncCallContext *funcctx;
436  int *fctx;
437 
438  /* stuff done only on the first call of the function */
439  if (SRF_IS_FIRSTCALL())
440  {
441  /* create a function context for cross-call persistence */
442  funcctx = SRF_FIRSTCALL_INIT();
443 
445  sizeof(int));
446  funcctx->user_fctx = fctx;
447 
448  fctx[0] = 0;
449  }
450 
451  /* stuff done on every call of the function */
452  funcctx = SRF_PERCALL_SETUP();
453  fctx = funcctx->user_fctx;
454 
455  fctx[0] += 1;
456 
457  /*
458  * We recheck pgstat_fetch_stat_numbackends() each time through, just in
459  * case the local status data has been refreshed since we started. It's
460  * plenty cheap enough if not. If a refresh does happen, we'll likely
461  * miss or duplicate some backend IDs, but we're content not to crash.
462  * (Refreshing midway through such a query would be problematic usage
463  * anyway, since the backend IDs we've already returned might no longer
464  * refer to extant sessions.)
465  */
466  if (fctx[0] <= pgstat_fetch_stat_numbackends())
467  {
468  /* do when there is more left to send */
469  LocalPgBackendStatus *local_beentry = pgstat_fetch_stat_local_beentry(fctx[0]);
470 
471  SRF_RETURN_NEXT(funcctx, Int32GetDatum(local_beentry->backend_id));
472  }
473  else
474  {
475  /* do when there is no more left */
476  SRF_RETURN_DONE(funcctx);
477  }
478 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:303
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:307
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:305
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:327
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
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_local_beentry(), pgstat_fetch_stat_numbackends(), 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 910 of file pgstatfuncs.c.

911 {
912  int32 beid = PG_GETARG_INT32(0);
913  PgBackendStatus *beentry;
914 
915  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
916  PG_RETURN_NULL();
917 
918  PG_RETURN_INT32(beentry->st_procpid);
919 }

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

◆ pg_stat_get_backend_start()

Datum pg_stat_get_backend_start ( PG_FUNCTION_ARGS  )

Definition at line 1065 of file pgstatfuncs.c.

1066 {
1067  int32 beid = PG_GETARG_INT32(0);
1068  TimestampTz result;
1069  PgBackendStatus *beentry;
1070 
1071  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1072  PG_RETURN_NULL();
1073 
1074  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1075  PG_RETURN_NULL();
1076 
1077  result = beentry->st_proc_start_timestamp;
1078 
1079  if (result == 0) /* probably can't happen? */
1080  PG_RETURN_NULL();
1081 
1082  PG_RETURN_TIMESTAMPTZ(result);
1083 }

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

◆ pg_stat_get_backend_userid()

Datum pg_stat_get_backend_userid ( PG_FUNCTION_ARGS  )

Definition at line 936 of file pgstatfuncs.c.

937 {
938  int32 beid = PG_GETARG_INT32(0);
939  PgBackendStatus *beentry;
940 
941  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
942  PG_RETURN_NULL();
943 
944  PG_RETURN_OID(beentry->st_userid);
945 }

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

◆ pg_stat_get_backend_wait_event()

Datum pg_stat_get_backend_wait_event ( PG_FUNCTION_ARGS  )

Definition at line 995 of file pgstatfuncs.c.

996 {
997  int32 beid = PG_GETARG_INT32(0);
998  PgBackendStatus *beentry;
999  PGPROC *proc;
1000  const char *wait_event = NULL;
1001 
1002  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1003  wait_event = "<backend information not available>";
1004  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1005  wait_event = "<insufficient privilege>";
1006  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1007  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1008 
1009  if (!wait_event)
1010  PG_RETURN_NULL();
1011 
1012  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1013 }

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_fetch_stat_beentry(), 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 974 of file pgstatfuncs.c.

975 {
976  int32 beid = PG_GETARG_INT32(0);
977  PgBackendStatus *beentry;
978  PGPROC *proc;
979  const char *wait_event_type = NULL;
980 
981  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
982  wait_event_type = "<backend information not available>";
983  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
984  wait_event_type = "<insufficient privilege>";
985  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
986  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
987 
988  if (!wait_event_type)
989  PG_RETURN_NULL();
990 
991  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
992 }

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_fetch_stat_beentry(), 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 1043 of file pgstatfuncs.c.

1044 {
1045  int32 beid = PG_GETARG_INT32(0);
1046  TimestampTz result;
1047  PgBackendStatus *beentry;
1048 
1049  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1050  PG_RETURN_NULL();
1051 
1052  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1053  PG_RETURN_NULL();
1054 
1055  result = beentry->st_xact_start_timestamp;
1056 
1057  if (result == 0) /* not in a transaction */
1058  PG_RETURN_NULL();
1059 
1060  PG_RETURN_TIMESTAMPTZ(result);
1061 }

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_fetch_stat_beentry(), 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 1677 of file pgstatfuncs.c.

1678 {
1679  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_checkpoints);
1680 }
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 1683 of file pgstatfuncs.c.

1684 {
1685  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean);
1686 }
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 1689 of file pgstatfuncs.c.

1690 {
1691  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean);
1692 }

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 1671 of file pgstatfuncs.c.

1672 {
1673  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->requested_checkpoints);
1674 }

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 1711 of file pgstatfuncs.c.

1712 {
1713  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp);
1714 }

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 1665 of file pgstatfuncs.c.

1666 {
1667  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->timed_checkpoints);
1668 }

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_blocks_fetched()

Datum pg_stat_get_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 235 of file pgstatfuncs.c.

236 {
237  Oid relid = PG_GETARG_OID(0);
238  int64 result;
239  PgStat_StatTabEntry *tabentry;
240 
241  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
242  result = 0;
243  else
244  result = (int64) (tabentry->blocks_fetched);
245 
246  PG_RETURN_INT64(result);
247 }
PgStat_Counter blocks_fetched
Definition: pgstat.h:372

References PgStat_StatTabEntry::blocks_fetched, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_blocks_hit()

Datum pg_stat_get_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 251 of file pgstatfuncs.c.

252 {
253  Oid relid = PG_GETARG_OID(0);
254  int64 result;
255  PgStat_StatTabEntry *tabentry;
256 
257  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
258  result = 0;
259  else
260  result = (int64) (tabentry->blocks_hit);
261 
262  PG_RETURN_INT64(result);
263 }
PgStat_Counter blocks_hit
Definition: pgstat.h:373

References PgStat_StatTabEntry::blocks_hit, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1729 of file pgstatfuncs.c.

1730 {
1732 }

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 1723 of file pgstatfuncs.c.

1724 {
1725  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_fsync_backend);
1726 }

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 1717 of file pgstatfuncs.c.

1718 {
1719  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_backend);
1720 }

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 1703 of file pgstatfuncs.c.

1704 {
1705  /* time is already in msec, just convert to double for presentation */
1706  PG_RETURN_FLOAT8((double)
1707  pgstat_fetch_stat_checkpointer()->checkpoint_sync_time);
1708 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367

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 1695 of file pgstatfuncs.c.

1696 {
1697  /* time is already in msec, just convert to double for presentation */
1698  PG_RETURN_FLOAT8((double)
1699  pgstat_fetch_stat_checkpointer()->checkpoint_write_time);
1700 }

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_db_active_time()

Datum pg_stat_get_db_active_time ( PG_FUNCTION_ARGS  )

Definition at line 1585 of file pgstatfuncs.c.

1586 {
1587  Oid dbid = PG_GETARG_OID(0);
1588  double result = 0.0;
1589  PgStat_StatDBEntry *dbentry;
1590 
1591  /* convert counter from microsec to millisec for display */
1592  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1593  result = ((double) dbentry->total_active_time) / 1000.0;
1594 
1595  PG_RETURN_FLOAT8(result);
1596 }
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)
PgStat_Counter total_active_time
Definition: pgstat.h:305

References PG_GETARG_OID, PG_RETURN_FLOAT8, pgstat_fetch_stat_dbentry(), and PgStat_StatDBEntry::total_active_time.

◆ pg_stat_get_db_blk_read_time()

Datum pg_stat_get_db_blk_read_time ( PG_FUNCTION_ARGS  )

Definition at line 1539 of file pgstatfuncs.c.

1540 {
1541  Oid dbid = PG_GETARG_OID(0);
1542  double result;
1543  PgStat_StatDBEntry *dbentry;
1544 
1545  /* convert counter from microsec to millisec for display */
1546  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1547  result = 0;
1548  else
1549  result = ((double) dbentry->n_block_read_time) / 1000.0;
1550 
1551  PG_RETURN_FLOAT8(result);
1552 }
PgStat_Counter n_block_read_time
Definition: pgstat.h:301

References PgStat_StatDBEntry::n_block_read_time, PG_GETARG_OID, PG_RETURN_FLOAT8, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_blk_write_time()

Datum pg_stat_get_db_blk_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1555 of file pgstatfuncs.c.

1556 {
1557  Oid dbid = PG_GETARG_OID(0);
1558  double result;
1559  PgStat_StatDBEntry *dbentry;
1560 
1561  /* convert counter from microsec to millisec for display */
1562  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1563  result = 0;
1564  else
1565  result = ((double) dbentry->n_block_write_time) / 1000.0;
1566 
1567  PG_RETURN_FLOAT8(result);
1568 }
PgStat_Counter n_block_write_time
Definition: pgstat.h:302

References PgStat_StatDBEntry::n_block_write_time, PG_GETARG_OID, PG_RETURN_FLOAT8, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_blocks_fetched()

Datum pg_stat_get_db_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1231 of file pgstatfuncs.c.

1232 {
1233  Oid dbid = PG_GETARG_OID(0);
1234  int64 result;
1235  PgStat_StatDBEntry *dbentry;
1236 
1237  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1238  result = 0;
1239  else
1240  result = (int64) (dbentry->n_blocks_fetched);
1241 
1242  PG_RETURN_INT64(result);
1243 }
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:283

References PgStat_StatDBEntry::n_blocks_fetched, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_blocks_hit()

Datum pg_stat_get_db_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 1247 of file pgstatfuncs.c.

1248 {
1249  Oid dbid = PG_GETARG_OID(0);
1250  int64 result;
1251  PgStat_StatDBEntry *dbentry;
1252 
1253  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1254  result = 0;
1255  else
1256  result = (int64) (dbentry->n_blocks_hit);
1257 
1258  PG_RETURN_INT64(result);
1259 }
PgStat_Counter n_blocks_hit
Definition: pgstat.h:284

References PgStat_StatDBEntry::n_blocks_hit, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_checksum_failures()

Datum pg_stat_get_db_checksum_failures ( PG_FUNCTION_ARGS  )

Definition at line 1500 of file pgstatfuncs.c.

1501 {
1502  Oid dbid = PG_GETARG_OID(0);
1503  int64 result;
1504  PgStat_StatDBEntry *dbentry;
1505 
1506  if (!DataChecksumsEnabled())
1507  PG_RETURN_NULL();
1508 
1509  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1510  result = 0;
1511  else
1512  result = (int64) (dbentry->n_checksum_failures);
1513 
1514  PG_RETURN_INT64(result);
1515 }
PgStat_Counter n_checksum_failures
Definition: pgstat.h:299
bool DataChecksumsEnabled(void)
Definition: xlog.c:4201

References DataChecksumsEnabled(), PgStat_StatDBEntry::n_checksum_failures, 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 1518 of file pgstatfuncs.c.

1519 {
1520  Oid dbid = PG_GETARG_OID(0);
1521  TimestampTz result;
1522  PgStat_StatDBEntry *dbentry;
1523 
1524  if (!DataChecksumsEnabled())
1525  PG_RETURN_NULL();
1526 
1527  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1528  result = 0;
1529  else
1530  result = dbentry->last_checksum_failure;
1531 
1532  if (result == 0)
1533  PG_RETURN_NULL();
1534  else
1535  PG_RETURN_TIMESTAMPTZ(result);
1536 }
TimestampTz last_checksum_failure
Definition: pgstat.h:300

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 1466 of file pgstatfuncs.c.

1467 {
1468  Oid dbid = PG_GETARG_OID(0);
1469  int64 result;
1470  PgStat_StatDBEntry *dbentry;
1471 
1472  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1473  result = 0;
1474  else
1475  result = (int64) (dbentry->n_conflict_tablespace +
1476  dbentry->n_conflict_lock +
1477  dbentry->n_conflict_snapshot +
1478  dbentry->n_conflict_bufferpin +
1479  dbentry->n_conflict_startup_deadlock);
1480 
1481  PG_RETURN_INT64(result);
1482 }
PgStat_Counter n_conflict_lock
Definition: pgstat.h:292
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:291
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:294
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:295
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:293

References PgStat_StatDBEntry::n_conflict_bufferpin, PgStat_StatDBEntry::n_conflict_lock, PgStat_StatDBEntry::n_conflict_snapshot, PgStat_StatDBEntry::n_conflict_startup_deadlock, PgStat_StatDBEntry::n_conflict_tablespace, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_bufferpin()

Datum pg_stat_get_db_conflict_bufferpin ( PG_FUNCTION_ARGS  )

Definition at line 1436 of file pgstatfuncs.c.

1437 {
1438  Oid dbid = PG_GETARG_OID(0);
1439  int64 result;
1440  PgStat_StatDBEntry *dbentry;
1441 
1442  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1443  result = 0;
1444  else
1445  result = (int64) (dbentry->n_conflict_bufferpin);
1446 
1447  PG_RETURN_INT64(result);
1448 }

References PgStat_StatDBEntry::n_conflict_bufferpin, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_lock()

Datum pg_stat_get_db_conflict_lock ( PG_FUNCTION_ARGS  )

Definition at line 1406 of file pgstatfuncs.c.

1407 {
1408  Oid dbid = PG_GETARG_OID(0);
1409  int64 result;
1410  PgStat_StatDBEntry *dbentry;
1411 
1412  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1413  result = 0;
1414  else
1415  result = (int64) (dbentry->n_conflict_lock);
1416 
1417  PG_RETURN_INT64(result);
1418 }

References PgStat_StatDBEntry::n_conflict_lock, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_snapshot()

Datum pg_stat_get_db_conflict_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1421 of file pgstatfuncs.c.

1422 {
1423  Oid dbid = PG_GETARG_OID(0);
1424  int64 result;
1425  PgStat_StatDBEntry *dbentry;
1426 
1427  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1428  result = 0;
1429  else
1430  result = (int64) (dbentry->n_conflict_snapshot);
1431 
1432  PG_RETURN_INT64(result);
1433 }

References PgStat_StatDBEntry::n_conflict_snapshot, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_startup_deadlock()

Datum pg_stat_get_db_conflict_startup_deadlock ( PG_FUNCTION_ARGS  )

Definition at line 1451 of file pgstatfuncs.c.

1452 {
1453  Oid dbid = PG_GETARG_OID(0);
1454  int64 result;
1455  PgStat_StatDBEntry *dbentry;
1456 
1457  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1458  result = 0;
1459  else
1460  result = (int64) (dbentry->n_conflict_startup_deadlock);
1461 
1462  PG_RETURN_INT64(result);
1463 }

References PgStat_StatDBEntry::n_conflict_startup_deadlock, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_conflict_tablespace()

Datum pg_stat_get_db_conflict_tablespace ( PG_FUNCTION_ARGS  )

Definition at line 1391 of file pgstatfuncs.c.

1392 {
1393  Oid dbid = PG_GETARG_OID(0);
1394  int64 result;
1395  PgStat_StatDBEntry *dbentry;
1396 
1397  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1398  result = 0;
1399  else
1400  result = (int64) (dbentry->n_conflict_tablespace);
1401 
1402  PG_RETURN_INT64(result);
1403 }

References PgStat_StatDBEntry::n_conflict_tablespace, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_deadlocks()

Datum pg_stat_get_db_deadlocks ( PG_FUNCTION_ARGS  )

Definition at line 1485 of file pgstatfuncs.c.

1486 {
1487  Oid dbid = PG_GETARG_OID(0);
1488  int64 result;
1489  PgStat_StatDBEntry *dbentry;
1490 
1491  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1492  result = 0;
1493  else
1494  result = (int64) (dbentry->n_deadlocks);
1495 
1496  PG_RETURN_INT64(result);
1497 }
PgStat_Counter n_deadlocks
Definition: pgstat.h:298

References PgStat_StatDBEntry::n_deadlocks, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_idle_in_transaction_time()

Datum pg_stat_get_db_idle_in_transaction_time ( PG_FUNCTION_ARGS  )

Definition at line 1599 of file pgstatfuncs.c.

1600 {
1601  Oid dbid = PG_GETARG_OID(0);
1602  double result = 0.0;
1603  PgStat_StatDBEntry *dbentry;
1604 
1605  /* convert counter from microsec to millisec for display */
1606  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1607  result = ((double) dbentry->total_idle_in_xact_time) / 1000.0;
1608 
1609  PG_RETURN_FLOAT8(result);
1610 }
PgStat_Counter total_idle_in_xact_time
Definition: pgstat.h:306

References PG_GETARG_OID, PG_RETURN_FLOAT8, pgstat_fetch_stat_dbentry(), and PgStat_StatDBEntry::total_idle_in_xact_time.

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 1178 of file pgstatfuncs.c.

1179 {
1180  Oid dbid = PG_GETARG_OID(0);
1181  int32 result;
1182  int tot_backends = pgstat_fetch_stat_numbackends();
1183  int beid;
1184 
1185  result = 0;
1186  for (beid = 1; beid <= tot_backends; beid++)
1187  {
1189 
1190  if (local_beentry->backendStatus.st_databaseid == dbid)
1191  result++;
1192  }
1193 
1194  PG_RETURN_INT32(result);
1195 }

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

◆ pg_stat_get_db_session_time()

Datum pg_stat_get_db_session_time ( PG_FUNCTION_ARGS  )

Definition at line 1571 of file pgstatfuncs.c.

1572 {
1573  Oid dbid = PG_GETARG_OID(0);
1574  double result = 0.0;
1575  PgStat_StatDBEntry *dbentry;
1576 
1577  /* convert counter from microsec to millisec for display */
1578  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1579  result = ((double) dbentry->total_session_time) / 1000.0;
1580 
1581  PG_RETURN_FLOAT8(result);
1582 }
PgStat_Counter total_session_time
Definition: pgstat.h:304

References PG_GETARG_OID, PG_RETURN_FLOAT8, pgstat_fetch_stat_dbentry(), and PgStat_StatDBEntry::total_session_time.

◆ pg_stat_get_db_sessions()

Datum pg_stat_get_db_sessions ( PG_FUNCTION_ARGS  )

Definition at line 1613 of file pgstatfuncs.c.

1614 {
1615  Oid dbid = PG_GETARG_OID(0);
1616  int64 result = 0;
1617  PgStat_StatDBEntry *dbentry;
1618 
1619  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1620  result = (int64) (dbentry->n_sessions);
1621 
1622  PG_RETURN_INT64(result);
1623 }
PgStat_Counter n_sessions
Definition: pgstat.h:303

References PgStat_StatDBEntry::n_sessions, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_sessions_abandoned()

Datum pg_stat_get_db_sessions_abandoned ( PG_FUNCTION_ARGS  )

Definition at line 1626 of file pgstatfuncs.c.

1627 {
1628  Oid dbid = PG_GETARG_OID(0);
1629  int64 result = 0;
1630  PgStat_StatDBEntry *dbentry;
1631 
1632  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1633  result = (int64) (dbentry->n_sessions_abandoned);
1634 
1635  PG_RETURN_INT64(result);
1636 }
PgStat_Counter n_sessions_abandoned
Definition: pgstat.h:307

References PgStat_StatDBEntry::n_sessions_abandoned, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_sessions_fatal()

Datum pg_stat_get_db_sessions_fatal ( PG_FUNCTION_ARGS  )

Definition at line 1639 of file pgstatfuncs.c.

1640 {
1641  Oid dbid = PG_GETARG_OID(0);
1642  int64 result = 0;
1643  PgStat_StatDBEntry *dbentry;
1644 
1645  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1646  result = (int64) (dbentry->n_sessions_fatal);
1647 
1648  PG_RETURN_INT64(result);
1649 }
PgStat_Counter n_sessions_fatal
Definition: pgstat.h:308

References PgStat_StatDBEntry::n_sessions_fatal, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_sessions_killed()

Datum pg_stat_get_db_sessions_killed ( PG_FUNCTION_ARGS  )

Definition at line 1652 of file pgstatfuncs.c.

1653 {
1654  Oid dbid = PG_GETARG_OID(0);
1655  int64 result = 0;
1656  PgStat_StatDBEntry *dbentry;
1657 
1658  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1659  result = (int64) (dbentry->n_sessions_killed);
1660 
1661  PG_RETURN_INT64(result);
1662 }
PgStat_Counter n_sessions_killed
Definition: pgstat.h:309

References PgStat_StatDBEntry::n_sessions_killed, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_stat_reset_time()

Datum pg_stat_get_db_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1342 of file pgstatfuncs.c.

1343 {
1344  Oid dbid = PG_GETARG_OID(0);
1345  TimestampTz result;
1346  PgStat_StatDBEntry *dbentry;
1347 
1348  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1349  result = 0;
1350  else
1351  result = dbentry->stat_reset_timestamp;
1352 
1353  if (result == 0)
1354  PG_RETURN_NULL();
1355  else
1356  PG_RETURN_TIMESTAMPTZ(result);
1357 }
TimestampTz stat_reset_timestamp
Definition: pgstat.h:311

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

◆ pg_stat_get_db_temp_bytes()

Datum pg_stat_get_db_temp_bytes ( PG_FUNCTION_ARGS  )

Definition at line 1376 of file pgstatfuncs.c.

1377 {
1378  Oid dbid = PG_GETARG_OID(0);
1379  int64 result;
1380  PgStat_StatDBEntry *dbentry;
1381 
1382  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1383  result = 0;
1384  else
1385  result = dbentry->n_temp_bytes;
1386 
1387  PG_RETURN_INT64(result);
1388 }
PgStat_Counter n_temp_bytes
Definition: pgstat.h:297

References PgStat_StatDBEntry::n_temp_bytes, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_temp_files()

Datum pg_stat_get_db_temp_files ( PG_FUNCTION_ARGS  )

Definition at line 1360 of file pgstatfuncs.c.

1361 {
1362  Oid dbid = PG_GETARG_OID(0);
1363  int64 result;
1364  PgStat_StatDBEntry *dbentry;
1365 
1366  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1367  result = 0;
1368  else
1369  result = dbentry->n_temp_files;
1370 
1371  PG_RETURN_INT64(result);
1372 }
PgStat_Counter n_temp_files
Definition: pgstat.h:296

References PgStat_StatDBEntry::n_temp_files, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_tuples_deleted()

Datum pg_stat_get_db_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 1327 of file pgstatfuncs.c.

1328 {
1329  Oid dbid = PG_GETARG_OID(0);
1330  int64 result;
1331  PgStat_StatDBEntry *dbentry;
1332 
1333  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1334  result = 0;
1335  else
1336  result = (int64) (dbentry->n_tuples_deleted);
1337 
1338  PG_RETURN_INT64(result);
1339 }
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:289

References PgStat_StatDBEntry::n_tuples_deleted, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_tuples_fetched()

Datum pg_stat_get_db_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1279 of file pgstatfuncs.c.

1280 {
1281  Oid dbid = PG_GETARG_OID(0);
1282  int64 result;
1283  PgStat_StatDBEntry *dbentry;
1284 
1285  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1286  result = 0;
1287  else
1288  result = (int64) (dbentry->n_tuples_fetched);
1289 
1290  PG_RETURN_INT64(result);
1291 }
PgStat_Counter n_tuples_fetched
Definition: pgstat.h:286

References PgStat_StatDBEntry::n_tuples_fetched, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_tuples_inserted()

Datum pg_stat_get_db_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1295 of file pgstatfuncs.c.

1296 {
1297  Oid dbid = PG_GETARG_OID(0);
1298  int64 result;
1299  PgStat_StatDBEntry *dbentry;
1300 
1301  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1302  result = 0;
1303  else
1304  result = (int64) (dbentry->n_tuples_inserted);
1305 
1306  PG_RETURN_INT64(result);
1307 }
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:287

References PgStat_StatDBEntry::n_tuples_inserted, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_tuples_returned()

Datum pg_stat_get_db_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 1263 of file pgstatfuncs.c.

1264 {
1265  Oid dbid = PG_GETARG_OID(0);
1266  int64 result;
1267  PgStat_StatDBEntry *dbentry;
1268 
1269  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1270  result = 0;
1271  else
1272  result = (int64) (dbentry->n_tuples_returned);
1273 
1274  PG_RETURN_INT64(result);
1275 }
PgStat_Counter n_tuples_returned
Definition: pgstat.h:285

References PgStat_StatDBEntry::n_tuples_returned, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_tuples_updated()

Datum pg_stat_get_db_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 1311 of file pgstatfuncs.c.

1312 {
1313  Oid dbid = PG_GETARG_OID(0);
1314  int64 result;
1315  PgStat_StatDBEntry *dbentry;
1316 
1317  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1318  result = 0;
1319  else
1320  result = (int64) (dbentry->n_tuples_updated);
1321 
1322  PG_RETURN_INT64(result);
1323 }
PgStat_Counter n_tuples_updated
Definition: pgstat.h:288

References PgStat_StatDBEntry::n_tuples_updated, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_xact_commit()

Datum pg_stat_get_db_xact_commit ( PG_FUNCTION_ARGS  )

Definition at line 1199 of file pgstatfuncs.c.

1200 {
1201  Oid dbid = PG_GETARG_OID(0);
1202  int64 result;
1203  PgStat_StatDBEntry *dbentry;
1204 
1205  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1206  result = 0;
1207  else
1208  result = (int64) (dbentry->n_xact_commit);
1209 
1210  PG_RETURN_INT64(result);
1211 }
PgStat_Counter n_xact_commit
Definition: pgstat.h:281

References PgStat_StatDBEntry::n_xact_commit, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_xact_rollback()

Datum pg_stat_get_db_xact_rollback ( PG_FUNCTION_ARGS  )

Definition at line 1215 of file pgstatfuncs.c.

1216 {
1217  Oid dbid = PG_GETARG_OID(0);
1218  int64 result;
1219  PgStat_StatDBEntry *dbentry;
1220 
1221  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1222  result = 0;
1223  else
1224  result = (int64) (dbentry->n_xact_rollback);
1225 
1226  PG_RETURN_INT64(result);
1227 }
PgStat_Counter n_xact_rollback
Definition: pgstat.h:282

References PgStat_StatDBEntry::n_xact_rollback, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_dead_tuples()

Datum pg_stat_get_dead_tuples ( PG_FUNCTION_ARGS  )

Definition at line 187 of file pgstatfuncs.c.

188 {
189  Oid relid = PG_GETARG_OID(0);
190  int64 result;
191  PgStat_StatTabEntry *tabentry;
192 
193  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
194  result = 0;
195  else
196  result = (int64) (tabentry->n_dead_tuples);
197 
198  PG_RETURN_INT64(result);
199 }
PgStat_Counter n_dead_tuples
Definition: pgstat.h:368

References PgStat_StatTabEntry::n_dead_tuples, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_function_calls()

Datum pg_stat_get_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 398 of file pgstatfuncs.c.

399 {
400  Oid funcid = PG_GETARG_OID(0);
401  PgStat_StatFuncEntry *funcentry;
402 
403  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
404  PG_RETURN_NULL();
405  PG_RETURN_INT64(funcentry->f_numcalls);
406 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
PgStat_Counter f_numcalls
Definition: pgstat.h:316

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

◆ pg_stat_get_function_self_time()

Datum pg_stat_get_function_self_time ( PG_FUNCTION_ARGS  )

Definition at line 421 of file pgstatfuncs.c.

422 {
423  Oid funcid = PG_GETARG_OID(0);
424  PgStat_StatFuncEntry *funcentry;
425 
426  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
427  PG_RETURN_NULL();
428  /* convert counter from microsec to millisec for display */
429  PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
430 }
PgStat_Counter f_self_time
Definition: pgstat.h:319

References PgStat_StatFuncEntry::f_self_time, PG_GETARG_OID, PG_RETURN_FLOAT8, PG_RETURN_NULL, and pgstat_fetch_stat_funcentry().

◆ pg_stat_get_function_total_time()

Datum pg_stat_get_function_total_time ( PG_FUNCTION_ARGS  )

Definition at line 409 of file pgstatfuncs.c.

410 {
411  Oid funcid = PG_GETARG_OID(0);
412  PgStat_StatFuncEntry *funcentry;
413 
414  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
415  PG_RETURN_NULL();
416  /* convert counter from microsec to millisec for display */
417  PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
418 }
PgStat_Counter f_total_time
Definition: pgstat.h:318

References PgStat_StatFuncEntry::f_total_time, PG_GETARG_OID, PG_RETURN_FLOAT8, PG_RETURN_NULL, and pgstat_fetch_stat_funcentry().

◆ pg_stat_get_ins_since_vacuum()

Datum pg_stat_get_ins_since_vacuum ( PG_FUNCTION_ARGS  )

Definition at line 219 of file pgstatfuncs.c.

220 {
221  Oid relid = PG_GETARG_OID(0);
222  int64 result;
223  PgStat_StatTabEntry *tabentry;
224 
225  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
226  result = 0;
227  else
228  result = (int64) (tabentry->inserts_since_vacuum);
229 
230  PG_RETURN_INT64(result);
231 }
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:370

References PgStat_StatTabEntry::inserts_since_vacuum, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_last_analyze_time()

Datum pg_stat_get_last_analyze_time ( PG_FUNCTION_ARGS  )

Definition at line 302 of file pgstatfuncs.c.

303 {
304  Oid relid = PG_GETARG_OID(0);
305  TimestampTz result;
306  PgStat_StatTabEntry *tabentry;
307 
308  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
309  result = 0;
310  else
311  result = tabentry->analyze_timestamp;
312 
313  if (result == 0)
314  PG_RETURN_NULL();
315  else
316  PG_RETURN_TIMESTAMPTZ(result);
317 }
TimestampTz analyze_timestamp
Definition: pgstat.h:379

References PgStat_StatTabEntry::analyze_timestamp, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_last_autoanalyze_time()

Datum pg_stat_get_last_autoanalyze_time ( PG_FUNCTION_ARGS  )

Definition at line 320 of file pgstatfuncs.c.

321 {
322  Oid relid = PG_GETARG_OID(0);
323  TimestampTz result;
324  PgStat_StatTabEntry *tabentry;
325 
326  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
327  result = 0;
328  else
329  result = tabentry->autovac_analyze_timestamp;
330 
331  if (result == 0)
332  PG_RETURN_NULL();
333  else
334  PG_RETURN_TIMESTAMPTZ(result);
335 }
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:381

References PgStat_StatTabEntry::autovac_analyze_timestamp, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_last_autovacuum_time()

Datum pg_stat_get_last_autovacuum_time ( PG_FUNCTION_ARGS  )

Definition at line 284 of file pgstatfuncs.c.

285 {
286  Oid relid = PG_GETARG_OID(0);
287  TimestampTz result;
288  PgStat_StatTabEntry *tabentry;
289 
290  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
291  result = 0;
292  else
293  result = tabentry->autovac_vacuum_timestamp;
294 
295  if (result == 0)
296  PG_RETURN_NULL();
297  else
298  PG_RETURN_TIMESTAMPTZ(result);
299 }
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:377

References PgStat_StatTabEntry::autovac_vacuum_timestamp, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_last_vacuum_time()

Datum pg_stat_get_last_vacuum_time ( PG_FUNCTION_ARGS  )

Definition at line 266 of file pgstatfuncs.c.

267 {
268  Oid relid = PG_GETARG_OID(0);
269  TimestampTz result;
270  PgStat_StatTabEntry *tabentry;
271 
272  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
273  result = 0;
274  else
275  result = tabentry->vacuum_timestamp;
276 
277  if (result == 0)
278  PG_RETURN_NULL();
279  else
280  PG_RETURN_TIMESTAMPTZ(result);
281 }
TimestampTz vacuum_timestamp
Definition: pgstat.h:375

References PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::vacuum_timestamp.

◆ pg_stat_get_lastscan()

Datum pg_stat_get_lastscan ( PG_FUNCTION_ARGS  )

Definition at line 56 of file pgstatfuncs.c.

57 {
58  Oid relid = PG_GETARG_OID(0);
59  TimestampTz result;
60  PgStat_StatTabEntry *tabentry;
61 
62  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
63  result = 0;
64  else
65  result = tabentry->lastscan;
66 
67  if (result == 0)
69  else
70  PG_RETURN_TIMESTAMPTZ(result);
71 }
TimestampTz lastscan
Definition: pgstat.h:357

References PgStat_StatTabEntry::lastscan, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_live_tuples()

Datum pg_stat_get_live_tuples ( PG_FUNCTION_ARGS  )

Definition at line 171 of file pgstatfuncs.c.

172 {
173  Oid relid = PG_GETARG_OID(0);
174  int64 result;
175  PgStat_StatTabEntry *tabentry;
176 
177  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
178  result = 0;
179  else
180  result = (int64) (tabentry->n_live_tuples);
181 
182  PG_RETURN_INT64(result);
183 }
PgStat_Counter n_live_tuples
Definition: pgstat.h:367

References PgStat_StatTabEntry::n_live_tuples, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_mod_since_analyze()

Datum pg_stat_get_mod_since_analyze ( PG_FUNCTION_ARGS  )

Definition at line 203 of file pgstatfuncs.c.

204 {
205  Oid relid = PG_GETARG_OID(0);
206  int64 result;
207  PgStat_StatTabEntry *tabentry;
208 
209  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
210  result = 0;
211  else
212  result = (int64) (tabentry->changes_since_analyze);
213 
214  PG_RETURN_INT64(result);
215 }
PgStat_Counter changes_since_analyze
Definition: pgstat.h:369

References PgStat_StatTabEntry::changes_since_analyze, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_numscans()

Datum pg_stat_get_numscans ( PG_FUNCTION_ARGS  )

Definition at line 40 of file pgstatfuncs.c.

41 {
42  Oid relid = PG_GETARG_OID(0);
43  int64 result;
44  PgStat_StatTabEntry *tabentry;
45 
46  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
47  result = 0;
48  else
49  result = (int64) (tabentry->numscans);
50 
51  PG_RETURN_INT64(result);
52 }
PgStat_Counter numscans
Definition: pgstat.h:356

References PgStat_StatTabEntry::numscans, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_tabentry().

◆ pg_stat_get_progress_info()

Datum pg_stat_get_progress_info ( PG_FUNCTION_ARGS  )

Definition at line 484 of file pgstatfuncs.c.

485 {
486 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
487  int num_backends = pgstat_fetch_stat_numbackends();
488  int curr_backend;
489  char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
490  ProgressCommandType cmdtype;
491  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
492 
493  /* Translate command name into command type code. */
494  if (pg_strcasecmp(cmd, "VACUUM") == 0)
495  cmdtype = PROGRESS_COMMAND_VACUUM;
496  else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
497  cmdtype = PROGRESS_COMMAND_ANALYZE;
498  else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
499  cmdtype = PROGRESS_COMMAND_CLUSTER;
500  else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
502  else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
503  cmdtype = PROGRESS_COMMAND_BASEBACKUP;
504  else if (pg_strcasecmp(cmd, "COPY") == 0)
505  cmdtype = PROGRESS_COMMAND_COPY;
506  else
507  ereport(ERROR,
508  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
509  errmsg("invalid command name: \"%s\"", cmd)));
510 
511  InitMaterializedSRF(fcinfo, 0);
512 
513  /* 1-based index */
514  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
515  {
516  LocalPgBackendStatus *local_beentry;
517  PgBackendStatus *beentry;
519  bool nulls[PG_STAT_GET_PROGRESS_COLS] = {0};
520  int i;
521 
522  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
523  beentry = &local_beentry->backendStatus;
524 
525  /*
526  * Report values for only those backends which are running the given
527  * command.
528  */
529  if (beentry->st_progress_command != cmdtype)
530  continue;
531 
532  /* Value available to all callers */
533  values[0] = Int32GetDatum(beentry->st_procpid);
534  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
535 
536  /* show rest of the values including relid only to role members */
537  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
538  {
540  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
541  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
542  }
543  else
544  {
545  nulls[2] = true;
546  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
547  nulls[i + 3] = true;
548  }
549 
550  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
551  }
552 
553  return (Datum) 0;
554 }
#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:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#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:222

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_local_beentry(), pgstat_fetch_stat_numbackends(), 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 2253 of file pgstatfuncs.c.

2254 {
2255 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2256  text *slotname_text = PG_GETARG_TEXT_P(0);
2257  NameData slotname;
2258  TupleDesc tupdesc;
2260  bool nulls[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0};
2261  PgStat_StatReplSlotEntry *slotent;
2262  PgStat_StatReplSlotEntry allzero;
2263 
2264  /* Initialise attributes information in the tuple descriptor */
2266  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2267  TEXTOID, -1, 0);
2268  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2269  INT8OID, -1, 0);
2270  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2271  INT8OID, -1, 0);
2272  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2273  INT8OID, -1, 0);
2274  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2275  INT8OID, -1, 0);
2276  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2277  INT8OID, -1, 0);
2278  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2279  INT8OID, -1, 0);
2280  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2281  INT8OID, -1, 0);
2282  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2283  INT8OID, -1, 0);
2284  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2285  TIMESTAMPTZOID, -1, 0);
2286  BlessTupleDesc(tupdesc);
2287 
2288  namestrcpy(&slotname, text_to_cstring(slotname_text));
2289  slotent = pgstat_fetch_replslot(slotname);
2290  if (!slotent)
2291  {
2292  /*
2293  * If the slot is not found, initialise its stats. This is possible if
2294  * the create slot message is lost.
2295  */
2296  memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2297  slotent = &allzero;
2298  }
2299 
2300  values[0] = CStringGetTextDatum(NameStr(slotname));
2301  values[1] = Int64GetDatum(slotent->spill_txns);
2302  values[2] = Int64GetDatum(slotent->spill_count);
2303  values[3] = Int64GetDatum(slotent->spill_bytes);
2304  values[4] = Int64GetDatum(slotent->stream_txns);
2305  values[5] = Int64GetDatum(slotent->stream_count);
2306  values[6] = Int64GetDatum(slotent->stream_bytes);
2307  values[7] = Int64GetDatum(slotent->total_txns);
2308  values[8] = Int64GetDatum(slotent->total_bytes);
2309 
2310  if (slotent->stat_reset_timestamp == 0)
2311  nulls[9] = true;
2312  else
2314 
2315  /* Returns the record as Datum */
2317 }
#define NameStr(name)
Definition: c.h:682
#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:332
PgStat_Counter stream_count
Definition: pgstat.h:328
PgStat_Counter total_txns
Definition: pgstat.h:330
PgStat_Counter total_bytes
Definition: pgstat.h:331
PgStat_Counter spill_txns
Definition: pgstat.h:324
PgStat_Counter stream_txns
Definition: pgstat.h:327
PgStat_Counter spill_count
Definition: pgstat.h:325
PgStat_Counter stream_bytes
Definition: pgstat.h:329
PgStat_Counter spill_bytes
Definition: pgstat.h:326
Definition: c.h:677

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 1802 of file pgstatfuncs.c.

1803 {
1804 #define PG_STAT_GET_SLRU_COLS 9
1805  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1806  int i;
1807  PgStat_SLRUStats *stats;
1808 
1809  InitMaterializedSRF(fcinfo, 0);
1810 
1811  /* request SLRU stats from the cumulative stats system */
1812  stats = pgstat_fetch_slru();
1813 
1814  for (i = 0;; i++)
1815  {
1816  /* for each row */
1818  bool nulls[PG_STAT_GET_SLRU_COLS] = {0};
1820  const char *name;
1821 
1823 
1824  if (!name)
1825  break;
1826 
1827  stat = stats[i];
1828 
1830  values[1] = Int64GetDatum(stat.blocks_zeroed);
1831  values[2] = Int64GetDatum(stat.blocks_hit);
1832  values[3] = Int64GetDatum(stat.blocks_read);
1833  values[4] = Int64GetDatum(stat.blocks_written);
1834  values[5] = Int64GetDatum(stat.blocks_exists);
1835  values[6] = Int64GetDatum(stat.flush);
1836  values[7] = Int64GetDatum(stat.truncate);
1837  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1838 
1839  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
1840  }
1841 
1842  return (Datum) 0;
1843 }
const char * name
Definition: encode.c:561
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:670
#define stat
Definition: win32_port.h:286

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 2034 of file pgstatfuncs.c.

2035 {
2036  bool have_snapshot;
2037  TimestampTz ts;
2038 
2039  ts = pgstat_get_stat_snapshot_timestamp(&have_snapshot);
2040 
2041  if (!have_snapshot)
2042  PG_RETURN_NULL();
2043 
2045 }
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:872

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 2324 of file pgstatfuncs.c.

2325 {
2326 #define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 4
2327  Oid subid = PG_GETARG_OID(0);
2328  TupleDesc tupdesc;
2330  bool nulls[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0};
2331  PgStat_StatSubEntry *subentry;
2332  PgStat_StatSubEntry allzero;
2333 
2334  /* Get subscription stats */
2335  subentry = pgstat_fetch_stat_subscription(subid);
2336 
2337  /* Initialise attributes information in the tuple descriptor */
2339  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subid",
2340  OIDOID, -1, 0);
2341  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "apply_error_count",
2342  INT8OID, -1, 0);
2343  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "sync_error_count",
2344  INT8OID, -1, 0);
2345  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "stats_reset",
2346  TIMESTAMPTZOID, -1, 0);
2347  BlessTupleDesc(tupdesc);
2348 
2349  if (!subentry)
2350  {
2351  /* If the subscription is not found, initialise its stats */
2352  memset(&allzero, 0, sizeof(PgStat_StatSubEntry));
2353  subentry = &allzero;
2354  }
2355 
2356  /* subid */
2357  values[0] = ObjectIdGetDatum(subid);
2358 
2359  /* apply_error_count */
2360  values[1] = Int64GetDatum(subentry->apply_error_count);
2361 
2362  /* sync_error_count */
2363  values[2] = Int64GetDatum(subentry->sync_error_count);
2364 
2365  /* stats_reset */
2366  if (subentry->stat_reset_timestamp == 0)
2367  nulls[3] = true;
2368  else
2370 
2371  /* Returns the record as Datum */
2373 }
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS
PgStat_Counter apply_error_count
Definition: pgstat.h:349
PgStat_Counter sync_error_count
Definition: pgstat.h:350
TimestampTz stat_reset_timestamp
Definition: pgstat.h:351

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_tuples_deleted()

Datum pg_stat_get_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 139 of file pgstatfuncs.c.

140 {
141  Oid relid = PG_GETARG_OID(0);
142  int64 result;
143  PgStat_StatTabEntry *tabentry;
144 
145  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
146  result = 0;
147  else
148  result = (int64) (tabentry->tuples_deleted);
149 
150  PG_RETURN_INT64(result);
151 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:364

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_deleted.

◆ pg_stat_get_tuples_fetched()

Datum pg_stat_get_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 91 of file pgstatfuncs.c.

92 {
93  Oid relid = PG_GETARG_OID(0);
94  int64 result;
95  PgStat_StatTabEntry *tabentry;
96 
97  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
98  result = 0;
99  else
100  result = (int64) (tabentry->tuples_fetched);
101 
102  PG_RETURN_INT64(result);
103 }
PgStat_Counter tuples_fetched
Definition: pgstat.h:360

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_fetched.

◆ pg_stat_get_tuples_hot_updated()

Datum pg_stat_get_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 155 of file pgstatfuncs.c.

156 {
157  Oid relid = PG_GETARG_OID(0);
158  int64 result;
159  PgStat_StatTabEntry *tabentry;
160 
161  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
162  result = 0;
163  else
164  result = (int64) (tabentry->tuples_hot_updated);
165 
166  PG_RETURN_INT64(result);
167 }
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:365

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_hot_updated.

◆ pg_stat_get_tuples_inserted()

Datum pg_stat_get_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 107 of file pgstatfuncs.c.

108 {
109  Oid relid = PG_GETARG_OID(0);
110  int64 result;
111  PgStat_StatTabEntry *tabentry;
112 
113  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
114  result = 0;
115  else
116  result = (int64) (tabentry->tuples_inserted);
117 
118  PG_RETURN_INT64(result);
119 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:362

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_inserted.

◆ pg_stat_get_tuples_returned()

Datum pg_stat_get_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 75 of file pgstatfuncs.c.

76 {
77  Oid relid = PG_GETARG_OID(0);
78  int64 result;
79  PgStat_StatTabEntry *tabentry;
80 
81  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
82  result = 0;
83  else
84  result = (int64) (tabentry->tuples_returned);
85 
86  PG_RETURN_INT64(result);
87 }
PgStat_Counter tuples_returned
Definition: pgstat.h:359

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_returned.

◆ pg_stat_get_tuples_updated()

Datum pg_stat_get_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 123 of file pgstatfuncs.c.

124 {
125  Oid relid = PG_GETARG_OID(0);
126  int64 result;
127  PgStat_StatTabEntry *tabentry;
128 
129  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
130  result = 0;
131  else
132  result = (int64) (tabentry->tuples_updated);
133 
134  PG_RETURN_INT64(result);
135 }
PgStat_Counter tuples_updated
Definition: pgstat.h:363

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::tuples_updated.

◆ pg_stat_get_vacuum_count()

Datum pg_stat_get_vacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 338 of file pgstatfuncs.c.

339 {
340  Oid relid = PG_GETARG_OID(0);
341  int64 result;
342  PgStat_StatTabEntry *tabentry;
343 
344  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
345  result = 0;
346  else
347  result = (int64) (tabentry->vacuum_count);
348 
349  PG_RETURN_INT64(result);
350 }
PgStat_Counter vacuum_count
Definition: pgstat.h:376

References PG_GETARG_OID, PG_RETURN_INT64, pgstat_fetch_stat_tabentry(), and PgStat_StatTabEntry::vacuum_count.

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

Definition at line 1738 of file pgstatfuncs.c.

1739 {
1740 #define PG_STAT_GET_WAL_COLS 9
1741  TupleDesc tupdesc;
1743  bool nulls[PG_STAT_GET_WAL_COLS] = {0};
1744  char buf[256];
1745  PgStat_WalStats *wal_stats;
1746 
1747  /* Initialise attributes information in the tuple descriptor */
1749  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1750  INT8OID, -1, 0);
1751  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1752  INT8OID, -1, 0);
1753  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1754  NUMERICOID, -1, 0);
1755  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1756  INT8OID, -1, 0);
1757  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1758  INT8OID, -1, 0);
1759  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1760  INT8OID, -1, 0);
1761  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1762  FLOAT8OID, -1, 0);
1763  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1764  FLOAT8OID, -1, 0);
1765  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1766  TIMESTAMPTZOID, -1, 0);
1767 
1768  BlessTupleDesc(tupdesc);
1769 
1770  /* Get statistics about WAL activity */
1771  wal_stats = pgstat_fetch_stat_wal();
1772 
1773  /* Fill values and NULLs */
1774  values[0] = Int64GetDatum(wal_stats->wal_records);
1775  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1776 
1777  /* Convert to numeric. */
1778  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1781  ObjectIdGetDatum(0),
1782  Int32GetDatum(-1));
1783 
1784  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1785  values[4] = Int64GetDatum(wal_stats->wal_write);
1786  values[5] = Int64GetDatum(wal_stats->wal_sync);
1787 
1788  /* Convert counters from microsec to millisec for display */
1789  values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1790  values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1791 
1793 
1794  /* Returns the record as Datum */
1796 }
#define UINT64_FORMAT
Definition: c.h:485
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1692
static char * buf
Definition: pg_test_fsync.c:67
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:53
#define PG_STAT_GET_WAL_COLS
#define snprintf
Definition: port.h:238
PgStat_Counter wal_write
Definition: pgstat.h:391
PgStat_Counter wal_buffers_full
Definition: pgstat.h:390
PgStat_Counter wal_write_time
Definition: pgstat.h:393
TimestampTz stat_reset_timestamp
Definition: pgstat.h:395
uint64 wal_bytes
Definition: pgstat.h:389
PgStat_Counter wal_sync_time
Definition: pgstat.h:394
PgStat_Counter wal_fpi
Definition: pgstat.h:388
PgStat_Counter wal_sync
Definition: pgstat.h:392
PgStat_Counter wal_records
Definition: pgstat.h:387

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_blocks_fetched()

Datum pg_stat_get_xact_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1969 of file pgstatfuncs.c.

1970 {
1971  Oid relid = PG_GETARG_OID(0);
1972  int64 result;
1973  PgStat_TableStatus *tabentry;
1974 
1975  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1976  result = 0;
1977  else
1978  result = (int64) (tabentry->t_counts.t_blocks_fetched);
1979 
1980  PG_RETURN_INT64(result);
1981 }
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:185
PgStat_TableCounts t_counts
Definition: pgstat.h:209

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableCounts::t_blocks_fetched, and PgStat_TableStatus::t_counts.

◆ pg_stat_get_xact_blocks_hit()

Datum pg_stat_get_xact_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 1984 of file pgstatfuncs.c.

1985 {
1986  Oid relid = PG_GETARG_OID(0);
1987  int64 result;
1988  PgStat_TableStatus *tabentry;
1989 
1990  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1991  result = 0;
1992  else
1993  result = (int64) (tabentry->t_counts.t_blocks_hit);
1994 
1995  PG_RETURN_INT64(result);
1996 }
PgStat_Counter t_blocks_hit
Definition: pgstat.h:186

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableCounts::t_blocks_hit, and PgStat_TableStatus::t_counts.

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 1999 of file pgstatfuncs.c.

2000 {
2001  Oid funcid = PG_GETARG_OID(0);
2002  PgStat_BackendFunctionEntry *funcentry;
2003 
2004  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2005  PG_RETURN_NULL();
2006  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
2007 }
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
PgStat_FunctionCounts f_counts
Definition: pgstat.h:119
PgStat_Counter f_numcalls
Definition: pgstat.h:108

References PgStat_BackendFunctionEntry::f_counts, PgStat_FunctionCounts::f_numcalls, find_funcstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, and PG_RETURN_NULL.

◆ pg_stat_get_xact_function_self_time()

Datum pg_stat_get_xact_function_self_time ( PG_FUNCTION_ARGS  )

Definition at line 2021 of file pgstatfuncs.c.

2022 {
2023  Oid funcid = PG_GETARG_OID(0);
2024  PgStat_BackendFunctionEntry *funcentry;
2025 
2026  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2027  PG_RETURN_NULL();
2029 }
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:135
instr_time f_self_time
Definition: pgstat.h:110

References PgStat_BackendFunctionEntry::f_counts, PgStat_FunctionCounts::f_self_time, find_funcstat_entry(), INSTR_TIME_GET_MILLISEC, PG_GETARG_OID, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

◆ pg_stat_get_xact_function_total_time()

Datum pg_stat_get_xact_function_total_time ( PG_FUNCTION_ARGS  )

Definition at line 2010 of file pgstatfuncs.c.

2011 {
2012  Oid funcid = PG_GETARG_OID(0);
2013  PgStat_BackendFunctionEntry *funcentry;
2014 
2015  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2016  PG_RETURN_NULL();
2018 }
instr_time f_total_time
Definition: pgstat.h:109

References PgStat_BackendFunctionEntry::f_counts, PgStat_FunctionCounts::f_total_time, find_funcstat_entry(), INSTR_TIME_GET_MILLISEC, PG_GETARG_OID, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

◆ pg_stat_get_xact_numscans()

Datum pg_stat_get_xact_numscans ( PG_FUNCTION_ARGS  )

Definition at line 1846 of file pgstatfuncs.c.

1847 {
1848  Oid relid = PG_GETARG_OID(0);
1849  int64 result;
1850  PgStat_TableStatus *tabentry;
1851 
1852  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1853  result = 0;
1854  else
1855  result = (int64) (tabentry->t_counts.t_numscans);
1856 
1857  PG_RETURN_INT64(result);
1858 }
PgStat_Counter t_numscans
Definition: pgstat.h:170

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_numscans.

◆ pg_stat_get_xact_tuples_deleted()

Datum pg_stat_get_xact_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 1933 of file pgstatfuncs.c.

1934 {
1935  Oid relid = PG_GETARG_OID(0);
1936  int64 result;
1937  PgStat_TableStatus *tabentry;
1939 
1940  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1941  result = 0;
1942  else
1943  {
1944  result = tabentry->t_counts.t_tuples_deleted;
1945  /* live subtransactions' counts aren't in t_tuples_deleted yet */
1946  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1947  result += trans->tuples_deleted;
1948  }
1949 
1950  PG_RETURN_INT64(result);
1951 }
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:177
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:208
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_deleted, PgStat_TableStatus::trans, and trans.

◆ pg_stat_get_xact_tuples_fetched()

Datum pg_stat_get_xact_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1876 of file pgstatfuncs.c.

1877 {
1878  Oid relid = PG_GETARG_OID(0);
1879  int64 result;
1880  PgStat_TableStatus *tabentry;
1881 
1882  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1883  result = 0;
1884  else
1885  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1886 
1887  PG_RETURN_INT64(result);
1888 }
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:173

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_tuples_fetched.

◆ pg_stat_get_xact_tuples_hot_updated()

Datum pg_stat_get_xact_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 1954 of file pgstatfuncs.c.

1955 {
1956  Oid relid = PG_GETARG_OID(0);
1957  int64 result;
1958  PgStat_TableStatus *tabentry;
1959 
1960  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1961  result = 0;
1962  else
1963  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
1964 
1965  PG_RETURN_INT64(result);
1966 }
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:178

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_tuples_hot_updated.

◆ pg_stat_get_xact_tuples_inserted()

Datum pg_stat_get_xact_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1891 of file pgstatfuncs.c.

1892 {
1893  Oid relid = PG_GETARG_OID(0);
1894  int64 result;
1895  PgStat_TableStatus *tabentry;
1897 
1898  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1899  result = 0;
1900  else
1901  {
1902  result = tabentry->t_counts.t_tuples_inserted;
1903  /* live subtransactions' counts aren't in t_tuples_inserted yet */
1904  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1905  result += trans->tuples_inserted;
1906  }
1907 
1908  PG_RETURN_INT64(result);
1909 }
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:175

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_inserted, PgStat_TableStatus::trans, and trans.

◆ pg_stat_get_xact_tuples_returned()

Datum pg_stat_get_xact_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 1861 of file pgstatfuncs.c.

1862 {
1863  Oid relid = PG_GETARG_OID(0);
1864  int64 result;
1865  PgStat_TableStatus *tabentry;
1866 
1867  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1868  result = 0;
1869  else
1870  result = (int64) (tabentry->t_counts.t_tuples_returned);
1871 
1872  PG_RETURN_INT64(result);
1873 }
PgStat_Counter t_tuples_returned
Definition: pgstat.h:172

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_tuples_returned.

◆ pg_stat_get_xact_tuples_updated()

Datum pg_stat_get_xact_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 1912 of file pgstatfuncs.c.

1913 {
1914  Oid relid = PG_GETARG_OID(0);
1915  int64 result;
1916  PgStat_TableStatus *tabentry;
1918 
1919  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1920  result = 0;
1921  else
1922  {
1923  result = tabentry->t_counts.t_tuples_updated;
1924  /* live subtransactions' counts aren't in t_tuples_updated yet */
1925  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1926  result += trans->tuples_updated;
1927  }
1928 
1929  PG_RETURN_INT64(result);
1930 }
PgStat_Counter t_tuples_updated
Definition: pgstat.h:176

References find_tabstat_entry(), PG_GETARG_OID, PG_RETURN_INT64, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, and trans.

◆ pg_stat_have_stats()

Datum pg_stat_have_stats ( PG_FUNCTION_ARGS  )

Definition at line 2383 of file pgstatfuncs.c.

2384 {
2385  char *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0));
2386  Oid dboid = PG_GETARG_OID(1);
2387  Oid objoid = PG_GETARG_OID(2);
2388  PgStat_Kind kind = pgstat_get_kind_from_str(stats_type);
2389 
2390  PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objoid));
2391 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:886
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1199
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 2069 of file pgstatfuncs.c.

2070 {
2072 
2073  PG_RETURN_VOID();
2074 }
void pgstat_reset_counters(void)
Definition: pgstat.c:686

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 2146 of file pgstatfuncs.c.

2147 {
2148  char *target = NULL;
2149 
2150  if (PG_ARGISNULL(0))
2152  else
2153  {
2154  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2155  pgstat_reset_replslot(target);
2156  }
2157 
2158  PG_RETURN_VOID();
2159 }
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:727
@ 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 2078 of file pgstatfuncs.c.

2079 {
2080  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2081 
2082  if (strcmp(target, "archiver") == 0)
2084  else if (strcmp(target, "bgwriter") == 0)
2085  {
2086  /*
2087  * Historically checkpointer was part of bgwriter, continue to reset
2088  * both for now.
2089  */
2092  }
2093  else if (strcmp(target, "recovery_prefetch") == 0)
2095  else if (strcmp(target, "wal") == 0)
2097  else
2098  ereport(ERROR,
2099  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2100  errmsg("unrecognized reset target: \"%s\"", target),
2101  errhint("Target must be \"archiver\", \"bgwriter\", \"recovery_prefetch\", or \"wal\".")));
2102 
2103  PG_RETURN_VOID();
2104 }
int errhint(const char *fmt,...)
Definition: elog.c:1153
@ PGSTAT_KIND_ARCHIVER
Definition: pgstat.h:48
@ PGSTAT_KIND_BGWRITER
Definition: pgstat.h:49
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
@ PGSTAT_KIND_WAL
Definition: pgstat.h:52
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_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 2118 of file pgstatfuncs.c.

2119 {
2120  Oid funcoid = PG_GETARG_OID(0);
2121 
2123 
2124  PG_RETURN_VOID();
2125 }
Oid MyDatabaseId
Definition: globals.c:89
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:705
@ 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 2108 of file pgstatfuncs.c.

2109 {
2110  Oid taboid = PG_GETARG_OID(0);
2111 
2113 
2114  PG_RETURN_VOID();
2115 }
@ PGSTAT_KIND_RELATION
Definition: pgstat.h:42

References 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 2129 of file pgstatfuncs.c.

2130 {
2131  char *target = NULL;
2132 
2133  if (PG_ARGISNULL(0))
2135  else
2136  {
2137  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2138  pgstat_reset_slru(target);
2139  }
2140 
2141  PG_RETURN_VOID();
2142 }
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:51
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 2163 of file pgstatfuncs.c.

2164 {
2165  Oid subid;
2166 
2167  if (PG_ARGISNULL(0))
2168  {
2169  /* Clear all subscription stats */
2171  }
2172  else
2173  {
2174  subid = PG_GETARG_OID(0);
2175 
2176  if (!OidIsValid(subid))
2177  ereport(ERROR,
2178  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2179  errmsg("invalid subscription OID %u", subid)));
2181  }
2182 
2183  PG_RETURN_VOID();
2184 }
#define OidIsValid(objectId)
Definition: c.h:711
@ 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().