PostgreSQL Source Code  git master
pgstatfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xlog.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)   (is_member_of_role(GetUserId(), DEFAULT_ROLE_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   5
 
#define PG_STAT_GET_SLRU_COLS   9
 
#define PG_STAT_GET_REPLICATION_SLOT_COLS   8
 

Functions

Datum pg_stat_get_numscans (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_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_get_archiver (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_replication_slots (PG_FUNCTION_ARGS)
 

Variables

PgStat_MsgBgWriter bgwriterStats
 

Macro Definition Documentation

◆ HAS_PGSTAT_PERMISSIONS

◆ PG_STAT_GET_ACTIVITY_COLS

#define PG_STAT_GET_ACTIVITY_COLS   30

Referenced by pg_stat_get_activity().

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

◆ PG_STAT_GET_SLRU_COLS

#define PG_STAT_GET_SLRU_COLS   9

Referenced by pg_stat_get_slru().

◆ PG_STAT_GET_WAL_COLS

#define PG_STAT_GET_WAL_COLS   5

Referenced by pg_stat_get_wal().

◆ UINT32_ACCESS_ONCE

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

Definition at line 34 of file pgstatfuncs.c.

Referenced by pg_stat_get_activity().

Function Documentation

◆ pg_backend_pid()

Datum pg_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 963 of file pgstatfuncs.c.

References MyProcPid, and PG_RETURN_INT32.

964 {
966 }
int MyProcPid
Definition: globals.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

◆ pg_stat_clear_snapshot()

Datum pg_stat_clear_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 2122 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_clear_snapshot().

2123 {
2125 
2126  PG_RETURN_VOID();
2127 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:6270

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 570 of file pgstatfuncs.c.

References SockAddr::addr, ReturnSetInfo::allowedModes, 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, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), GetBackendTypeDesc(), GetBackgroundWorkerTypeByPid(), PgBackendGSSStatus::gss_auth, PgBackendGSSStatus::gss_enc, PgBackendGSSStatus::gss_princ, HAS_PGSTAT_PERMISSIONS, i, inet_in(), Int32GetDatum, InvalidOid, IsA, lengthof, PGPROC::lockGroupLeader, MemoryContextSwitchTo(), MemSet, NI_MAXHOST, NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, 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, ReturnSetInfo::returnMode, SockAddr::salen, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, PgBackendSSLStatus::ssl_bits, PgBackendSSLStatus::ssl_cipher, PgBackendSSLStatus::ssl_client_dn, PgBackendSSLStatus::ssl_client_serial, PgBackendSSLStatus::ssl_compression, 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_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_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, UINT32_ACCESS_ONCE, values, PGPROC::wait_event_info, and work_mem.

571 {
572 #define PG_STAT_GET_ACTIVITY_COLS 30
573  int num_backends = pgstat_fetch_stat_numbackends();
574  int curr_backend;
575  int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
576  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
577  TupleDesc tupdesc;
578  Tuplestorestate *tupstore;
579  MemoryContext per_query_ctx;
580  MemoryContext oldcontext;
581 
582  /* check to see if caller supports us returning a tuplestore */
583  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
584  ereport(ERROR,
585  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
586  errmsg("set-valued function called in context that cannot accept a set")));
587  if (!(rsinfo->allowedModes & SFRM_Materialize))
588  ereport(ERROR,
589  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
590  errmsg("materialize mode required, but it is not allowed in this context")));
591 
592  /* Build a tuple descriptor for our result type */
593  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
594  elog(ERROR, "return type must be a row type");
595 
596  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
597  oldcontext = MemoryContextSwitchTo(per_query_ctx);
598 
599  tupstore = tuplestore_begin_heap(true, false, work_mem);
600  rsinfo->returnMode = SFRM_Materialize;
601  rsinfo->setResult = tupstore;
602  rsinfo->setDesc = tupdesc;
603 
604  MemoryContextSwitchTo(oldcontext);
605 
606  /* 1-based index */
607  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
608  {
609  /* for each row */
611  bool nulls[PG_STAT_GET_ACTIVITY_COLS];
612  LocalPgBackendStatus *local_beentry;
613  PgBackendStatus *beentry;
614  PGPROC *proc;
615  const char *wait_event_type = NULL;
616  const char *wait_event = NULL;
617 
618  MemSet(values, 0, sizeof(values));
619  MemSet(nulls, 0, sizeof(nulls));
620 
621  /* Get the next one in the list */
622  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
623  if (!local_beentry)
624  {
625  int i;
626 
627  /* Ignore missing entries if looking for specific PID */
628  if (pid != -1)
629  continue;
630 
631  for (i = 0; i < lengthof(nulls); i++)
632  nulls[i] = true;
633 
634  nulls[5] = false;
635  values[5] = CStringGetTextDatum("<backend information not available>");
636 
637  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
638  continue;
639  }
640 
641  beentry = &local_beentry->backendStatus;
642 
643  /* If looking for specific PID, ignore all the others */
644  if (pid != -1 && beentry->st_procpid != pid)
645  continue;
646 
647  /* Values available to all callers */
648  if (beentry->st_databaseid != InvalidOid)
649  values[0] = ObjectIdGetDatum(beentry->st_databaseid);
650  else
651  nulls[0] = true;
652 
653  values[1] = Int32GetDatum(beentry->st_procpid);
654 
655  if (beentry->st_userid != InvalidOid)
656  values[2] = ObjectIdGetDatum(beentry->st_userid);
657  else
658  nulls[2] = true;
659 
660  if (beentry->st_appname)
661  values[3] = CStringGetTextDatum(beentry->st_appname);
662  else
663  nulls[3] = true;
664 
665  if (TransactionIdIsValid(local_beentry->backend_xid))
666  values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
667  else
668  nulls[15] = true;
669 
670  if (TransactionIdIsValid(local_beentry->backend_xmin))
671  values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
672  else
673  nulls[16] = true;
674 
675  /* Values only available to role member or pg_read_all_stats */
676  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
677  {
678  SockAddr zero_clientaddr;
679  char *clipped_activity;
680 
681  switch (beentry->st_state)
682  {
683  case STATE_IDLE:
684  values[4] = CStringGetTextDatum("idle");
685  break;
686  case STATE_RUNNING:
687  values[4] = CStringGetTextDatum("active");
688  break;
690  values[4] = CStringGetTextDatum("idle in transaction");
691  break;
692  case STATE_FASTPATH:
693  values[4] = CStringGetTextDatum("fastpath function call");
694  break;
696  values[4] = CStringGetTextDatum("idle in transaction (aborted)");
697  break;
698  case STATE_DISABLED:
699  values[4] = CStringGetTextDatum("disabled");
700  break;
701  case STATE_UNDEFINED:
702  nulls[4] = true;
703  break;
704  }
705 
706  clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
707  values[5] = CStringGetTextDatum(clipped_activity);
708  pfree(clipped_activity);
709 
710  /* leader_pid */
711  nulls[29] = true;
712 
713  proc = BackendPidGetProc(beentry->st_procpid);
714 
715  if (proc == NULL && (beentry->st_backendType != B_BACKEND))
716  {
717  /*
718  * For an auxiliary process, retrieve process info from
719  * AuxiliaryProcs stored in shared-memory.
720  */
721  proc = AuxiliaryPidGetProc(beentry->st_procpid);
722  }
723 
724  /*
725  * If a PGPROC entry was retrieved, display wait events and lock
726  * group leader information if any. To avoid extra overhead, no
727  * extra lock is being held, so there is no guarantee of
728  * consistency across multiple rows.
729  */
730  if (proc != NULL)
731  {
732  uint32 raw_wait_event;
733  PGPROC *leader;
734 
735  raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
736  wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
737  wait_event = pgstat_get_wait_event(raw_wait_event);
738 
739  leader = proc->lockGroupLeader;
740 
741  /*
742  * Show the leader only for active parallel workers. This
743  * leaves the field as NULL for the leader of a parallel
744  * group.
745  */
746  if (leader && leader->pid != beentry->st_procpid)
747  {
748  values[29] = Int32GetDatum(leader->pid);
749  nulls[29] = false;
750  }
751  }
752 
753  if (wait_event_type)
754  values[6] = CStringGetTextDatum(wait_event_type);
755  else
756  nulls[6] = true;
757 
758  if (wait_event)
759  values[7] = CStringGetTextDatum(wait_event);
760  else
761  nulls[7] = true;
762 
763  /*
764  * Don't expose transaction time for walsenders; it confuses
765  * monitoring, particularly because we don't keep the time up-to-
766  * date.
767  */
768  if (beentry->st_xact_start_timestamp != 0 &&
769  beentry->st_backendType != B_WAL_SENDER)
770  values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
771  else
772  nulls[8] = true;
773 
774  if (beentry->st_activity_start_timestamp != 0)
775  values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
776  else
777  nulls[9] = true;
778 
779  if (beentry->st_proc_start_timestamp != 0)
780  values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
781  else
782  nulls[10] = true;
783 
784  if (beentry->st_state_start_timestamp != 0)
785  values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp);
786  else
787  nulls[11] = true;
788 
789  /* A zeroed client addr means we don't know */
790  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
791  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
792  sizeof(zero_clientaddr)) == 0)
793  {
794  nulls[12] = true;
795  nulls[13] = true;
796  nulls[14] = true;
797  }
798  else
799  {
800  if (beentry->st_clientaddr.addr.ss_family == AF_INET
801 #ifdef HAVE_IPV6
802  || beentry->st_clientaddr.addr.ss_family == AF_INET6
803 #endif
804  )
805  {
806  char remote_host[NI_MAXHOST];
807  char remote_port[NI_MAXSERV];
808  int ret;
809 
810  remote_host[0] = '\0';
811  remote_port[0] = '\0';
812  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
813  beentry->st_clientaddr.salen,
814  remote_host, sizeof(remote_host),
815  remote_port, sizeof(remote_port),
817  if (ret == 0)
818  {
819  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
820  values[12] = DirectFunctionCall1(inet_in,
821  CStringGetDatum(remote_host));
822  if (beentry->st_clienthostname &&
823  beentry->st_clienthostname[0])
824  values[13] = CStringGetTextDatum(beentry->st_clienthostname);
825  else
826  nulls[13] = true;
827  values[14] = Int32GetDatum(atoi(remote_port));
828  }
829  else
830  {
831  nulls[12] = true;
832  nulls[13] = true;
833  nulls[14] = true;
834  }
835  }
836  else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
837  {
838  /*
839  * Unix sockets always reports NULL for host and -1 for
840  * port, so it's possible to tell the difference to
841  * connections we have no permissions to view, or with
842  * errors.
843  */
844  nulls[12] = true;
845  nulls[13] = true;
846  values[14] = Int32GetDatum(-1);
847  }
848  else
849  {
850  /* Unknown address type, should never happen */
851  nulls[12] = true;
852  nulls[13] = true;
853  nulls[14] = true;
854  }
855  }
856  /* Add backend type */
857  if (beentry->st_backendType == B_BG_WORKER)
858  {
859  const char *bgw_type;
860 
861  bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
862  if (bgw_type)
863  values[17] = CStringGetTextDatum(bgw_type);
864  else
865  nulls[17] = true;
866  }
867  else
868  values[17] =
870 
871  /* SSL information */
872  if (beentry->st_ssl)
873  {
874  values[18] = BoolGetDatum(true); /* ssl */
875  values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version);
876  values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher);
877  values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
878  values[22] = BoolGetDatum(beentry->st_sslstatus->ssl_compression);
879 
880  if (beentry->st_sslstatus->ssl_client_dn[0])
881  values[23] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn);
882  else
883  nulls[23] = true;
884 
885  if (beentry->st_sslstatus->ssl_client_serial[0])
886  values[24] = DirectFunctionCall3(numeric_in,
889  Int32GetDatum(-1));
890  else
891  nulls[24] = true;
892 
893  if (beentry->st_sslstatus->ssl_issuer_dn[0])
894  values[25] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn);
895  else
896  nulls[25] = true;
897  }
898  else
899  {
900  values[18] = BoolGetDatum(false); /* ssl */
901  nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = nulls[25] = true;
902  }
903 
904  /* GSSAPI information */
905  if (beentry->st_gss)
906  {
907  values[26] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
908  values[27] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ);
909  values[28] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
910  }
911  else
912  {
913  values[26] = BoolGetDatum(false); /* gss_auth */
914  nulls[27] = true; /* No GSS principal */
915  values[28] = BoolGetDatum(false); /* GSS Encryption not in
916  * use */
917  }
918  }
919  else
920  {
921  /* No permissions to view data about this session */
922  values[5] = CStringGetTextDatum("<insufficient privilege>");
923  nulls[4] = true;
924  nulls[6] = true;
925  nulls[7] = true;
926  nulls[8] = true;
927  nulls[9] = true;
928  nulls[10] = true;
929  nulls[11] = true;
930  nulls[12] = true;
931  nulls[13] = true;
932  nulls[14] = true;
933  nulls[17] = true;
934  nulls[18] = true;
935  nulls[19] = true;
936  nulls[20] = true;
937  nulls[21] = true;
938  nulls[22] = true;
939  nulls[23] = true;
940  nulls[24] = true;
941  nulls[25] = true;
942  nulls[26] = true;
943  nulls[27] = true;
944  nulls[28] = true;
945  nulls[29] = true;
946  }
947 
948  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
949 
950  /* If only a single backend was requested, and we found it, break. */
951  if (pid != -1)
952  break;
953  }
954 
955  /* clean up and return the tuplestore */
956  tuplestore_donestoring(tupstore);
957 
958  return (Datum) 0;
959 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
char ssl_issuer_dn[NAMEDATALEN]
Definition: pgstat.h:1149
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1288
char gss_princ[NAMEDATALEN]
Definition: pgstat.h:1163
bool ssl_compression
Definition: pgstat.h:1138
char ssl_cipher[NAMEDATALEN]
Definition: pgstat.h:1140
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:232
uint32 wait_event_info
Definition: proc.h:226
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: pgstat.c:3760
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:1211
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3062
#define PG_STAT_GET_ACTIVITY_COLS
char ssl_client_dn[NAMEDATALEN]
Definition: pgstat.h:1141
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
struct sockaddr_storage addr
Definition: pqcomm.h:64
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TransactionId backend_xmin
Definition: pgstat.h:1343
char ssl_version[NAMEDATALEN]
Definition: pgstat.h:1139
SockAddr st_clientaddr
Definition: pgstat.h:1217
int errcode(int sqlerrcode)
Definition: elog.c:694
#define MemSet(start, val, len)
Definition: c.h:1008
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define lengthof(array)
Definition: c.h:734
char ssl_client_serial[NAMEDATALEN]
Definition: pgstat.h:1147
BackendState st_state
Definition: pgstat.h:1229
char * st_clienthostname
Definition: pgstat.h:1218
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
Definition: pgstat.c:2796
Oid st_databaseid
Definition: pgstat.h:1215
void pfree(void *pointer)
Definition: mcxt.c:1057
#define NI_MAXHOST
Definition: getaddrinfo.h:88
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
TransactionId backend_xid
Definition: pgstat.h:1337
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:984
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: pgstat.c:3815
char * st_activity_raw
Definition: pgstat.h:1241
#define CStringGetDatum(X)
Definition: postgres.h:578
#define NI_MAXSERV
Definition: getaddrinfo.h:91
unsigned int uint32
Definition: c.h:441
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:621
TimestampTz st_state_start_timestamp
Definition: pgstat.h:1212
char * st_appname
Definition: pgstat.h:1232
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define TransactionIdGetDatum(X)
Definition: postgres.h:521
PgBackendStatus backendStatus
Definition: pgstat.h:1331
uintptr_t Datum
Definition: postgres.h:367
BackendType st_backendType
Definition: pgstat.h:1206
int work_mem
Definition: globals.c:122
#define BoolGetDatum(X)
Definition: postgres.h:402
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2815
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2118
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
char * pgstat_clip_activity(const char *raw_activity)
Definition: pgstat.c:7220
#define Int32GetDatum(X)
Definition: postgres.h:479
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:905
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:1210
#define elog(elevel,...)
Definition: elog.h:227
int i
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:34
#define CStringGetTextDatum(s)
Definition: builtins.h:82
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Definition: proc.h:121
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:1209
int pid
Definition: proc.h:146
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:1222
PGPROC * lockGroupLeader
Definition: proc.h:251
PgBackendGSSStatus * st_gssstatus
Definition: pgstat.h:1226

◆ pg_stat_get_analyze_count()

Datum pg_stat_get_analyze_count ( PG_FUNCTION_ARGS  )

Definition at line 351 of file pgstatfuncs.c.

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

352 {
353  Oid relid = PG_GETARG_OID(0);
354  int64 result;
355  PgStat_StatTabEntry *tabentry;
356 
357  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
358  result = 0;
359  else
360  result = (int64) (tabentry->analyze_count);
361 
362  PG_RETURN_INT64(result);
363 }
PgStat_Counter analyze_count
Definition: pgstat.h:777
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

Definition at line 2200 of file pgstatfuncs.c.

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, MemSet, PG_RETURN_DATUM, pgstat_fetch_stat_archiver(), PgStat_ArchiverStats::stat_reset_timestamp, TimestampTzGetDatum, TupleDescInitEntry(), and values.

2201 {
2202  TupleDesc tupdesc;
2203  Datum values[7];
2204  bool nulls[7];
2205  PgStat_ArchiverStats *archiver_stats;
2206 
2207  /* Initialise values and NULL flags arrays */
2208  MemSet(values, 0, sizeof(values));
2209  MemSet(nulls, 0, sizeof(nulls));
2210 
2211  /* Initialise attributes information in the tuple descriptor */
2212  tupdesc = CreateTemplateTupleDesc(7);
2213  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2214  INT8OID, -1, 0);
2215  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2216  TEXTOID, -1, 0);
2217  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2218  TIMESTAMPTZOID, -1, 0);
2219  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2220  INT8OID, -1, 0);
2221  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2222  TEXTOID, -1, 0);
2223  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2224  TIMESTAMPTZOID, -1, 0);
2225  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2226  TIMESTAMPTZOID, -1, 0);
2227 
2228  BlessTupleDesc(tupdesc);
2229 
2230  /* Get statistics about the archiver process */
2231  archiver_stats = pgstat_fetch_stat_archiver();
2232 
2233  /* Fill values and NULLs */
2234  values[0] = Int64GetDatum(archiver_stats->archived_count);
2235  if (*(archiver_stats->last_archived_wal) == '\0')
2236  nulls[1] = true;
2237  else
2238  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2239 
2240  if (archiver_stats->last_archived_timestamp == 0)
2241  nulls[2] = true;
2242  else
2243  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
2244 
2245  values[3] = Int64GetDatum(archiver_stats->failed_count);
2246  if (*(archiver_stats->last_failed_wal) == '\0')
2247  nulls[4] = true;
2248  else
2249  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2250 
2251  if (archiver_stats->last_failed_timestamp == 0)
2252  nulls[5] = true;
2253  else
2254  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2255 
2256  if (archiver_stats->stat_reset_timestamp == 0)
2257  nulls[6] = true;
2258  else
2259  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2260 
2261  /* Returns the record as Datum */
2262  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2263 }
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:808
PgStat_Counter archived_count
Definition: pgstat.h:803
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define MemSet(start, val, len)
Definition: c.h:1008
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
Definition: pgstat.c:2831
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TimestampTz last_failed_timestamp
Definition: pgstat.h:810
PgStat_Counter failed_count
Definition: pgstat.h:807
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static Datum values[MAXATTR]
Definition: bootstrap.c:165
TimestampTz stat_reset_timestamp
Definition: pgstat.h:811
TimestampTz last_archived_timestamp
Definition: pgstat.h:806
#define CStringGetTextDatum(s)
Definition: builtins.h:82
int16 AttrNumber
Definition: attnum.h:21
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:804

◆ pg_stat_get_autoanalyze_count()

Datum pg_stat_get_autoanalyze_count ( PG_FUNCTION_ARGS  )

Definition at line 366 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_autovacuum_count()

Datum pg_stat_get_autovacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 336 of file pgstatfuncs.c.

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

337 {
338  Oid relid = PG_GETARG_OID(0);
339  int64 result;
340  PgStat_StatTabEntry *tabentry;
341 
342  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
343  result = 0;
344  else
345  result = (int64) (tabentry->autovac_vacuum_count);
346 
347  PG_RETURN_INT64(result);
348 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:775

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

Definition at line 1009 of file pgstatfuncs.c.

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.

1010 {
1011  int32 beid = PG_GETARG_INT32(0);
1012  PgBackendStatus *beentry;
1013  const char *activity;
1014  char *clipped_activity;
1015  text *ret;
1016 
1017  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1018  activity = "<backend information not available>";
1019  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1020  activity = "<insufficient privilege>";
1021  else if (*(beentry->st_activity_raw) == '\0')
1022  activity = "<command string not enabled>";
1023  else
1024  activity = beentry->st_activity_raw;
1025 
1026  clipped_activity = pgstat_clip_activity(activity);
1027  ret = cstring_to_text(activity);
1028  pfree(clipped_activity);
1029 
1030  PG_RETURN_TEXT_P(ret);
1031 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:429
void pfree(void *pointer)
Definition: mcxt.c:1057
char * st_activity_raw
Definition: pgstat.h:1241
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
char * pgstat_clip_activity(const char *raw_activity)
Definition: pgstat.c:7220
Definition: c.h:621

◆ pg_stat_get_backend_activity_start()

Datum pg_stat_get_backend_activity_start ( PG_FUNCTION_ARGS  )

Definition at line 1077 of file pgstatfuncs.c.

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.

1078 {
1079  int32 beid = PG_GETARG_INT32(0);
1080  TimestampTz result;
1081  PgBackendStatus *beentry;
1082 
1083  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1084  PG_RETURN_NULL();
1085 
1086  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1087  PG_RETURN_NULL();
1088 
1089  result = beentry->st_activity_start_timestamp;
1090 
1091  /*
1092  * No time recorded for start of current query -- this is the case if the
1093  * user hasn't enabled query-level stats collection.
1094  */
1095  if (result == 0)
1096  PG_RETURN_NULL();
1097 
1098  PG_RETURN_TIMESTAMPTZ(result);
1099 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:1211
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_client_addr()

Datum pg_stat_get_backend_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1147 of file pgstatfuncs.c.

References SockAddr::addr, clean_ipv6_addr(), CStringGetDatum, DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, inet_in(), NI_MAXHOST, NI_NUMERICHOST, NI_NUMERICSERV, PG_GETARG_INT32, pg_getnameinfo_all(), PG_RETURN_INET_P, PG_RETURN_NULL, pgstat_fetch_stat_beentry(), SockAddr::salen, PgBackendStatus::st_clientaddr, and PgBackendStatus::st_userid.

1148 {
1149  int32 beid = PG_GETARG_INT32(0);
1150  PgBackendStatus *beentry;
1151  SockAddr zero_clientaddr;
1152  char remote_host[NI_MAXHOST];
1153  int ret;
1154 
1155  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1156  PG_RETURN_NULL();
1157 
1158  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1159  PG_RETURN_NULL();
1160 
1161  /* A zeroed client addr means we don't know */
1162  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1163  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1164  sizeof(zero_clientaddr)) == 0)
1165  PG_RETURN_NULL();
1166 
1167  switch (beentry->st_clientaddr.addr.ss_family)
1168  {
1169  case AF_INET:
1170 #ifdef HAVE_IPV6
1171  case AF_INET6:
1172 #endif
1173  break;
1174  default:
1175  PG_RETURN_NULL();
1176  }
1177 
1178  remote_host[0] = '\0';
1179  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1180  beentry->st_clientaddr.salen,
1181  remote_host, sizeof(remote_host),
1182  NULL, 0,
1184  if (ret != 0)
1185  PG_RETURN_NULL();
1186 
1187  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1188 
1190  CStringGetDatum(remote_host)));
1191 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
SockAddr st_clientaddr
Definition: pgstat.h:1217
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
signed int int32
Definition: c.h:429
#define NI_MAXHOST
Definition: getaddrinfo.h:88
#define CStringGetDatum(X)
Definition: postgres.h:578
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:122
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2118
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_client_port()

Datum pg_stat_get_backend_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1194 of file pgstatfuncs.c.

References SockAddr::addr, CStringGetDatum, DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, int4in(), NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, 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.

1195 {
1196  int32 beid = PG_GETARG_INT32(0);
1197  PgBackendStatus *beentry;
1198  SockAddr zero_clientaddr;
1199  char remote_port[NI_MAXSERV];
1200  int ret;
1201 
1202  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1203  PG_RETURN_NULL();
1204 
1205  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1206  PG_RETURN_NULL();
1207 
1208  /* A zeroed client addr means we don't know */
1209  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1210  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1211  sizeof(zero_clientaddr)) == 0)
1212  PG_RETURN_NULL();
1213 
1214  switch (beentry->st_clientaddr.addr.ss_family)
1215  {
1216  case AF_INET:
1217 #ifdef HAVE_IPV6
1218  case AF_INET6:
1219 #endif
1220  break;
1221  case AF_UNIX:
1222  PG_RETURN_INT32(-1);
1223  default:
1224  PG_RETURN_NULL();
1225  }
1226 
1227  remote_port[0] = '\0';
1228  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1229  beentry->st_clientaddr.salen,
1230  NULL, 0,
1231  remote_port, sizeof(remote_port),
1233  if (ret != 0)
1234  PG_RETURN_NULL();
1235 
1237  CStringGetDatum(remote_port)));
1238 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
SockAddr st_clientaddr
Definition: pgstat.h:1217
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
signed int int32
Definition: c.h:429
#define CStringGetDatum(X)
Definition: postgres.h:578
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_dbid()

Datum pg_stat_get_backend_dbid ( PG_FUNCTION_ARGS  )

Definition at line 983 of file pgstatfuncs.c.

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

984 {
985  int32 beid = PG_GETARG_INT32(0);
986  PgBackendStatus *beentry;
987 
988  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
989  PG_RETURN_NULL();
990 
991  PG_RETURN_OID(beentry->st_databaseid);
992 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:429
Oid st_databaseid
Definition: pgstat.h:1215
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_idset()

Datum pg_stat_get_backend_idset ( PG_FUNCTION_ARGS  )

Definition at line 416 of file pgstatfuncs.c.

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

417 {
418  FuncCallContext *funcctx;
419  int *fctx;
420  int32 result;
421 
422  /* stuff done only on the first call of the function */
423  if (SRF_IS_FIRSTCALL())
424  {
425  /* create a function context for cross-call persistence */
426  funcctx = SRF_FIRSTCALL_INIT();
427 
429  2 * sizeof(int));
430  funcctx->user_fctx = fctx;
431 
432  fctx[0] = 0;
433  fctx[1] = pgstat_fetch_stat_numbackends();
434  }
435 
436  /* stuff done on every call of the function */
437  funcctx = SRF_PERCALL_SETUP();
438  fctx = funcctx->user_fctx;
439 
440  fctx[0] += 1;
441  result = fctx[0];
442 
443  if (result <= fctx[1])
444  {
445  /* do when there is more left to send */
446  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
447  }
448  else
449  {
450  /* do when there is no more left */
451  SRF_RETURN_DONE(funcctx);
452  }
453 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
signed int int32
Definition: c.h:429
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2815
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define Int32GetDatum(X)
Definition: postgres.h:479
void * user_fctx
Definition: funcapi.h:82
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ pg_stat_get_backend_pid()

Datum pg_stat_get_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 970 of file pgstatfuncs.c.

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

971 {
972  int32 beid = PG_GETARG_INT32(0);
973  PgBackendStatus *beentry;
974 
975  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
976  PG_RETURN_NULL();
977 
978  PG_RETURN_INT32(beentry->st_procpid);
979 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
signed int int32
Definition: c.h:429
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_start()

Datum pg_stat_get_backend_start ( PG_FUNCTION_ARGS  )

Definition at line 1125 of file pgstatfuncs.c.

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.

1126 {
1127  int32 beid = PG_GETARG_INT32(0);
1128  TimestampTz result;
1129  PgBackendStatus *beentry;
1130 
1131  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1132  PG_RETURN_NULL();
1133 
1134  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1135  PG_RETURN_NULL();
1136 
1137  result = beentry->st_proc_start_timestamp;
1138 
1139  if (result == 0) /* probably can't happen? */
1140  PG_RETURN_NULL();
1141 
1142  PG_RETURN_TIMESTAMPTZ(result);
1143 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:1209
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_userid()

Datum pg_stat_get_backend_userid ( PG_FUNCTION_ARGS  )

Definition at line 996 of file pgstatfuncs.c.

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

997 {
998  int32 beid = PG_GETARG_INT32(0);
999  PgBackendStatus *beentry;
1000 
1001  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1002  PG_RETURN_NULL();
1003 
1004  PG_RETURN_OID(beentry->st_userid);
1005 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:429
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_wait_event()

Datum pg_stat_get_backend_wait_event ( PG_FUNCTION_ARGS  )

Definition at line 1055 of file pgstatfuncs.c.

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.

1056 {
1057  int32 beid = PG_GETARG_INT32(0);
1058  PgBackendStatus *beentry;
1059  PGPROC *proc;
1060  const char *wait_event = NULL;
1061 
1062  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1063  wait_event = "<backend information not available>";
1064  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1065  wait_event = "<insufficient privilege>";
1066  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1067  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1068 
1069  if (!wait_event)
1070  PG_RETURN_NULL();
1071 
1072  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1073 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
uint32 wait_event_info
Definition: proc.h:226
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3062
signed int int32
Definition: c.h:429
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: pgstat.c:3815
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
Definition: proc.h:121
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_wait_event_type()

Datum pg_stat_get_backend_wait_event_type ( PG_FUNCTION_ARGS  )

Definition at line 1034 of file pgstatfuncs.c.

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.

1035 {
1036  int32 beid = PG_GETARG_INT32(0);
1037  PgBackendStatus *beentry;
1038  PGPROC *proc;
1039  const char *wait_event_type = NULL;
1040 
1041  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1042  wait_event_type = "<backend information not available>";
1043  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1044  wait_event_type = "<insufficient privilege>";
1045  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1046  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
1047 
1048  if (!wait_event_type)
1049  PG_RETURN_NULL();
1050 
1051  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
1052 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
uint32 wait_event_info
Definition: proc.h:226
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: pgstat.c:3760
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3062
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
Definition: proc.h:121
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_backend_xact_start()

Datum pg_stat_get_backend_xact_start ( PG_FUNCTION_ARGS  )

Definition at line 1103 of file pgstatfuncs.c.

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.

1104 {
1105  int32 beid = PG_GETARG_INT32(0);
1106  TimestampTz result;
1107  PgBackendStatus *beentry;
1108 
1109  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1110  PG_RETURN_NULL();
1111 
1112  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1113  PG_RETURN_NULL();
1114 
1115  result = beentry->st_xact_start_timestamp;
1116 
1117  if (result == 0) /* not in a transaction */
1118  PG_RETURN_NULL();
1119 
1120  PG_RETURN_TIMESTAMPTZ(result);
1121 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:1210
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_bgwriter_buf_written_checkpoints()

Datum pg_stat_get_bgwriter_buf_written_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1741 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1742 {
1743  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1744 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_bgwriter_buf_written_clean()

Datum pg_stat_get_bgwriter_buf_written_clean ( PG_FUNCTION_ARGS  )

Definition at line 1747 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1748 {
1749  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1750 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_bgwriter_maxwritten_clean()

Datum pg_stat_get_bgwriter_maxwritten_clean ( PG_FUNCTION_ARGS  )

Definition at line 1753 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1754 {
1755  PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1756 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_bgwriter_requested_checkpoints()

Datum pg_stat_get_bgwriter_requested_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1735 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1736 {
1737  PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1738 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_bgwriter_stat_reset_time()

Datum pg_stat_get_bgwriter_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1773 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_global().

1774 {
1775  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1776 }
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ pg_stat_get_bgwriter_timed_checkpoints()

Datum pg_stat_get_bgwriter_timed_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1729 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1730 {
1731  PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1732 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_blocks_fetched()

Datum pg_stat_get_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 218 of file pgstatfuncs.c.

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

219 {
220  Oid relid = PG_GETARG_OID(0);
221  int64 result;
222  PgStat_StatTabEntry *tabentry;
223 
224  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
225  result = 0;
226  else
227  result = (int64) (tabentry->blocks_fetched);
228 
229  PG_RETURN_INT64(result);
230 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter blocks_fetched
Definition: pgstat.h:769

◆ pg_stat_get_blocks_hit()

Datum pg_stat_get_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 234 of file pgstatfuncs.c.

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

235 {
236  Oid relid = PG_GETARG_OID(0);
237  int64 result;
238  PgStat_StatTabEntry *tabentry;
239 
240  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
241  result = 0;
242  else
243  result = (int64) (tabentry->blocks_hit);
244 
245  PG_RETURN_INT64(result);
246 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter blocks_hit
Definition: pgstat.h:770

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1791 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1792 {
1793  PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1794 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_buf_fsync_backend()

Datum pg_stat_get_buf_fsync_backend ( PG_FUNCTION_ARGS  )

Definition at line 1785 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1786 {
1787  PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1788 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_buf_written_backend()

Datum pg_stat_get_buf_written_backend ( PG_FUNCTION_ARGS  )

Definition at line 1779 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1780 {
1781  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1782 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_checkpoint_sync_time()

Datum pg_stat_get_checkpoint_sync_time ( PG_FUNCTION_ARGS  )

Definition at line 1766 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_global().

1767 {
1768  /* time is already in msec, just convert to double for presentation */
1769  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1770 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_checkpoint_write_time()

Datum pg_stat_get_checkpoint_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1759 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_global().

1760 {
1761  /* time is already in msec, just convert to double for presentation */
1762  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1763 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848

◆ pg_stat_get_db_active_time()

Datum pg_stat_get_db_active_time ( PG_FUNCTION_ARGS  )

Definition at line 1649 of file pgstatfuncs.c.

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

1650 {
1651  Oid dbid = PG_GETARG_OID(0);
1652  double result = 0.0;
1653  PgStat_StatDBEntry *dbentry;
1654 
1655  /* convert counter from microsec to millisec for display */
1656  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1657  result = ((double) dbentry->total_active_time) / 1000.0;
1658 
1659  PG_RETURN_FLOAT8(result);
1660 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter total_active_time
Definition: pgstat.h:728
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_blk_read_time()

Datum pg_stat_get_db_blk_read_time ( PG_FUNCTION_ARGS  )

Definition at line 1603 of file pgstatfuncs.c.

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

1604 {
1605  Oid dbid = PG_GETARG_OID(0);
1606  double result;
1607  PgStat_StatDBEntry *dbentry;
1608 
1609  /* convert counter from microsec to millisec for display */
1610  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1611  result = 0;
1612  else
1613  result = ((double) dbentry->n_block_read_time) / 1000.0;
1614 
1615  PG_RETURN_FLOAT8(result);
1616 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_block_read_time
Definition: pgstat.h:724

◆ pg_stat_get_db_blk_write_time()

Datum pg_stat_get_db_blk_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1619 of file pgstatfuncs.c.

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

1620 {
1621  Oid dbid = PG_GETARG_OID(0);
1622  double result;
1623  PgStat_StatDBEntry *dbentry;
1624 
1625  /* convert counter from microsec to millisec for display */
1626  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1627  result = 0;
1628  else
1629  result = ((double) dbentry->n_block_write_time) / 1000.0;
1630 
1631  PG_RETURN_FLOAT8(result);
1632 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_block_write_time
Definition: pgstat.h:725

◆ pg_stat_get_db_blocks_fetched()

Datum pg_stat_get_db_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1295 of file pgstatfuncs.c.

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

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_blocks_fetched);
1305 
1306  PG_RETURN_INT64(result);
1307 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:706
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_blocks_hit()

Datum pg_stat_get_db_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 1311 of file pgstatfuncs.c.

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

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_blocks_hit);
1321 
1322  PG_RETURN_INT64(result);
1323 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_blocks_hit
Definition: pgstat.h:707
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_checksum_failures()

Datum pg_stat_get_db_checksum_failures ( PG_FUNCTION_ARGS  )

Definition at line 1564 of file pgstatfuncs.c.

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

1565 {
1566  Oid dbid = PG_GETARG_OID(0);
1567  int64 result;
1568  PgStat_StatDBEntry *dbentry;
1569 
1570  if (!DataChecksumsEnabled())
1571  PG_RETURN_NULL();
1572 
1573  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1574  result = 0;
1575  else
1576  result = (int64) (dbentry->n_checksum_failures);
1577 
1578  PG_RETURN_INT64(result);
1579 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
bool DataChecksumsEnabled(void)
Definition: xlog.c:4951
PgStat_Counter n_checksum_failures
Definition: pgstat.h:722
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_db_checksum_last_failure()

Datum pg_stat_get_db_checksum_last_failure ( PG_FUNCTION_ARGS  )

Definition at line 1582 of file pgstatfuncs.c.

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

1583 {
1584  Oid dbid = PG_GETARG_OID(0);
1585  TimestampTz result;
1586  PgStat_StatDBEntry *dbentry;
1587 
1588  if (!DataChecksumsEnabled())
1589  PG_RETURN_NULL();
1590 
1591  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1592  result = 0;
1593  else
1594  result = dbentry->last_checksum_failure;
1595 
1596  if (result == 0)
1597  PG_RETURN_NULL();
1598  else
1599  PG_RETURN_TIMESTAMPTZ(result);
1600 }
bool DataChecksumsEnabled(void)
Definition: xlog.c:4951
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz last_checksum_failure
Definition: pgstat.h:723
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_db_conflict_all()

Datum pg_stat_get_db_conflict_all ( PG_FUNCTION_ARGS  )

Definition at line 1530 of file pgstatfuncs.c.

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

1531 {
1532  Oid dbid = PG_GETARG_OID(0);
1533  int64 result;
1534  PgStat_StatDBEntry *dbentry;
1535 
1536  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1537  result = 0;
1538  else
1539  result = (int64) (dbentry->n_conflict_tablespace +
1540  dbentry->n_conflict_lock +
1541  dbentry->n_conflict_snapshot +
1542  dbentry->n_conflict_bufferpin +
1543  dbentry->n_conflict_startup_deadlock);
1544 
1545  PG_RETURN_INT64(result);
1546 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:717
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:718
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:714
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:716
PgStat_Counter n_conflict_lock
Definition: pgstat.h:715

◆ pg_stat_get_db_conflict_bufferpin()

Datum pg_stat_get_db_conflict_bufferpin ( PG_FUNCTION_ARGS  )

Definition at line 1500 of file pgstatfuncs.c.

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

1501 {
1502  Oid dbid = PG_GETARG_OID(0);
1503  int64 result;
1504  PgStat_StatDBEntry *dbentry;
1505 
1506  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1507  result = 0;
1508  else
1509  result = (int64) (dbentry->n_conflict_bufferpin);
1510 
1511  PG_RETURN_INT64(result);
1512 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:717
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_conflict_lock()

Datum pg_stat_get_db_conflict_lock ( PG_FUNCTION_ARGS  )

Definition at line 1470 of file pgstatfuncs.c.

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

1471 {
1472  Oid dbid = PG_GETARG_OID(0);
1473  int64 result;
1474  PgStat_StatDBEntry *dbentry;
1475 
1476  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1477  result = 0;
1478  else
1479  result = (int64) (dbentry->n_conflict_lock);
1480 
1481  PG_RETURN_INT64(result);
1482 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_conflict_lock
Definition: pgstat.h:715

◆ pg_stat_get_db_conflict_snapshot()

Datum pg_stat_get_db_conflict_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1485 of file pgstatfuncs.c.

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

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_conflict_snapshot);
1495 
1496  PG_RETURN_INT64(result);
1497 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:716

◆ pg_stat_get_db_conflict_startup_deadlock()

Datum pg_stat_get_db_conflict_startup_deadlock ( PG_FUNCTION_ARGS  )

Definition at line 1515 of file pgstatfuncs.c.

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

1516 {
1517  Oid dbid = PG_GETARG_OID(0);
1518  int64 result;
1519  PgStat_StatDBEntry *dbentry;
1520 
1521  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1522  result = 0;
1523  else
1524  result = (int64) (dbentry->n_conflict_startup_deadlock);
1525 
1526  PG_RETURN_INT64(result);
1527 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:718
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_conflict_tablespace()

Datum pg_stat_get_db_conflict_tablespace ( PG_FUNCTION_ARGS  )

Definition at line 1455 of file pgstatfuncs.c.

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

1456 {
1457  Oid dbid = PG_GETARG_OID(0);
1458  int64 result;
1459  PgStat_StatDBEntry *dbentry;
1460 
1461  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1462  result = 0;
1463  else
1464  result = (int64) (dbentry->n_conflict_tablespace);
1465 
1466  PG_RETURN_INT64(result);
1467 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:714
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_deadlocks()

Datum pg_stat_get_db_deadlocks ( PG_FUNCTION_ARGS  )

Definition at line 1549 of file pgstatfuncs.c.

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

1550 {
1551  Oid dbid = PG_GETARG_OID(0);
1552  int64 result;
1553  PgStat_StatDBEntry *dbentry;
1554 
1555  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1556  result = 0;
1557  else
1558  result = (int64) (dbentry->n_deadlocks);
1559 
1560  PG_RETURN_INT64(result);
1561 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_deadlocks
Definition: pgstat.h:721

◆ pg_stat_get_db_idle_in_transaction_time()

Datum pg_stat_get_db_idle_in_transaction_time ( PG_FUNCTION_ARGS  )

Definition at line 1663 of file pgstatfuncs.c.

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

1664 {
1665  Oid dbid = PG_GETARG_OID(0);
1666  double result = 0.0;
1667  PgStat_StatDBEntry *dbentry;
1668 
1669  /* convert counter from microsec to millisec for display */
1670  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1671  result = ((double) dbentry->total_idle_in_xact_time) / 1000.0;
1672 
1673  PG_RETURN_FLOAT8(result);
1674 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter total_idle_in_xact_time
Definition: pgstat.h:729

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 1242 of file pgstatfuncs.c.

References PG_GETARG_OID, PG_RETURN_INT32, pgstat_fetch_stat_beentry(), pgstat_fetch_stat_numbackends(), and PgBackendStatus::st_databaseid.

1243 {
1244  Oid dbid = PG_GETARG_OID(0);
1245  int32 result;
1246  int tot_backends = pgstat_fetch_stat_numbackends();
1247  int beid;
1248 
1249  result = 0;
1250  for (beid = 1; beid <= tot_backends; beid++)
1251  {
1252  PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1253 
1254  if (beentry && beentry->st_databaseid == dbid)
1255  result++;
1256  }
1257 
1258  PG_RETURN_INT32(result);
1259 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
Oid st_databaseid
Definition: pgstat.h:1215
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2815
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
Definition: pgstat.c:2774

◆ pg_stat_get_db_session_time()

Datum pg_stat_get_db_session_time ( PG_FUNCTION_ARGS  )

Definition at line 1635 of file pgstatfuncs.c.

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

1636 {
1637  Oid dbid = PG_GETARG_OID(0);
1638  double result = 0.0;
1639  PgStat_StatDBEntry *dbentry;
1640 
1641  /* convert counter from microsec to millisec for display */
1642  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1643  result = ((double) dbentry->total_session_time) / 1000.0;
1644 
1645  PG_RETURN_FLOAT8(result);
1646 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter total_session_time
Definition: pgstat.h:727

◆ pg_stat_get_db_sessions()

Datum pg_stat_get_db_sessions ( PG_FUNCTION_ARGS  )

Definition at line 1677 of file pgstatfuncs.c.

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

1678 {
1679  Oid dbid = PG_GETARG_OID(0);
1680  int64 result = 0;
1681  PgStat_StatDBEntry *dbentry;
1682 
1683  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1684  result = (int64) (dbentry->n_sessions);
1685 
1686  PG_RETURN_INT64(result);
1687 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_sessions
Definition: pgstat.h:726

◆ pg_stat_get_db_sessions_abandoned()

Datum pg_stat_get_db_sessions_abandoned ( PG_FUNCTION_ARGS  )

Definition at line 1690 of file pgstatfuncs.c.

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

1691 {
1692  Oid dbid = PG_GETARG_OID(0);
1693  int64 result = 0;
1694  PgStat_StatDBEntry *dbentry;
1695 
1696  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1697  result = (int64) (dbentry->n_sessions_abandoned);
1698 
1699  PG_RETURN_INT64(result);
1700 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_abandoned
Definition: pgstat.h:730
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_sessions_fatal()

Datum pg_stat_get_db_sessions_fatal ( PG_FUNCTION_ARGS  )

Definition at line 1703 of file pgstatfuncs.c.

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

1704 {
1705  Oid dbid = PG_GETARG_OID(0);
1706  int64 result = 0;
1707  PgStat_StatDBEntry *dbentry;
1708 
1709  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1710  result = (int64) (dbentry->n_sessions_fatal);
1711 
1712  PG_RETURN_INT64(result);
1713 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_fatal
Definition: pgstat.h:731
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_sessions_killed()

Datum pg_stat_get_db_sessions_killed ( PG_FUNCTION_ARGS  )

Definition at line 1716 of file pgstatfuncs.c.

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

1717 {
1718  Oid dbid = PG_GETARG_OID(0);
1719  int64 result = 0;
1720  PgStat_StatDBEntry *dbentry;
1721 
1722  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1723  result = (int64) (dbentry->n_sessions_killed);
1724 
1725  PG_RETURN_INT64(result);
1726 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_killed
Definition: pgstat.h:732
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_stat_reset_time()

Datum pg_stat_get_db_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1406 of file pgstatfuncs.c.

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

1407 {
1408  Oid dbid = PG_GETARG_OID(0);
1409  TimestampTz result;
1410  PgStat_StatDBEntry *dbentry;
1411 
1412  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1413  result = 0;
1414  else
1415  result = dbentry->stat_reset_timestamp;
1416 
1417  if (result == 0)
1418  PG_RETURN_NULL();
1419  else
1420  PG_RETURN_TIMESTAMPTZ(result);
1421 }
int64 TimestampTz
Definition: timestamp.h:39
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
TimestampTz stat_reset_timestamp
Definition: pgstat.h:734
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_db_temp_bytes()

Datum pg_stat_get_db_temp_bytes ( PG_FUNCTION_ARGS  )

Definition at line 1440 of file pgstatfuncs.c.

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

1441 {
1442  Oid dbid = PG_GETARG_OID(0);
1443  int64 result;
1444  PgStat_StatDBEntry *dbentry;
1445 
1446  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1447  result = 0;
1448  else
1449  result = dbentry->n_temp_bytes;
1450 
1451  PG_RETURN_INT64(result);
1452 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_temp_bytes
Definition: pgstat.h:720

◆ pg_stat_get_db_temp_files()

Datum pg_stat_get_db_temp_files ( PG_FUNCTION_ARGS  )

Definition at line 1424 of file pgstatfuncs.c.

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

1425 {
1426  Oid dbid = PG_GETARG_OID(0);
1427  int64 result;
1428  PgStat_StatDBEntry *dbentry;
1429 
1430  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1431  result = 0;
1432  else
1433  result = dbentry->n_temp_files;
1434 
1435  PG_RETURN_INT64(result);
1436 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_temp_files
Definition: pgstat.h:719
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_tuples_deleted()

Datum pg_stat_get_db_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 1391 of file pgstatfuncs.c.

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

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_tuples_deleted);
1401 
1402  PG_RETURN_INT64(result);
1403 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:712

◆ pg_stat_get_db_tuples_fetched()

Datum pg_stat_get_db_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1343 of file pgstatfuncs.c.

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

1344 {
1345  Oid dbid = PG_GETARG_OID(0);
1346  int64 result;
1347  PgStat_StatDBEntry *dbentry;
1348 
1349  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1350  result = 0;
1351  else
1352  result = (int64) (dbentry->n_tuples_fetched);
1353 
1354  PG_RETURN_INT64(result);
1355 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_tuples_fetched
Definition: pgstat.h:709

◆ pg_stat_get_db_tuples_inserted()

Datum pg_stat_get_db_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1359 of file pgstatfuncs.c.

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

1360 {
1361  Oid dbid = PG_GETARG_OID(0);
1362  int64 result;
1363  PgStat_StatDBEntry *dbentry;
1364 
1365  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1366  result = 0;
1367  else
1368  result = (int64) (dbentry->n_tuples_inserted);
1369 
1370  PG_RETURN_INT64(result);
1371 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:710
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_tuples_returned()

Datum pg_stat_get_db_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 1327 of file pgstatfuncs.c.

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

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_returned);
1337 
1338  PG_RETURN_INT64(result);
1339 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_tuples_returned
Definition: pgstat.h:708
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_tuples_updated()

Datum pg_stat_get_db_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 1375 of file pgstatfuncs.c.

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

1376 {
1377  Oid dbid = PG_GETARG_OID(0);
1378  int64 result;
1379  PgStat_StatDBEntry *dbentry;
1380 
1381  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1382  result = 0;
1383  else
1384  result = (int64) (dbentry->n_tuples_updated);
1385 
1386  PG_RETURN_INT64(result);
1387 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_tuples_updated
Definition: pgstat.h:711

◆ pg_stat_get_db_xact_commit()

Datum pg_stat_get_db_xact_commit ( PG_FUNCTION_ARGS  )

Definition at line 1263 of file pgstatfuncs.c.

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

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_xact_commit);
1273 
1274  PG_RETURN_INT64(result);
1275 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_xact_commit
Definition: pgstat.h:704
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_db_xact_rollback()

Datum pg_stat_get_db_xact_rollback ( PG_FUNCTION_ARGS  )

Definition at line 1279 of file pgstatfuncs.c.

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

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_xact_rollback);
1289 
1290  PG_RETURN_INT64(result);
1291 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2659
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_xact_rollback
Definition: pgstat.h:705

◆ pg_stat_get_dead_tuples()

Datum pg_stat_get_dead_tuples ( PG_FUNCTION_ARGS  )

Definition at line 170 of file pgstatfuncs.c.

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

171 {
172  Oid relid = PG_GETARG_OID(0);
173  int64 result;
174  PgStat_StatTabEntry *tabentry;
175 
176  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
177  result = 0;
178  else
179  result = (int64) (tabentry->n_dead_tuples);
180 
181  PG_RETURN_INT64(result);
182 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_dead_tuples
Definition: pgstat.h:765
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_function_calls()

Datum pg_stat_get_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 381 of file pgstatfuncs.c.

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

382 {
383  Oid funcid = PG_GETARG_OID(0);
384  PgStat_StatFuncEntry *funcentry;
385 
386  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
387  PG_RETURN_NULL();
388  PG_RETURN_INT64(funcentry->f_numcalls);
389 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2742
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter f_numcalls
Definition: pgstat.h:791
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_function_self_time()

Datum pg_stat_get_function_self_time ( PG_FUNCTION_ARGS  )

Definition at line 404 of file pgstatfuncs.c.

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

405 {
406  Oid funcid = PG_GETARG_OID(0);
407  PgStat_StatFuncEntry *funcentry;
408 
409  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
410  PG_RETURN_NULL();
411  /* convert counter from microsec to millisec for display */
412  PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
413 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2742
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter f_self_time
Definition: pgstat.h:794
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_function_total_time()

Datum pg_stat_get_function_total_time ( PG_FUNCTION_ARGS  )

Definition at line 392 of file pgstatfuncs.c.

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

393 {
394  Oid funcid = PG_GETARG_OID(0);
395  PgStat_StatFuncEntry *funcentry;
396 
397  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
398  PG_RETURN_NULL();
399  /* convert counter from microsec to millisec for display */
400  PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
401 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2742
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter f_total_time
Definition: pgstat.h:793
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_ins_since_vacuum()

Datum pg_stat_get_ins_since_vacuum ( PG_FUNCTION_ARGS  )

Definition at line 202 of file pgstatfuncs.c.

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

203 {
204  Oid relid = PG_GETARG_OID(0);
205  int64 result;
206  PgStat_StatTabEntry *tabentry;
207 
208  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
209  result = 0;
210  else
211  result = (int64) (tabentry->inserts_since_vacuum);
212 
213  PG_RETURN_INT64(result);
214 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:767
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_last_analyze_time()

Datum pg_stat_get_last_analyze_time ( PG_FUNCTION_ARGS  )

Definition at line 285 of file pgstatfuncs.c.

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

286 {
287  Oid relid = PG_GETARG_OID(0);
288  TimestampTz result;
289  PgStat_StatTabEntry *tabentry;
290 
291  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
292  result = 0;
293  else
294  result = tabentry->analyze_timestamp;
295 
296  if (result == 0)
297  PG_RETURN_NULL();
298  else
299  PG_RETURN_TIMESTAMPTZ(result);
300 }
int64 TimestampTz
Definition: timestamp.h:39
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
TimestampTz analyze_timestamp
Definition: pgstat.h:776
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_last_autoanalyze_time()

Datum pg_stat_get_last_autoanalyze_time ( PG_FUNCTION_ARGS  )

Definition at line 303 of file pgstatfuncs.c.

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

304 {
305  Oid relid = PG_GETARG_OID(0);
306  TimestampTz result;
307  PgStat_StatTabEntry *tabentry;
308 
309  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
310  result = 0;
311  else
312  result = tabentry->autovac_analyze_timestamp;
313 
314  if (result == 0)
315  PG_RETURN_NULL();
316  else
317  PG_RETURN_TIMESTAMPTZ(result);
318 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:778
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_last_autovacuum_time()

Datum pg_stat_get_last_autovacuum_time ( PG_FUNCTION_ARGS  )

Definition at line 267 of file pgstatfuncs.c.

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

268 {
269  Oid relid = PG_GETARG_OID(0);
270  TimestampTz result;
271  PgStat_StatTabEntry *tabentry;
272 
273  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
274  result = 0;
275  else
276  result = tabentry->autovac_vacuum_timestamp;
277 
278  if (result == 0)
279  PG_RETURN_NULL();
280  else
281  PG_RETURN_TIMESTAMPTZ(result);
282 }
int64 TimestampTz
Definition: timestamp.h:39
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:774
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_last_vacuum_time()

Datum pg_stat_get_last_vacuum_time ( PG_FUNCTION_ARGS  )

Definition at line 249 of file pgstatfuncs.c.

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

250 {
251  Oid relid = PG_GETARG_OID(0);
252  TimestampTz result;
253  PgStat_StatTabEntry *tabentry;
254 
255  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
256  result = 0;
257  else
258  result = tabentry->vacuum_timestamp;
259 
260  if (result == 0)
261  PG_RETURN_NULL();
262  else
263  PG_RETURN_TIMESTAMPTZ(result);
264 }
int64 TimestampTz
Definition: timestamp.h:39
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz vacuum_timestamp
Definition: pgstat.h:772
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_live_tuples()

Datum pg_stat_get_live_tuples ( PG_FUNCTION_ARGS  )

Definition at line 154 of file pgstatfuncs.c.

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

155 {
156  Oid relid = PG_GETARG_OID(0);
157  int64 result;
158  PgStat_StatTabEntry *tabentry;
159 
160  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
161  result = 0;
162  else
163  result = (int64) (tabentry->n_live_tuples);
164 
165  PG_RETURN_INT64(result);
166 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_live_tuples
Definition: pgstat.h:764
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_mod_since_analyze()

Datum pg_stat_get_mod_since_analyze ( PG_FUNCTION_ARGS  )

Definition at line 186 of file pgstatfuncs.c.

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

187 {
188  Oid relid = PG_GETARG_OID(0);
189  int64 result;
190  PgStat_StatTabEntry *tabentry;
191 
192  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
193  result = 0;
194  else
195  result = (int64) (tabentry->changes_since_analyze);
196 
197  PG_RETURN_INT64(result);
198 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter changes_since_analyze
Definition: pgstat.h:766

◆ pg_stat_get_numscans()

Datum pg_stat_get_numscans ( PG_FUNCTION_ARGS  )

Definition at line 42 of file pgstatfuncs.c.

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

43 {
44  Oid relid = PG_GETARG_OID(0);
45  int64 result;
46  PgStat_StatTabEntry *tabentry;
47 
48  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
49  result = 0;
50  else
51  result = (int64) (tabentry->numscans);
52 
53  PG_RETURN_INT64(result);
54 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter numscans
Definition: pgstat.h:754
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_progress_info()

Datum pg_stat_get_progress_info ( PG_FUNCTION_ARGS  )

Definition at line 459 of file pgstatfuncs.c.

References ReturnSetInfo::allowedModes, LocalPgBackendStatus::backendStatus, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), HAS_PGSTAT_PERMISSIONS, i, Int32GetDatum, Int64GetDatum(), IsA, MemoryContextSwitchTo(), MemSet, 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::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, 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_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

460 {
461 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
462  int num_backends = pgstat_fetch_stat_numbackends();
463  int curr_backend;
464  char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
465  ProgressCommandType cmdtype;
466  TupleDesc tupdesc;
467  Tuplestorestate *tupstore;
468  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
469  MemoryContext per_query_ctx;
470  MemoryContext oldcontext;
471 
472  /* check to see if caller supports us returning a tuplestore */
473  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
474  ereport(ERROR,
475  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
476  errmsg("set-valued function called in context that cannot accept a set")));
477  if (!(rsinfo->allowedModes & SFRM_Materialize))
478  ereport(ERROR,
479  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
480  errmsg("materialize mode required, but it is not allowed in this context")));
481 
482  /* Build a tuple descriptor for our result type */
483  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
484  elog(ERROR, "return type must be a row type");
485 
486  /* Translate command name into command type code. */
487  if (pg_strcasecmp(cmd, "VACUUM") == 0)
488  cmdtype = PROGRESS_COMMAND_VACUUM;
489  else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
490  cmdtype = PROGRESS_COMMAND_ANALYZE;
491  else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
492  cmdtype = PROGRESS_COMMAND_CLUSTER;
493  else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
495  else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
496  cmdtype = PROGRESS_COMMAND_BASEBACKUP;
497  else if (pg_strcasecmp(cmd, "COPY") == 0)
498  cmdtype = PROGRESS_COMMAND_COPY;
499  else
500  ereport(ERROR,
501  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
502  errmsg("invalid command name: \"%s\"", cmd)));
503 
504  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
505  oldcontext = MemoryContextSwitchTo(per_query_ctx);
506 
507  tupstore = tuplestore_begin_heap(true, false, work_mem);
508  rsinfo->returnMode = SFRM_Materialize;
509  rsinfo->setResult = tupstore;
510  rsinfo->setDesc = tupdesc;
511  MemoryContextSwitchTo(oldcontext);
512 
513  /* 1-based index */
514  for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
515  {
516  LocalPgBackendStatus *local_beentry;
517  PgBackendStatus *beentry;
519  bool nulls[PG_STAT_GET_PROGRESS_COLS];
520  int i;
521 
522  MemSet(values, 0, sizeof(values));
523  MemSet(nulls, 0, sizeof(nulls));
524 
525  local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
526 
527  if (!local_beentry)
528  continue;
529 
530  beentry = &local_beentry->backendStatus;
531 
532  /*
533  * Report values for only those backends which are running the given
534  * command.
535  */
536  if (!beentry || beentry->st_progress_command != cmdtype)
537  continue;
538 
539  /* Value available to all callers */
540  values[0] = Int32GetDatum(beentry->st_procpid);
541  values[1] = ObjectIdGetDatum(beentry->st_databaseid);
542 
543  /* show rest of the values including relid only to role members */
544  if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
545  {
546  values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
547  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
548  values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
549  }
550  else
551  {
552  nulls[2] = true;
553  for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
554  nulls[i + 3] = true;
555  }
556 
557  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
558  }
559 
560  /* clean up and return the tuplestore */
561  tuplestore_donestoring(tupstore);
562 
563  return (Datum) 0;
564 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Oid st_progress_command_target
Definition: pgstat.h:1253
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:694
#define MemSet(start, val, len)
Definition: c.h:1008
ProgressCommandType st_progress_command
Definition: pgstat.h:1252
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
LocalPgBackendStatus * pgstat_fetch_stat_local_beentry(int beid)
Definition: pgstat.c:2796
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
Oid st_databaseid
Definition: pgstat.h:1215
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:1118
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:36
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
PgBackendStatus backendStatus
Definition: pgstat.h:1331
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:122
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
int pgstat_fetch_stat_numbackends(void)
Definition: pgstat.c:2815
ProgressCommandType
Definition: pgstat.h:1107
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1254
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
char * text_to_cstring(const text *t)
Definition: varlena.c:222
ExprContext * econtext
Definition: execnodes.h:302
#define Int32GetDatum(X)
Definition: postgres.h:479
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
#define PG_STAT_GET_PROGRESS_COLS
int i

◆ pg_stat_get_replication_slots()

Datum pg_stat_get_replication_slots ( PG_FUNCTION_ARGS  )

Definition at line 2267 of file pgstatfuncs.c.

References ReturnSetInfo::allowedModes, cstring_to_text(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), i, Int64GetDatum(), IsA, MemoryContextSwitchTo(), MemSet, PG_STAT_GET_REPLICATION_SLOT_COLS, pgstat_fetch_replslot(), PointerGetDatum, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, PgStat_ReplSlotStats::slotname, PgStat_ReplSlotStats::spill_bytes, PgStat_ReplSlotStats::spill_count, PgStat_ReplSlotStats::spill_txns, PgStat_ReplSlotStats::stat_reset_timestamp, PgStat_ReplSlotStats::stream_bytes, PgStat_ReplSlotStats::stream_count, PgStat_ReplSlotStats::stream_txns, TimestampTzGetDatum, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

2268 {
2269 #define PG_STAT_GET_REPLICATION_SLOT_COLS 8
2270  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2271  TupleDesc tupdesc;
2272  Tuplestorestate *tupstore;
2273  MemoryContext per_query_ctx;
2274  MemoryContext oldcontext;
2275  PgStat_ReplSlotStats *slotstats;
2276  int nstats;
2277  int i;
2278 
2279  /* check to see if caller supports us returning a tuplestore */
2280  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
2281  ereport(ERROR,
2282  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2283  errmsg("set-valued function called in context that cannot accept a set")));
2284  if (!(rsinfo->allowedModes & SFRM_Materialize))
2285  ereport(ERROR,
2286  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2287  errmsg("materialize mode required, but it is not allowed in this context")));
2288 
2289  /* Build a tuple descriptor for our result type */
2290  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
2291  elog(ERROR, "return type must be a row type");
2292 
2293  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
2294  oldcontext = MemoryContextSwitchTo(per_query_ctx);
2295 
2296  tupstore = tuplestore_begin_heap(true, false, work_mem);
2297  rsinfo->returnMode = SFRM_Materialize;
2298  rsinfo->setResult = tupstore;
2299  rsinfo->setDesc = tupdesc;
2300 
2301  MemoryContextSwitchTo(oldcontext);
2302 
2303  slotstats = pgstat_fetch_replslot(&nstats);
2304  for (i = 0; i < nstats; i++)
2305  {
2308  PgStat_ReplSlotStats *s = &(slotstats[i]);
2309 
2310  MemSet(values, 0, sizeof(values));
2311  MemSet(nulls, 0, sizeof(nulls));
2312 
2313  values[0] = PointerGetDatum(cstring_to_text(s->slotname));
2314  values[1] = Int64GetDatum(s->spill_txns);
2315  values[2] = Int64GetDatum(s->spill_count);
2316  values[3] = Int64GetDatum(s->spill_bytes);
2317  values[4] = Int64GetDatum(s->stream_txns);
2318  values[5] = Int64GetDatum(s->stream_count);
2319  values[6] = Int64GetDatum(s->stream_bytes);
2320 
2321  if (s->stat_reset_timestamp == 0)
2322  nulls[7] = true;
2323  else
2324  values[7] = TimestampTzGetDatum(s->stat_reset_timestamp);
2325 
2326  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2327  }
2328 
2329  tuplestore_donestoring(tupstore);
2330 
2331  return (Datum) 0;
2332 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
PgStat_Counter stream_count
Definition: pgstat.h:870
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define PG_STAT_GET_REPLICATION_SLOT_COLS
#define PointerGetDatum(X)
Definition: postgres.h:556
TimestampTz stat_reset_timestamp
Definition: pgstat.h:872
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:694
#define MemSet(start, val, len)
Definition: c.h:1008
PgStat_Counter spill_txns
Definition: pgstat.h:866
PgStat_Counter spill_bytes
Definition: pgstat.h:868
PgStat_Counter stream_bytes
Definition: pgstat.h:871
#define ERROR
Definition: elog.h:45
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:122
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
text * cstring_to_text(const char *s)
Definition: varlena.c:189
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
PgStat_ReplSlotStats * pgstat_fetch_replslot(int *nslots_p)
Definition: pgstat.c:2897
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
int i
char slotname[NAMEDATALEN]
Definition: pgstat.h:865
PgStat_Counter spill_count
Definition: pgstat.h:867
PgStat_Counter stream_txns
Definition: pgstat.h:869

◆ pg_stat_get_slru()

Datum pg_stat_get_slru ( PG_FUNCTION_ARGS  )

Definition at line 1853 of file pgstatfuncs.c.

References ReturnSetInfo::allowedModes, PgStat_SLRUStats::blocks_exists, PgStat_SLRUStats::blocks_hit, PgStat_SLRUStats::blocks_read, PgStat_SLRUStats::blocks_written, PgStat_SLRUStats::blocks_zeroed, cstring_to_text(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, PgStat_SLRUStats::flush, get_call_result_type(), i, Int64GetDatum(), IsA, MemoryContextSwitchTo(), MemSet, name, PG_STAT_GET_SLRU_COLS, pgstat_fetch_slru(), pgstat_slru_name(), PointerGetDatum, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, PgStat_SLRUStats::stat_reset_timestamp, TimestampTzGetDatum, PgStat_SLRUStats::truncate, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

1854 {
1855 #define PG_STAT_GET_SLRU_COLS 9
1856  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1857  TupleDesc tupdesc;
1858  Tuplestorestate *tupstore;
1859  MemoryContext per_query_ctx;
1860  MemoryContext oldcontext;
1861  int i;
1862  PgStat_SLRUStats *stats;
1863 
1864  /* check to see if caller supports us returning a tuplestore */
1865  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1866  ereport(ERROR,
1867  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1868  errmsg("set-valued function called in context that cannot accept a set")));
1869  if (!(rsinfo->allowedModes & SFRM_Materialize))
1870  ereport(ERROR,
1871  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1872  errmsg("materialize mode required, but it is not allowed in this context")));
1873 
1874  /* Build a tuple descriptor for our result type */
1875  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1876  elog(ERROR, "return type must be a row type");
1877 
1878  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1879  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1880 
1881  tupstore = tuplestore_begin_heap(true, false, work_mem);
1882  rsinfo->returnMode = SFRM_Materialize;
1883  rsinfo->setResult = tupstore;
1884  rsinfo->setDesc = tupdesc;
1885 
1886  MemoryContextSwitchTo(oldcontext);
1887 
1888  /* request SLRU stats from the stat collector */
1889  stats = pgstat_fetch_slru();
1890 
1891  for (i = 0;; i++)
1892  {
1893  /* for each row */
1895  bool nulls[PG_STAT_GET_SLRU_COLS];
1896  PgStat_SLRUStats stat = stats[i];
1897  const char *name;
1898 
1899  name = pgstat_slru_name(i);
1900 
1901  if (!name)
1902  break;
1903 
1904  MemSet(values, 0, sizeof(values));
1905  MemSet(nulls, 0, sizeof(nulls));
1906 
1907  values[0] = PointerGetDatum(cstring_to_text(name));
1908  values[1] = Int64GetDatum(stat.blocks_zeroed);
1909  values[2] = Int64GetDatum(stat.blocks_hit);
1910  values[3] = Int64GetDatum(stat.blocks_read);
1911  values[4] = Int64GetDatum(stat.blocks_written);
1912  values[5] = Int64GetDatum(stat.blocks_exists);
1913  values[6] = Int64GetDatum(stat.flush);
1914  values[7] = Int64GetDatum(stat.truncate);
1915  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1916 
1917  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1918  }
1919 
1920  /* clean up and return the tuplestore */
1921  tuplestore_donestoring(tupstore);
1922 
1923  return (Datum) 0;
1924 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define PG_STAT_GET_SLRU_COLS
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define PointerGetDatum(X)
Definition: postgres.h:556
TimestampTz stat_reset_timestamp
Definition: pgstat.h:857
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:694
#define MemSet(start, val, len)
Definition: c.h:1008
PgStat_Counter blocks_exists
Definition: pgstat.h:854
#define ERROR
Definition: elog.h:45
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter blocks_read
Definition: pgstat.h:852
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
PgStat_Counter blocks_hit
Definition: pgstat.h:851
PgStat_Counter blocks_zeroed
Definition: pgstat.h:850
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:122
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
text * cstring_to_text(const char *s)
Definition: varlena.c:189
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat.c:2880
PgStat_Counter flush
Definition: pgstat.h:855
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
const char * name
Definition: encode.c:515
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
int i
const char * pgstat_slru_name(int slru_idx)
Definition: pgstat.c:7339
PgStat_Counter truncate
Definition: pgstat.h:856
PgStat_Counter blocks_written
Definition: pgstat.h:853

◆ pg_stat_get_snapshot_timestamp()

Datum pg_stat_get_snapshot_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2115 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_global().

2116 {
2117  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
2118 }
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2848
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ pg_stat_get_tuples_deleted()

Datum pg_stat_get_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 122 of file pgstatfuncs.c.

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

123 {
124  Oid relid = PG_GETARG_OID(0);
125  int64 result;
126  PgStat_StatTabEntry *tabentry;
127 
128  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
129  result = 0;
130  else
131  result = (int64) (tabentry->tuples_deleted);
132 
133  PG_RETURN_INT64(result);
134 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_deleted
Definition: pgstat.h:761

◆ pg_stat_get_tuples_fetched()

Datum pg_stat_get_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 74 of file pgstatfuncs.c.

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

75 {
76  Oid relid = PG_GETARG_OID(0);
77  int64 result;
78  PgStat_StatTabEntry *tabentry;
79 
80  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
81  result = 0;
82  else
83  result = (int64) (tabentry->tuples_fetched);
84 
85  PG_RETURN_INT64(result);
86 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_fetched
Definition: pgstat.h:757

◆ pg_stat_get_tuples_hot_updated()

Datum pg_stat_get_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 138 of file pgstatfuncs.c.

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

139 {
140  Oid relid = PG_GETARG_OID(0);
141  int64 result;
142  PgStat_StatTabEntry *tabentry;
143 
144  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
145  result = 0;
146  else
147  result = (int64) (tabentry->tuples_hot_updated);
148 
149  PG_RETURN_INT64(result);
150 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:762
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_tuples_inserted()

Datum pg_stat_get_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 90 of file pgstatfuncs.c.

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

91 {
92  Oid relid = PG_GETARG_OID(0);
93  int64 result;
94  PgStat_StatTabEntry *tabentry;
95 
96  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
97  result = 0;
98  else
99  result = (int64) (tabentry->tuples_inserted);
100 
101  PG_RETURN_INT64(result);
102 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter tuples_inserted
Definition: pgstat.h:759
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_tuples_returned()

Datum pg_stat_get_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 58 of file pgstatfuncs.c.

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

59 {
60  Oid relid = PG_GETARG_OID(0);
61  int64 result;
62  PgStat_StatTabEntry *tabentry;
63 
64  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
65  result = 0;
66  else
67  result = (int64) (tabentry->tuples_returned);
68 
69  PG_RETURN_INT64(result);
70 }
PgStat_Counter tuples_returned
Definition: pgstat.h:756
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_tuples_updated()

Datum pg_stat_get_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 106 of file pgstatfuncs.c.

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

107 {
108  Oid relid = PG_GETARG_OID(0);
109  int64 result;
110  PgStat_StatTabEntry *tabentry;
111 
112  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
113  result = 0;
114  else
115  result = (int64) (tabentry->tuples_updated);
116 
117  PG_RETURN_INT64(result);
118 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_updated
Definition: pgstat.h:760

◆ pg_stat_get_vacuum_count()

Datum pg_stat_get_vacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 321 of file pgstatfuncs.c.

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

322 {
323  Oid relid = PG_GETARG_OID(0);
324  int64 result;
325  PgStat_StatTabEntry *tabentry;
326 
327  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
328  result = 0;
329  else
330  result = (int64) (tabentry->vacuum_count);
331 
332  PG_RETURN_INT64(result);
333 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter vacuum_count
Definition: pgstat.h:773
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2686
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

Definition at line 1800 of file pgstatfuncs.c.

References BlessTupleDesc(), buf, CreateTemplateTupleDesc(), CStringGetDatum, DirectFunctionCall3, heap_form_tuple(), HeapTupleGetDatum, Int32GetDatum, Int64GetDatum(), MemSet, 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, and PgStat_WalStats::wal_records.

1801 {
1802 #define PG_STAT_GET_WAL_COLS 5
1803  TupleDesc tupdesc;
1805  bool nulls[PG_STAT_GET_WAL_COLS];
1806  char buf[256];
1807  PgStat_WalStats *wal_stats;
1808 
1809  /* Initialise values and NULL flags arrays */
1810  MemSet(values, 0, sizeof(values));
1811  MemSet(nulls, 0, sizeof(nulls));
1812 
1813  /* Initialise attributes information in the tuple descriptor */
1815  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1816  INT8OID, -1, 0);
1817  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1818  INT8OID, -1, 0);
1819  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1820  NUMERICOID, -1, 0);
1821  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1822  INT8OID, -1, 0);
1823  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stats_reset",
1824  TIMESTAMPTZOID, -1, 0);
1825 
1826  BlessTupleDesc(tupdesc);
1827 
1828  /* Get statistics about WAL activity */
1829  wal_stats = pgstat_fetch_stat_wal();
1830 
1831  /* Fill values and NULLs */
1832  values[0] = Int64GetDatum(wal_stats->wal_records);
1833  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1834 
1835  /* Convert to numeric. */
1836  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1837  values[2] = DirectFunctionCall3(numeric_in,
1838  CStringGetDatum(buf),
1839  ObjectIdGetDatum(0),
1840  Int32GetDatum(-1));
1841 
1842  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1843  values[4] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
1844 
1845  /* Returns the record as Datum */
1846  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
1847 }
PgStat_Counter wal_records
Definition: pgstat.h:838
TimestampTz stat_reset_timestamp
Definition: pgstat.h:842
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define PG_STAT_GET_WAL_COLS
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat.c:2864
#define MemSet(start, val, len)
Definition: c.h:1008
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter wal_fpi
Definition: pgstat.h:839
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
static char * buf
Definition: pg_test_fsync.c:68
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:621
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
uint64 wal_bytes
Definition: pgstat.h:840
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define Int32GetDatum(X)
Definition: postgres.h:479
PgStat_Counter wal_buffers_full
Definition: pgstat.h:841
#define snprintf
Definition: port.h:216
int16 AttrNumber
Definition: attnum.h:21
#define UINT64_FORMAT
Definition: c.h:484

◆ pg_stat_get_xact_blocks_fetched()

Datum pg_stat_get_xact_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 2050 of file pgstatfuncs.c.

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

2051 {
2052  Oid relid = PG_GETARG_OID(0);
2053  int64 result;
2054  PgStat_TableStatus *tabentry;
2055 
2056  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2057  result = 0;
2058  else
2059  result = (int64) (tabentry->t_counts.t_blocks_fetched);
2060 
2061  PG_RETURN_INT64(result);
2062 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:131

◆ pg_stat_get_xact_blocks_hit()

Datum pg_stat_get_xact_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 2065 of file pgstatfuncs.c.

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

2066 {
2067  Oid relid = PG_GETARG_OID(0);
2068  int64 result;
2069  PgStat_TableStatus *tabentry;
2070 
2071  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2072  result = 0;
2073  else
2074  result = (int64) (tabentry->t_counts.t_blocks_hit);
2075 
2076  PG_RETURN_INT64(result);
2077 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter t_blocks_hit
Definition: pgstat.h:132

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 2080 of file pgstatfuncs.c.

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

2081 {
2082  Oid funcid = PG_GETARG_OID(0);
2083  PgStat_BackendFunctionEntry *funcentry;
2084 
2085  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2086  PG_RETURN_NULL();
2087  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
2088 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter f_numcalls
Definition: pgstat.h:551
PgStat_FunctionCounts f_counts
Definition: pgstat.h:563
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1906
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_xact_function_self_time()

Datum pg_stat_get_xact_function_self_time ( PG_FUNCTION_ARGS  )

Definition at line 2102 of file pgstatfuncs.c.

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.

2103 {
2104  Oid funcid = PG_GETARG_OID(0);
2105  PgStat_BackendFunctionEntry *funcentry;
2106 
2107  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2108  PG_RETURN_NULL();
2110 }
instr_time f_self_time
Definition: pgstat.h:553
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_FunctionCounts f_counts
Definition: pgstat.h:563
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1906
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_xact_function_total_time()

Datum pg_stat_get_xact_function_total_time ( PG_FUNCTION_ARGS  )

Definition at line 2091 of file pgstatfuncs.c.

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.

2092 {
2093  Oid funcid = PG_GETARG_OID(0);
2094  PgStat_BackendFunctionEntry *funcentry;
2095 
2096  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2097  PG_RETURN_NULL();
2099 }
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
instr_time f_total_time
Definition: pgstat.h:552
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_FunctionCounts f_counts
Definition: pgstat.h:563
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1906
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_get_xact_numscans()

Datum pg_stat_get_xact_numscans ( PG_FUNCTION_ARGS  )

Definition at line 1927 of file pgstatfuncs.c.

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

1928 {
1929  Oid relid = PG_GETARG_OID(0);
1930  int64 result;
1931  PgStat_TableStatus *tabentry;
1932 
1933  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1934  result = 0;
1935  else
1936  result = (int64) (tabentry->t_counts.t_numscans);
1937 
1938  PG_RETURN_INT64(result);
1939 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter t_numscans
Definition: pgstat.h:116

◆ pg_stat_get_xact_tuples_deleted()

Datum pg_stat_get_xact_tuples_deleted ( PG_FUNCTION_ARGS  )

Definition at line 2014 of file pgstatfuncs.c.

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

2015 {
2016  Oid relid = PG_GETARG_OID(0);
2017  int64 result;
2018  PgStat_TableStatus *tabentry;
2020 
2021  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2022  result = 0;
2023  else
2024  {
2025  result = tabentry->t_counts.t_tuples_deleted;
2026  /* live subtransactions' counts aren't in t_tuples_deleted yet */
2027  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2028  result += trans->tuples_deleted;
2029  }
2030 
2031  PG_RETURN_INT64(result);
2032 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:123
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400

◆ pg_stat_get_xact_tuples_fetched()

Datum pg_stat_get_xact_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1957 of file pgstatfuncs.c.

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

1958 {
1959  Oid relid = PG_GETARG_OID(0);
1960  int64 result;
1961  PgStat_TableStatus *tabentry;
1962 
1963  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1964  result = 0;
1965  else
1966  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1967 
1968  PG_RETURN_INT64(result);
1969 }
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:119
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_xact_tuples_hot_updated()

Datum pg_stat_get_xact_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 2035 of file pgstatfuncs.c.

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

2036 {
2037  Oid relid = PG_GETARG_OID(0);
2038  int64 result;
2039  PgStat_TableStatus *tabentry;
2040 
2041  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2042  result = 0;
2043  else
2044  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
2045 
2046  PG_RETURN_INT64(result);
2047 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:124
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_xact_tuples_inserted()

Datum pg_stat_get_xact_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1972 of file pgstatfuncs.c.

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

1973 {
1974  Oid relid = PG_GETARG_OID(0);
1975  int64 result;
1976  PgStat_TableStatus *tabentry;
1978 
1979  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1980  result = 0;
1981  else
1982  {
1983  result = tabentry->t_counts.t_tuples_inserted;
1984  /* live subtransactions' counts aren't in t_tuples_inserted yet */
1985  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
1986  result += trans->tuples_inserted;
1987  }
1988 
1989  PG_RETURN_INT64(result);
1990 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:121

◆ pg_stat_get_xact_tuples_returned()

Datum pg_stat_get_xact_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 1942 of file pgstatfuncs.c.

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

1943 {
1944  Oid relid = PG_GETARG_OID(0);
1945  int64 result;
1946  PgStat_TableStatus *tabentry;
1947 
1948  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1949  result = 0;
1950  else
1951  result = (int64) (tabentry->t_counts.t_tuples_returned);
1952 
1953  PG_RETURN_INT64(result);
1954 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter t_tuples_returned
Definition: pgstat.h:118
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_xact_tuples_updated()

Datum pg_stat_get_xact_tuples_updated ( PG_FUNCTION_ARGS  )

Definition at line 1993 of file pgstatfuncs.c.

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

1994 {
1995  Oid relid = PG_GETARG_OID(0);
1996  int64 result;
1997  PgStat_TableStatus *tabentry;
1999 
2000  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2001  result = 0;
2002  else
2003  {
2004  result = tabentry->t_counts.t_tuples_updated;
2005  /* live subtransactions' counts aren't in t_tuples_updated yet */
2006  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2007  result += trans->tuples_updated;
2008  }
2009 
2010  PG_RETURN_INT64(result);
2011 }
PgStat_Counter tuples_updated
Definition: pgstat.h:186
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:176
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2106
PgStat_Counter t_tuples_updated
Definition: pgstat.h:122
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400

◆ pg_stat_reset()

Datum pg_stat_reset ( PG_FUNCTION_ARGS  )

Definition at line 2132 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_reset_counters().

2133 {
2135 
2136  PG_RETURN_VOID();
2137 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_reset_counters(void)
Definition: pgstat.c:1420

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 2187 of file pgstatfuncs.c.

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_reset_replslot_counter(), and text_to_cstring().

2188 {
2189  char *target = NULL;
2190 
2191  if (!PG_ARGISNULL(0))
2192  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2193 
2195 
2196  PG_RETURN_VOID();
2197 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
void pgstat_reset_replslot_counter(const char *name)
Definition: pgstat.c:1525
char * text_to_cstring(const text *t)
Definition: varlena.c:222

◆ pg_stat_reset_shared()

Datum pg_stat_reset_shared ( PG_FUNCTION_ARGS  )

Definition at line 2141 of file pgstatfuncs.c.

References PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_reset_shared_counters(), and text_to_cstring().

2142 {
2143  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2144 
2146 
2147  PG_RETURN_VOID();
2148 }
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1442
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_VOID()
Definition: fmgr.h:349
char * text_to_cstring(const text *t)
Definition: varlena.c:222

◆ pg_stat_reset_single_function_counters()

Datum pg_stat_reset_single_function_counters ( PG_FUNCTION_ARGS  )

Definition at line 2162 of file pgstatfuncs.c.

References PG_GETARG_OID, PG_RETURN_VOID, pgstat_reset_single_counter(), and RESET_FUNCTION.

2163 {
2164  Oid funcoid = PG_GETARG_OID(0);
2165 
2167 
2168  PG_RETURN_VOID();
2169 }
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
Definition: pgstat.c:1475
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ pg_stat_reset_single_table_counters()

Datum pg_stat_reset_single_table_counters ( PG_FUNCTION_ARGS  )

Definition at line 2152 of file pgstatfuncs.c.

References PG_GETARG_OID, PG_RETURN_VOID, pgstat_reset_single_counter(), and RESET_TABLE.

2153 {
2154  Oid taboid = PG_GETARG_OID(0);
2155 
2157 
2158  PG_RETURN_VOID();
2159 }
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
Definition: pgstat.c:1475
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ pg_stat_reset_slru()

Datum pg_stat_reset_slru ( PG_FUNCTION_ARGS  )

Definition at line 2173 of file pgstatfuncs.c.

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_reset_slru_counter(), and text_to_cstring().

2174 {
2175  char *target = NULL;
2176 
2177  if (!PG_ARGISNULL(0))
2178  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2179 
2180  pgstat_reset_slru_counter(target);
2181 
2182  PG_RETURN_VOID();
2183 }
void pgstat_reset_slru_counter(const char *name)
Definition: pgstat.c:1501
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
char * text_to_cstring(const text *t)
Definition: varlena.c:222

Variable Documentation

◆ bgwriterStats

PgStat_MsgBgWriter bgwriterStats