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 "replication/slot.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(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
 
#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3
 
#define PG_STAT_GET_ACTIVITY_COLS   30
 
#define PG_STAT_GET_WAL_COLS   9
 
#define PG_STAT_GET_SLRU_COLS   9
 
#define PG_STAT_GET_REPLICATION_SLOT_COLS   10
 

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_slot (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   10

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

Referenced by pg_stat_get_wal().

◆ UINT32_ACCESS_ONCE

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

Definition at line 35 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 967 of file pgstatfuncs.c.

References MyProcPid, and PG_RETURN_INT32.

968 {
970 }
int MyProcPid
Definition: globals.c:43
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

◆ pg_stat_clear_snapshot()

Datum pg_stat_clear_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 2141 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_clear_snapshot().

2142 {
2144 
2145  PG_RETURN_VOID();
2146 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4709

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 571 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_issuer_dn, PgBackendSSLStatus::ssl_version, PgBackendStatus::st_activity_raw, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_appname, PgBackendStatus::st_backendType, PgBackendStatus::st_clientaddr, PgBackendStatus::st_clienthostname, PgBackendStatus::st_databaseid, PgBackendStatus::st_gss, PgBackendStatus::st_gssstatus, PgBackendStatus::st_proc_start_timestamp, PgBackendStatus::st_procpid, PgBackendStatus::st_query_id, PgBackendStatus::st_ssl, PgBackendStatus::st_sslstatus, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_userid, PgBackendStatus::st_xact_start_timestamp, STATE_DISABLED, STATE_FASTPATH, STATE_IDLE, STATE_IDLEINTRANSACTION, STATE_IDLEINTRANSACTION_ABORTED, STATE_RUNNING, STATE_UNDEFINED, TimestampTzGetDatum, TransactionIdGetDatum, TransactionIdIsValid, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, UINT32_ACCESS_ONCE, UInt64GetDatum, values, PGPROC::wait_event_info, and work_mem.

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

◆ pg_stat_get_analyze_count()

Datum pg_stat_get_analyze_count ( PG_FUNCTION_ARGS  )

Definition at line 352 of file pgstatfuncs.c.

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

353 {
354  Oid relid = PG_GETARG_OID(0);
355  int64 result;
356  PgStat_StatTabEntry *tabentry;
357 
358  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
359  result = 0;
360  else
361  result = (int64) (tabentry->analyze_count);
362 
363  PG_RETURN_INT64(result);
364 }
PgStat_Counter analyze_count
Definition: pgstat.h:807
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

Definition at line 2244 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.

2245 {
2246  TupleDesc tupdesc;
2247  Datum values[7];
2248  bool nulls[7];
2249  PgStat_ArchiverStats *archiver_stats;
2250 
2251  /* Initialise values and NULL flags arrays */
2252  MemSet(values, 0, sizeof(values));
2253  MemSet(nulls, 0, sizeof(nulls));
2254 
2255  /* Initialise attributes information in the tuple descriptor */
2256  tupdesc = CreateTemplateTupleDesc(7);
2257  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2258  INT8OID, -1, 0);
2259  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2260  TEXTOID, -1, 0);
2261  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2262  TIMESTAMPTZOID, -1, 0);
2263  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2264  INT8OID, -1, 0);
2265  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2266  TEXTOID, -1, 0);
2267  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2268  TIMESTAMPTZOID, -1, 0);
2269  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2270  TIMESTAMPTZOID, -1, 0);
2271 
2272  BlessTupleDesc(tupdesc);
2273 
2274  /* Get statistics about the archiver process */
2275  archiver_stats = pgstat_fetch_stat_archiver();
2276 
2277  /* Fill values and NULLs */
2278  values[0] = Int64GetDatum(archiver_stats->archived_count);
2279  if (*(archiver_stats->last_archived_wal) == '\0')
2280  nulls[1] = true;
2281  else
2282  values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2283 
2284  if (archiver_stats->last_archived_timestamp == 0)
2285  nulls[2] = true;
2286  else
2287  values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp);
2288 
2289  values[3] = Int64GetDatum(archiver_stats->failed_count);
2290  if (*(archiver_stats->last_failed_wal) == '\0')
2291  nulls[4] = true;
2292  else
2293  values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2294 
2295  if (archiver_stats->last_failed_timestamp == 0)
2296  nulls[5] = true;
2297  else
2298  values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2299 
2300  if (archiver_stats->stat_reset_timestamp == 0)
2301  nulls[6] = true;
2302  else
2303  values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2304 
2305  /* Returns the record as Datum */
2306  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2307 }
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:838
PgStat_Counter archived_count
Definition: pgstat.h:833
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
#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:2826
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TimestampTz last_failed_timestamp
Definition: pgstat.h:840
PgStat_Counter failed_count
Definition: pgstat.h:837
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:166
TimestampTz stat_reset_timestamp
Definition: pgstat.h:841
TimestampTz last_archived_timestamp
Definition: pgstat.h:836
#define CStringGetTextDatum(s)
Definition: builtins.h:82
int16 AttrNumber
Definition: attnum.h:21
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:834

◆ pg_stat_get_autoanalyze_count()

Datum pg_stat_get_autoanalyze_count ( PG_FUNCTION_ARGS  )

Definition at line 367 of file pgstatfuncs.c.

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

368 {
369  Oid relid = PG_GETARG_OID(0);
370  int64 result;
371  PgStat_StatTabEntry *tabentry;
372 
373  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
374  result = 0;
375  else
376  result = (int64) (tabentry->autovac_analyze_count);
377 
378  PG_RETURN_INT64(result);
379 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:809

◆ pg_stat_get_autovacuum_count()

Datum pg_stat_get_autovacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 337 of file pgstatfuncs.c.

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

338 {
339  Oid relid = PG_GETARG_OID(0);
340  int64 result;
341  PgStat_StatTabEntry *tabentry;
342 
343  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
344  result = 0;
345  else
346  result = (int64) (tabentry->autovac_vacuum_count);
347 
348  PG_RETURN_INT64(result);
349 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:805

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

Definition at line 1013 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.

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

◆ pg_stat_get_backend_activity_start()

Datum pg_stat_get_backend_activity_start ( PG_FUNCTION_ARGS  )

Definition at line 1081 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.

1082 {
1083  int32 beid = PG_GETARG_INT32(0);
1084  TimestampTz result;
1085  PgBackendStatus *beentry;
1086 
1087  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1088  PG_RETURN_NULL();
1089 
1090  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1091  PG_RETURN_NULL();
1092 
1093  result = beentry->st_activity_start_timestamp;
1094 
1095  /*
1096  * No time recorded for start of current query -- this is the case if the
1097  * user hasn't enabled query-level stats collection.
1098  */
1099  if (result == 0)
1100  PG_RETURN_NULL();
1101 
1102  PG_RETURN_TIMESTAMPTZ(result);
1103 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
TimestampTz st_activity_start_timestamp
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:37
#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 1151 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.

1152 {
1153  int32 beid = PG_GETARG_INT32(0);
1154  PgBackendStatus *beentry;
1155  SockAddr zero_clientaddr;
1156  char remote_host[NI_MAXHOST];
1157  int ret;
1158 
1159  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1160  PG_RETURN_NULL();
1161 
1162  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1163  PG_RETURN_NULL();
1164 
1165  /* A zeroed client addr means we don't know */
1166  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1167  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1168  sizeof(zero_clientaddr)) == 0)
1169  PG_RETURN_NULL();
1170 
1171  switch (beentry->st_clientaddr.addr.ss_family)
1172  {
1173  case AF_INET:
1174 #ifdef HAVE_IPV6
1175  case AF_INET6:
1176 #endif
1177  break;
1178  default:
1179  PG_RETURN_NULL();
1180  }
1181 
1182  remote_host[0] = '\0';
1183  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1184  beentry->st_clientaddr.salen,
1185  remote_host, sizeof(remote_host),
1186  NULL, 0,
1188  if (ret != 0)
1189  PG_RETURN_NULL();
1190 
1191  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1192 
1194  CStringGetDatum(remote_host)));
1195 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
SockAddr st_clientaddr
#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:622
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
#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
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 1198 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.

1199 {
1200  int32 beid = PG_GETARG_INT32(0);
1201  PgBackendStatus *beentry;
1202  SockAddr zero_clientaddr;
1203  char remote_port[NI_MAXSERV];
1204  int ret;
1205 
1206  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1207  PG_RETURN_NULL();
1208 
1209  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1210  PG_RETURN_NULL();
1211 
1212  /* A zeroed client addr means we don't know */
1213  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1214  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1215  sizeof(zero_clientaddr)) == 0)
1216  PG_RETURN_NULL();
1217 
1218  switch (beentry->st_clientaddr.addr.ss_family)
1219  {
1220  case AF_INET:
1221 #ifdef HAVE_IPV6
1222  case AF_INET6:
1223 #endif
1224  break;
1225  case AF_UNIX:
1226  PG_RETURN_INT32(-1);
1227  default:
1228  PG_RETURN_NULL();
1229  }
1230 
1231  remote_port[0] = '\0';
1232  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1233  beentry->st_clientaddr.salen,
1234  NULL, 0,
1235  remote_port, sizeof(remote_port),
1237  if (ret != 0)
1238  PG_RETURN_NULL();
1239 
1241  CStringGetDatum(remote_port)));
1242 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
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
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
signed int int32
Definition: c.h:429
#define CStringGetDatum(X)
Definition: postgres.h:622
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
#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
#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 987 of file pgstatfuncs.c.

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

988 {
989  int32 beid = PG_GETARG_INT32(0);
990  PgBackendStatus *beentry;
991 
992  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
993  PG_RETURN_NULL();
994 
995  PG_RETURN_OID(beentry->st_databaseid);
996 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
signed int int32
Definition: c.h:429
#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 417 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.

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

◆ pg_stat_get_backend_pid()

Datum pg_stat_get_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 974 of file pgstatfuncs.c.

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

975 {
976  int32 beid = PG_GETARG_INT32(0);
977  PgBackendStatus *beentry;
978 
979  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
980  PG_RETURN_NULL();
981 
982  PG_RETURN_INT32(beentry->st_procpid);
983 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
signed int int32
Definition: c.h:429
#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 1129 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.

1130 {
1131  int32 beid = PG_GETARG_INT32(0);
1132  TimestampTz result;
1133  PgBackendStatus *beentry;
1134 
1135  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1136  PG_RETURN_NULL();
1137 
1138  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1139  PG_RETURN_NULL();
1140 
1141  result = beentry->st_proc_start_timestamp;
1142 
1143  if (result == 0) /* probably can't happen? */
1144  PG_RETURN_NULL();
1145 
1146  PG_RETURN_TIMESTAMPTZ(result);
1147 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
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:37
TimestampTz st_proc_start_timestamp
#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 1000 of file pgstatfuncs.c.

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

1001 {
1002  int32 beid = PG_GETARG_INT32(0);
1003  PgBackendStatus *beentry;
1004 
1005  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1006  PG_RETURN_NULL();
1007 
1008  PG_RETURN_OID(beentry->st_userid);
1009 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
signed int int32
Definition: c.h:429
#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 1059 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.

1060 {
1061  int32 beid = PG_GETARG_INT32(0);
1062  PgBackendStatus *beentry;
1063  PGPROC *proc;
1064  const char *wait_event = NULL;
1065 
1066  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1067  wait_event = "<backend information not available>";
1068  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1069  wait_event = "<insufficient privilege>";
1070  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1071  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1072 
1073  if (!wait_event)
1074  PG_RETURN_NULL();
1075 
1076  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1077 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
uint32 wait_event_info
Definition: proc.h:226
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3133
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: wait_event.c:129
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:190
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 1038 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.

1039 {
1040  int32 beid = PG_GETARG_INT32(0);
1041  PgBackendStatus *beentry;
1042  PGPROC *proc;
1043  const char *wait_event_type = NULL;
1044 
1045  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1046  wait_event_type = "<backend information not available>";
1047  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1048  wait_event_type = "<insufficient privilege>";
1049  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1050  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
1051 
1052  if (!wait_event_type)
1053  PG_RETURN_NULL();
1054 
1055  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
1056 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
uint32 wait_event_info
Definition: proc.h:226
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3133
signed int int32
Definition: c.h:429
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:190
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:74
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 1107 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.

1108 {
1109  int32 beid = PG_GETARG_INT32(0);
1110  TimestampTz result;
1111  PgBackendStatus *beentry;
1112 
1113  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1114  PG_RETURN_NULL();
1115 
1116  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1117  PG_RETURN_NULL();
1118 
1119  result = beentry->st_xact_start_timestamp;
1120 
1121  if (result == 0) /* not in a transaction */
1122  PG_RETURN_NULL();
1123 
1124  PG_RETURN_TIMESTAMPTZ(result);
1125 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
PgBackendStatus * pgstat_fetch_stat_beentry(int beid)
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:37
TimestampTz st_xact_start_timestamp
#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 1745 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1746 {
1747  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1748 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_bgwriter_buf_written_clean()

Datum pg_stat_get_bgwriter_buf_written_clean ( PG_FUNCTION_ARGS  )

Definition at line 1751 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1752 {
1753  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1754 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_bgwriter_maxwritten_clean()

Datum pg_stat_get_bgwriter_maxwritten_clean ( PG_FUNCTION_ARGS  )

Definition at line 1757 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1758 {
1759  PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1760 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_bgwriter_requested_checkpoints()

Datum pg_stat_get_bgwriter_requested_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1739 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1740 {
1741  PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1742 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_bgwriter_stat_reset_time()

Datum pg_stat_get_bgwriter_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1777 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_global().

1778 {
1779  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stat_reset_timestamp);
1780 }
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843
#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 1733 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1734 {
1735  PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1736 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_blocks_fetched()

Datum pg_stat_get_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 219 of file pgstatfuncs.c.

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

220 {
221  Oid relid = PG_GETARG_OID(0);
222  int64 result;
223  PgStat_StatTabEntry *tabentry;
224 
225  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
226  result = 0;
227  else
228  result = (int64) (tabentry->blocks_fetched);
229 
230  PG_RETURN_INT64(result);
231 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter blocks_fetched
Definition: pgstat.h:799

◆ pg_stat_get_blocks_hit()

Datum pg_stat_get_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 235 of file pgstatfuncs.c.

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

236 {
237  Oid relid = PG_GETARG_OID(0);
238  int64 result;
239  PgStat_StatTabEntry *tabentry;
240 
241  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
242  result = 0;
243  else
244  result = (int64) (tabentry->blocks_hit);
245 
246  PG_RETURN_INT64(result);
247 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter blocks_hit
Definition: pgstat.h:800

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1795 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1796 {
1797  PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1798 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_buf_fsync_backend()

Datum pg_stat_get_buf_fsync_backend ( PG_FUNCTION_ARGS  )

Definition at line 1789 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1790 {
1791  PG_RETURN_INT64(pgstat_fetch_global()->buf_fsync_backend);
1792 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_buf_written_backend()

Datum pg_stat_get_buf_written_backend ( PG_FUNCTION_ARGS  )

Definition at line 1783 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_global().

1784 {
1785  PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1786 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_checkpoint_sync_time()

Datum pg_stat_get_checkpoint_sync_time ( PG_FUNCTION_ARGS  )

Definition at line 1770 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_global().

1771 {
1772  /* time is already in msec, just convert to double for presentation */
1773  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_sync_time);
1774 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_checkpoint_write_time()

Datum pg_stat_get_checkpoint_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1763 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_global().

1764 {
1765  /* time is already in msec, just convert to double for presentation */
1766  PG_RETURN_FLOAT8((double) pgstat_fetch_global()->checkpoint_write_time);
1767 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843

◆ pg_stat_get_db_active_time()

Datum pg_stat_get_db_active_time ( PG_FUNCTION_ARGS  )

Definition at line 1653 of file pgstatfuncs.c.

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

1654 {
1655  Oid dbid = PG_GETARG_OID(0);
1656  double result = 0.0;
1657  PgStat_StatDBEntry *dbentry;
1658 
1659  /* convert counter from microsec to millisec for display */
1660  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1661  result = ((double) dbentry->total_active_time) / 1000.0;
1662 
1663  PG_RETURN_FLOAT8(result);
1664 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter total_active_time
Definition: pgstat.h:757
#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 1607 of file pgstatfuncs.c.

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

1608 {
1609  Oid dbid = PG_GETARG_OID(0);
1610  double result;
1611  PgStat_StatDBEntry *dbentry;
1612 
1613  /* convert counter from microsec to millisec for display */
1614  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1615  result = 0;
1616  else
1617  result = ((double) dbentry->n_block_read_time) / 1000.0;
1618 
1619  PG_RETURN_FLOAT8(result);
1620 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:753

◆ pg_stat_get_db_blk_write_time()

Datum pg_stat_get_db_blk_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1623 of file pgstatfuncs.c.

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

1624 {
1625  Oid dbid = PG_GETARG_OID(0);
1626  double result;
1627  PgStat_StatDBEntry *dbentry;
1628 
1629  /* convert counter from microsec to millisec for display */
1630  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1631  result = 0;
1632  else
1633  result = ((double) dbentry->n_block_write_time) / 1000.0;
1634 
1635  PG_RETURN_FLOAT8(result);
1636 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:754

◆ pg_stat_get_db_blocks_fetched()

Datum pg_stat_get_db_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1299 of file pgstatfuncs.c.

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

1300 {
1301  Oid dbid = PG_GETARG_OID(0);
1302  int64 result;
1303  PgStat_StatDBEntry *dbentry;
1304 
1305  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1306  result = 0;
1307  else
1308  result = (int64) (dbentry->n_blocks_fetched);
1309 
1310  PG_RETURN_INT64(result);
1311 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:735
#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 1315 of file pgstatfuncs.c.

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

1316 {
1317  Oid dbid = PG_GETARG_OID(0);
1318  int64 result;
1319  PgStat_StatDBEntry *dbentry;
1320 
1321  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1322  result = 0;
1323  else
1324  result = (int64) (dbentry->n_blocks_hit);
1325 
1326  PG_RETURN_INT64(result);
1327 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_blocks_hit
Definition: pgstat.h:736
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1568 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().

1569 {
1570  Oid dbid = PG_GETARG_OID(0);
1571  int64 result;
1572  PgStat_StatDBEntry *dbentry;
1573 
1574  if (!DataChecksumsEnabled())
1575  PG_RETURN_NULL();
1576 
1577  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1578  result = 0;
1579  else
1580  result = (int64) (dbentry->n_checksum_failures);
1581 
1582  PG_RETURN_INT64(result);
1583 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
bool DataChecksumsEnabled(void)
Definition: xlog.c:4978
PgStat_Counter n_checksum_failures
Definition: pgstat.h:751
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1586 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().

1587 {
1588  Oid dbid = PG_GETARG_OID(0);
1589  TimestampTz result;
1590  PgStat_StatDBEntry *dbentry;
1591 
1592  if (!DataChecksumsEnabled())
1593  PG_RETURN_NULL();
1594 
1595  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1596  result = 0;
1597  else
1598  result = dbentry->last_checksum_failure;
1599 
1600  if (result == 0)
1601  PG_RETURN_NULL();
1602  else
1603  PG_RETURN_TIMESTAMPTZ(result);
1604 }
bool DataChecksumsEnabled(void)
Definition: xlog.c:4978
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz last_checksum_failure
Definition: pgstat.h:752
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1534 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().

1535 {
1536  Oid dbid = PG_GETARG_OID(0);
1537  int64 result;
1538  PgStat_StatDBEntry *dbentry;
1539 
1540  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1541  result = 0;
1542  else
1543  result = (int64) (dbentry->n_conflict_tablespace +
1544  dbentry->n_conflict_lock +
1545  dbentry->n_conflict_snapshot +
1546  dbentry->n_conflict_bufferpin +
1547  dbentry->n_conflict_startup_deadlock);
1548 
1549  PG_RETURN_INT64(result);
1550 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:746
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:747
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:743
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:745
PgStat_Counter n_conflict_lock
Definition: pgstat.h:744

◆ pg_stat_get_db_conflict_bufferpin()

Datum pg_stat_get_db_conflict_bufferpin ( PG_FUNCTION_ARGS  )

Definition at line 1504 of file pgstatfuncs.c.

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

1505 {
1506  Oid dbid = PG_GETARG_OID(0);
1507  int64 result;
1508  PgStat_StatDBEntry *dbentry;
1509 
1510  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1511  result = 0;
1512  else
1513  result = (int64) (dbentry->n_conflict_bufferpin);
1514 
1515  PG_RETURN_INT64(result);
1516 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:746
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1474 of file pgstatfuncs.c.

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

1475 {
1476  Oid dbid = PG_GETARG_OID(0);
1477  int64 result;
1478  PgStat_StatDBEntry *dbentry;
1479 
1480  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1481  result = 0;
1482  else
1483  result = (int64) (dbentry->n_conflict_lock);
1484 
1485  PG_RETURN_INT64(result);
1486 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:744

◆ pg_stat_get_db_conflict_snapshot()

Datum pg_stat_get_db_conflict_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1489 of file pgstatfuncs.c.

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

1490 {
1491  Oid dbid = PG_GETARG_OID(0);
1492  int64 result;
1493  PgStat_StatDBEntry *dbentry;
1494 
1495  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1496  result = 0;
1497  else
1498  result = (int64) (dbentry->n_conflict_snapshot);
1499 
1500  PG_RETURN_INT64(result);
1501 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:745

◆ pg_stat_get_db_conflict_startup_deadlock()

Datum pg_stat_get_db_conflict_startup_deadlock ( PG_FUNCTION_ARGS  )

Definition at line 1519 of file pgstatfuncs.c.

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

1520 {
1521  Oid dbid = PG_GETARG_OID(0);
1522  int64 result;
1523  PgStat_StatDBEntry *dbentry;
1524 
1525  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1526  result = 0;
1527  else
1528  result = (int64) (dbentry->n_conflict_startup_deadlock);
1529 
1530  PG_RETURN_INT64(result);
1531 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:747
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1459 of file pgstatfuncs.c.

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

1460 {
1461  Oid dbid = PG_GETARG_OID(0);
1462  int64 result;
1463  PgStat_StatDBEntry *dbentry;
1464 
1465  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1466  result = 0;
1467  else
1468  result = (int64) (dbentry->n_conflict_tablespace);
1469 
1470  PG_RETURN_INT64(result);
1471 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:743
#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 1553 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_idle_in_transaction_time()

Datum pg_stat_get_db_idle_in_transaction_time ( PG_FUNCTION_ARGS  )

Definition at line 1667 of file pgstatfuncs.c.

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

1668 {
1669  Oid dbid = PG_GETARG_OID(0);
1670  double result = 0.0;
1671  PgStat_StatDBEntry *dbentry;
1672 
1673  /* convert counter from microsec to millisec for display */
1674  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1675  result = ((double) dbentry->total_idle_in_xact_time) / 1000.0;
1676 
1677  PG_RETURN_FLOAT8(result);
1678 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:758

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 1246 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_session_time()

Datum pg_stat_get_db_session_time ( PG_FUNCTION_ARGS  )

Definition at line 1639 of file pgstatfuncs.c.

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

1640 {
1641  Oid dbid = PG_GETARG_OID(0);
1642  double result = 0.0;
1643  PgStat_StatDBEntry *dbentry;
1644 
1645  /* convert counter from microsec to millisec for display */
1646  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1647  result = ((double) dbentry->total_session_time) / 1000.0;
1648 
1649  PG_RETURN_FLOAT8(result);
1650 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:756

◆ pg_stat_get_db_sessions()

Datum pg_stat_get_db_sessions ( PG_FUNCTION_ARGS  )

Definition at line 1681 of file pgstatfuncs.c.

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

1682 {
1683  Oid dbid = PG_GETARG_OID(0);
1684  int64 result = 0;
1685  PgStat_StatDBEntry *dbentry;
1686 
1687  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1688  result = (int64) (dbentry->n_sessions);
1689 
1690  PG_RETURN_INT64(result);
1691 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter n_sessions
Definition: pgstat.h:755

◆ pg_stat_get_db_sessions_abandoned()

Datum pg_stat_get_db_sessions_abandoned ( PG_FUNCTION_ARGS  )

Definition at line 1694 of file pgstatfuncs.c.

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

1695 {
1696  Oid dbid = PG_GETARG_OID(0);
1697  int64 result = 0;
1698  PgStat_StatDBEntry *dbentry;
1699 
1700  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1701  result = (int64) (dbentry->n_sessions_abandoned);
1702 
1703  PG_RETURN_INT64(result);
1704 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_abandoned
Definition: pgstat.h:759
#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 1707 of file pgstatfuncs.c.

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

1708 {
1709  Oid dbid = PG_GETARG_OID(0);
1710  int64 result = 0;
1711  PgStat_StatDBEntry *dbentry;
1712 
1713  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1714  result = (int64) (dbentry->n_sessions_fatal);
1715 
1716  PG_RETURN_INT64(result);
1717 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_fatal
Definition: pgstat.h:760
#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 1720 of file pgstatfuncs.c.

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

1721 {
1722  Oid dbid = PG_GETARG_OID(0);
1723  int64 result = 0;
1724  PgStat_StatDBEntry *dbentry;
1725 
1726  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) != NULL)
1727  result = (int64) (dbentry->n_sessions_killed);
1728 
1729  PG_RETURN_INT64(result);
1730 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_sessions_killed
Definition: pgstat.h:761
#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 1410 of file pgstatfuncs.c.

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

1411 {
1412  Oid dbid = PG_GETARG_OID(0);
1413  TimestampTz result;
1414  PgStat_StatDBEntry *dbentry;
1415 
1416  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1417  result = 0;
1418  else
1419  result = dbentry->stat_reset_timestamp;
1420 
1421  if (result == 0)
1422  PG_RETURN_NULL();
1423  else
1424  PG_RETURN_TIMESTAMPTZ(result);
1425 }
int64 TimestampTz
Definition: timestamp.h:39
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
TimestampTz stat_reset_timestamp
Definition: pgstat.h:763
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 1444 of file pgstatfuncs.c.

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

1445 {
1446  Oid dbid = PG_GETARG_OID(0);
1447  int64 result;
1448  PgStat_StatDBEntry *dbentry;
1449 
1450  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1451  result = 0;
1452  else
1453  result = dbentry->n_temp_bytes;
1454 
1455  PG_RETURN_INT64(result);
1456 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:749

◆ pg_stat_get_db_temp_files()

Datum pg_stat_get_db_temp_files ( PG_FUNCTION_ARGS  )

Definition at line 1428 of file pgstatfuncs.c.

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

1429 {
1430  Oid dbid = PG_GETARG_OID(0);
1431  int64 result;
1432  PgStat_StatDBEntry *dbentry;
1433 
1434  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1435  result = 0;
1436  else
1437  result = dbentry->n_temp_files;
1438 
1439  PG_RETURN_INT64(result);
1440 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_temp_files
Definition: pgstat.h:748
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1395 of file pgstatfuncs.c.

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

1396 {
1397  Oid dbid = PG_GETARG_OID(0);
1398  int64 result;
1399  PgStat_StatDBEntry *dbentry;
1400 
1401  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1402  result = 0;
1403  else
1404  result = (int64) (dbentry->n_tuples_deleted);
1405 
1406  PG_RETURN_INT64(result);
1407 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:741

◆ pg_stat_get_db_tuples_fetched()

Datum pg_stat_get_db_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1347 of file pgstatfuncs.c.

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

1348 {
1349  Oid dbid = PG_GETARG_OID(0);
1350  int64 result;
1351  PgStat_StatDBEntry *dbentry;
1352 
1353  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1354  result = 0;
1355  else
1356  result = (int64) (dbentry->n_tuples_fetched);
1357 
1358  PG_RETURN_INT64(result);
1359 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:738

◆ pg_stat_get_db_tuples_inserted()

Datum pg_stat_get_db_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1363 of file pgstatfuncs.c.

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

1364 {
1365  Oid dbid = PG_GETARG_OID(0);
1366  int64 result;
1367  PgStat_StatDBEntry *dbentry;
1368 
1369  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1370  result = 0;
1371  else
1372  result = (int64) (dbentry->n_tuples_inserted);
1373 
1374  PG_RETURN_INT64(result);
1375 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:739
#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 1331 of file pgstatfuncs.c.

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

1332 {
1333  Oid dbid = PG_GETARG_OID(0);
1334  int64 result;
1335  PgStat_StatDBEntry *dbentry;
1336 
1337  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1338  result = 0;
1339  else
1340  result = (int64) (dbentry->n_tuples_returned);
1341 
1342  PG_RETURN_INT64(result);
1343 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter n_tuples_returned
Definition: pgstat.h:737
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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 1379 of file pgstatfuncs.c.

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

1380 {
1381  Oid dbid = PG_GETARG_OID(0);
1382  int64 result;
1383  PgStat_StatDBEntry *dbentry;
1384 
1385  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1386  result = 0;
1387  else
1388  result = (int64) (dbentry->n_tuples_updated);
1389 
1390  PG_RETURN_INT64(result);
1391 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:740

◆ pg_stat_get_db_xact_commit()

Datum pg_stat_get_db_xact_commit ( PG_FUNCTION_ARGS  )

Definition at line 1267 of file pgstatfuncs.c.

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

1268 {
1269  Oid dbid = PG_GETARG_OID(0);
1270  int64 result;
1271  PgStat_StatDBEntry *dbentry;
1272 
1273  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1274  result = 0;
1275  else
1276  result = (int64) (dbentry->n_xact_commit);
1277 
1278  PG_RETURN_INT64(result);
1279 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter n_xact_commit
Definition: pgstat.h:733
#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 1283 of file pgstatfuncs.c.

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

1284 {
1285  Oid dbid = PG_GETARG_OID(0);
1286  int64 result;
1287  PgStat_StatDBEntry *dbentry;
1288 
1289  if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1290  result = 0;
1291  else
1292  result = (int64) (dbentry->n_xact_rollback);
1293 
1294  PG_RETURN_INT64(result);
1295 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:734

◆ pg_stat_get_dead_tuples()

Datum pg_stat_get_dead_tuples ( PG_FUNCTION_ARGS  )

Definition at line 171 of file pgstatfuncs.c.

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

172 {
173  Oid relid = PG_GETARG_OID(0);
174  int64 result;
175  PgStat_StatTabEntry *tabentry;
176 
177  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
178  result = 0;
179  else
180  result = (int64) (tabentry->n_dead_tuples);
181 
182  PG_RETURN_INT64(result);
183 }
#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:794
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 382 of file pgstatfuncs.c.

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

383 {
384  Oid funcid = PG_GETARG_OID(0);
385  PgStat_StatFuncEntry *funcentry;
386 
387  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
388  PG_RETURN_NULL();
389  PG_RETURN_INT64(funcentry->f_numcalls);
390 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2796
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter f_numcalls
Definition: pgstat.h:821
#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 405 of file pgstatfuncs.c.

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

406 {
407  Oid funcid = PG_GETARG_OID(0);
408  PgStat_StatFuncEntry *funcentry;
409 
410  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
411  PG_RETURN_NULL();
412  /* convert counter from microsec to millisec for display */
413  PG_RETURN_FLOAT8(((double) funcentry->f_self_time) / 1000.0);
414 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2796
#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:824
#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 393 of file pgstatfuncs.c.

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

394 {
395  Oid funcid = PG_GETARG_OID(0);
396  PgStat_StatFuncEntry *funcentry;
397 
398  if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
399  PG_RETURN_NULL();
400  /* convert counter from microsec to millisec for display */
401  PG_RETURN_FLOAT8(((double) funcentry->f_total_time) / 1000.0);
402 }
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
Definition: pgstat.c:2796
#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:823
#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 203 of file pgstatfuncs.c.

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

204 {
205  Oid relid = PG_GETARG_OID(0);
206  int64 result;
207  PgStat_StatTabEntry *tabentry;
208 
209  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
210  result = 0;
211  else
212  result = (int64) (tabentry->inserts_since_vacuum);
213 
214  PG_RETURN_INT64(result);
215 }
#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:797
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 286 of file pgstatfuncs.c.

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

287 {
288  Oid relid = PG_GETARG_OID(0);
289  TimestampTz result;
290  PgStat_StatTabEntry *tabentry;
291 
292  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
293  result = 0;
294  else
295  result = tabentry->analyze_timestamp;
296 
297  if (result == 0)
298  PG_RETURN_NULL();
299  else
300  PG_RETURN_TIMESTAMPTZ(result);
301 }
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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
TimestampTz analyze_timestamp
Definition: pgstat.h:806
#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 304 of file pgstatfuncs.c.

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

305 {
306  Oid relid = PG_GETARG_OID(0);
307  TimestampTz result;
308  PgStat_StatTabEntry *tabentry;
309 
310  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
311  result = 0;
312  else
313  result = tabentry->autovac_analyze_timestamp;
314 
315  if (result == 0)
316  PG_RETURN_NULL();
317  else
318  PG_RETURN_TIMESTAMPTZ(result);
319 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:808
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:2740
#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 268 of file pgstatfuncs.c.

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

269 {
270  Oid relid = PG_GETARG_OID(0);
271  TimestampTz result;
272  PgStat_StatTabEntry *tabentry;
273 
274  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
275  result = 0;
276  else
277  result = tabentry->autovac_vacuum_timestamp;
278 
279  if (result == 0)
280  PG_RETURN_NULL();
281  else
282  PG_RETURN_TIMESTAMPTZ(result);
283 }
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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:804
#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 250 of file pgstatfuncs.c.

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

251 {
252  Oid relid = PG_GETARG_OID(0);
253  TimestampTz result;
254  PgStat_StatTabEntry *tabentry;
255 
256  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
257  result = 0;
258  else
259  result = tabentry->vacuum_timestamp;
260 
261  if (result == 0)
262  PG_RETURN_NULL();
263  else
264  PG_RETURN_TIMESTAMPTZ(result);
265 }
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:802
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 155 of file pgstatfuncs.c.

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

156 {
157  Oid relid = PG_GETARG_OID(0);
158  int64 result;
159  PgStat_StatTabEntry *tabentry;
160 
161  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
162  result = 0;
163  else
164  result = (int64) (tabentry->n_live_tuples);
165 
166  PG_RETURN_INT64(result);
167 }
#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:793
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 187 of file pgstatfuncs.c.

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

188 {
189  Oid relid = PG_GETARG_OID(0);
190  int64 result;
191  PgStat_StatTabEntry *tabentry;
192 
193  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
194  result = 0;
195  else
196  result = (int64) (tabentry->changes_since_analyze);
197 
198  PG_RETURN_INT64(result);
199 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter changes_since_analyze
Definition: pgstat.h:795

◆ pg_stat_get_numscans()

Datum pg_stat_get_numscans ( PG_FUNCTION_ARGS  )

Definition at line 43 of file pgstatfuncs.c.

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

44 {
45  Oid relid = PG_GETARG_OID(0);
46  int64 result;
47  PgStat_StatTabEntry *tabentry;
48 
49  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
50  result = 0;
51  else
52  result = (int64) (tabentry->numscans);
53 
54  PG_RETURN_INT64(result);
55 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter numscans
Definition: pgstat.h:783
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 460 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.

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

◆ pg_stat_get_replication_slot()

Datum pg_stat_get_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 2314 of file pgstatfuncs.c.

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

2315 {
2316 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2317  text *slotname_text = PG_GETARG_TEXT_P(0);
2318  NameData slotname;
2319  TupleDesc tupdesc;
2322  PgStat_StatReplSlotEntry *slotent;
2323  PgStat_StatReplSlotEntry allzero;
2324 
2325  /* Initialise values and NULL flags arrays */
2326  MemSet(values, 0, sizeof(values));
2327  MemSet(nulls, 0, sizeof(nulls));
2328 
2329  /* Initialise attributes information in the tuple descriptor */
2331  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2332  TEXTOID, -1, 0);
2333  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2334  INT8OID, -1, 0);
2335  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2336  INT8OID, -1, 0);
2337  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2338  INT8OID, -1, 0);
2339  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2340  INT8OID, -1, 0);
2341  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2342  INT8OID, -1, 0);
2343  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2344  INT8OID, -1, 0);
2345  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2346  INT8OID, -1, 0);
2347  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2348  INT8OID, -1, 0);
2349  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2350  TIMESTAMPTZOID, -1, 0);
2351  BlessTupleDesc(tupdesc);
2352 
2353  namestrcpy(&slotname, text_to_cstring(slotname_text));
2354  slotent = pgstat_fetch_replslot(slotname);
2355  if (!slotent)
2356  {
2357  /*
2358  * If the slot is not found, initialise its stats. This is possible if
2359  * the create slot message is lost.
2360  */
2361  memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2362  slotent = &allzero;
2363  }
2364 
2365  values[0] = CStringGetTextDatum(NameStr(slotname));
2366  values[1] = Int64GetDatum(slotent->spill_txns);
2367  values[2] = Int64GetDatum(slotent->spill_count);
2368  values[3] = Int64GetDatum(slotent->spill_bytes);
2369  values[4] = Int64GetDatum(slotent->stream_txns);
2370  values[5] = Int64GetDatum(slotent->stream_count);
2371  values[6] = Int64GetDatum(slotent->stream_bytes);
2372  values[7] = Int64GetDatum(slotent->total_txns);
2373  values[8] = Int64GetDatum(slotent->total_bytes);
2374 
2375  if (slotent->stat_reset_timestamp == 0)
2376  nulls[9] = true;
2377  else
2378  values[9] = TimestampTzGetDatum(slotent->stat_reset_timestamp);
2379 
2380  /* Returns the record as Datum */
2381  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2382 }
#define PG_STAT_GET_REPLICATION_SLOT_COLS
PgStat_Counter spill_bytes
Definition: pgstat.h:902
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define MemSet(start, val, len)
Definition: c.h:1008
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
Definition: c.h:675
PgStat_Counter total_bytes
Definition: pgstat.h:907
TimestampTz stat_reset_timestamp
Definition: pgstat.h:908
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
PgStat_Counter stream_bytes
Definition: pgstat.h:905
PgStat_Counter spill_txns
Definition: pgstat.h:900
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
PgStat_Counter spill_count
Definition: pgstat.h:901
static Datum values[MAXATTR]
Definition: bootstrap.c:166
char * text_to_cstring(const text *t)
Definition: varlena.c:223
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
Definition: pgstat.c:2891
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:336
PgStat_Counter stream_count
Definition: pgstat.h:904
#define NameStr(name)
Definition: c.h:681
#define CStringGetTextDatum(s)
Definition: builtins.h:82
Definition: c.h:621
PgStat_Counter total_txns
Definition: pgstat.h:906
int16 AttrNumber
Definition: attnum.h:21
PgStat_Counter stream_txns
Definition: pgstat.h:903

◆ pg_stat_get_slru()

Datum pg_stat_get_slru ( PG_FUNCTION_ARGS  )

Definition at line 1872 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.

1873 {
1874 #define PG_STAT_GET_SLRU_COLS 9
1875  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1876  TupleDesc tupdesc;
1877  Tuplestorestate *tupstore;
1878  MemoryContext per_query_ctx;
1879  MemoryContext oldcontext;
1880  int i;
1881  PgStat_SLRUStats *stats;
1882 
1883  /* check to see if caller supports us returning a tuplestore */
1884  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1885  ereport(ERROR,
1886  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1887  errmsg("set-valued function called in context that cannot accept a set")));
1888  if (!(rsinfo->allowedModes & SFRM_Materialize))
1889  ereport(ERROR,
1890  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1891  errmsg("materialize mode required, but it is not allowed in this context")));
1892 
1893  /* Build a tuple descriptor for our result type */
1894  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1895  elog(ERROR, "return type must be a row type");
1896 
1897  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1898  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1899 
1900  tupstore = tuplestore_begin_heap(true, false, work_mem);
1901  rsinfo->returnMode = SFRM_Materialize;
1902  rsinfo->setResult = tupstore;
1903  rsinfo->setDesc = tupdesc;
1904 
1905  MemoryContextSwitchTo(oldcontext);
1906 
1907  /* request SLRU stats from the stat collector */
1908  stats = pgstat_fetch_slru();
1909 
1910  for (i = 0;; i++)
1911  {
1912  /* for each row */
1914  bool nulls[PG_STAT_GET_SLRU_COLS];
1915  PgStat_SLRUStats stat = stats[i];
1916  const char *name;
1917 
1918  name = pgstat_slru_name(i);
1919 
1920  if (!name)
1921  break;
1922 
1923  MemSet(values, 0, sizeof(values));
1924  MemSet(nulls, 0, sizeof(nulls));
1925 
1926  values[0] = PointerGetDatum(cstring_to_text(name));
1927  values[1] = Int64GetDatum(stat.blocks_zeroed);
1928  values[2] = Int64GetDatum(stat.blocks_hit);
1929  values[3] = Int64GetDatum(stat.blocks_read);
1930  values[4] = Int64GetDatum(stat.blocks_written);
1931  values[5] = Int64GetDatum(stat.blocks_exists);
1932  values[6] = Int64GetDatum(stat.flush);
1933  values[7] = Int64GetDatum(stat.truncate);
1934  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1935 
1936  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1937  }
1938 
1939  /* clean up and return the tuplestore */
1940  tuplestore_donestoring(tupstore);
1941 
1942  return (Datum) 0;
1943 }
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:590
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define PointerGetDatum(X)
Definition: postgres.h:600
TimestampTz stat_reset_timestamp
Definition: pgstat.h:891
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MemSet(start, val, len)
Definition: c.h:1008
PgStat_Counter blocks_exists
Definition: pgstat.h:888
#define ERROR
Definition: elog.h:46
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter blocks_read
Definition: pgstat.h:886
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
PgStat_Counter blocks_hit
Definition: pgstat.h:885
PgStat_Counter blocks_zeroed
Definition: pgstat.h:884
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:411
int work_mem
Definition: globals.c:124
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
text * cstring_to_text(const char *s)
Definition: varlena.c:190
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat.c:2875
PgStat_Counter flush
Definition: pgstat.h:889
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
const char * name
Definition: encode.c:515
Tuplestorestate * setResult
Definition: execnodes.h:310
static Datum values[MAXATTR]
Definition: bootstrap.c:166
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
const char * pgstat_slru_name(int slru_idx)
Definition: pgstat.c:5801
PgStat_Counter truncate
Definition: pgstat.h:890
PgStat_Counter blocks_written
Definition: pgstat.h:887

◆ pg_stat_get_snapshot_timestamp()

Datum pg_stat_get_snapshot_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2134 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_global().

2135 {
2136  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
2137 }
PgStat_GlobalStats * pgstat_fetch_global(void)
Definition: pgstat.c:2843
#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 123 of file pgstatfuncs.c.

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

124 {
125  Oid relid = PG_GETARG_OID(0);
126  int64 result;
127  PgStat_StatTabEntry *tabentry;
128 
129  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
130  result = 0;
131  else
132  result = (int64) (tabentry->tuples_deleted);
133 
134  PG_RETURN_INT64(result);
135 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_deleted
Definition: pgstat.h:790

◆ pg_stat_get_tuples_fetched()

Datum pg_stat_get_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 75 of file pgstatfuncs.c.

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

76 {
77  Oid relid = PG_GETARG_OID(0);
78  int64 result;
79  PgStat_StatTabEntry *tabentry;
80 
81  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
82  result = 0;
83  else
84  result = (int64) (tabentry->tuples_fetched);
85 
86  PG_RETURN_INT64(result);
87 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_fetched
Definition: pgstat.h:786

◆ pg_stat_get_tuples_hot_updated()

Datum pg_stat_get_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 139 of file pgstatfuncs.c.

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

140 {
141  Oid relid = PG_GETARG_OID(0);
142  int64 result;
143  PgStat_StatTabEntry *tabentry;
144 
145  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
146  result = 0;
147  else
148  result = (int64) (tabentry->tuples_hot_updated);
149 
150  PG_RETURN_INT64(result);
151 }
#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:791
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 91 of file pgstatfuncs.c.

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

92 {
93  Oid relid = PG_GETARG_OID(0);
94  int64 result;
95  PgStat_StatTabEntry *tabentry;
96 
97  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
98  result = 0;
99  else
100  result = (int64) (tabentry->tuples_inserted);
101 
102  PG_RETURN_INT64(result);
103 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter tuples_inserted
Definition: pgstat.h:788
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2740
#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 59 of file pgstatfuncs.c.

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

60 {
61  Oid relid = PG_GETARG_OID(0);
62  int64 result;
63  PgStat_StatTabEntry *tabentry;
64 
65  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
66  result = 0;
67  else
68  result = (int64) (tabentry->tuples_returned);
69 
70  PG_RETURN_INT64(result);
71 }
PgStat_Counter tuples_returned
Definition: pgstat.h:785
#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:2740
#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 107 of file pgstatfuncs.c.

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

108 {
109  Oid relid = PG_GETARG_OID(0);
110  int64 result;
111  PgStat_StatTabEntry *tabentry;
112 
113  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
114  result = 0;
115  else
116  result = (int64) (tabentry->tuples_updated);
117 
118  PG_RETURN_INT64(result);
119 }
#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:2740
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter tuples_updated
Definition: pgstat.h:789

◆ pg_stat_get_vacuum_count()

Datum pg_stat_get_vacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 322 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

Definition at line 1804 of file pgstatfuncs.c.

References BlessTupleDesc(), buf, CreateTemplateTupleDesc(), CStringGetDatum, DirectFunctionCall3, Float8GetDatum(), 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, PgStat_WalStats::wal_records, PgStat_WalStats::wal_sync, PgStat_WalStats::wal_sync_time, PgStat_WalStats::wal_write, and PgStat_WalStats::wal_write_time.

1805 {
1806 #define PG_STAT_GET_WAL_COLS 9
1807  TupleDesc tupdesc;
1809  bool nulls[PG_STAT_GET_WAL_COLS];
1810  char buf[256];
1811  PgStat_WalStats *wal_stats;
1812 
1813  /* Initialise values and NULL flags arrays */
1814  MemSet(values, 0, sizeof(values));
1815  MemSet(nulls, 0, sizeof(nulls));
1816 
1817  /* Initialise attributes information in the tuple descriptor */
1819  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1820  INT8OID, -1, 0);
1821  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1822  INT8OID, -1, 0);
1823  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1824  NUMERICOID, -1, 0);
1825  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1826  INT8OID, -1, 0);
1827  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1828  INT8OID, -1, 0);
1829  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1830  INT8OID, -1, 0);
1831  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1832  FLOAT8OID, -1, 0);
1833  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1834  FLOAT8OID, -1, 0);
1835  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1836  TIMESTAMPTZOID, -1, 0);
1837 
1838  BlessTupleDesc(tupdesc);
1839 
1840  /* Get statistics about WAL activity */
1841  wal_stats = pgstat_fetch_stat_wal();
1842 
1843  /* Fill values and NULLs */
1844  values[0] = Int64GetDatum(wal_stats->wal_records);
1845  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1846 
1847  /* Convert to numeric. */
1848  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1849  values[2] = DirectFunctionCall3(numeric_in,
1850  CStringGetDatum(buf),
1851  ObjectIdGetDatum(0),
1852  Int32GetDatum(-1));
1853 
1854  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1855  values[4] = Int64GetDatum(wal_stats->wal_write);
1856  values[5] = Int64GetDatum(wal_stats->wal_sync);
1857 
1858  /* Convert counters from microsec to millisec for display */
1859  values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1860  values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1861 
1862  values[8] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
1863 
1864  /* Returns the record as Datum */
1865  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
1866 }
PgStat_Counter wal_records
Definition: pgstat.h:868
PgStat_Counter wal_write
Definition: pgstat.h:872
TimestampTz stat_reset_timestamp
Definition: pgstat.h:876
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
PgStat_Counter wal_sync_time
Definition: pgstat.h:875
#define PG_STAT_GET_WAL_COLS
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat.c:2859
#define MemSet(start, val, len)
Definition: c.h:1008
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1706
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
PgStat_Counter wal_write_time
Definition: pgstat.h:874
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter wal_fpi
Definition: pgstat.h:869
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
static char * buf
Definition: pg_test_fsync.c:68
#define CStringGetDatum(X)
Definition: postgres.h:622
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:625
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
uint64 wal_bytes
Definition: pgstat.h:870
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define Int32GetDatum(X)
Definition: postgres.h:523
PgStat_Counter wal_buffers_full
Definition: pgstat.h:871
PgStat_Counter wal_sync
Definition: pgstat.h:873
#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 2069 of file pgstatfuncs.c.

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

2070 {
2071  Oid relid = PG_GETARG_OID(0);
2072  int64 result;
2073  PgStat_TableStatus *tabentry;
2074 
2075  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2076  result = 0;
2077  else
2078  result = (int64) (tabentry->t_counts.t_blocks_fetched);
2079 
2080  PG_RETURN_INT64(result);
2081 }
#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:2160
#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 2084 of file pgstatfuncs.c.

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

2085 {
2086  Oid relid = PG_GETARG_OID(0);
2087  int64 result;
2088  PgStat_TableStatus *tabentry;
2089 
2090  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2091  result = 0;
2092  else
2093  result = (int64) (tabentry->t_counts.t_blocks_hit);
2094 
2095  PG_RETURN_INT64(result);
2096 }
#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:2160
#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 2099 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.

2100 {
2101  Oid funcid = PG_GETARG_OID(0);
2102  PgStat_BackendFunctionEntry *funcentry;
2103 
2104  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2105  PG_RETURN_NULL();
2106  PG_RETURN_INT64(funcentry->f_counts.f_numcalls);
2107 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter f_numcalls
Definition: pgstat.h:579
PgStat_FunctionCounts f_counts
Definition: pgstat.h:591
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1959
#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 2121 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.

2122 {
2123  Oid funcid = PG_GETARG_OID(0);
2124  PgStat_BackendFunctionEntry *funcentry;
2125 
2126  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2127  PG_RETURN_NULL();
2129 }
instr_time f_self_time
Definition: pgstat.h:581
#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:591
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1959
#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 2110 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.

2111 {
2112  Oid funcid = PG_GETARG_OID(0);
2113  PgStat_BackendFunctionEntry *funcentry;
2114 
2115  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2116  PG_RETURN_NULL();
2118 }
#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:580
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_FunctionCounts f_counts
Definition: pgstat.h:591
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1959
#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 1946 of file pgstatfuncs.c.

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

1947 {
1948  Oid relid = PG_GETARG_OID(0);
1949  int64 result;
1950  PgStat_TableStatus *tabentry;
1951 
1952  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1953  result = 0;
1954  else
1955  result = (int64) (tabentry->t_counts.t_numscans);
1956 
1957  PG_RETURN_INT64(result);
1958 }
#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:2160
#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 2033 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.

2034 {
2035  Oid relid = PG_GETARG_OID(0);
2036  int64 result;
2037  PgStat_TableStatus *tabentry;
2039 
2040  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2041  result = 0;
2042  else
2043  {
2044  result = tabentry->t_counts.t_tuples_deleted;
2045  /* live subtransactions' counts aren't in t_tuples_deleted yet */
2046  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2047  result += trans->tuples_deleted;
2048  }
2049 
2050  PG_RETURN_INT64(result);
2051 }
#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:2160
#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 1976 of file pgstatfuncs.c.

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

1977 {
1978  Oid relid = PG_GETARG_OID(0);
1979  int64 result;
1980  PgStat_TableStatus *tabentry;
1981 
1982  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1983  result = 0;
1984  else
1985  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1986 
1987  PG_RETURN_INT64(result);
1988 }
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:2160
#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 2054 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.

2055 {
2056  Oid relid = PG_GETARG_OID(0);
2057  int64 result;
2058  PgStat_TableStatus *tabentry;
2059 
2060  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2061  result = 0;
2062  else
2063  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
2064 
2065  PG_RETURN_INT64(result);
2066 }
#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:2160
#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 1991 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.

1992 {
1993  Oid relid = PG_GETARG_OID(0);
1994  int64 result;
1995  PgStat_TableStatus *tabentry;
1997 
1998  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1999  result = 0;
2000  else
2001  {
2002  result = tabentry->t_counts.t_tuples_inserted;
2003  /* live subtransactions' counts aren't in t_tuples_inserted yet */
2004  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2005  result += trans->tuples_inserted;
2006  }
2007 
2008  PG_RETURN_INT64(result);
2009 }
#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:2160
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 1961 of file pgstatfuncs.c.

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

1962 {
1963  Oid relid = PG_GETARG_OID(0);
1964  int64 result;
1965  PgStat_TableStatus *tabentry;
1966 
1967  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1968  result = 0;
1969  else
1970  result = (int64) (tabentry->t_counts.t_tuples_returned);
1971 
1972  PG_RETURN_INT64(result);
1973 }
#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:2160
#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 2012 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.

2013 {
2014  Oid relid = PG_GETARG_OID(0);
2015  int64 result;
2016  PgStat_TableStatus *tabentry;
2018 
2019  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2020  result = 0;
2021  else
2022  {
2023  result = tabentry->t_counts.t_tuples_updated;
2024  /* live subtransactions' counts aren't in t_tuples_updated yet */
2025  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2026  result += trans->tuples_updated;
2027  }
2028 
2029  PG_RETURN_INT64(result);
2030 }
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:2160
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 2151 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_reset_counters().

2152 {
2154 
2155  PG_RETURN_VOID();
2156 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_reset_counters(void)
Definition: pgstat.c:1421

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 2206 of file pgstatfuncs.c.

References ereport, errcode(), errmsg(), ERROR, PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_reset_replslot_counter(), SearchNamedReplicationSlot(), SlotIsPhysical, and text_to_cstring().

2207 {
2208  char *target = NULL;
2209 
2210  if (!PG_ARGISNULL(0))
2211  {
2212  ReplicationSlot *slot;
2213 
2214  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2215 
2216  /*
2217  * Check if the slot exists with the given name. It is possible that
2218  * by the time this message is executed the slot is dropped but at
2219  * least this check will ensure that the given name is for a valid
2220  * slot.
2221  */
2222  slot = SearchNamedReplicationSlot(target, true);
2223 
2224  if (!slot)
2225  ereport(ERROR,
2226  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2227  errmsg("replication slot \"%s\" does not exist",
2228  target)));
2229 
2230  /*
2231  * Nothing to do for physical slots as we collect stats only for
2232  * logical slots.
2233  */
2234  if (SlotIsPhysical(slot))
2235  PG_RETURN_VOID();
2236  }
2237 
2239 
2240  PG_RETURN_VOID();
2241 }
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:348
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SlotIsPhysical(slot)
Definition: slot.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#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:1526
char * text_to_cstring(const text *t)
Definition: varlena.c:223
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_stat_reset_shared()

Datum pg_stat_reset_shared ( PG_FUNCTION_ARGS  )

Definition at line 2160 of file pgstatfuncs.c.

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

2161 {
2162  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2163 
2165 
2166  PG_RETURN_VOID();
2167 }
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1443
#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:223

◆ pg_stat_reset_single_function_counters()

Datum pg_stat_reset_single_function_counters ( PG_FUNCTION_ARGS  )

Definition at line 2181 of file pgstatfuncs.c.

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

2182 {
2183  Oid funcoid = PG_GETARG_OID(0);
2184 
2186 
2187  PG_RETURN_VOID();
2188 }
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:1476
#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 2171 of file pgstatfuncs.c.

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

2172 {
2173  Oid taboid = PG_GETARG_OID(0);
2174 
2176 
2177  PG_RETURN_VOID();
2178 }
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:1476
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ pg_stat_reset_slru()

Datum pg_stat_reset_slru ( PG_FUNCTION_ARGS  )

Definition at line 2192 of file pgstatfuncs.c.

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

2193 {
2194  char *target = NULL;
2195 
2196  if (!PG_ARGISNULL(0))
2197  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2198 
2199  pgstat_reset_slru_counter(target);
2200 
2201  PG_RETURN_VOID();
2202 }
void pgstat_reset_slru_counter(const char *name)
Definition: pgstat.c:1502
#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:223

Variable Documentation

◆ bgwriterStats

PgStat_MsgBgWriter bgwriterStats