35 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
37 #define HAS_PGSTAT_PERMISSIONS(role) (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
39 #define PG_STAT_GET_RELENTRY_INT64(stat) \
41 CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \
43 Oid relid = PG_GETARG_OID(0); \
45 PgStat_StatTabEntry *tabentry; \
47 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
50 result = (int64) (tabentry->stat); \
52 PG_RETURN_INT64(result); \
109 #define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat) \
111 CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \
113 Oid relid = PG_GETARG_OID(0); \
114 TimestampTz result; \
115 PgStat_StatTabEntry *tabentry; \
117 if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
120 result = tabentry->stat; \
125 PG_RETURN_TIMESTAMPTZ(result); \
155 #define PG_STAT_GET_FUNCENTRY_FLOAT8_MS(stat) \
157 CppConcat(pg_stat_get_function_,stat)(PG_FUNCTION_ARGS) \
159 Oid funcid = PG_GETARG_OID(0); \
161 PgStat_StatFuncEntry *funcentry; \
163 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL) \
165 result = ((double) funcentry->stat) / 1000.0; \
166 PG_RETURN_FLOAT8(result); \
229 #define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
251 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
252 errmsg(
"invalid command name: \"%s\"", cmd)));
257 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
305 #define PG_STAT_GET_ACTIVITY_COLS 31
314 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
322 const char *wait_event_type = NULL;
323 const char *wait_event = NULL;
365 char *clipped_activity;
394 pfree(clipped_activity);
486 memset(&zero_clientaddr, 0,
sizeof(zero_clientaddr));
488 sizeof(zero_clientaddr)) == 0)
499 char remote_host[NI_MAXHOST];
500 char remote_port[NI_MAXSERV];
503 remote_host[0] =
'\0';
504 remote_port[0] =
'\0';
507 remote_host,
sizeof(remote_host),
508 remote_port,
sizeof(remote_port),
509 NI_NUMERICHOST | NI_NUMERICSERV);
552 const char *bgw_type;
593 nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] =
true;
708 #define PG_STAT_GET_SUBXACT_COLS 2
745 const char *activity;
746 char *clipped_activity;
750 activity =
"<backend information not available>";
752 activity =
"<insufficient privilege>";
754 activity =
"<command string not enabled>";
760 pfree(clipped_activity);
771 const char *wait_event_type = NULL;
774 wait_event_type =
"<backend information not available>";
776 wait_event_type =
"<insufficient privilege>";
780 if (!wait_event_type)
792 const char *wait_event = NULL;
795 wait_event =
"<backend information not available>";
797 wait_event =
"<insufficient privilege>";
884 char remote_host[NI_MAXHOST];
894 memset(&zero_clientaddr, 0,
sizeof(zero_clientaddr));
896 sizeof(zero_clientaddr)) == 0)
908 remote_host[0] =
'\0';
911 remote_host,
sizeof(remote_host),
913 NI_NUMERICHOST | NI_NUMERICSERV);
929 char remote_port[NI_MAXSERV];
939 memset(&zero_clientaddr, 0,
sizeof(zero_clientaddr));
941 sizeof(zero_clientaddr)) == 0)
955 remote_port[0] =
'\0';
959 remote_port,
sizeof(remote_port),
960 NI_NUMERICHOST | NI_NUMERICSERV);
978 for (
idx = 1;
idx <= tot_backends;
idx++)
990 #define PG_STAT_GET_DBENTRY_INT64(stat) \
992 CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
994 Oid dbid = PG_GETARG_OID(0); \
996 PgStat_StatDBEntry *dbentry; \
998 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
1001 result = (int64) (dbentry->stat); \
1003 PG_RETURN_INT64(result); \
1151 #define PG_STAT_GET_DBENTRY_FLOAT8_MS(stat) \
1153 CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
1155 Oid dbid = PG_GETARG_OID(0); \
1157 PgStat_StatDBEntry *dbentry; \
1159 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
1162 result = ((double) dbentry->stat) / 1000.0; \
1164 PG_RETURN_FLOAT8(result); \
1331 elog(
ERROR,
"unrecognized IOOp value: %d", io_op);
1358 elog(
ERROR,
"unrecognized IOOp value: %d", io_op);
1362 static inline double
1365 return val_ms * (double) 0.001;
1446 bktype_stats->
counts[io_obj][io_context][io_op];
1451 nulls[op_idx] =
true;
1460 bktype_stats->
times[io_obj][io_context][io_op];
1465 nulls[time_idx] =
true;
1483 #define PG_STAT_GET_WAL_COLS 9
1509 TIMESTAMPTZOID, -1, 0);
1547 #define PG_STAT_GET_SLRU_COLS 9
1588 #define PG_STAT_GET_XACT_RELENTRY_INT64(stat) \
1590 CppConcat(pg_stat_get_xact_,stat)(PG_FUNCTION_ARGS) \
1592 Oid relid = PG_GETARG_OID(0); \
1594 PgStat_TableStatus *tabentry; \
1596 if ((tabentry = find_tabstat_entry(relid)) == NULL) \
1599 result = (int64) (tabentry->counts.stat); \
1601 PG_RETURN_INT64(result); \
1645 #define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(stat) \
1647 CppConcat(pg_stat_get_xact_function_,stat)(PG_FUNCTION_ARGS) \
1649 Oid funcid = PG_GETARG_OID(0); \
1650 PgStat_FunctionCounts *funcentry; \
1652 if ((funcentry = find_funcstat_entry(funcid)) == NULL) \
1654 PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->stat)); \
1716 char *target = NULL;
1734 if (strcmp(target,
"archiver") == 0)
1736 else if (strcmp(target,
"bgwriter") == 0)
1738 else if (strcmp(target,
"checkpointer") == 0)
1740 else if (strcmp(target,
"io") == 0)
1742 else if (strcmp(target,
"recovery_prefetch") == 0)
1744 else if (strcmp(target,
"slru") == 0)
1746 else if (strcmp(target,
"wal") == 0)
1750 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1751 errmsg(
"unrecognized reset target: \"%s\"", target),
1752 errhint(
"Target must be \"archiver\", \"bgwriter\", \"checkpointer\", \"io\", \"recovery_prefetch\", \"slru\", or \"wal\".")));
1786 char *target = NULL;
1803 char *target = NULL;
1833 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1834 errmsg(
"invalid subscription OID %u", subid)));
1846 bool nulls[7] = {0};
1856 TIMESTAMPTZOID, -1, 0);
1862 TIMESTAMPTZOID, -1, 0);
1864 TIMESTAMPTZOID, -1, 0);
1910 #define PG_STAT_GET_REPLICATION_SLOT_COLS 10
1940 TIMESTAMPTZOID, -1, 0);
1981 #define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 10
2014 TIMESTAMPTZOID, -1, 0);
2021 subentry = &allzero;
Datum idx(PG_FUNCTION_ARGS)
Datum numeric_in(PG_FUNCTION_ARGS)
#define PGSTAT_NUM_PROGRESS_PARAM
@ PROGRESS_COMMAND_ANALYZE
@ PROGRESS_COMMAND_CLUSTER
@ PROGRESS_COMMAND_CREATE_INDEX
@ PROGRESS_COMMAND_VACUUM
@ PROGRESS_COMMAND_BASEBACKUP
int pgstat_fetch_stat_numbackends(void)
PgBackendStatus * pgstat_get_beentry_by_proc_number(ProcNumber procNumber)
LocalPgBackendStatus * pgstat_get_local_beentry_by_index(int idx)
LocalPgBackendStatus * pgstat_get_local_beentry_by_proc_number(ProcNumber procNumber)
char * pgstat_clip_activity(const char *raw_activity)
@ STATE_IDLEINTRANSACTION_ABORTED
@ STATE_IDLEINTRANSACTION
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define Assert(condition)
#define OidIsValid(objectId)
bool IsSharedRelation(Oid relationId)
#define CONFLICT_NUM_TYPES
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Datum Int64GetDatum(int64 X)
Datum Float8GetDatum(float8 X)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_FLOAT8(x)
#define PG_RETURN_INT64(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_INT64(n)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_RETURN_DATUM(x)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define PG_RETURN_BOOL(x)
#define PG_GETARG_TEXT_P(n)
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
#define SRF_IS_FIRSTCALL()
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
#define SRF_RETURN_DONE(_funcctx)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Datum int4in(PG_FUNCTION_ARGS)
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
if(TABLE==NULL||TABLE_index==NULL)
pid_t GetLeaderApplyWorkerPid(pid_t pid)
void pfree(void *pointer)
void * MemoryContextAlloc(MemoryContext context, Size size)
#define BACKEND_NUM_TYPES
const char * GetBackendTypeDesc(BackendType backendType)
void namestrcpy(Name name, const char *str)
void clean_ipv6_addr(int addr_family, char *addr)
Datum inet_in(PG_FUNCTION_ARGS)
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_reset_counters(void)
void pgstat_reset_of_kind(PgStat_Kind kind)
void pgstat_force_next_flush(void)
void pgstat_clear_snapshot(void)
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
#define PGSTAT_KIND_ARCHIVER
#define IOCONTEXT_NUM_TYPES
#define PGSTAT_KIND_BGWRITER
#define PGSTAT_KIND_REPLSLOT
#define PGSTAT_KIND_FUNCTION
#define PGSTAT_KIND_RELATION
#define PGSTAT_KIND_CHECKPOINTER
#define PGSTAT_KIND_SUBSCRIPTION
#define IOOBJECT_NUM_TYPES
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)
PgStat_FunctionCounts * find_funcstat_entry(Oid func_id)
bool pgstat_tracks_io_bktype(BackendType bktype)
const char * pgstat_get_io_object_name(IOObject io_object)
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
PgStat_IO * pgstat_fetch_stat_io(void)
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
const char * pgstat_get_io_context_name(IOContext io_context)
bool pgstat_tracks_io_object(BackendType bktype, IOObject io_object, IOContext io_context)
void pgstat_reset_replslot(const char *name)
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
void pgstat_reset_slru(const char *name)
PgStat_SLRUStats * pgstat_fetch_slru(void)
const char * pgstat_get_slru_name(int slru_idx)
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
PgStat_WalStats * pgstat_fetch_stat_wal(void)
#define PG_STAT_GET_XACT_RELENTRY_INT64(stat)
Datum pg_stat_get_progress_info(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_num_requested(PG_FUNCTION_ARGS)
Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS)
#define PG_STAT_GET_WAL_COLS
Datum pg_stat_get_wal(PG_FUNCTION_ARGS)
Datum pg_stat_reset_replication_slot(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_num_timed(PG_FUNCTION_ARGS)
#define UINT32_ACCESS_ONCE(var)
Datum pg_stat_get_activity(PG_FUNCTION_ARGS)
Datum pg_stat_get_slru(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS)
Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS)
Datum pg_stat_get_subscription_stats(PG_FUNCTION_ARGS)
#define PG_STAT_GET_SLRU_COLS
#define PG_STAT_GET_DBENTRY_INT64(stat)
Datum pg_stat_get_checkpointer_buffers_written(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_sync_time(PG_FUNCTION_ARGS)
static double pg_stat_us_to_ms(PgStat_Counter val_ms)
#define PG_STAT_GET_ACTIVITY_COLS
static io_stat_col pgstat_get_io_time_index(IOOp io_op)
Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS(stat)
Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
Datum pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS)
Datum pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS)
Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
Datum pg_stat_reset_slru(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_restartpoints_requested(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_num_performed(PG_FUNCTION_ARGS)
Datum pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS)
static io_stat_col pgstat_get_io_op_index(IOOp io_op)
Datum pg_stat_have_stats(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS
Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
Datum pg_stat_get_io(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_restartpoints_performed(PG_FUNCTION_ARGS)
#define PG_STAT_GET_DBENTRY_FLOAT8_MS(stat)
Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
Datum pg_stat_reset(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
#define PG_STAT_GET_SUBXACT_COLS
#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(stat)
#define PG_STAT_GET_RELENTRY_INT64(stat)
Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS)
#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat)
#define PG_STAT_GET_PROGRESS_COLS
Datum pg_stat_get_checkpointer_write_time(PG_FUNCTION_ARGS)
#define HAS_PGSTAT_PERMISSIONS(role)
Datum pg_stat_get_checkpointer_slru_written(PG_FUNCTION_ARGS)
Datum pg_stat_get_archiver(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_restartpoints_timed(PG_FUNCTION_ARGS)
Datum pg_stat_reset_shared(PG_FUNCTION_ARGS)
Datum pg_stat_force_next_flush(PG_FUNCTION_ARGS)
Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
Datum pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
Datum pg_stat_get_checkpointer_stat_reset_time(PG_FUNCTION_ARGS)
Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_subxact(PG_FUNCTION_ARGS)
Datum pg_stat_get_replication_slot(PG_FUNCTION_ARGS)
Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
Datum pg_backend_pid(PG_FUNCTION_ARGS)
#define PG_STAT_GET_REPLICATION_SLOT_COLS
int pg_strcasecmp(const char *s1, const char *s2)
static Datum PointerGetDatum(const void *X)
static Datum TransactionIdGetDatum(TransactionId X)
static Datum UInt64GetDatum(uint64 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
PGPROC * BackendPidGetProc(int pid)
PGPROC * AuxiliaryPidGetProc(int pid)
MemoryContext multi_call_memory_ctx
TransactionId backend_xid
PgBackendStatus backendStatus
int backend_subxact_count
TransactionId backend_xmin
bool backend_subxact_overflowed
char gss_princ[NAMEDATALEN]
char ssl_version[NAMEDATALEN]
char ssl_cipher[NAMEDATALEN]
char ssl_client_dn[NAMEDATALEN]
char ssl_client_serial[NAMEDATALEN]
char ssl_issuer_dn[NAMEDATALEN]
BackendType st_backendType
TimestampTz st_state_start_timestamp
TimestampTz st_proc_start_timestamp
PgBackendGSSStatus * st_gssstatus
TimestampTz st_activity_start_timestamp
ProgressCommandType st_progress_command
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
PgBackendSSLStatus * st_sslstatus
TimestampTz st_xact_start_timestamp
Oid st_progress_command_target
TimestampTz last_failed_timestamp
TimestampTz stat_reset_timestamp
TimestampTz last_archived_timestamp
char last_failed_wal[MAX_XFN_CHARS+1]
PgStat_Counter failed_count
PgStat_Counter archived_count
char last_archived_wal[MAX_XFN_CHARS+1]
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
TimestampTz stat_reset_timestamp
PgStat_Counter conflict_startup_deadlock
PgStat_Counter conflict_lock
TimestampTz stat_reset_timestamp
PgStat_Counter conflict_snapshot
TimestampTz last_checksum_failure
PgStat_Counter conflict_bufferpin
PgStat_Counter conflict_logicalslot
PgStat_Counter checksum_failures
PgStat_Counter conflict_tablespace
TimestampTz stat_reset_timestamp
PgStat_Counter stream_count
PgStat_Counter total_txns
PgStat_Counter total_bytes
PgStat_Counter spill_txns
PgStat_Counter stream_txns
PgStat_Counter spill_count
PgStat_Counter stream_bytes
PgStat_Counter spill_bytes
PgStat_Counter apply_error_count
PgStat_Counter sync_error_count
PgStat_Counter conflict_count[CONFLICT_NUM_TYPES]
TimestampTz stat_reset_timestamp
PgStat_Counter wal_buffers_full
PgStat_Counter wal_write_time
TimestampTz stat_reset_timestamp
PgStat_Counter wal_sync_time
PgStat_Counter wal_records
Tuplestorestate * setResult
struct sockaddr_storage addr
#define TransactionIdIsValid(xid)
TupleDesc CreateTemplateTupleDesc(int natts)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
static Datum TimestampTzGetDatum(TimestampTz X)
#define PG_RETURN_TIMESTAMPTZ(x)
char * text_to_cstring(const text *t)
text * cstring_to_text(const char *s)
const char * pgstat_get_wait_event(uint32 wait_event_info)
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
bool DataChecksumsEnabled(void)
void XLogPrefetchResetStats(void)