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)
 

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

References MyProcPid, and PG_RETURN_INT32.

965 {
967 }
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 2140 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_clear_snapshot().

2141 {
2143 
2144  PG_RETURN_VOID();
2145 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4867

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

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

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

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

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

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

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

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

◆ pg_stat_get_autoanalyze_count()

Datum pg_stat_get_autoanalyze_count ( PG_FUNCTION_ARGS  )

Definition at line 364 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_autovacuum_count()

Datum pg_stat_get_autovacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 334 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

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

1011 {
1012  int32 beid = PG_GETARG_INT32(0);
1013  PgBackendStatus *beentry;
1014  const char *activity;
1015  char *clipped_activity;
1016  text *ret;
1017 
1018  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1019  activity = "<backend information not available>";
1020  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1021  activity = "<insufficient privilege>";
1022  else if (*(beentry->st_activity_raw) == '\0')
1023  activity = "<command string not enabled>";
1024  else
1025  activity = beentry->st_activity_raw;
1026 
1027  clipped_activity = pgstat_clip_activity(activity);
1028  ret = cstring_to_text(activity);
1029  pfree(clipped_activity);
1030 
1031  PG_RETURN_TEXT_P(ret);
1032 }
#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:189
Definition: c.h:621

◆ pg_stat_get_backend_activity_start()

Datum pg_stat_get_backend_activity_start ( PG_FUNCTION_ARGS  )

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

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

1149 {
1150  int32 beid = PG_GETARG_INT32(0);
1151  PgBackendStatus *beentry;
1152  SockAddr zero_clientaddr;
1153  char remote_host[NI_MAXHOST];
1154  int ret;
1155 
1156  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1157  PG_RETURN_NULL();
1158 
1159  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1160  PG_RETURN_NULL();
1161 
1162  /* A zeroed client addr means we don't know */
1163  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1164  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1165  sizeof(zero_clientaddr)) == 0)
1166  PG_RETURN_NULL();
1167 
1168  switch (beentry->st_clientaddr.addr.ss_family)
1169  {
1170  case AF_INET:
1171 #ifdef HAVE_IPV6
1172  case AF_INET6:
1173 #endif
1174  break;
1175  default:
1176  PG_RETURN_NULL();
1177  }
1178 
1179  remote_host[0] = '\0';
1180  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1181  beentry->st_clientaddr.salen,
1182  remote_host, sizeof(remote_host),
1183  NULL, 0,
1185  if (ret != 0)
1186  PG_RETURN_NULL();
1187 
1188  clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
1189 
1191  CStringGetDatum(remote_host)));
1192 }
#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 1195 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.

1196 {
1197  int32 beid = PG_GETARG_INT32(0);
1198  PgBackendStatus *beentry;
1199  SockAddr zero_clientaddr;
1200  char remote_port[NI_MAXSERV];
1201  int ret;
1202 
1203  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1204  PG_RETURN_NULL();
1205 
1206  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1207  PG_RETURN_NULL();
1208 
1209  /* A zeroed client addr means we don't know */
1210  memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
1211  if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
1212  sizeof(zero_clientaddr)) == 0)
1213  PG_RETURN_NULL();
1214 
1215  switch (beentry->st_clientaddr.addr.ss_family)
1216  {
1217  case AF_INET:
1218 #ifdef HAVE_IPV6
1219  case AF_INET6:
1220 #endif
1221  break;
1222  case AF_UNIX:
1223  PG_RETURN_INT32(-1);
1224  default:
1225  PG_RETURN_NULL();
1226  }
1227 
1228  remote_port[0] = '\0';
1229  ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
1230  beentry->st_clientaddr.salen,
1231  NULL, 0,
1232  remote_port, sizeof(remote_port),
1234  if (ret != 0)
1235  PG_RETURN_NULL();
1236 
1238  CStringGetDatum(remote_port)));
1239 }
#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 984 of file pgstatfuncs.c.

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

985 {
986  int32 beid = PG_GETARG_INT32(0);
987  PgBackendStatus *beentry;
988 
989  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
990  PG_RETURN_NULL();
991 
992  PG_RETURN_OID(beentry->st_databaseid);
993 }
#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 414 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.

415 {
416  FuncCallContext *funcctx;
417  int *fctx;
418  int32 result;
419 
420  /* stuff done only on the first call of the function */
421  if (SRF_IS_FIRSTCALL())
422  {
423  /* create a function context for cross-call persistence */
424  funcctx = SRF_FIRSTCALL_INIT();
425 
427  2 * sizeof(int));
428  funcctx->user_fctx = fctx;
429 
430  fctx[0] = 0;
431  fctx[1] = pgstat_fetch_stat_numbackends();
432  }
433 
434  /* stuff done on every call of the function */
435  funcctx = SRF_PERCALL_SETUP();
436  fctx = funcctx->user_fctx;
437 
438  fctx[0] += 1;
439  result = fctx[0];
440 
441  if (result <= fctx[1])
442  {
443  /* do when there is more left to send */
444  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
445  }
446  else
447  {
448  /* do when there is no more left */
449  SRF_RETURN_DONE(funcctx);
450  }
451 }
#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 971 of file pgstatfuncs.c.

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

972 {
973  int32 beid = PG_GETARG_INT32(0);
974  PgBackendStatus *beentry;
975 
976  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
977  PG_RETURN_NULL();
978 
979  PG_RETURN_INT32(beentry->st_procpid);
980 }
#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 1126 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.

1127 {
1128  int32 beid = PG_GETARG_INT32(0);
1129  TimestampTz result;
1130  PgBackendStatus *beentry;
1131 
1132  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1133  PG_RETURN_NULL();
1134 
1135  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1136  PG_RETURN_NULL();
1137 
1138  result = beentry->st_proc_start_timestamp;
1139 
1140  if (result == 0) /* probably can't happen? */
1141  PG_RETURN_NULL();
1142 
1143  PG_RETURN_TIMESTAMPTZ(result);
1144 }
#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 997 of file pgstatfuncs.c.

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

998 {
999  int32 beid = PG_GETARG_INT32(0);
1000  PgBackendStatus *beentry;
1001 
1002  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1003  PG_RETURN_NULL();
1004 
1005  PG_RETURN_OID(beentry->st_userid);
1006 }
#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 1056 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.

1057 {
1058  int32 beid = PG_GETARG_INT32(0);
1059  PgBackendStatus *beentry;
1060  PGPROC *proc;
1061  const char *wait_event = NULL;
1062 
1063  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1064  wait_event = "<backend information not available>";
1065  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1066  wait_event = "<insufficient privilege>";
1067  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1068  wait_event = pgstat_get_wait_event(proc->wait_event_info);
1069 
1070  if (!wait_event)
1071  PG_RETURN_NULL();
1072 
1073  PG_RETURN_TEXT_P(cstring_to_text(wait_event));
1074 }
#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:189
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 1035 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.

1036 {
1037  int32 beid = PG_GETARG_INT32(0);
1038  PgBackendStatus *beentry;
1039  PGPROC *proc;
1040  const char *wait_event_type = NULL;
1041 
1042  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1043  wait_event_type = "<backend information not available>";
1044  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1045  wait_event_type = "<insufficient privilege>";
1046  else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
1047  wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
1048 
1049  if (!wait_event_type)
1050  PG_RETURN_NULL();
1051 
1052  PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
1053 }
#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:189
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 1104 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.

1105 {
1106  int32 beid = PG_GETARG_INT32(0);
1107  TimestampTz result;
1108  PgBackendStatus *beentry;
1109 
1110  if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
1111  PG_RETURN_NULL();
1112 
1113  else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
1114  PG_RETURN_NULL();
1115 
1116  result = beentry->st_xact_start_timestamp;
1117 
1118  if (result == 0) /* not in a transaction */
1119  PG_RETURN_NULL();
1120 
1121  PG_RETURN_TIMESTAMPTZ(result);
1122 }
#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 1742 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

1743 {
1744  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_checkpoints);
1745 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
Definition: pgstat.c:2919

◆ pg_stat_get_bgwriter_buf_written_clean()

Datum pg_stat_get_bgwriter_buf_written_clean ( PG_FUNCTION_ARGS  )

Definition at line 1748 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

1749 {
1750  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean);
1751 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
Definition: pgstat.c:2903

◆ pg_stat_get_bgwriter_maxwritten_clean()

Datum pg_stat_get_bgwriter_maxwritten_clean ( PG_FUNCTION_ARGS  )

Definition at line 1754 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

1755 {
1756  PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean);
1757 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
Definition: pgstat.c:2903

◆ pg_stat_get_bgwriter_requested_checkpoints()

Datum pg_stat_get_bgwriter_requested_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1736 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

1737 {
1738  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->requested_checkpoints);
1739 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
Definition: pgstat.c:2919

◆ pg_stat_get_bgwriter_stat_reset_time()

Datum pg_stat_get_bgwriter_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1776 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_bgwriter().

1777 {
1778  PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp);
1779 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
Definition: pgstat.c:2903

◆ pg_stat_get_bgwriter_timed_checkpoints()

Datum pg_stat_get_bgwriter_timed_checkpoints ( PG_FUNCTION_ARGS  )

Definition at line 1730 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

1731 {
1732  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->timed_checkpoints);
1733 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
Definition: pgstat.c:2919

◆ pg_stat_get_blocks_fetched()

Datum pg_stat_get_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 216 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_blocks_hit()

Datum pg_stat_get_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 232 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1794 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

1795 {
1797 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
Definition: pgstat.c:2903

◆ pg_stat_get_buf_fsync_backend()

Datum pg_stat_get_buf_fsync_backend ( PG_FUNCTION_ARGS  )

Definition at line 1788 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

1789 {
1790  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_fsync_backend);
1791 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
Definition: pgstat.c:2919

◆ pg_stat_get_buf_written_backend()

Datum pg_stat_get_buf_written_backend ( PG_FUNCTION_ARGS  )

Definition at line 1782 of file pgstatfuncs.c.

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

1783 {
1784  PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buf_written_backend);
1785 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
Definition: pgstat.c:2919

◆ pg_stat_get_checkpoint_sync_time()

Datum pg_stat_get_checkpoint_sync_time ( PG_FUNCTION_ARGS  )

Definition at line 1768 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

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

◆ pg_stat_get_checkpoint_write_time()

Datum pg_stat_get_checkpoint_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1760 of file pgstatfuncs.c.

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

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

◆ pg_stat_get_db_active_time()

Datum pg_stat_get_db_active_time ( PG_FUNCTION_ARGS  )

Definition at line 1650 of file pgstatfuncs.c.

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

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

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

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

◆ pg_stat_get_db_blk_write_time()

Datum pg_stat_get_db_blk_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1620 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_blocks_fetched()

Datum pg_stat_get_db_blocks_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1296 of file pgstatfuncs.c.

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

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

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

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

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

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

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

◆ pg_stat_get_db_conflict_bufferpin()

Datum pg_stat_get_db_conflict_bufferpin ( PG_FUNCTION_ARGS  )

Definition at line 1501 of file pgstatfuncs.c.

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

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

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

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

◆ pg_stat_get_db_conflict_snapshot()

Datum pg_stat_get_db_conflict_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1486 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_conflict_startup_deadlock()

Datum pg_stat_get_db_conflict_startup_deadlock ( PG_FUNCTION_ARGS  )

Definition at line 1516 of file pgstatfuncs.c.

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

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

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

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

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

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

◆ pg_stat_get_db_idle_in_transaction_time()

Datum pg_stat_get_db_idle_in_transaction_time ( PG_FUNCTION_ARGS  )

Definition at line 1664 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 1243 of file pgstatfuncs.c.

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

1244 {
1245  Oid dbid = PG_GETARG_OID(0);
1246  int32 result;
1247  int tot_backends = pgstat_fetch_stat_numbackends();
1248  int beid;
1249 
1250  result = 0;
1251  for (beid = 1; beid <= tot_backends; beid++)
1252  {
1253  PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
1254 
1255  if (beentry && beentry->st_databaseid == dbid)
1256  result++;
1257  }
1258 
1259  PG_RETURN_INT32(result);
1260 }
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 1636 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_sessions()

Datum pg_stat_get_db_sessions ( PG_FUNCTION_ARGS  )

Definition at line 1678 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_sessions_abandoned()

Datum pg_stat_get_db_sessions_abandoned ( PG_FUNCTION_ARGS  )

Definition at line 1691 of file pgstatfuncs.c.

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

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

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

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

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

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

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

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

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

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

◆ pg_stat_get_db_temp_files()

Datum pg_stat_get_db_temp_files ( PG_FUNCTION_ARGS  )

Definition at line 1425 of file pgstatfuncs.c.

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

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

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

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

◆ pg_stat_get_db_tuples_fetched()

Datum pg_stat_get_db_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 1344 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_db_tuples_inserted()

Datum pg_stat_get_db_tuples_inserted ( PG_FUNCTION_ARGS  )

Definition at line 1360 of file pgstatfuncs.c.

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

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

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

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

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

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

◆ pg_stat_get_db_xact_commit()

Datum pg_stat_get_db_xact_commit ( PG_FUNCTION_ARGS  )

Definition at line 1264 of file pgstatfuncs.c.

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

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

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

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

◆ pg_stat_get_dead_tuples()

Datum pg_stat_get_dead_tuples ( PG_FUNCTION_ARGS  )

Definition at line 168 of file pgstatfuncs.c.

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

169 {
170  Oid relid = PG_GETARG_OID(0);
171  int64 result;
172  PgStat_StatTabEntry *tabentry;
173 
174  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
175  result = 0;
176  else
177  result = (int64) (tabentry->n_dead_tuples);
178 
179  PG_RETURN_INT64(result);
180 }
#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:798
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2801
#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 379 of file pgstatfuncs.c.

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

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

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

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

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

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

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

201 {
202  Oid relid = PG_GETARG_OID(0);
203  int64 result;
204  PgStat_StatTabEntry *tabentry;
205 
206  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
207  result = 0;
208  else
209  result = (int64) (tabentry->inserts_since_vacuum);
210 
211  PG_RETURN_INT64(result);
212 }
#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:800
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2801
#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 283 of file pgstatfuncs.c.

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

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

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

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

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

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

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

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

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

153 {
154  Oid relid = PG_GETARG_OID(0);
155  int64 result;
156  PgStat_StatTabEntry *tabentry;
157 
158  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
159  result = 0;
160  else
161  result = (int64) (tabentry->n_live_tuples);
162 
163  PG_RETURN_INT64(result);
164 }
#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:797
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2801
#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 184 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_numscans()

Datum pg_stat_get_numscans ( PG_FUNCTION_ARGS  )

Definition at line 40 of file pgstatfuncs.c.

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

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

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

2314 {
2315 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2316  text *slotname_text = PG_GETARG_TEXT_P(0);
2317  NameData slotname;
2318  TupleDesc tupdesc;
2321  PgStat_StatReplSlotEntry *slotent;
2322  PgStat_StatReplSlotEntry allzero;
2323 
2324  /* Initialise values and NULL flags arrays */
2325  MemSet(values, 0, sizeof(values));
2326  MemSet(nulls, 0, sizeof(nulls));
2327 
2328  /* Initialise attributes information in the tuple descriptor */
2330  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2331  TEXTOID, -1, 0);
2332  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2333  INT8OID, -1, 0);
2334  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2335  INT8OID, -1, 0);
2336  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2337  INT8OID, -1, 0);
2338  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2339  INT8OID, -1, 0);
2340  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2341  INT8OID, -1, 0);
2342  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2343  INT8OID, -1, 0);
2344  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2345  INT8OID, -1, 0);
2346  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2347  INT8OID, -1, 0);
2348  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2349  TIMESTAMPTZOID, -1, 0);
2350  BlessTupleDesc(tupdesc);
2351 
2352  namestrcpy(&slotname, text_to_cstring(slotname_text));
2353  slotent = pgstat_fetch_replslot(slotname);
2354  if (!slotent)
2355  {
2356  /*
2357  * If the slot is not found, initialise its stats. This is possible if
2358  * the create slot message is lost.
2359  */
2360  memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2361  slotent = &allzero;
2362  }
2363 
2364  values[0] = CStringGetTextDatum(NameStr(slotname));
2365  values[1] = Int64GetDatum(slotent->spill_txns);
2366  values[2] = Int64GetDatum(slotent->spill_count);
2367  values[3] = Int64GetDatum(slotent->spill_bytes);
2368  values[4] = Int64GetDatum(slotent->stream_txns);
2369  values[5] = Int64GetDatum(slotent->stream_count);
2370  values[6] = Int64GetDatum(slotent->stream_bytes);
2371  values[7] = Int64GetDatum(slotent->total_txns);
2372  values[8] = Int64GetDatum(slotent->total_bytes);
2373 
2374  if (slotent->stat_reset_timestamp == 0)
2375  nulls[9] = true;
2376  else
2377  values[9] = TimestampTzGetDatum(slotent->stat_reset_timestamp);
2378 
2379  /* Returns the record as Datum */
2380  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
2381 }
#define PG_STAT_GET_REPLICATION_SLOT_COLS
PgStat_Counter spill_bytes
Definition: pgstat.h:923
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:928
TimestampTz stat_reset_timestamp
Definition: pgstat.h:929
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:926
PgStat_Counter spill_txns
Definition: pgstat.h:921
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
PgStat_Counter spill_count
Definition: pgstat.h:922
static Datum values[MAXATTR]
Definition: bootstrap.c:156
char * text_to_cstring(const text *t)
Definition: varlena.c:222
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
Definition: pgstat.c:2983
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:336
PgStat_Counter stream_count
Definition: pgstat.h:925
#define NameStr(name)
Definition: c.h:681
#define CStringGetTextDatum(s)
Definition: builtins.h:86
Definition: c.h:621
PgStat_Counter total_txns
Definition: pgstat.h:927
int16 AttrNumber
Definition: attnum.h:21
PgStat_Counter stream_txns
Definition: pgstat.h:924

◆ pg_stat_get_slru()

Datum pg_stat_get_slru ( PG_FUNCTION_ARGS  )

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

1872 {
1873 #define PG_STAT_GET_SLRU_COLS 9
1874  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1875  TupleDesc tupdesc;
1876  Tuplestorestate *tupstore;
1877  MemoryContext per_query_ctx;
1878  MemoryContext oldcontext;
1879  int i;
1880  PgStat_SLRUStats *stats;
1881 
1882  /* check to see if caller supports us returning a tuplestore */
1883  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1884  ereport(ERROR,
1885  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1886  errmsg("set-valued function called in context that cannot accept a set")));
1887  if (!(rsinfo->allowedModes & SFRM_Materialize))
1888  ereport(ERROR,
1889  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1890  errmsg("materialize mode required, but it is not allowed in this context")));
1891 
1892  /* Build a tuple descriptor for our result type */
1893  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1894  elog(ERROR, "return type must be a row type");
1895 
1896  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1897  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1898 
1899  tupstore = tuplestore_begin_heap(true, false, work_mem);
1900  rsinfo->returnMode = SFRM_Materialize;
1901  rsinfo->setResult = tupstore;
1902  rsinfo->setDesc = tupdesc;
1903 
1904  MemoryContextSwitchTo(oldcontext);
1905 
1906  /* request SLRU stats from the stat collector */
1907  stats = pgstat_fetch_slru();
1908 
1909  for (i = 0;; i++)
1910  {
1911  /* for each row */
1913  bool nulls[PG_STAT_GET_SLRU_COLS];
1914  PgStat_SLRUStats stat = stats[i];
1915  const char *name;
1916 
1917  name = pgstat_slru_name(i);
1918 
1919  if (!name)
1920  break;
1921 
1922  MemSet(values, 0, sizeof(values));
1923  MemSet(nulls, 0, sizeof(nulls));
1924 
1925  values[0] = PointerGetDatum(cstring_to_text(name));
1926  values[1] = Int64GetDatum(stat.blocks_zeroed);
1927  values[2] = Int64GetDatum(stat.blocks_hit);
1928  values[3] = Int64GetDatum(stat.blocks_read);
1929  values[4] = Int64GetDatum(stat.blocks_written);
1930  values[5] = Int64GetDatum(stat.blocks_exists);
1931  values[6] = Int64GetDatum(stat.flush);
1932  values[7] = Int64GetDatum(stat.truncate);
1933  values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1934 
1935  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1936  }
1937 
1938  /* clean up and return the tuplestore */
1939  tuplestore_donestoring(tupstore);
1940 
1941  return (Datum) 0;
1942 }
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:588
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:912
#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:909
#define ERROR
Definition: elog.h:46
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter blocks_read
Definition: pgstat.h:907
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
PgStat_Counter blocks_hit
Definition: pgstat.h:906
PgStat_Counter blocks_zeroed
Definition: pgstat.h:905
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:306
SetFunctionReturnMode returnMode
Definition: execnodes.h:308
text * cstring_to_text(const char *s)
Definition: varlena.c:189
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat.c:2967
PgStat_Counter flush
Definition: pgstat.h:910
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:234
const char * name
Definition: encode.c:561
Tuplestorestate * setResult
Definition: execnodes.h:311
static Datum values[MAXATTR]
Definition: bootstrap.c:156
ExprContext * econtext
Definition: execnodes.h:304
TupleDesc setDesc
Definition: execnodes.h:312
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:5967
PgStat_Counter truncate
Definition: pgstat.h:911
PgStat_Counter blocks_written
Definition: pgstat.h:908

◆ pg_stat_get_snapshot_timestamp()

Datum pg_stat_get_snapshot_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2133 of file pgstatfuncs.c.

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_global().

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

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

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

◆ pg_stat_get_tuples_fetched()

Datum pg_stat_get_tuples_fetched ( PG_FUNCTION_ARGS  )

Definition at line 72 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_tuples_hot_updated()

Datum pg_stat_get_tuples_hot_updated ( PG_FUNCTION_ARGS  )

Definition at line 136 of file pgstatfuncs.c.

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

137 {
138  Oid relid = PG_GETARG_OID(0);
139  int64 result;
140  PgStat_StatTabEntry *tabentry;
141 
142  if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
143  result = 0;
144  else
145  result = (int64) (tabentry->tuples_hot_updated);
146 
147  PG_RETURN_INT64(result);
148 }
#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:795
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
Definition: pgstat.c:2801
#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 88 of file pgstatfuncs.c.

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

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

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

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

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

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

◆ pg_stat_get_vacuum_count()

Datum pg_stat_get_vacuum_count ( PG_FUNCTION_ARGS  )

Definition at line 319 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

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

1804 {
1805 #define PG_STAT_GET_WAL_COLS 9
1806  TupleDesc tupdesc;
1808  bool nulls[PG_STAT_GET_WAL_COLS];
1809  char buf[256];
1810  PgStat_WalStats *wal_stats;
1811 
1812  /* Initialise values and NULL flags arrays */
1813  MemSet(values, 0, sizeof(values));
1814  MemSet(nulls, 0, sizeof(nulls));
1815 
1816  /* Initialise attributes information in the tuple descriptor */
1818  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1819  INT8OID, -1, 0);
1820  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1821  INT8OID, -1, 0);
1822  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1823  NUMERICOID, -1, 0);
1824  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1825  INT8OID, -1, 0);
1826  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1827  INT8OID, -1, 0);
1828  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1829  INT8OID, -1, 0);
1830  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1831  FLOAT8OID, -1, 0);
1832  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1833  FLOAT8OID, -1, 0);
1834  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1835  TIMESTAMPTZOID, -1, 0);
1836 
1837  BlessTupleDesc(tupdesc);
1838 
1839  /* Get statistics about WAL activity */
1840  wal_stats = pgstat_fetch_stat_wal();
1841 
1842  /* Fill values and NULLs */
1843  values[0] = Int64GetDatum(wal_stats->wal_records);
1844  values[1] = Int64GetDatum(wal_stats->wal_fpi);
1845 
1846  /* Convert to numeric. */
1847  snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1848  values[2] = DirectFunctionCall3(numeric_in,
1849  CStringGetDatum(buf),
1850  ObjectIdGetDatum(0),
1851  Int32GetDatum(-1));
1852 
1853  values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1854  values[4] = Int64GetDatum(wal_stats->wal_write);
1855  values[5] = Int64GetDatum(wal_stats->wal_sync);
1856 
1857  /* Convert counters from microsec to millisec for display */
1858  values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1859  values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1860 
1861  values[8] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
1862 
1863  /* Returns the record as Datum */
1864  PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
1865 }
PgStat_Counter wal_records
Definition: pgstat.h:889
PgStat_Counter wal_write
Definition: pgstat.h:893
TimestampTz stat_reset_timestamp
Definition: pgstat.h:897
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
PgStat_Counter wal_sync_time
Definition: pgstat.h:896
#define PG_STAT_GET_WAL_COLS
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat.c:2951
#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:895
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
PgStat_Counter wal_fpi
Definition: pgstat.h:890
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:616
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:891
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define Int32GetDatum(X)
Definition: postgres.h:523
PgStat_Counter wal_buffers_full
Definition: pgstat.h:892
PgStat_Counter wal_sync
Definition: pgstat.h:894
#define snprintf
Definition: port.h:217
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 2068 of file pgstatfuncs.c.

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

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

◆ pg_stat_get_xact_blocks_hit()

Datum pg_stat_get_xact_blocks_hit ( PG_FUNCTION_ARGS  )

Definition at line 2083 of file pgstatfuncs.c.

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

2084 {
2085  Oid relid = PG_GETARG_OID(0);
2086  int64 result;
2087  PgStat_TableStatus *tabentry;
2088 
2089  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2090  result = 0;
2091  else
2092  result = (int64) (tabentry->t_counts.t_blocks_hit);
2093 
2094  PG_RETURN_INT64(result);
2095 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter t_blocks_hit
Definition: pgstat.h:133

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

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

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

2121 {
2122  Oid funcid = PG_GETARG_OID(0);
2123  PgStat_BackendFunctionEntry *funcentry;
2124 
2125  if ((funcentry = find_funcstat_entry(funcid)) == NULL)
2126  PG_RETURN_NULL();
2128 }
instr_time f_self_time
Definition: pgstat.h:578
#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:588
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_BackendFunctionEntry * find_funcstat_entry(Oid func_id)
Definition: pgstat.c:1965
#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 2109 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.

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

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

1946 {
1947  Oid relid = PG_GETARG_OID(0);
1948  int64 result;
1949  PgStat_TableStatus *tabentry;
1950 
1951  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1952  result = 0;
1953  else
1954  result = (int64) (tabentry->t_counts.t_numscans);
1955 
1956  PG_RETURN_INT64(result);
1957 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
PgStat_Counter t_numscans
Definition: pgstat.h:117

◆ pg_stat_get_xact_tuples_deleted()

Datum pg_stat_get_xact_tuples_deleted ( PG_FUNCTION_ARGS  )

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

2033 {
2034  Oid relid = PG_GETARG_OID(0);
2035  int64 result;
2036  PgStat_TableStatus *tabentry;
2038 
2039  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2040  result = 0;
2041  else
2042  {
2043  result = tabentry->t_counts.t_tuples_deleted;
2044  /* live subtransactions' counts aren't in t_tuples_deleted yet */
2045  for (trans = tabentry->trans; trans != NULL; trans = trans->upper)
2046  result += trans->tuples_deleted;
2047  }
2048 
2049  PG_RETURN_INT64(result);
2050 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:197
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:124
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
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 1975 of file pgstatfuncs.c.

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

1976 {
1977  Oid relid = PG_GETARG_OID(0);
1978  int64 result;
1979  PgStat_TableStatus *tabentry;
1980 
1981  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1982  result = 0;
1983  else
1984  result = (int64) (tabentry->t_counts.t_tuples_fetched);
1985 
1986  PG_RETURN_INT64(result);
1987 }
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:120
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#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 2053 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.

2054 {
2055  Oid relid = PG_GETARG_OID(0);
2056  int64 result;
2057  PgStat_TableStatus *tabentry;
2058 
2059  if ((tabentry = find_tabstat_entry(relid)) == NULL)
2060  result = 0;
2061  else
2062  result = (int64) (tabentry->t_counts.t_tuples_hot_updated);
2063 
2064  PG_RETURN_INT64(result);
2065 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:125
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#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 1990 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.

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

◆ pg_stat_get_xact_tuples_returned()

Datum pg_stat_get_xact_tuples_returned ( PG_FUNCTION_ARGS  )

Definition at line 1960 of file pgstatfuncs.c.

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

1961 {
1962  Oid relid = PG_GETARG_OID(0);
1963  int64 result;
1964  PgStat_TableStatus *tabentry;
1965 
1966  if ((tabentry = find_tabstat_entry(relid)) == NULL)
1967  result = 0;
1968  else
1969  result = (int64) (tabentry->t_counts.t_tuples_returned);
1970 
1971  PG_RETURN_INT64(result);
1972 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
PgStat_TableCounts t_counts
Definition: pgstat.h:177
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter t_tuples_returned
Definition: pgstat.h:119
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2171
#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 2011 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.

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

◆ pg_stat_reset()

Datum pg_stat_reset ( PG_FUNCTION_ARGS  )

Definition at line 2150 of file pgstatfuncs.c.

References PG_RETURN_VOID, and pgstat_reset_counters().

2151 {
2153 
2154  PG_RETURN_VOID();
2155 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_reset_counters(void)
Definition: pgstat.c:1422

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 2205 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().

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

◆ pg_stat_reset_shared()

Datum pg_stat_reset_shared ( PG_FUNCTION_ARGS  )

Definition at line 2159 of file pgstatfuncs.c.

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

2160 {
2161  char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2162 
2164 
2165  PG_RETURN_VOID();
2166 }
void pgstat_reset_shared_counters(const char *target)
Definition: pgstat.c:1444
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_VOID()
Definition: fmgr.h:349
char * text_to_cstring(const text *t)
Definition: varlena.c:222

◆ pg_stat_reset_single_function_counters()

Datum pg_stat_reset_single_function_counters ( PG_FUNCTION_ARGS  )

Definition at line 2180 of file pgstatfuncs.c.

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

2181 {
2182  Oid funcoid = PG_GETARG_OID(0);
2183 
2185 
2186  PG_RETURN_VOID();
2187 }
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:1477
#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 2170 of file pgstatfuncs.c.

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

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

◆ pg_stat_reset_slru()

Datum pg_stat_reset_slru ( PG_FUNCTION_ARGS  )

Definition at line 2191 of file pgstatfuncs.c.

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

2192 {
2193  char *target = NULL;
2194 
2195  if (!PG_ARGISNULL(0))
2196  target = text_to_cstring(PG_GETARG_TEXT_PP(0));
2197 
2198  pgstat_reset_slru_counter(target);
2199 
2200  PG_RETURN_VOID();
2201 }
void pgstat_reset_slru_counter(const char *name)
Definition: pgstat.c:1503
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
char * text_to_cstring(const text *t)
Definition: varlena.c:222