PostgreSQL Source Code git master
pgstatfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xlog.h"
#include "access/xlogprefetcher.h"
#include "catalog/catalog.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.h"
#include "replication/logicallauncher.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/timestamp.h"
Include dependency graph for pgstatfuncs.c:

Go to the source code of this file.

Macros

#define UINT32_ACCESS_ONCE(var)   ((uint32)(*((volatile uint32 *)&(var))))
 
#define HAS_PGSTAT_PERMISSIONS(role)   (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
 
#define PG_STAT_GET_RELENTRY_INT64(stat)
 
#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat)
 
#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3
 
#define PG_STAT_GET_ACTIVITY_COLS   31
 
#define PG_STAT_GET_SUBXACT_COLS   2
 
#define PG_STAT_GET_DBENTRY_INT64(stat)
 
#define PG_STAT_GET_DBENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_WAL_COLS   9
 
#define PG_STAT_GET_SLRU_COLS   9
 
#define PG_STAT_GET_XACT_RELENTRY_INT64(stat)
 
#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(stat)
 
#define PG_STAT_GET_REPLICATION_SLOT_COLS   10
 
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS   10
 

Typedefs

typedef enum io_stat_col io_stat_col
 

Enumerations

enum  io_stat_col {
  IO_COL_INVALID = -1 , IO_COL_BACKEND_TYPE , IO_COL_OBJECT , IO_COL_CONTEXT ,
  IO_COL_READS , IO_COL_READ_BYTES , IO_COL_READ_TIME , IO_COL_WRITES ,
  IO_COL_WRITE_BYTES , IO_COL_WRITE_TIME , IO_COL_WRITEBACKS , IO_COL_WRITEBACK_TIME ,
  IO_COL_EXTENDS , IO_COL_EXTEND_BYTES , IO_COL_EXTEND_TIME , IO_COL_HITS ,
  IO_COL_EVICTIONS , IO_COL_REUSES , IO_COL_FSYNCS , IO_COL_FSYNC_TIME ,
  IO_COL_RESET_TIME , IO_NUM_COLUMNS
}
 

Functions

Datum pg_stat_get_function_calls (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_subxact (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_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_db_conflict_all (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_checkpointer_num_timed (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_num_requested (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_num_performed (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_restartpoints_timed (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_restartpoints_requested (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_restartpoints_performed (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_buffers_written (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_slru_written (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_checkpointer_write_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_sync_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_checkpointer_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_bgwriter_stat_reset_time (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_buf_alloc (PG_FUNCTION_ARGS)
 
static io_stat_col pgstat_get_io_op_index (IOOp io_op)
 
static io_stat_col pgstat_get_io_byte_index (IOOp io_op)
 
static io_stat_col pgstat_get_io_time_index (IOOp io_op)
 
static double pg_stat_us_to_ms (PgStat_Counter val_ms)
 
static void pg_stat_io_build_tuples (ReturnSetInfo *rsinfo, PgStat_BktypeIO *bktype_stats, BackendType bktype, TimestampTz stat_reset_timestamp)
 
Datum pg_stat_get_io (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_backend_io (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_wal (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_slru (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_xact_function_calls (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_snapshot_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_stat_clear_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_stat_force_next_flush (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_shared (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_single_table_counters (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_single_function_counters (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_backend_stats (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_slru (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_stat_reset_subscription_stats (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_archiver (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_stat_get_subscription_stats (PG_FUNCTION_ARGS)
 
Datum pg_stat_have_stats (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ HAS_PGSTAT_PERMISSIONS

#define HAS_PGSTAT_PERMISSIONS (   role)    (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))

Definition at line 37 of file pgstatfuncs.c.

◆ PG_STAT_GET_ACTIVITY_COLS

#define PG_STAT_GET_ACTIVITY_COLS   31

◆ PG_STAT_GET_DBENTRY_FLOAT8_MS

#define PG_STAT_GET_DBENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid dbid = PG_GETARG_OID(0); \
double result; \
PgStat_StatDBEntry *dbentry; \
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
result = 0; \
else \
result = ((double) dbentry->stat) / 1000.0; \
}
#define CppConcat(x, y)
Definition: c.h:350
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)

Definition at line 1151 of file pgstatfuncs.c.

◆ PG_STAT_GET_DBENTRY_INT64

#define PG_STAT_GET_DBENTRY_INT64 (   stat)
Value:
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid dbid = PG_GETARG_OID(0); \
int64 result; \
PgStat_StatDBEntry *dbentry; \
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \
result = 0; \
else \
result = (int64) (dbentry->stat); \
PG_RETURN_INT64(result); \
}
int64_t int64
Definition: c.h:485
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368

Definition at line 984 of file pgstatfuncs.c.

◆ PG_STAT_GET_FUNCENTRY_FLOAT8_MS

#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_function_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid funcid = PG_GETARG_OID(0); \
double result; \
PgStat_StatFuncEntry *funcentry; \
if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL) \
PG_RETURN_NULL(); \
result = ((double) funcentry->stat) / 1000.0; \
PG_RETURN_FLOAT8(result); \
}
PgStat_StatFuncEntry * pgstat_fetch_stat_funcentry(Oid func_id)

Definition at line 155 of file pgstatfuncs.c.

◆ PG_STAT_GET_PROGRESS_COLS

#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3

◆ PG_STAT_GET_RELENTRY_INT64

#define PG_STAT_GET_RELENTRY_INT64 (   stat)
Value:
{ \
Oid relid = PG_GETARG_OID(0); \
int64 result; \
PgStat_StatTabEntry *tabentry; \
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
result = 0; \
else \
result = (int64) (tabentry->stat); \
PG_RETURN_INT64(result); \
}
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)

Definition at line 39 of file pgstatfuncs.c.

◆ PG_STAT_GET_RELENTRY_TIMESTAMPTZ

#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ (   stat)
Value:
{ \
Oid relid = PG_GETARG_OID(0); \
TimestampTz result; \
PgStat_StatTabEntry *tabentry; \
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
result = 0; \
else \
result = tabentry->stat; \
if (result == 0) \
PG_RETURN_NULL(); \
}
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68

Definition at line 109 of file pgstatfuncs.c.

◆ PG_STAT_GET_REPLICATION_SLOT_COLS

#define PG_STAT_GET_REPLICATION_SLOT_COLS   10

◆ PG_STAT_GET_SLRU_COLS

#define PG_STAT_GET_SLRU_COLS   9

◆ PG_STAT_GET_SUBSCRIPTION_STATS_COLS

#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS   10

◆ PG_STAT_GET_SUBXACT_COLS

#define PG_STAT_GET_SUBXACT_COLS   2

◆ PG_STAT_GET_WAL_COLS

#define PG_STAT_GET_WAL_COLS   9

◆ PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS

#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS (   stat)
Value:
CppConcat(pg_stat_get_xact_function_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid funcid = PG_GETARG_OID(0); \
PgStat_FunctionCounts *funcentry; \
if ((funcentry = find_funcstat_entry(funcid)) == NULL) \
PG_RETURN_NULL(); \
PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->stat)); \
}
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
PgStat_FunctionCounts * find_funcstat_entry(Oid func_id)

Definition at line 1774 of file pgstatfuncs.c.

◆ PG_STAT_GET_XACT_RELENTRY_INT64

#define PG_STAT_GET_XACT_RELENTRY_INT64 (   stat)
Value:
CppConcat(pg_stat_get_xact_,stat)(PG_FUNCTION_ARGS) \
{ \
Oid relid = PG_GETARG_OID(0); \
int64 result; \
PgStat_TableStatus *tabentry; \
if ((tabentry = find_tabstat_entry(relid)) == NULL) \
result = 0; \
else \
result = (int64) (tabentry->counts.stat); \
PG_RETURN_INT64(result); \
}
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)

Definition at line 1717 of file pgstatfuncs.c.

◆ UINT32_ACCESS_ONCE

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

Definition at line 35 of file pgstatfuncs.c.

Typedef Documentation

◆ io_stat_col

typedef enum io_stat_col io_stat_col

Enumeration Type Documentation

◆ io_stat_col

Enumerator
IO_COL_INVALID 
IO_COL_BACKEND_TYPE 
IO_COL_OBJECT 
IO_COL_CONTEXT 
IO_COL_READS 
IO_COL_READ_BYTES 
IO_COL_READ_TIME 
IO_COL_WRITES 
IO_COL_WRITE_BYTES 
IO_COL_WRITE_TIME 
IO_COL_WRITEBACKS 
IO_COL_WRITEBACK_TIME 
IO_COL_EXTENDS 
IO_COL_EXTEND_BYTES 
IO_COL_EXTEND_TIME 
IO_COL_HITS 
IO_COL_EVICTIONS 
IO_COL_REUSES 
IO_COL_FSYNCS 
IO_COL_FSYNC_TIME 
IO_COL_RESET_TIME 
IO_NUM_COLUMNS 

Definition at line 1281 of file pgstatfuncs.c.

1282{
1283 IO_COL_INVALID = -1,
1305} io_stat_col;
io_stat_col
Definition: pgstatfuncs.c:1282
@ IO_COL_READS
Definition: pgstatfuncs.c:1287
@ IO_NUM_COLUMNS
Definition: pgstatfuncs.c:1304
@ IO_COL_RESET_TIME
Definition: pgstatfuncs.c:1303
@ IO_COL_WRITE_TIME
Definition: pgstatfuncs.c:1292
@ IO_COL_HITS
Definition: pgstatfuncs.c:1298
@ IO_COL_EXTENDS
Definition: pgstatfuncs.c:1295
@ IO_COL_WRITEBACK_TIME
Definition: pgstatfuncs.c:1294
@ IO_COL_REUSES
Definition: pgstatfuncs.c:1300
@ IO_COL_WRITES
Definition: pgstatfuncs.c:1290
@ IO_COL_OBJECT
Definition: pgstatfuncs.c:1285
@ IO_COL_EVICTIONS
Definition: pgstatfuncs.c:1299
@ IO_COL_WRITEBACKS
Definition: pgstatfuncs.c:1293
@ IO_COL_CONTEXT
Definition: pgstatfuncs.c:1286
@ IO_COL_READ_BYTES
Definition: pgstatfuncs.c:1288
@ IO_COL_EXTEND_BYTES
Definition: pgstatfuncs.c:1296
@ IO_COL_BACKEND_TYPE
Definition: pgstatfuncs.c:1284
@ IO_COL_FSYNC_TIME
Definition: pgstatfuncs.c:1302
@ IO_COL_EXTEND_TIME
Definition: pgstatfuncs.c:1297
@ IO_COL_FSYNCS
Definition: pgstatfuncs.c:1301
@ IO_COL_WRITE_BYTES
Definition: pgstatfuncs.c:1291
@ IO_COL_INVALID
Definition: pgstatfuncs.c:1283
@ IO_COL_READ_TIME
Definition: pgstatfuncs.c:1289

Function Documentation

◆ pg_backend_pid()

Datum pg_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 659 of file pgstatfuncs.c.

660{
662}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int MyProcPid
Definition: globals.c:46

References MyProcPid, and PG_RETURN_INT32.

◆ pg_stat_clear_snapshot()

Datum pg_stat_clear_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1809 of file pgstatfuncs.c.

1810{
1812
1814}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:918

References PG_RETURN_VOID, and pgstat_clear_snapshot().

◆ pg_stat_force_next_flush()

Datum pg_stat_force_next_flush ( PG_FUNCTION_ARGS  )

Definition at line 1819 of file pgstatfuncs.c.

1820{
1822
1824}
void pgstat_force_next_flush(void)
Definition: pgstat.c:830

References PG_RETURN_VOID, and pgstat_force_next_flush().

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 303 of file pgstatfuncs.c.

304{
305#define PG_STAT_GET_ACTIVITY_COLS 31
306 int num_backends = pgstat_fetch_stat_numbackends();
307 int curr_backend;
308 int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
309 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
310
311 InitMaterializedSRF(fcinfo, 0);
312
313 /* 1-based index */
314 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
315 {
316 /* for each row */
318 bool nulls[PG_STAT_GET_ACTIVITY_COLS] = {0};
319 LocalPgBackendStatus *local_beentry;
320 PgBackendStatus *beentry;
321 PGPROC *proc;
322 const char *wait_event_type = NULL;
323 const char *wait_event = NULL;
324
325 /* Get the next one in the list */
326 local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
327 beentry = &local_beentry->backendStatus;
328
329 /* If looking for specific PID, ignore all the others */
330 if (pid != -1 && beentry->st_procpid != pid)
331 continue;
332
333 /* Values available to all callers */
334 if (beentry->st_databaseid != InvalidOid)
336 else
337 nulls[0] = true;
338
339 values[1] = Int32GetDatum(beentry->st_procpid);
340
341 if (beentry->st_userid != InvalidOid)
342 values[2] = ObjectIdGetDatum(beentry->st_userid);
343 else
344 nulls[2] = true;
345
346 if (beentry->st_appname)
348 else
349 nulls[3] = true;
350
351 if (TransactionIdIsValid(local_beentry->backend_xid))
352 values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
353 else
354 nulls[15] = true;
355
356 if (TransactionIdIsValid(local_beentry->backend_xmin))
357 values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
358 else
359 nulls[16] = true;
360
361 /* Values only available to role member or pg_read_all_stats */
362 if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
363 {
364 char *clipped_activity;
365
366 switch (beentry->st_state)
367 {
368 case STATE_IDLE:
369 values[4] = CStringGetTextDatum("idle");
370 break;
371 case STATE_RUNNING:
372 values[4] = CStringGetTextDatum("active");
373 break;
375 values[4] = CStringGetTextDatum("idle in transaction");
376 break;
377 case STATE_FASTPATH:
378 values[4] = CStringGetTextDatum("fastpath function call");
379 break;
381 values[4] = CStringGetTextDatum("idle in transaction (aborted)");
382 break;
383 case STATE_DISABLED:
384 values[4] = CStringGetTextDatum("disabled");
385 break;
386 case STATE_UNDEFINED:
387 nulls[4] = true;
388 break;
389 }
390
391 clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
392 values[5] = CStringGetTextDatum(clipped_activity);
393 pfree(clipped_activity);
394
395 /* leader_pid */
396 nulls[29] = true;
397
398 proc = BackendPidGetProc(beentry->st_procpid);
399
400 if (proc == NULL && (beentry->st_backendType != B_BACKEND))
401 {
402 /*
403 * For an auxiliary process, retrieve process info from
404 * AuxiliaryProcs stored in shared-memory.
405 */
406 proc = AuxiliaryPidGetProc(beentry->st_procpid);
407 }
408
409 /*
410 * If a PGPROC entry was retrieved, display wait events and lock
411 * group leader or apply leader information if any. To avoid
412 * extra overhead, no extra lock is being held, so there is no
413 * guarantee of consistency across multiple rows.
414 */
415 if (proc != NULL)
416 {
417 uint32 raw_wait_event;
418 PGPROC *leader;
419
420 raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
421 wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
422 wait_event = pgstat_get_wait_event(raw_wait_event);
423
424 leader = proc->lockGroupLeader;
425
426 /*
427 * Show the leader only for active parallel workers. This
428 * leaves the field as NULL for the leader of a parallel group
429 * or the leader of parallel apply workers.
430 */
431 if (leader && leader->pid != beentry->st_procpid)
432 {
433 values[29] = Int32GetDatum(leader->pid);
434 nulls[29] = false;
435 }
436 else if (beentry->st_backendType == B_BG_WORKER)
437 {
438 int leader_pid = GetLeaderApplyWorkerPid(beentry->st_procpid);
439
440 if (leader_pid != InvalidPid)
441 {
442 values[29] = Int32GetDatum(leader_pid);
443 nulls[29] = false;
444 }
445 }
446 }
447
448 if (wait_event_type)
449 values[6] = CStringGetTextDatum(wait_event_type);
450 else
451 nulls[6] = true;
452
453 if (wait_event)
454 values[7] = CStringGetTextDatum(wait_event);
455 else
456 nulls[7] = true;
457
458 /*
459 * Don't expose transaction time for walsenders; it confuses
460 * monitoring, particularly because we don't keep the time up-to-
461 * date.
462 */
463 if (beentry->st_xact_start_timestamp != 0 &&
464 beentry->st_backendType != B_WAL_SENDER)
466 else
467 nulls[8] = true;
468
469 if (beentry->st_activity_start_timestamp != 0)
471 else
472 nulls[9] = true;
473
474 if (beentry->st_proc_start_timestamp != 0)
476 else
477 nulls[10] = true;
478
479 if (beentry->st_state_start_timestamp != 0)
481 else
482 nulls[11] = true;
483
484 /* A zeroed client addr means we don't know */
486 sizeof(beentry->st_clientaddr)))
487 {
488 nulls[12] = true;
489 nulls[13] = true;
490 nulls[14] = true;
491 }
492 else
493 {
494 if (beentry->st_clientaddr.addr.ss_family == AF_INET ||
495 beentry->st_clientaddr.addr.ss_family == AF_INET6)
496 {
497 char remote_host[NI_MAXHOST];
498 char remote_port[NI_MAXSERV];
499 int ret;
500
501 remote_host[0] = '\0';
502 remote_port[0] = '\0';
503 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
504 beentry->st_clientaddr.salen,
505 remote_host, sizeof(remote_host),
506 remote_port, sizeof(remote_port),
507 NI_NUMERICHOST | NI_NUMERICSERV);
508 if (ret == 0)
509 {
510 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
512 CStringGetDatum(remote_host));
513 if (beentry->st_clienthostname &&
514 beentry->st_clienthostname[0])
516 else
517 nulls[13] = true;
518 values[14] = Int32GetDatum(atoi(remote_port));
519 }
520 else
521 {
522 nulls[12] = true;
523 nulls[13] = true;
524 nulls[14] = true;
525 }
526 }
527 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
528 {
529 /*
530 * Unix sockets always reports NULL for host and -1 for
531 * port, so it's possible to tell the difference to
532 * connections we have no permissions to view, or with
533 * errors.
534 */
535 nulls[12] = true;
536 nulls[13] = true;
537 values[14] = Int32GetDatum(-1);
538 }
539 else
540 {
541 /* Unknown address type, should never happen */
542 nulls[12] = true;
543 nulls[13] = true;
544 nulls[14] = true;
545 }
546 }
547 /* Add backend type */
548 if (beentry->st_backendType == B_BG_WORKER)
549 {
550 const char *bgw_type;
551
552 bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
553 if (bgw_type)
554 values[17] = CStringGetTextDatum(bgw_type);
555 else
556 nulls[17] = true;
557 }
558 else
559 values[17] =
561
562 /* SSL information */
563 if (beentry->st_ssl)
564 {
565 values[18] = BoolGetDatum(true); /* ssl */
568 values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
569
570 if (beentry->st_sslstatus->ssl_client_dn[0])
572 else
573 nulls[22] = true;
574
575 if (beentry->st_sslstatus->ssl_client_serial[0])
579 Int32GetDatum(-1));
580 else
581 nulls[23] = true;
582
583 if (beentry->st_sslstatus->ssl_issuer_dn[0])
585 else
586 nulls[24] = true;
587 }
588 else
589 {
590 values[18] = BoolGetDatum(false); /* ssl */
591 nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = true;
592 }
593
594 /* GSSAPI information */
595 if (beentry->st_gss)
596 {
597 values[25] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
599 values[27] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
600 values[28] = BoolGetDatum(beentry->st_gssstatus->gss_delegation); /* GSS credentials
601 * delegated */
602 }
603 else
604 {
605 values[25] = BoolGetDatum(false); /* gss_auth */
606 nulls[26] = true; /* No GSS principal */
607 values[27] = BoolGetDatum(false); /* GSS Encryption not in
608 * use */
609 values[28] = BoolGetDatum(false); /* GSS credentials not
610 * delegated */
611 }
612 if (beentry->st_query_id == 0)
613 nulls[30] = true;
614 else
615 values[30] = UInt64GetDatum(beentry->st_query_id);
616 }
617 else
618 {
619 /* No permissions to view data about this session */
620 values[5] = CStringGetTextDatum("<insufficient privilege>");
621 nulls[4] = true;
622 nulls[6] = true;
623 nulls[7] = true;
624 nulls[8] = true;
625 nulls[9] = true;
626 nulls[10] = true;
627 nulls[11] = true;
628 nulls[12] = true;
629 nulls[13] = true;
630 nulls[14] = true;
631 nulls[17] = true;
632 nulls[18] = true;
633 nulls[19] = true;
634 nulls[20] = true;
635 nulls[21] = true;
636 nulls[22] = true;
637 nulls[23] = true;
638 nulls[24] = true;
639 nulls[25] = true;
640 nulls[26] = true;
641 nulls[27] = true;
642 nulls[28] = true;
643 nulls[29] = true;
644 nulls[30] = true;
645 }
646
647 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
648
649 /* If only a single backend was requested, and we found it, break. */
650 if (pid != -1)
651 break;
652 }
653
654 return (Datum) 0;
655}
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:637
int pgstat_fetch_stat_numbackends(void)
char * pgstat_clip_activity(const char *raw_activity)
LocalPgBackendStatus * pgstat_get_local_beentry_by_index(int idx)
@ STATE_UNDEFINED
@ STATE_IDLEINTRANSACTION_ABORTED
@ STATE_IDLE
@ STATE_IDLEINTRANSACTION
@ STATE_DISABLED
@ STATE_FASTPATH
@ STATE_RUNNING
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1371
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
uint32_t uint32
Definition: c.h:488
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:114
pid_t GetLeaderApplyWorkerPid(pid_t pid)
Definition: launcher.c:1250
void pfree(void *pointer)
Definition: mcxt.c:1521
static bool pg_memory_is_all_zeros(const void *ptr, size_t len)
Definition: memutils.h:219
@ B_WAL_SENDER
Definition: miscadmin.h:346
@ B_BG_WORKER
Definition: miscadmin.h:345
@ B_BACKEND
Definition: miscadmin.h:341
#define InvalidPid
Definition: miscadmin.h:32
const char * GetBackendTypeDesc(BackendType backendType)
Definition: miscinit.c:263
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2089
Datum inet_in(PG_FUNCTION_ARGS)
Definition: network.c:121
#define UINT32_ACCESS_ONCE(var)
Definition: pgstatfuncs.c:35
#define PG_STAT_GET_ACTIVITY_COLS
#define HAS_PGSTAT_PERMISSIONS(role)
Definition: pgstatfuncs.c:37
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:277
uintptr_t Datum
Definition: postgres.h:69
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:441
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:37
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3196
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:1040
TransactionId backend_xid
PgBackendStatus backendStatus
TransactionId backend_xmin
Definition: proc.h:162
uint32 wait_event_info
Definition: proc.h:279
int pid
Definition: proc.h:182
PGPROC * lockGroupLeader
Definition: proc.h:304
char gss_princ[NAMEDATALEN]
char ssl_version[NAMEDATALEN]
char ssl_cipher[NAMEDATALEN]
char ssl_client_dn[NAMEDATALEN]
char ssl_client_serial[NAMEDATALEN]
char ssl_issuer_dn[NAMEDATALEN]
BackendType st_backendType
TimestampTz st_state_start_timestamp
TimestampTz st_proc_start_timestamp
PgBackendGSSStatus * st_gssstatus
BackendState st_state
TimestampTz st_activity_start_timestamp
SockAddr st_clientaddr
PgBackendSSLStatus * st_sslstatus
TimestampTz st_xact_start_timestamp
char * st_clienthostname
TupleDesc setDesc
Definition: execnodes.h:358
Tuplestorestate * setResult
Definition: execnodes.h:357
struct sockaddr_storage addr
Definition: pqcomm.h:32
socklen_t salen
Definition: pqcomm.h:33
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:373
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: wait_event.c:431

References SockAddr::addr, AuxiliaryPidGetProc(), B_BACKEND, B_BG_WORKER, B_WAL_SENDER, LocalPgBackendStatus::backend_xid, LocalPgBackendStatus::backend_xmin, BackendPidGetProc(), LocalPgBackendStatus::backendStatus, BoolGetDatum(), clean_ipv6_addr(), CStringGetDatum(), CStringGetTextDatum, DirectFunctionCall1, DirectFunctionCall3, GetBackendTypeDesc(), GetBackgroundWorkerTypeByPid(), GetLeaderApplyWorkerPid(), PgBackendGSSStatus::gss_auth, PgBackendGSSStatus::gss_delegation, PgBackendGSSStatus::gss_enc, PgBackendGSSStatus::gss_princ, HAS_PGSTAT_PERMISSIONS, inet_in(), InitMaterializedSRF(), Int32GetDatum(), InvalidOid, InvalidPid, PGPROC::lockGroupLeader, numeric_in(), ObjectIdGetDatum(), pfree(), PG_ARGISNULL, PG_GETARG_INT32, pg_getnameinfo_all(), pg_memory_is_all_zeros(), PG_STAT_GET_ACTIVITY_COLS, pgstat_clip_activity(), pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), pgstat_get_wait_event(), pgstat_get_wait_event_type(), PGPROC::pid, SockAddr::salen, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, PgBackendSSLStatus::ssl_bits, PgBackendSSLStatus::ssl_cipher, PgBackendSSLStatus::ssl_client_dn, PgBackendSSLStatus::ssl_client_serial, PgBackendSSLStatus::ssl_issuer_dn, PgBackendSSLStatus::ssl_version, PgBackendStatus::st_activity_raw, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_appname, PgBackendStatus::st_backendType, PgBackendStatus::st_clientaddr, PgBackendStatus::st_clienthostname, PgBackendStatus::st_databaseid, PgBackendStatus::st_gss, PgBackendStatus::st_gssstatus, PgBackendStatus::st_proc_start_timestamp, PgBackendStatus::st_procpid, PgBackendStatus::st_query_id, PgBackendStatus::st_ssl, PgBackendStatus::st_sslstatus, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_userid, PgBackendStatus::st_xact_start_timestamp, STATE_DISABLED, STATE_FASTPATH, STATE_IDLE, STATE_IDLEINTRANSACTION, STATE_IDLEINTRANSACTION_ABORTED, STATE_RUNNING, STATE_UNDEFINED, TimestampTzGetDatum(), TransactionIdGetDatum(), TransactionIdIsValid, tuplestore_putvalues(), UINT32_ACCESS_ONCE, UInt64GetDatum(), values, and PGPROC::wait_event_info.

◆ pg_stat_get_archiver()

Datum pg_stat_get_archiver ( PG_FUNCTION_ARGS  )

Definition at line 1995 of file pgstatfuncs.c.

1996{
1997 TupleDesc tupdesc;
1998 Datum values[7] = {0};
1999 bool nulls[7] = {0};
2000 PgStat_ArchiverStats *archiver_stats;
2001
2002 /* Initialise attributes information in the tuple descriptor */
2003 tupdesc = CreateTemplateTupleDesc(7);
2004 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2005 INT8OID, -1, 0);
2006 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2007 TEXTOID, -1, 0);
2008 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2009 TIMESTAMPTZOID, -1, 0);
2010 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2011 INT8OID, -1, 0);
2012 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2013 TEXTOID, -1, 0);
2014 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2015 TIMESTAMPTZOID, -1, 0);
2016 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2017 TIMESTAMPTZOID, -1, 0);
2018
2019 BlessTupleDesc(tupdesc);
2020
2021 /* Get statistics about the archiver process */
2022 archiver_stats = pgstat_fetch_stat_archiver();
2023
2024 /* Fill values and NULLs */
2025 values[0] = Int64GetDatum(archiver_stats->archived_count);
2026 if (*(archiver_stats->last_archived_wal) == '\0')
2027 nulls[1] = true;
2028 else
2029 values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2030
2031 if (archiver_stats->last_archived_timestamp == 0)
2032 nulls[2] = true;
2033 else
2035
2036 values[3] = Int64GetDatum(archiver_stats->failed_count);
2037 if (*(archiver_stats->last_failed_wal) == '\0')
2038 nulls[4] = true;
2039 else
2040 values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2041
2042 if (archiver_stats->last_failed_timestamp == 0)
2043 nulls[5] = true;
2044 else
2045 values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2046
2047 if (archiver_stats->stat_reset_timestamp == 0)
2048 nulls[6] = true;
2049 else
2050 values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2051
2052 /* Returns the record as Datum */
2054}
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2258
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
PgStat_ArchiverStats * pgstat_fetch_stat_archiver(void)
TimestampTz last_failed_timestamp
Definition: pgstat.h:226
TimestampTz stat_reset_timestamp
Definition: pgstat.h:227
TimestampTz last_archived_timestamp
Definition: pgstat.h:222
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:224
PgStat_Counter failed_count
Definition: pgstat.h:223
PgStat_Counter archived_count
Definition: pgstat.h:219
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:220
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:164
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:798

References PgStat_ArchiverStats::archived_count, BlessTupleDesc(), CreateTemplateTupleDesc(), CStringGetTextDatum, PgStat_ArchiverStats::failed_count, heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), PgStat_ArchiverStats::last_archived_timestamp, PgStat_ArchiverStats::last_archived_wal, PgStat_ArchiverStats::last_failed_timestamp, PgStat_ArchiverStats::last_failed_wal, PG_RETURN_DATUM, pgstat_fetch_stat_archiver(), PgStat_ArchiverStats::stat_reset_timestamp, TimestampTzGetDatum(), TupleDescInitEntry(), and values.

◆ pg_stat_get_backend_activity()

Datum pg_stat_get_backend_activity ( PG_FUNCTION_ARGS  )

Definition at line 739 of file pgstatfuncs.c.

740{
741 int32 procNumber = PG_GETARG_INT32(0);
742 PgBackendStatus *beentry;
743 const char *activity;
744 char *clipped_activity;
745 text *ret;
746
747 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
748 activity = "<backend information not available>";
749 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
750 activity = "<insufficient privilege>";
751 else if (*(beentry->st_activity_raw) == '\0')
752 activity = "<command string not enabled>";
753 else
754 activity = beentry->st_activity_raw;
755
756 clipped_activity = pgstat_clip_activity(activity);
757 ret = cstring_to_text(activity);
758 pfree(clipped_activity);
759
760 PG_RETURN_TEXT_P(ret);
761}
PgBackendStatus * pgstat_get_beentry_by_proc_number(ProcNumber procNumber)
int32_t int32
Definition: c.h:484
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:644
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References cstring_to_text(), HAS_PGSTAT_PERMISSIONS, pfree(), PG_GETARG_INT32, PG_RETURN_TEXT_P, pgstat_clip_activity(), pgstat_get_beentry_by_proc_number(), PgBackendStatus::st_activity_raw, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_activity_start()

Datum pg_stat_get_backend_activity_start ( PG_FUNCTION_ARGS  )

Definition at line 807 of file pgstatfuncs.c.

808{
809 int32 procNumber = PG_GETARG_INT32(0);
810 TimestampTz result;
811 PgBackendStatus *beentry;
812
813 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
815
816 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
818
819 result = beentry->st_activity_start_timestamp;
820
821 /*
822 * No time recorded for start of current query -- this is the case if the
823 * user hasn't enabled query-level stats collection.
824 */
825 if (result == 0)
827
828 PG_RETURN_TIMESTAMPTZ(result);
829}
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_NULL()
Definition: fmgr.h:345

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_get_beentry_by_proc_number(), PgBackendStatus::st_activity_start_timestamp, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_client_addr()

Datum pg_stat_get_backend_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 877 of file pgstatfuncs.c.

878{
879 int32 procNumber = PG_GETARG_INT32(0);
880 PgBackendStatus *beentry;
881 char remote_host[NI_MAXHOST];
882 int ret;
883
884 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
886
887 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
889
890 /* A zeroed client addr means we don't know */
892 sizeof(beentry->st_clientaddr)))
894
895 switch (beentry->st_clientaddr.addr.ss_family)
896 {
897 case AF_INET:
898 case AF_INET6:
899 break;
900 default:
902 }
903
904 remote_host[0] = '\0';
905 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
906 beentry->st_clientaddr.salen,
907 remote_host, sizeof(remote_host),
908 NULL, 0,
909 NI_NUMERICHOST | NI_NUMERICSERV);
910 if (ret != 0)
912
913 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
914
916 CStringGetDatum(remote_host)));
917}

References SockAddr::addr, clean_ipv6_addr(), CStringGetDatum(), DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, inet_in(), PG_GETARG_INT32, pg_getnameinfo_all(), pg_memory_is_all_zeros(), PG_RETURN_DATUM, PG_RETURN_NULL, pgstat_get_beentry_by_proc_number(), SockAddr::salen, PgBackendStatus::st_clientaddr, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_client_port()

Datum pg_stat_get_backend_client_port ( PG_FUNCTION_ARGS  )

Definition at line 920 of file pgstatfuncs.c.

921{
922 int32 procNumber = PG_GETARG_INT32(0);
923 PgBackendStatus *beentry;
924 char remote_port[NI_MAXSERV];
925 int ret;
926
927 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
929
930 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
932
933 /* A zeroed client addr means we don't know */
935 sizeof(beentry->st_clientaddr)))
937
938 switch (beentry->st_clientaddr.addr.ss_family)
939 {
940 case AF_INET:
941 case AF_INET6:
942 break;
943 case AF_UNIX:
944 PG_RETURN_INT32(-1);
945 default:
947 }
948
949 remote_port[0] = '\0';
950 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
951 beentry->st_clientaddr.salen,
952 NULL, 0,
953 remote_port, sizeof(remote_port),
954 NI_NUMERICHOST | NI_NUMERICSERV);
955 if (ret != 0)
957
959 CStringGetDatum(remote_port)));
960}
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:287

References SockAddr::addr, CStringGetDatum(), DirectFunctionCall1, HAS_PGSTAT_PERMISSIONS, int4in(), PG_GETARG_INT32, pg_getnameinfo_all(), pg_memory_is_all_zeros(), PG_RETURN_DATUM, PG_RETURN_INT32, PG_RETURN_NULL, pgstat_get_beentry_by_proc_number(), SockAddr::salen, PgBackendStatus::st_clientaddr, and PgBackendStatus::st_userid.

◆ pg_stat_get_backend_dbid()

Datum pg_stat_get_backend_dbid ( PG_FUNCTION_ARGS  )

Definition at line 679 of file pgstatfuncs.c.

680{
681 int32 procNumber = PG_GETARG_INT32(0);
682 PgBackendStatus *beentry;
683
684 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
686
688}
#define PG_RETURN_OID(x)
Definition: fmgr.h:360

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

◆ pg_stat_get_backend_idset()

Datum pg_stat_get_backend_idset ( PG_FUNCTION_ARGS  )

Definition at line 176 of file pgstatfuncs.c.

177{
178 FuncCallContext *funcctx;
179 int *fctx;
180
181 /* stuff done only on the first call of the function */
182 if (SRF_IS_FIRSTCALL())
183 {
184 /* create a function context for cross-call persistence */
185 funcctx = SRF_FIRSTCALL_INIT();
186
188 sizeof(int));
189 funcctx->user_fctx = fctx;
190
191 fctx[0] = 0;
192 }
193
194 /* stuff done on every call of the function */
195 funcctx = SRF_PERCALL_SETUP();
196 fctx = funcctx->user_fctx;
197
198 fctx[0] += 1;
199
200 /*
201 * We recheck pgstat_fetch_stat_numbackends() each time through, just in
202 * case the local status data has been refreshed since we started. It's
203 * plenty cheap enough if not. If a refresh does happen, we'll likely
204 * miss or duplicate some backend IDs, but we're content not to crash.
205 * (Refreshing midway through such a query would be problematic usage
206 * anyway, since the backend IDs we've already returned might no longer
207 * refer to extant sessions.)
208 */
209 if (fctx[0] <= pgstat_fetch_stat_numbackends())
210 {
211 /* do when there is more left to send */
213
214 SRF_RETURN_NEXT(funcctx, Int32GetDatum(local_beentry->proc_number));
215 }
216 else
217 {
218 /* do when there is no more left */
219 SRF_RETURN_DONE(funcctx);
220 }
221}
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101

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

◆ pg_stat_get_backend_io()

Datum pg_stat_get_backend_io ( PG_FUNCTION_ARGS  )

Definition at line 1546 of file pgstatfuncs.c.

1547{
1548 ReturnSetInfo *rsinfo;
1549 BackendType bktype;
1550 int pid;
1551 PGPROC *proc;
1552 ProcNumber procNumber;
1553 PgStat_Backend *backend_stats;
1554 PgStat_BktypeIO *bktype_stats;
1555 PgBackendStatus *beentry;
1556
1557 InitMaterializedSRF(fcinfo, 0);
1558 rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1559
1560 pid = PG_GETARG_INT32(0);
1561 proc = BackendPidGetProc(pid);
1562
1563 /*
1564 * This could be an auxiliary process but these do not report backend
1565 * statistics due to pgstat_tracks_backend_bktype(), so there is no need
1566 * for an extra call to AuxiliaryPidGetProc().
1567 */
1568 if (!proc)
1569 return (Datum) 0;
1570
1571 procNumber = GetNumberFromPGProc(proc);
1572
1573 beentry = pgstat_get_beentry_by_proc_number(procNumber);
1574 if (!beentry)
1575 return (Datum) 0;
1576
1577 backend_stats = pgstat_fetch_stat_backend(procNumber);
1578 if (!backend_stats)
1579 return (Datum) 0;
1580
1581 bktype = beentry->st_backendType;
1582
1583 /* if PID does not match, leave */
1584 if (beentry->st_procpid != pid)
1585 return (Datum) 0;
1586
1587 /* backend may be gone, so recheck in case */
1588 if (bktype == B_INVALID)
1589 return (Datum) 0;
1590
1591 bktype_stats = &backend_stats->io_stats;
1592
1593 /*
1594 * In Assert builds, we can afford an extra loop through all of the
1595 * counters (in pg_stat_io_build_tuples()), checking that only expected
1596 * stats are non-zero, since it keeps the non-Assert code cleaner.
1597 */
1598 Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype));
1599
1600 /* save tuples with data from this PgStat_BktypeIO */
1601 pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype,
1602 backend_stats->stat_reset_timestamp);
1603 return (Datum) 0;
1604}
#define Assert(condition)
Definition: c.h:815
BackendType
Definition: miscadmin.h:337
@ B_INVALID
Definition: miscadmin.h:338
PgStat_Backend * pgstat_fetch_stat_backend(ProcNumber procNumber)
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
Definition: pgstat_io.c:37
static void pg_stat_io_build_tuples(ReturnSetInfo *rsinfo, PgStat_BktypeIO *bktype_stats, BackendType bktype, TimestampTz stat_reset_timestamp)
Definition: pgstatfuncs.c:1408
#define GetNumberFromPGProc(proc)
Definition: proc.h:424
int ProcNumber
Definition: procnumber.h:24
TimestampTz stat_reset_timestamp
Definition: pgstat.h:343
PgStat_BktypeIO io_stats
Definition: pgstat.h:344

References Assert, B_INVALID, BackendPidGetProc(), GetNumberFromPGProc, InitMaterializedSRF(), PgStat_Backend::io_stats, PG_GETARG_INT32, pg_stat_io_build_tuples(), pgstat_bktype_io_stats_valid(), pgstat_fetch_stat_backend(), pgstat_get_beentry_by_proc_number(), PgBackendStatus::st_backendType, PgBackendStatus::st_procpid, and PgStat_Backend::stat_reset_timestamp.

◆ pg_stat_get_backend_pid()

Datum pg_stat_get_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 666 of file pgstatfuncs.c.

667{
668 int32 procNumber = PG_GETARG_INT32(0);
669 PgBackendStatus *beentry;
670
671 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
673
674 PG_RETURN_INT32(beentry->st_procpid);
675}

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

◆ pg_stat_get_backend_start()

Datum pg_stat_get_backend_start ( PG_FUNCTION_ARGS  )

Definition at line 855 of file pgstatfuncs.c.

856{
857 int32 procNumber = PG_GETARG_INT32(0);
858 TimestampTz result;
859 PgBackendStatus *beentry;
860
861 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
863
864 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
866
867 result = beentry->st_proc_start_timestamp;
868
869 if (result == 0) /* probably can't happen? */
871
872 PG_RETURN_TIMESTAMPTZ(result);
873}

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

◆ pg_stat_get_backend_subxact()

Datum pg_stat_get_backend_subxact ( PG_FUNCTION_ARGS  )

Definition at line 704 of file pgstatfuncs.c.

705{
706#define PG_STAT_GET_SUBXACT_COLS 2
707 TupleDesc tupdesc;
709 bool nulls[PG_STAT_GET_SUBXACT_COLS] = {0};
710 int32 procNumber = PG_GETARG_INT32(0);
711 LocalPgBackendStatus *local_beentry;
712
713 /* Initialise attributes information in the tuple descriptor */
715 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subxact_count",
716 INT4OID, -1, 0);
717 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "subxact_overflow",
718 BOOLOID, -1, 0);
719
720 BlessTupleDesc(tupdesc);
721
722 if ((local_beentry = pgstat_get_local_beentry_by_proc_number(procNumber)) != NULL)
723 {
724 /* Fill values and NULLs */
725 values[0] = Int32GetDatum(local_beentry->backend_subxact_count);
727 }
728 else
729 {
730 nulls[0] = true;
731 nulls[1] = true;
732 }
733
734 /* Returns the record as Datum */
736}
LocalPgBackendStatus * pgstat_get_local_beentry_by_proc_number(ProcNumber procNumber)
#define PG_STAT_GET_SUBXACT_COLS

References LocalPgBackendStatus::backend_subxact_count, LocalPgBackendStatus::backend_subxact_overflowed, BlessTupleDesc(), BoolGetDatum(), CreateTemplateTupleDesc(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), PG_GETARG_INT32, PG_RETURN_DATUM, PG_STAT_GET_SUBXACT_COLS, pgstat_get_local_beentry_by_proc_number(), TupleDescInitEntry(), and values.

◆ pg_stat_get_backend_userid()

Datum pg_stat_get_backend_userid ( PG_FUNCTION_ARGS  )

Definition at line 692 of file pgstatfuncs.c.

693{
694 int32 procNumber = PG_GETARG_INT32(0);
695 PgBackendStatus *beentry;
696
697 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
699
700 PG_RETURN_OID(beentry->st_userid);
701}

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

◆ pg_stat_get_backend_wait_event()

Datum pg_stat_get_backend_wait_event ( PG_FUNCTION_ARGS  )

Definition at line 785 of file pgstatfuncs.c.

786{
787 int32 procNumber = PG_GETARG_INT32(0);
788 PgBackendStatus *beentry;
789 PGPROC *proc;
790 const char *wait_event = NULL;
791
792 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
793 wait_event = "<backend information not available>";
794 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
795 wait_event = "<insufficient privilege>";
796 else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
797 wait_event = pgstat_get_wait_event(proc->wait_event_info);
798
799 if (!wait_event)
801
803}

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_get_beentry_by_proc_number(), pgstat_get_wait_event(), PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and PGPROC::wait_event_info.

◆ pg_stat_get_backend_wait_event_type()

Datum pg_stat_get_backend_wait_event_type ( PG_FUNCTION_ARGS  )

Definition at line 764 of file pgstatfuncs.c.

765{
766 int32 procNumber = PG_GETARG_INT32(0);
767 PgBackendStatus *beentry;
768 PGPROC *proc;
769 const char *wait_event_type = NULL;
770
771 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
772 wait_event_type = "<backend information not available>";
773 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
774 wait_event_type = "<insufficient privilege>";
775 else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
776 wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
777
778 if (!wait_event_type)
780
781 PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
782}

References BackendPidGetProc(), cstring_to_text(), HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TEXT_P, pgstat_get_beentry_by_proc_number(), pgstat_get_wait_event_type(), PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and PGPROC::wait_event_info.

◆ pg_stat_get_backend_xact_start()

Datum pg_stat_get_backend_xact_start ( PG_FUNCTION_ARGS  )

Definition at line 833 of file pgstatfuncs.c.

834{
835 int32 procNumber = PG_GETARG_INT32(0);
836 TimestampTz result;
837 PgBackendStatus *beentry;
838
839 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
841
842 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
844
845 result = beentry->st_xact_start_timestamp;
846
847 if (result == 0) /* not in a transaction */
849
850 PG_RETURN_TIMESTAMPTZ(result);
851}

References HAS_PGSTAT_PERMISSIONS, PG_GETARG_INT32, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, pgstat_get_beentry_by_proc_number(), PgBackendStatus::st_userid, and PgBackendStatus::st_xact_start_timestamp.

◆ pg_stat_get_bgwriter_buf_written_clean()

Datum pg_stat_get_bgwriter_buf_written_clean ( PG_FUNCTION_ARGS  )

Definition at line 1231 of file pgstatfuncs.c.

1232{
1233 PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean);
1234}
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_bgwriter_maxwritten_clean()

Datum pg_stat_get_bgwriter_maxwritten_clean ( PG_FUNCTION_ARGS  )

Definition at line 1237 of file pgstatfuncs.c.

1238{
1239 PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean);
1240}

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_bgwriter_stat_reset_time()

Datum pg_stat_get_bgwriter_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1265 of file pgstatfuncs.c.

1266{
1267 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp);
1268}

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_buf_alloc()

Datum pg_stat_get_buf_alloc ( PG_FUNCTION_ARGS  )

Definition at line 1271 of file pgstatfuncs.c.

1272{
1274}

References PG_RETURN_INT64, and pgstat_fetch_stat_bgwriter().

◆ pg_stat_get_checkpointer_buffers_written()

Datum pg_stat_get_checkpointer_buffers_written ( PG_FUNCTION_ARGS  )

Definition at line 1219 of file pgstatfuncs.c.

1220{
1222}
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_num_performed()

Datum pg_stat_get_checkpointer_num_performed ( PG_FUNCTION_ARGS  )

Definition at line 1195 of file pgstatfuncs.c.

1196{
1198}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_num_requested()

Datum pg_stat_get_checkpointer_num_requested ( PG_FUNCTION_ARGS  )

Definition at line 1189 of file pgstatfuncs.c.

1190{
1192}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_num_timed()

Datum pg_stat_get_checkpointer_num_timed ( PG_FUNCTION_ARGS  )

Definition at line 1183 of file pgstatfuncs.c.

1184{
1186}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_restartpoints_performed()

Datum pg_stat_get_checkpointer_restartpoints_performed ( PG_FUNCTION_ARGS  )

Definition at line 1213 of file pgstatfuncs.c.

1214{
1215 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_performed);
1216}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_restartpoints_requested()

Datum pg_stat_get_checkpointer_restartpoints_requested ( PG_FUNCTION_ARGS  )

Definition at line 1207 of file pgstatfuncs.c.

1208{
1209 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_requested);
1210}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_restartpoints_timed()

Datum pg_stat_get_checkpointer_restartpoints_timed ( PG_FUNCTION_ARGS  )

Definition at line 1201 of file pgstatfuncs.c.

1202{
1203 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_timed);
1204}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_slru_written()

Datum pg_stat_get_checkpointer_slru_written ( PG_FUNCTION_ARGS  )

Definition at line 1225 of file pgstatfuncs.c.

1226{
1228}

References PG_RETURN_INT64, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_stat_reset_time()

Datum pg_stat_get_checkpointer_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1259 of file pgstatfuncs.c.

1260{
1262}

References PG_RETURN_TIMESTAMPTZ, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_sync_time()

Datum pg_stat_get_checkpointer_sync_time ( PG_FUNCTION_ARGS  )

Definition at line 1251 of file pgstatfuncs.c.

1252{
1253 /* time is already in msec, just convert to double for presentation */
1254 PG_RETURN_FLOAT8((double)
1255 pgstat_fetch_stat_checkpointer()->sync_time);
1256}

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_checkpointer_write_time()

Datum pg_stat_get_checkpointer_write_time ( PG_FUNCTION_ARGS  )

Definition at line 1243 of file pgstatfuncs.c.

1244{
1245 /* time is already in msec, just convert to double for presentation */
1246 PG_RETURN_FLOAT8((double)
1247 pgstat_fetch_stat_checkpointer()->write_time);
1248}

References PG_RETURN_FLOAT8, and pgstat_fetch_stat_checkpointer().

◆ pg_stat_get_db_checksum_failures()

Datum pg_stat_get_db_checksum_failures ( PG_FUNCTION_ARGS  )

Definition at line 1112 of file pgstatfuncs.c.

1113{
1114 Oid dbid = PG_GETARG_OID(0);
1115 int64 result;
1116 PgStat_StatDBEntry *dbentry;
1117
1118 if (!DataChecksumsEnabled())
1120
1121 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1122 result = 0;
1123 else
1124 result = (int64) (dbentry->checksum_failures);
1125
1126 PG_RETURN_INT64(result);
1127}
unsigned int Oid
Definition: postgres_ext.h:32
PgStat_Counter checksum_failures
Definition: pgstat.h:380
bool DataChecksumsEnabled(void)
Definition: xlog.c:4588

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

◆ pg_stat_get_db_checksum_last_failure()

Datum pg_stat_get_db_checksum_last_failure ( PG_FUNCTION_ARGS  )

Definition at line 1130 of file pgstatfuncs.c.

1131{
1132 Oid dbid = PG_GETARG_OID(0);
1133 TimestampTz result;
1134 PgStat_StatDBEntry *dbentry;
1135
1136 if (!DataChecksumsEnabled())
1138
1139 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1140 result = 0;
1141 else
1142 result = dbentry->last_checksum_failure;
1143
1144 if (result == 0)
1146 else
1147 PG_RETURN_TIMESTAMPTZ(result);
1148}
TimestampTz last_checksum_failure
Definition: pgstat.h:381

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

◆ pg_stat_get_db_conflict_all()

Datum pg_stat_get_db_conflict_all ( PG_FUNCTION_ARGS  )

Definition at line 1092 of file pgstatfuncs.c.

1093{
1094 Oid dbid = PG_GETARG_OID(0);
1095 int64 result;
1096 PgStat_StatDBEntry *dbentry;
1097
1098 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1099 result = 0;
1100 else
1101 result = (int64) (dbentry->conflict_tablespace +
1102 dbentry->conflict_lock +
1103 dbentry->conflict_snapshot +
1104 dbentry->conflict_logicalslot +
1105 dbentry->conflict_bufferpin +
1106 dbentry->conflict_startup_deadlock);
1107
1108 PG_RETURN_INT64(result);
1109}
PgStat_Counter conflict_startup_deadlock
Definition: pgstat.h:376
PgStat_Counter conflict_lock
Definition: pgstat.h:372
PgStat_Counter conflict_snapshot
Definition: pgstat.h:373
PgStat_Counter conflict_bufferpin
Definition: pgstat.h:375
PgStat_Counter conflict_logicalslot
Definition: pgstat.h:374
PgStat_Counter conflict_tablespace
Definition: pgstat.h:371

References PgStat_StatDBEntry::conflict_bufferpin, PgStat_StatDBEntry::conflict_lock, PgStat_StatDBEntry::conflict_logicalslot, PgStat_StatDBEntry::conflict_snapshot, PgStat_StatDBEntry::conflict_startup_deadlock, PgStat_StatDBEntry::conflict_tablespace, PG_GETARG_OID, PG_RETURN_INT64, and pgstat_fetch_stat_dbentry().

◆ pg_stat_get_db_numbackends()

Datum pg_stat_get_db_numbackends ( PG_FUNCTION_ARGS  )

Definition at line 964 of file pgstatfuncs.c.

965{
966 Oid dbid = PG_GETARG_OID(0);
967 int32 result;
968 int tot_backends = pgstat_fetch_stat_numbackends();
969 int idx;
970
971 result = 0;
972 for (idx = 1; idx <= tot_backends; idx++)
973 {
975
976 if (local_beentry->backendStatus.st_databaseid == dbid)
977 result++;
978 }
979
980 PG_RETURN_INT32(result);
981}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259

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

◆ pg_stat_get_db_stat_reset_time()

Datum pg_stat_get_db_stat_reset_time ( PG_FUNCTION_ARGS  )

Definition at line 1073 of file pgstatfuncs.c.

1074{
1075 Oid dbid = PG_GETARG_OID(0);
1076 TimestampTz result;
1077 PgStat_StatDBEntry *dbentry;
1078
1079 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1080 result = 0;
1081 else
1082 result = dbentry->stat_reset_timestamp;
1083
1084 if (result == 0)
1086 else
1087 PG_RETURN_TIMESTAMPTZ(result);
1088}
TimestampTz stat_reset_timestamp
Definition: pgstat.h:394

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

◆ pg_stat_get_function_calls()

Datum pg_stat_get_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 144 of file pgstatfuncs.c.

145{
146 Oid funcid = PG_GETARG_OID(0);
147 PgStat_StatFuncEntry *funcentry;
148
149 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
151 PG_RETURN_INT64(funcentry->numcalls);
152}
PgStat_Counter numcalls
Definition: pgstat.h:399

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

◆ pg_stat_get_io()

Datum pg_stat_get_io ( PG_FUNCTION_ARGS  )

Definition at line 1505 of file pgstatfuncs.c.

1506{
1507 ReturnSetInfo *rsinfo;
1508 PgStat_IO *backends_io_stats;
1509
1510 InitMaterializedSRF(fcinfo, 0);
1511 rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1512
1513 backends_io_stats = pgstat_fetch_stat_io();
1514
1515 for (int bktype = 0; bktype < BACKEND_NUM_TYPES; bktype++)
1516 {
1517 PgStat_BktypeIO *bktype_stats = &backends_io_stats->stats[bktype];
1518
1519 /*
1520 * In Assert builds, we can afford an extra loop through all of the
1521 * counters (in pg_stat_io_build_tuples()), checking that only
1522 * expected stats are non-zero, since it keeps the non-Assert code
1523 * cleaner.
1524 */
1525 Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype));
1526
1527 /*
1528 * For those BackendTypes without IO Operation stats, skip
1529 * representing them in the view altogether.
1530 */
1531 if (!pgstat_tracks_io_bktype(bktype))
1532 continue;
1533
1534 /* save tuples with data from this PgStat_BktypeIO */
1535 pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype,
1536 backends_io_stats->stat_reset_timestamp);
1537 }
1538
1539 return (Datum) 0;
1540}
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:375
PgStat_IO * pgstat_fetch_stat_io(void)
Definition: pgstat_io.c:151
bool pgstat_tracks_io_bktype(BackendType bktype)
Definition: pgstat_io.c:345
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:338
TimestampTz stat_reset_timestamp
Definition: pgstat.h:337

References Assert, BACKEND_NUM_TYPES, InitMaterializedSRF(), pg_stat_io_build_tuples(), pgstat_bktype_io_stats_valid(), pgstat_fetch_stat_io(), pgstat_tracks_io_bktype(), PgStat_IO::stat_reset_timestamp, and PgStat_IO::stats.

◆ pg_stat_get_progress_info()

Datum pg_stat_get_progress_info ( PG_FUNCTION_ARGS  )

Definition at line 227 of file pgstatfuncs.c.

228{
229#define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
230 int num_backends = pgstat_fetch_stat_numbackends();
231 int curr_backend;
232 char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
233 ProgressCommandType cmdtype;
234 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
235
236 /* Translate command name into command type code. */
237 if (pg_strcasecmp(cmd, "VACUUM") == 0)
238 cmdtype = PROGRESS_COMMAND_VACUUM;
239 else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
240 cmdtype = PROGRESS_COMMAND_ANALYZE;
241 else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
242 cmdtype = PROGRESS_COMMAND_CLUSTER;
243 else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
245 else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
247 else if (pg_strcasecmp(cmd, "COPY") == 0)
248 cmdtype = PROGRESS_COMMAND_COPY;
249 else
251 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
252 errmsg("invalid command name: \"%s\"", cmd)));
253
254 InitMaterializedSRF(fcinfo, 0);
255
256 /* 1-based index */
257 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
258 {
259 LocalPgBackendStatus *local_beentry;
260 PgBackendStatus *beentry;
262 bool nulls[PG_STAT_GET_PROGRESS_COLS] = {0};
263 int i;
264
265 local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
266 beentry = &local_beentry->backendStatus;
267
268 /*
269 * Report values for only those backends which are running the given
270 * command.
271 */
272 if (beentry->st_progress_command != cmdtype)
273 continue;
274
275 /* Value available to all callers */
276 values[0] = Int32GetDatum(beentry->st_procpid);
278
279 /* show rest of the values including relid only to role members */
280 if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
281 {
283 for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
284 values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
285 }
286 else
287 {
288 nulls[2] = true;
289 for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
290 nulls[i + 3] = true;
291 }
292
293 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
294 }
295
296 return (Datum) 0;
297}
#define PGSTAT_NUM_PROGRESS_PARAM
ProgressCommandType
@ PROGRESS_COMMAND_ANALYZE
@ PROGRESS_COMMAND_CLUSTER
@ PROGRESS_COMMAND_CREATE_INDEX
@ PROGRESS_COMMAND_VACUUM
@ PROGRESS_COMMAND_BASEBACKUP
@ PROGRESS_COMMAND_COPY
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int i
Definition: isn.c:72
#define PG_STAT_GET_PROGRESS_COLS
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
ProgressCommandType st_progress_command
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Oid st_progress_command_target
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References LocalPgBackendStatus::backendStatus, ereport, errcode(), errmsg(), ERROR, HAS_PGSTAT_PERMISSIONS, i, if(), InitMaterializedSRF(), Int32GetDatum(), Int64GetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_STAT_GET_PROGRESS_COLS, pg_strcasecmp(), pgstat_fetch_stat_numbackends(), pgstat_get_local_beentry_by_index(), PGSTAT_NUM_PROGRESS_PARAM, PROGRESS_COMMAND_ANALYZE, PROGRESS_COMMAND_BASEBACKUP, PROGRESS_COMMAND_CLUSTER, PROGRESS_COMMAND_COPY, PROGRESS_COMMAND_CREATE_INDEX, PROGRESS_COMMAND_VACUUM, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, PgBackendStatus::st_databaseid, PgBackendStatus::st_procpid, PgBackendStatus::st_progress_command, PgBackendStatus::st_progress_command_target, PgBackendStatus::st_progress_param, PgBackendStatus::st_userid, text_to_cstring(), tuplestore_putvalues(), and values.

◆ pg_stat_get_replication_slot()

Datum pg_stat_get_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 2061 of file pgstatfuncs.c.

2062{
2063#define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2064 text *slotname_text = PG_GETARG_TEXT_P(0);
2065 NameData slotname;
2066 TupleDesc tupdesc;
2068 bool nulls[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0};
2069 PgStat_StatReplSlotEntry *slotent;
2071
2072 /* Initialise attributes information in the tuple descriptor */
2074 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2075 TEXTOID, -1, 0);
2076 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2077 INT8OID, -1, 0);
2078 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2079 INT8OID, -1, 0);
2080 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2081 INT8OID, -1, 0);
2082 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2083 INT8OID, -1, 0);
2084 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2085 INT8OID, -1, 0);
2086 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2087 INT8OID, -1, 0);
2088 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2089 INT8OID, -1, 0);
2090 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2091 INT8OID, -1, 0);
2092 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2093 TIMESTAMPTZOID, -1, 0);
2094 BlessTupleDesc(tupdesc);
2095
2096 namestrcpy(&slotname, text_to_cstring(slotname_text));
2097 slotent = pgstat_fetch_replslot(slotname);
2098 if (!slotent)
2099 {
2100 /*
2101 * If the slot is not found, initialise its stats. This is possible if
2102 * the create slot message is lost.
2103 */
2104 memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2105 slotent = &allzero;
2106 }
2107
2108 values[0] = CStringGetTextDatum(NameStr(slotname));
2109 values[1] = Int64GetDatum(slotent->spill_txns);
2110 values[2] = Int64GetDatum(slotent->spill_count);
2111 values[3] = Int64GetDatum(slotent->spill_bytes);
2112 values[4] = Int64GetDatum(slotent->stream_txns);
2113 values[5] = Int64GetDatum(slotent->stream_count);
2114 values[6] = Int64GetDatum(slotent->stream_bytes);
2115 values[7] = Int64GetDatum(slotent->total_txns);
2116 values[8] = Int64GetDatum(slotent->total_bytes);
2117
2118 if (slotent->stat_reset_timestamp == 0)
2119 nulls[9] = true;
2120 else
2122
2123 /* Returns the record as Datum */
2125}
#define NameStr(name)
Definition: c.h:703
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:336
void namestrcpy(Name name, const char *str)
Definition: name.c:233
PgStat_StatReplSlotEntry * pgstat_fetch_replslot(NameData slotname)
#define PG_STAT_GET_REPLICATION_SLOT_COLS
TimestampTz stat_reset_timestamp
Definition: pgstat.h:415
PgStat_Counter stream_count
Definition: pgstat.h:411
PgStat_Counter total_txns
Definition: pgstat.h:413
PgStat_Counter total_bytes
Definition: pgstat.h:414
PgStat_Counter spill_txns
Definition: pgstat.h:407
PgStat_Counter stream_txns
Definition: pgstat.h:410
PgStat_Counter spill_count
Definition: pgstat.h:408
PgStat_Counter stream_bytes
Definition: pgstat.h:412
PgStat_Counter spill_bytes
Definition: pgstat.h:409
Definition: c.h:698

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

◆ pg_stat_get_slru()

Datum pg_stat_get_slru ( PG_FUNCTION_ARGS  )

Definition at line 1674 of file pgstatfuncs.c.

1675{
1676#define PG_STAT_GET_SLRU_COLS 9
1677 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1678 int i;
1679 PgStat_SLRUStats *stats;
1680
1681 InitMaterializedSRF(fcinfo, 0);
1682
1683 /* request SLRU stats from the cumulative stats system */
1684 stats = pgstat_fetch_slru();
1685
1686 for (i = 0;; i++)
1687 {
1688 /* for each row */
1690 bool nulls[PG_STAT_GET_SLRU_COLS] = {0};
1692 const char *name;
1693
1695
1696 if (!name)
1697 break;
1698
1699 stat = stats[i];
1700
1702 values[1] = Int64GetDatum(stat.blocks_zeroed);
1703 values[2] = Int64GetDatum(stat.blocks_hit);
1704 values[3] = Int64GetDatum(stat.blocks_read);
1705 values[4] = Int64GetDatum(stat.blocks_written);
1706 values[5] = Int64GetDatum(stat.blocks_exists);
1707 values[6] = Int64GetDatum(stat.flush);
1708 values[7] = Int64GetDatum(stat.truncate);
1709 values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1710
1711 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
1712 }
1713
1714 return (Datum) 0;
1715}
PgStat_SLRUStats * pgstat_fetch_slru(void)
Definition: pgstat_slru.c:105
const char * pgstat_get_slru_name(int slru_idx)
Definition: pgstat_slru.c:118
#define PG_STAT_GET_SLRU_COLS
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
const char * name
#define stat
Definition: win32_port.h:274

References cstring_to_text(), i, InitMaterializedSRF(), Int64GetDatum(), name, PG_STAT_GET_SLRU_COLS, pgstat_fetch_slru(), pgstat_get_slru_name(), PointerGetDatum(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, stat, TimestampTzGetDatum(), tuplestore_putvalues(), and values.

◆ pg_stat_get_snapshot_timestamp()

Datum pg_stat_get_snapshot_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1794 of file pgstatfuncs.c.

1795{
1796 bool have_snapshot;
1797 TimestampTz ts;
1798
1799 ts = pgstat_get_stat_snapshot_timestamp(&have_snapshot);
1800
1801 if (!have_snapshot)
1803
1805}
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:1046

References PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, and pgstat_get_stat_snapshot_timestamp().

◆ pg_stat_get_subscription_stats()

Datum pg_stat_get_subscription_stats ( PG_FUNCTION_ARGS  )

Definition at line 2132 of file pgstatfuncs.c.

2133{
2134#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 10
2135 Oid subid = PG_GETARG_OID(0);
2136 TupleDesc tupdesc;
2138 bool nulls[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0};
2139 PgStat_StatSubEntry *subentry;
2140 PgStat_StatSubEntry allzero;
2141 int i = 0;
2142
2143 /* Get subscription stats */
2144 subentry = pgstat_fetch_stat_subscription(subid);
2145
2146 /* Initialise attributes information in the tuple descriptor */
2148 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subid",
2149 OIDOID, -1, 0);
2150 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "apply_error_count",
2151 INT8OID, -1, 0);
2152 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "sync_error_count",
2153 INT8OID, -1, 0);
2154 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "confl_insert_exists",
2155 INT8OID, -1, 0);
2156 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "confl_update_origin_differs",
2157 INT8OID, -1, 0);
2158 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "confl_update_exists",
2159 INT8OID, -1, 0);
2160 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "confl_update_missing",
2161 INT8OID, -1, 0);
2162 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "confl_delete_origin_differs",
2163 INT8OID, -1, 0);
2164 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "confl_delete_missing",
2165 INT8OID, -1, 0);
2166 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2167 TIMESTAMPTZOID, -1, 0);
2168 BlessTupleDesc(tupdesc);
2169
2170 if (!subentry)
2171 {
2172 /* If the subscription is not found, initialise its stats */
2173 memset(&allzero, 0, sizeof(PgStat_StatSubEntry));
2174 subentry = &allzero;
2175 }
2176
2177 /* subid */
2178 values[i++] = ObjectIdGetDatum(subid);
2179
2180 /* apply_error_count */
2181 values[i++] = Int64GetDatum(subentry->apply_error_count);
2182
2183 /* sync_error_count */
2184 values[i++] = Int64GetDatum(subentry->sync_error_count);
2185
2186 /* conflict count */
2187 for (int nconflict = 0; nconflict < CONFLICT_NUM_TYPES; nconflict++)
2188 values[i++] = Int64GetDatum(subentry->conflict_count[nconflict]);
2189
2190 /* stats_reset */
2191 if (subentry->stat_reset_timestamp == 0)
2192 nulls[i] = true;
2193 else
2195
2197
2198 /* Returns the record as Datum */
2200}
#define CONFLICT_NUM_TYPES
Definition: conflict.h:51
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS
PgStat_Counter apply_error_count
Definition: pgstat.h:432
PgStat_Counter sync_error_count
Definition: pgstat.h:433
PgStat_Counter conflict_count[CONFLICT_NUM_TYPES]
Definition: pgstat.h:434
TimestampTz stat_reset_timestamp
Definition: pgstat.h:435

References PgStat_StatSubEntry::apply_error_count, Assert, BlessTupleDesc(), PgStat_StatSubEntry::conflict_count, CONFLICT_NUM_TYPES, CreateTemplateTupleDesc(), heap_form_tuple(), HeapTupleGetDatum(), i, Int64GetDatum(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_DATUM, PG_STAT_GET_SUBSCRIPTION_STATS_COLS, pgstat_fetch_stat_subscription(), PgStat_StatSubEntry::stat_reset_timestamp, PgStat_StatSubEntry::sync_error_count, TimestampTzGetDatum(), TupleDescInitEntry(), and values.

◆ pg_stat_get_wal()

Datum pg_stat_get_wal ( PG_FUNCTION_ARGS  )

Definition at line 1610 of file pgstatfuncs.c.

1611{
1612#define PG_STAT_GET_WAL_COLS 9
1613 TupleDesc tupdesc;
1615 bool nulls[PG_STAT_GET_WAL_COLS] = {0};
1616 char buf[256];
1617 PgStat_WalStats *wal_stats;
1618
1619 /* Initialise attributes information in the tuple descriptor */
1621 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1622 INT8OID, -1, 0);
1623 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1624 INT8OID, -1, 0);
1625 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1626 NUMERICOID, -1, 0);
1627 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1628 INT8OID, -1, 0);
1629 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "wal_write",
1630 INT8OID, -1, 0);
1631 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_sync",
1632 INT8OID, -1, 0);
1633 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_write_time",
1634 FLOAT8OID, -1, 0);
1635 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "wal_sync_time",
1636 FLOAT8OID, -1, 0);
1637 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "stats_reset",
1638 TIMESTAMPTZOID, -1, 0);
1639
1640 BlessTupleDesc(tupdesc);
1641
1642 /* Get statistics about WAL activity */
1643 wal_stats = pgstat_fetch_stat_wal();
1644
1645 /* Fill values and NULLs */
1646 values[0] = Int64GetDatum(wal_stats->wal_records);
1647 values[1] = Int64GetDatum(wal_stats->wal_fpi);
1648
1649 /* Convert to numeric. */
1650 snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
1654 Int32GetDatum(-1));
1655
1656 values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
1657 values[4] = Int64GetDatum(wal_stats->wal_write);
1658 values[5] = Int64GetDatum(wal_stats->wal_sync);
1659
1660 /* Convert counters from microsec to millisec for display */
1661 values[6] = Float8GetDatum(((double) wal_stats->wal_write_time) / 1000.0);
1662 values[7] = Float8GetDatum(((double) wal_stats->wal_sync_time) / 1000.0);
1663
1665
1666 /* Returns the record as Datum */
1668}
#define UINT64_FORMAT
Definition: c.h:507
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
static char * buf
Definition: pg_test_fsync.c:72
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:67
#define PG_STAT_GET_WAL_COLS
#define snprintf
Definition: port.h:238
PgStat_Counter wal_write
Definition: pgstat.h:476
PgStat_Counter wal_buffers_full
Definition: pgstat.h:475
PgStat_Counter wal_write_time
Definition: pgstat.h:478
TimestampTz stat_reset_timestamp
Definition: pgstat.h:480
uint64 wal_bytes
Definition: pgstat.h:474
PgStat_Counter wal_sync_time
Definition: pgstat.h:479
PgStat_Counter wal_fpi
Definition: pgstat.h:473
PgStat_Counter wal_sync
Definition: pgstat.h:477
PgStat_Counter wal_records
Definition: pgstat.h:472

References BlessTupleDesc(), buf, CreateTemplateTupleDesc(), CStringGetDatum(), DirectFunctionCall3, Float8GetDatum(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), numeric_in(), ObjectIdGetDatum(), PG_RETURN_DATUM, PG_STAT_GET_WAL_COLS, pgstat_fetch_stat_wal(), snprintf, PgStat_WalStats::stat_reset_timestamp, TimestampTzGetDatum(), TupleDescInitEntry(), UINT64_FORMAT, values, PgStat_WalStats::wal_buffers_full, PgStat_WalStats::wal_bytes, PgStat_WalStats::wal_fpi, PgStat_WalStats::wal_records, PgStat_WalStats::wal_sync, PgStat_WalStats::wal_sync_time, PgStat_WalStats::wal_write, and PgStat_WalStats::wal_write_time.

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 1764 of file pgstatfuncs.c.

1765{
1766 Oid funcid = PG_GETARG_OID(0);
1767 PgStat_FunctionCounts *funcentry;
1768
1769 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1771 PG_RETURN_INT64(funcentry->numcalls);
1772}
PgStat_Counter numcalls
Definition: pgstat.h:83

References find_funcstat_entry(), PgStat_FunctionCounts::numcalls, PG_GETARG_OID, PG_RETURN_INT64, and PG_RETURN_NULL.

◆ pg_stat_have_stats()

Datum pg_stat_have_stats ( PG_FUNCTION_ARGS  )

Definition at line 2210 of file pgstatfuncs.c.

2211{
2212 char *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0));
2213 Oid dboid = PG_GETARG_OID(1);
2214 uint64 objid = PG_GETARG_INT64(2);
2215 PgStat_Kind kind = pgstat_get_kind_from_str(stats_type);
2216
2217 PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objid));
2218}
uint64_t uint64
Definition: c.h:489
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:1063
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1420
#define PgStat_Kind
Definition: pgstat_kind.h:17

References PG_GETARG_INT64, PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_BOOL, pgstat_get_kind_from_str(), pgstat_have_entry(), PgStat_Kind, and text_to_cstring().

◆ pg_stat_io_build_tuples()

static void pg_stat_io_build_tuples ( ReturnSetInfo rsinfo,
PgStat_BktypeIO bktype_stats,
BackendType  bktype,
TimestampTz  stat_reset_timestamp 
)
static

Definition at line 1408 of file pgstatfuncs.c.

1412{
1413 Datum bktype_desc = CStringGetTextDatum(GetBackendTypeDesc(bktype));
1414
1415 for (int io_obj = 0; io_obj < IOOBJECT_NUM_TYPES; io_obj++)
1416 {
1417 const char *obj_name = pgstat_get_io_object_name(io_obj);
1418
1419 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
1420 {
1421 const char *context_name = pgstat_get_io_context_name(io_context);
1422
1424 bool nulls[IO_NUM_COLUMNS] = {0};
1425
1426 /*
1427 * Some combinations of BackendType, IOObject, and IOContext are
1428 * not valid for any type of IOOp. In such cases, omit the entire
1429 * row from the view.
1430 */
1431 if (!pgstat_tracks_io_object(bktype, io_obj, io_context))
1432 continue;
1433
1434 values[IO_COL_BACKEND_TYPE] = bktype_desc;
1435 values[IO_COL_CONTEXT] = CStringGetTextDatum(context_name);
1437 if (stat_reset_timestamp != 0)
1438 values[IO_COL_RESET_TIME] = TimestampTzGetDatum(stat_reset_timestamp);
1439 else
1440 nulls[IO_COL_RESET_TIME] = true;
1441
1442 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
1443 {
1444 int op_idx = pgstat_get_io_op_index(io_op);
1445 int time_idx = pgstat_get_io_time_index(io_op);
1446 int byte_idx = pgstat_get_io_byte_index(io_op);
1447
1448 /*
1449 * Some combinations of BackendType and IOOp, of IOContext and
1450 * IOOp, and of IOObject and IOOp are not tracked. Set these
1451 * cells in the view NULL.
1452 */
1453 if (pgstat_tracks_io_op(bktype, io_obj, io_context, io_op))
1454 {
1455 PgStat_Counter count =
1456 bktype_stats->counts[io_obj][io_context][io_op];
1457
1458 values[op_idx] = Int64GetDatum(count);
1459 }
1460 else
1461 nulls[op_idx] = true;
1462
1463 if (!nulls[op_idx])
1464 {
1465 /* not every operation is timed */
1466 if (time_idx != IO_COL_INVALID)
1467 {
1468 PgStat_Counter time =
1469 bktype_stats->times[io_obj][io_context][io_op];
1470
1471 values[time_idx] = Float8GetDatum(pg_stat_us_to_ms(time));
1472 }
1473
1474 /* not every IO is tracked in bytes */
1475 if (byte_idx != IO_COL_INVALID)
1476 {
1477 char buf[256];
1478 PgStat_Counter byte =
1479 bktype_stats->bytes[io_obj][io_context][io_op];
1480
1481 /* Convert to numeric */
1482 snprintf(buf, sizeof buf, UINT64_FORMAT, byte);
1486 Int32GetDatum(-1));
1487 }
1488 }
1489 else
1490 {
1491 if (time_idx != IO_COL_INVALID)
1492 nulls[time_idx] = true;
1493 if (byte_idx != IO_COL_INVALID)
1494 nulls[byte_idx] = true;
1495 }
1496 }
1497
1498 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
1499 values, nulls);
1500 }
1501 }
1502}
#define IOOP_NUM_TYPES
Definition: pgstat.h:315
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:289
int64 PgStat_Counter
Definition: pgstat.h:66
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:279
const char * pgstat_get_io_context_name(IOContext io_context)
Definition: pgstat_io.c:236
const char * pgstat_get_io_object_name(IOObject io_object)
Definition: pgstat_io.c:255
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:451
bool pgstat_tracks_io_object(BackendType bktype, IOObject io_object, IOContext io_context)
Definition: pgstat_io.c:386
static double pg_stat_us_to_ms(PgStat_Counter val_ms)
Definition: pgstatfuncs.c:1395
static io_stat_col pgstat_get_io_time_index(IOOp io_op)
Definition: pgstatfuncs.c:1370
static io_stat_col pgstat_get_io_op_index(IOOp io_op)
Definition: pgstatfuncs.c:1312
static io_stat_col pgstat_get_io_byte_index(IOOp io_op)
Definition: pgstatfuncs.c:1343
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:323
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:324

References buf, PgStat_BktypeIO::bytes, PgStat_BktypeIO::counts, CStringGetDatum(), CStringGetTextDatum, DirectFunctionCall3, Float8GetDatum(), GetBackendTypeDesc(), Int32GetDatum(), Int64GetDatum(), IO_COL_BACKEND_TYPE, IO_COL_CONTEXT, IO_COL_INVALID, IO_COL_OBJECT, IO_COL_RESET_TIME, IO_NUM_COLUMNS, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, numeric_in(), ObjectIdGetDatum(), pg_stat_us_to_ms(), pgstat_get_io_byte_index(), pgstat_get_io_context_name(), pgstat_get_io_object_name(), pgstat_get_io_op_index(), pgstat_get_io_time_index(), pgstat_tracks_io_object(), pgstat_tracks_io_op(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, snprintf, PgStat_BktypeIO::times, TimestampTzGetDatum(), tuplestore_putvalues(), UINT64_FORMAT, and values.

Referenced by pg_stat_get_backend_io(), and pg_stat_get_io().

◆ pg_stat_reset()

Datum pg_stat_reset ( PG_FUNCTION_ARGS  )

Definition at line 1829 of file pgstatfuncs.c.

1830{
1832
1834}
void pgstat_reset_counters(void)
Definition: pgstat.c:851

References PG_RETURN_VOID, and pgstat_reset_counters().

◆ pg_stat_reset_backend_stats()

Datum pg_stat_reset_backend_stats ( PG_FUNCTION_ARGS  )

Definition at line 1915 of file pgstatfuncs.c.

1916{
1917 PGPROC *proc;
1918 int backend_pid = PG_GETARG_INT32(0);
1919
1920 proc = BackendPidGetProc(backend_pid);
1921
1922 /*
1923 * This could be an auxiliary process but these do not report backend
1924 * statistics due to pgstat_tracks_backend_bktype(), so there is no need
1925 * for an extra call to AuxiliaryPidGetProc().
1926 */
1927 if (!proc)
1929
1931
1933}
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:870
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32

References BackendPidGetProc(), GetNumberFromPGProc, InvalidOid, PG_GETARG_INT32, PG_RETURN_VOID, PGSTAT_KIND_BACKEND, and pgstat_reset().

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 1954 of file pgstatfuncs.c.

1955{
1956 char *target = NULL;
1957
1958 if (PG_ARGISNULL(0))
1960 else
1961 {
1963 pgstat_reset_replslot(target);
1964 }
1965
1967}
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:892
#define PGSTAT_KIND_REPLSLOT
Definition: pgstat_kind.h:30
void pgstat_reset_replslot(const char *name)

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_REPLSLOT, pgstat_reset_of_kind(), pgstat_reset_replslot(), and text_to_cstring().

◆ pg_stat_reset_shared()

Datum pg_stat_reset_shared ( PG_FUNCTION_ARGS  )

Definition at line 1843 of file pgstatfuncs.c.

1844{
1845 char *target = NULL;
1846
1847 if (PG_ARGISNULL(0))
1848 {
1849 /* Reset all the statistics when nothing is specified */
1857
1859 }
1860
1862
1863 if (strcmp(target, "archiver") == 0)
1865 else if (strcmp(target, "bgwriter") == 0)
1867 else if (strcmp(target, "checkpointer") == 0)
1869 else if (strcmp(target, "io") == 0)
1871 else if (strcmp(target, "recovery_prefetch") == 0)
1873 else if (strcmp(target, "slru") == 0)
1875 else if (strcmp(target, "wal") == 0)
1877 else
1878 ereport(ERROR,
1879 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1880 errmsg("unrecognized reset target: \"%s\"", target),
1881 errhint("Target must be \"archiver\", \"bgwriter\", \"checkpointer\", \"io\", \"recovery_prefetch\", \"slru\", or \"wal\".")));
1882
1884}
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define PGSTAT_KIND_ARCHIVER
Definition: pgstat_kind.h:35
#define PGSTAT_KIND_WAL
Definition: pgstat_kind.h:40
#define PGSTAT_KIND_BGWRITER
Definition: pgstat_kind.h:36
#define PGSTAT_KIND_SLRU
Definition: pgstat_kind.h:39
#define PGSTAT_KIND_CHECKPOINTER
Definition: pgstat_kind.h:37
#define PGSTAT_KIND_IO
Definition: pgstat_kind.h:38
void XLogPrefetchResetStats(void)

References ereport, errcode(), errhint(), errmsg(), ERROR, PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_ARCHIVER, PGSTAT_KIND_BGWRITER, PGSTAT_KIND_CHECKPOINTER, PGSTAT_KIND_IO, PGSTAT_KIND_SLRU, PGSTAT_KIND_WAL, pgstat_reset_of_kind(), text_to_cstring(), and XLogPrefetchResetStats().

◆ pg_stat_reset_single_function_counters()

Datum pg_stat_reset_single_function_counters ( PG_FUNCTION_ARGS  )

Definition at line 1902 of file pgstatfuncs.c.

1903{
1904 Oid funcoid = PG_GETARG_OID(0);
1905
1907
1909}
Oid MyDatabaseId
Definition: globals.c:93
#define PGSTAT_KIND_FUNCTION
Definition: pgstat_kind.h:29

References MyDatabaseId, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_FUNCTION, and pgstat_reset().

◆ pg_stat_reset_single_table_counters()

Datum pg_stat_reset_single_table_counters ( PG_FUNCTION_ARGS  )

Definition at line 1891 of file pgstatfuncs.c.

1892{
1893 Oid taboid = PG_GETARG_OID(0);
1894 Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
1895
1896 pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
1897
1899}
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:273
#define PGSTAT_KIND_RELATION
Definition: pgstat_kind.h:28

References InvalidOid, IsSharedRelation(), MyDatabaseId, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_RELATION, and pgstat_reset().

◆ pg_stat_reset_slru()

Datum pg_stat_reset_slru ( PG_FUNCTION_ARGS  )

Definition at line 1937 of file pgstatfuncs.c.

1938{
1939 char *target = NULL;
1940
1941 if (PG_ARGISNULL(0))
1943 else
1944 {
1946 pgstat_reset_slru(target);
1947 }
1948
1950}
void pgstat_reset_slru(const char *name)
Definition: pgstat_slru.c:45

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_KIND_SLRU, pgstat_reset_of_kind(), pgstat_reset_slru(), and text_to_cstring().

◆ pg_stat_reset_subscription_stats()

Datum pg_stat_reset_subscription_stats ( PG_FUNCTION_ARGS  )

Definition at line 1971 of file pgstatfuncs.c.

1972{
1973 Oid subid;
1974
1975 if (PG_ARGISNULL(0))
1976 {
1977 /* Clear all subscription stats */
1979 }
1980 else
1981 {
1982 subid = PG_GETARG_OID(0);
1983
1984 if (!OidIsValid(subid))
1985 ereport(ERROR,
1986 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1987 errmsg("invalid subscription OID %u", subid)));
1989 }
1990
1992}
#define OidIsValid(objectId)
Definition: c.h:732
#define PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat_kind.h:31

References ereport, errcode(), errmsg(), ERROR, InvalidOid, OidIsValid, PG_ARGISNULL, PG_GETARG_OID, PG_RETURN_VOID, PGSTAT_KIND_SUBSCRIPTION, pgstat_reset(), and pgstat_reset_of_kind().

◆ pg_stat_us_to_ms()

static double pg_stat_us_to_ms ( PgStat_Counter  val_ms)
inlinestatic

Definition at line 1395 of file pgstatfuncs.c.

1396{
1397 return val_ms * (double) 0.001;
1398}

Referenced by pg_stat_io_build_tuples().

◆ pgstat_get_io_byte_index()

static io_stat_col pgstat_get_io_byte_index ( IOOp  io_op)
static

Definition at line 1343 of file pgstatfuncs.c.

1344{
1345 switch (io_op)
1346 {
1347 case IOOP_EXTEND:
1348 return IO_COL_EXTEND_BYTES;
1349 case IOOP_READ:
1350 return IO_COL_READ_BYTES;
1351 case IOOP_WRITE:
1352 return IO_COL_WRITE_BYTES;
1353 case IOOP_EVICT:
1354 case IOOP_FSYNC:
1355 case IOOP_HIT:
1356 case IOOP_REUSE:
1357 case IOOP_WRITEBACK:
1358 return IO_COL_INVALID;
1359 }
1360
1361 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1363}
#define pg_unreachable()
Definition: c.h:318
#define elog(elevel,...)
Definition: elog.h:225
@ IOOP_EXTEND
Definition: pgstat.h:310
@ IOOP_FSYNC
Definition: pgstat.h:304
@ IOOP_READ
Definition: pgstat.h:311
@ IOOP_WRITEBACK
Definition: pgstat.h:307
@ IOOP_HIT
Definition: pgstat.h:305
@ IOOP_EVICT
Definition: pgstat.h:303
@ IOOP_REUSE
Definition: pgstat.h:306
@ IOOP_WRITE
Definition: pgstat.h:312

References elog, ERROR, IO_COL_EXTEND_BYTES, IO_COL_INVALID, IO_COL_READ_BYTES, IO_COL_WRITE_BYTES, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, and pg_unreachable.

Referenced by pg_stat_io_build_tuples().

◆ pgstat_get_io_op_index()

static io_stat_col pgstat_get_io_op_index ( IOOp  io_op)
static

Definition at line 1312 of file pgstatfuncs.c.

1313{
1314 switch (io_op)
1315 {
1316 case IOOP_EVICT:
1317 return IO_COL_EVICTIONS;
1318 case IOOP_EXTEND:
1319 return IO_COL_EXTENDS;
1320 case IOOP_FSYNC:
1321 return IO_COL_FSYNCS;
1322 case IOOP_HIT:
1323 return IO_COL_HITS;
1324 case IOOP_READ:
1325 return IO_COL_READS;
1326 case IOOP_REUSE:
1327 return IO_COL_REUSES;
1328 case IOOP_WRITE:
1329 return IO_COL_WRITES;
1330 case IOOP_WRITEBACK:
1331 return IO_COL_WRITEBACKS;
1332 }
1333
1334 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1336}

References elog, ERROR, IO_COL_EVICTIONS, IO_COL_EXTENDS, IO_COL_FSYNCS, IO_COL_HITS, IO_COL_READS, IO_COL_REUSES, IO_COL_WRITEBACKS, IO_COL_WRITES, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, and pg_unreachable.

Referenced by pg_stat_io_build_tuples().

◆ pgstat_get_io_time_index()

static io_stat_col pgstat_get_io_time_index ( IOOp  io_op)
static

Definition at line 1370 of file pgstatfuncs.c.

1371{
1372 switch (io_op)
1373 {
1374 case IOOP_READ:
1375 return IO_COL_READ_TIME;
1376 case IOOP_WRITE:
1377 return IO_COL_WRITE_TIME;
1378 case IOOP_WRITEBACK:
1379 return IO_COL_WRITEBACK_TIME;
1380 case IOOP_EXTEND:
1381 return IO_COL_EXTEND_TIME;
1382 case IOOP_FSYNC:
1383 return IO_COL_FSYNC_TIME;
1384 case IOOP_EVICT:
1385 case IOOP_HIT:
1386 case IOOP_REUSE:
1387 return IO_COL_INVALID;
1388 }
1389
1390 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1392}

References elog, ERROR, IO_COL_EXTEND_TIME, IO_COL_FSYNC_TIME, IO_COL_INVALID, IO_COL_READ_TIME, IO_COL_WRITE_TIME, IO_COL_WRITEBACK_TIME, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, and pg_unreachable.

Referenced by pg_stat_io_build_tuples().