PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_FLOAT8(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_WAL_COLS   5
 
#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   11
 

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)
 
static Datum pg_stat_wal_build_tuple (PgStat_WalCounters wal_counters, TimestampTz stat_reset_timestamp)
 
Datum pg_stat_get_backend_wal (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:364
#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:81
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)

Definition at line 1182 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:499
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368

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

◆ PG_STAT_GET_PROGRESS_COLS

#define PG_STAT_GET_PROGRESS_COLS   PGSTAT_NUM_PROGRESS_PARAM + 3

◆ PG_STAT_GET_RELENTRY_FLOAT8

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

Definition at line 109 of file pgstatfuncs.c.

◆ 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); \
}

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 137 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   11

◆ PG_STAT_GET_SUBXACT_COLS

#define PG_STAT_GET_SUBXACT_COLS   2

◆ 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 1803 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 1746 of file pgstatfuncs.c.

◆ PG_STAT_WAL_COLS

#define PG_STAT_WAL_COLS   5

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

1313{
1314 IO_COL_INVALID = -1,
1336} io_stat_col;
io_stat_col
Definition: pgstatfuncs.c:1313
@ IO_COL_READS
Definition: pgstatfuncs.c:1318
@ IO_NUM_COLUMNS
Definition: pgstatfuncs.c:1335
@ IO_COL_RESET_TIME
Definition: pgstatfuncs.c:1334
@ IO_COL_WRITE_TIME
Definition: pgstatfuncs.c:1323
@ IO_COL_HITS
Definition: pgstatfuncs.c:1329
@ IO_COL_EXTENDS
Definition: pgstatfuncs.c:1326
@ IO_COL_WRITEBACK_TIME
Definition: pgstatfuncs.c:1325
@ IO_COL_REUSES
Definition: pgstatfuncs.c:1331
@ IO_COL_WRITES
Definition: pgstatfuncs.c:1321
@ IO_COL_OBJECT
Definition: pgstatfuncs.c:1316
@ IO_COL_EVICTIONS
Definition: pgstatfuncs.c:1330
@ IO_COL_WRITEBACKS
Definition: pgstatfuncs.c:1324
@ IO_COL_CONTEXT
Definition: pgstatfuncs.c:1317
@ IO_COL_READ_BYTES
Definition: pgstatfuncs.c:1319
@ IO_COL_EXTEND_BYTES
Definition: pgstatfuncs.c:1327
@ IO_COL_BACKEND_TYPE
Definition: pgstatfuncs.c:1315
@ IO_COL_FSYNC_TIME
Definition: pgstatfuncs.c:1333
@ IO_COL_EXTEND_TIME
Definition: pgstatfuncs.c:1328
@ IO_COL_FSYNCS
Definition: pgstatfuncs.c:1332
@ IO_COL_WRITE_BYTES
Definition: pgstatfuncs.c:1322
@ IO_COL_INVALID
Definition: pgstatfuncs.c:1314
@ IO_COL_READ_TIME
Definition: pgstatfuncs.c:1320

Function Documentation

◆ pg_backend_pid()

Datum pg_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 690 of file pgstatfuncs.c.

691{
693}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int MyProcPid
Definition: globals.c:48

References MyProcPid, and PG_RETURN_INT32.

◆ pg_stat_clear_snapshot()

Datum pg_stat_clear_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 1838 of file pgstatfuncs.c.

1839{
1841
1843}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_clear_snapshot(void)
Definition: pgstat.c:917

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

1849{
1851
1853}
void pgstat_force_next_flush(void)
Definition: pgstat.c:829

References PG_RETURN_VOID, and pgstat_force_next_flush().

◆ pg_stat_get_activity()

Datum pg_stat_get_activity ( PG_FUNCTION_ARGS  )

Definition at line 331 of file pgstatfuncs.c.

332{
333#define PG_STAT_GET_ACTIVITY_COLS 31
334 int num_backends = pgstat_fetch_stat_numbackends();
335 int curr_backend;
336 int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
337 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
338
339 InitMaterializedSRF(fcinfo, 0);
340
341 /* 1-based index */
342 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
343 {
344 /* for each row */
346 bool nulls[PG_STAT_GET_ACTIVITY_COLS] = {0};
347 LocalPgBackendStatus *local_beentry;
348 PgBackendStatus *beentry;
349 PGPROC *proc;
350 const char *wait_event_type = NULL;
351 const char *wait_event = NULL;
352
353 /* Get the next one in the list */
354 local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
355 beentry = &local_beentry->backendStatus;
356
357 /* If looking for specific PID, ignore all the others */
358 if (pid != -1 && beentry->st_procpid != pid)
359 continue;
360
361 /* Values available to all callers */
362 if (beentry->st_databaseid != InvalidOid)
364 else
365 nulls[0] = true;
366
367 values[1] = Int32GetDatum(beentry->st_procpid);
368
369 if (beentry->st_userid != InvalidOid)
370 values[2] = ObjectIdGetDatum(beentry->st_userid);
371 else
372 nulls[2] = true;
373
374 if (beentry->st_appname)
376 else
377 nulls[3] = true;
378
379 if (TransactionIdIsValid(local_beentry->backend_xid))
380 values[15] = TransactionIdGetDatum(local_beentry->backend_xid);
381 else
382 nulls[15] = true;
383
384 if (TransactionIdIsValid(local_beentry->backend_xmin))
385 values[16] = TransactionIdGetDatum(local_beentry->backend_xmin);
386 else
387 nulls[16] = true;
388
389 /* Values only available to role member or pg_read_all_stats */
390 if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
391 {
392 char *clipped_activity;
393
394 switch (beentry->st_state)
395 {
396 case STATE_STARTING:
397 values[4] = CStringGetTextDatum("starting");
398 break;
399 case STATE_IDLE:
400 values[4] = CStringGetTextDatum("idle");
401 break;
402 case STATE_RUNNING:
403 values[4] = CStringGetTextDatum("active");
404 break;
406 values[4] = CStringGetTextDatum("idle in transaction");
407 break;
408 case STATE_FASTPATH:
409 values[4] = CStringGetTextDatum("fastpath function call");
410 break;
412 values[4] = CStringGetTextDatum("idle in transaction (aborted)");
413 break;
414 case STATE_DISABLED:
415 values[4] = CStringGetTextDatum("disabled");
416 break;
417 case STATE_UNDEFINED:
418 nulls[4] = true;
419 break;
420 }
421
422 clipped_activity = pgstat_clip_activity(beentry->st_activity_raw);
423 values[5] = CStringGetTextDatum(clipped_activity);
424 pfree(clipped_activity);
425
426 /* leader_pid */
427 nulls[29] = true;
428
429 proc = BackendPidGetProc(beentry->st_procpid);
430
431 if (proc == NULL && (beentry->st_backendType != B_BACKEND))
432 {
433 /*
434 * For an auxiliary process, retrieve process info from
435 * AuxiliaryProcs stored in shared-memory.
436 */
437 proc = AuxiliaryPidGetProc(beentry->st_procpid);
438 }
439
440 /*
441 * If a PGPROC entry was retrieved, display wait events and lock
442 * group leader or apply leader information if any. To avoid
443 * extra overhead, no extra lock is being held, so there is no
444 * guarantee of consistency across multiple rows.
445 */
446 if (proc != NULL)
447 {
448 uint32 raw_wait_event;
449 PGPROC *leader;
450
451 raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
452 wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
453 wait_event = pgstat_get_wait_event(raw_wait_event);
454
455 leader = proc->lockGroupLeader;
456
457 /*
458 * Show the leader only for active parallel workers. This
459 * leaves the field as NULL for the leader of a parallel group
460 * or the leader of parallel apply workers.
461 */
462 if (leader && leader->pid != beentry->st_procpid)
463 {
464 values[29] = Int32GetDatum(leader->pid);
465 nulls[29] = false;
466 }
467 else if (beentry->st_backendType == B_BG_WORKER)
468 {
469 int leader_pid = GetLeaderApplyWorkerPid(beentry->st_procpid);
470
471 if (leader_pid != InvalidPid)
472 {
473 values[29] = Int32GetDatum(leader_pid);
474 nulls[29] = false;
475 }
476 }
477 }
478
479 if (wait_event_type)
480 values[6] = CStringGetTextDatum(wait_event_type);
481 else
482 nulls[6] = true;
483
484 if (wait_event)
485 values[7] = CStringGetTextDatum(wait_event);
486 else
487 nulls[7] = true;
488
489 /*
490 * Don't expose transaction time for walsenders; it confuses
491 * monitoring, particularly because we don't keep the time up-to-
492 * date.
493 */
494 if (beentry->st_xact_start_timestamp != 0 &&
495 beentry->st_backendType != B_WAL_SENDER)
497 else
498 nulls[8] = true;
499
500 if (beentry->st_activity_start_timestamp != 0)
502 else
503 nulls[9] = true;
504
505 if (beentry->st_proc_start_timestamp != 0)
507 else
508 nulls[10] = true;
509
510 if (beentry->st_state_start_timestamp != 0)
512 else
513 nulls[11] = true;
514
515 /* A zeroed client addr means we don't know */
517 sizeof(beentry->st_clientaddr)))
518 {
519 nulls[12] = true;
520 nulls[13] = true;
521 nulls[14] = true;
522 }
523 else
524 {
525 if (beentry->st_clientaddr.addr.ss_family == AF_INET ||
526 beentry->st_clientaddr.addr.ss_family == AF_INET6)
527 {
528 char remote_host[NI_MAXHOST];
529 char remote_port[NI_MAXSERV];
530 int ret;
531
532 remote_host[0] = '\0';
533 remote_port[0] = '\0';
534 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
535 beentry->st_clientaddr.salen,
536 remote_host, sizeof(remote_host),
537 remote_port, sizeof(remote_port),
538 NI_NUMERICHOST | NI_NUMERICSERV);
539 if (ret == 0)
540 {
541 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
543 CStringGetDatum(remote_host));
544 if (beentry->st_clienthostname &&
545 beentry->st_clienthostname[0])
547 else
548 nulls[13] = true;
549 values[14] = Int32GetDatum(atoi(remote_port));
550 }
551 else
552 {
553 nulls[12] = true;
554 nulls[13] = true;
555 nulls[14] = true;
556 }
557 }
558 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
559 {
560 /*
561 * Unix sockets always reports NULL for host and -1 for
562 * port, so it's possible to tell the difference to
563 * connections we have no permissions to view, or with
564 * errors.
565 */
566 nulls[12] = true;
567 nulls[13] = true;
568 values[14] = Int32GetDatum(-1);
569 }
570 else
571 {
572 /* Unknown address type, should never happen */
573 nulls[12] = true;
574 nulls[13] = true;
575 nulls[14] = true;
576 }
577 }
578 /* Add backend type */
579 if (beentry->st_backendType == B_BG_WORKER)
580 {
581 const char *bgw_type;
582
583 bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid);
584 if (bgw_type)
585 values[17] = CStringGetTextDatum(bgw_type);
586 else
587 nulls[17] = true;
588 }
589 else
590 values[17] =
592
593 /* SSL information */
594 if (beentry->st_ssl)
595 {
596 values[18] = BoolGetDatum(true); /* ssl */
599 values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits);
600
601 if (beentry->st_sslstatus->ssl_client_dn[0])
603 else
604 nulls[22] = true;
605
606 if (beentry->st_sslstatus->ssl_client_serial[0])
610 Int32GetDatum(-1));
611 else
612 nulls[23] = true;
613
614 if (beentry->st_sslstatus->ssl_issuer_dn[0])
616 else
617 nulls[24] = true;
618 }
619 else
620 {
621 values[18] = BoolGetDatum(false); /* ssl */
622 nulls[19] = nulls[20] = nulls[21] = nulls[22] = nulls[23] = nulls[24] = true;
623 }
624
625 /* GSSAPI information */
626 if (beentry->st_gss)
627 {
628 values[25] = BoolGetDatum(beentry->st_gssstatus->gss_auth); /* gss_auth */
630 values[27] = BoolGetDatum(beentry->st_gssstatus->gss_enc); /* GSS Encryption in use */
631 values[28] = BoolGetDatum(beentry->st_gssstatus->gss_delegation); /* GSS credentials
632 * delegated */
633 }
634 else
635 {
636 values[25] = BoolGetDatum(false); /* gss_auth */
637 nulls[26] = true; /* No GSS principal */
638 values[27] = BoolGetDatum(false); /* GSS Encryption not in
639 * use */
640 values[28] = BoolGetDatum(false); /* GSS credentials not
641 * delegated */
642 }
643 if (beentry->st_query_id == 0)
644 nulls[30] = true;
645 else
646 values[30] = UInt64GetDatum(beentry->st_query_id);
647 }
648 else
649 {
650 /* No permissions to view data about this session */
651 values[5] = CStringGetTextDatum("<insufficient privilege>");
652 nulls[4] = true;
653 nulls[6] = true;
654 nulls[7] = true;
655 nulls[8] = true;
656 nulls[9] = true;
657 nulls[10] = true;
658 nulls[11] = true;
659 nulls[12] = true;
660 nulls[13] = true;
661 nulls[14] = true;
662 nulls[17] = true;
663 nulls[18] = true;
664 nulls[19] = true;
665 nulls[20] = true;
666 nulls[21] = true;
667 nulls[22] = true;
668 nulls[23] = true;
669 nulls[24] = true;
670 nulls[25] = true;
671 nulls[26] = true;
672 nulls[27] = true;
673 nulls[28] = true;
674 nulls[29] = true;
675 nulls[30] = true;
676 }
677
678 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
679
680 /* If only a single backend was requested, and we found it, break. */
681 if (pid != -1)
682 break;
683 }
684
685 return (Datum) 0;
686}
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_STARTING
@ 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:502
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:686
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:2146
static bool pg_memory_is_all_zeros(const void *ptr, size_t len)
Definition: memutils.h:239
@ B_WAL_SENDER
Definition: miscadmin.h:347
@ B_BG_WORKER
Definition: miscadmin.h:346
@ B_BACKEND
Definition: miscadmin.h:342
#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:2060
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:35
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3196
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:1092
TransactionId backend_xid
PgBackendStatus backendStatus
TransactionId backend_xmin
Definition: proc.h:163
uint32 wait_event_info
Definition: proc.h:280
int pid
Definition: proc.h:183
PGPROC * lockGroupLeader
Definition: proc.h:305
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:359
Tuplestorestate * setResult
Definition: execnodes.h:358
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_STARTING, 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 2035 of file pgstatfuncs.c.

2036{
2037 TupleDesc tupdesc;
2038 Datum values[7] = {0};
2039 bool nulls[7] = {0};
2040 PgStat_ArchiverStats *archiver_stats;
2041
2042 /* Initialise attributes information in the tuple descriptor */
2043 tupdesc = CreateTemplateTupleDesc(7);
2044 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count",
2045 INT8OID, -1, 0);
2046 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal",
2047 TEXTOID, -1, 0);
2048 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time",
2049 TIMESTAMPTZOID, -1, 0);
2050 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count",
2051 INT8OID, -1, 0);
2052 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal",
2053 TEXTOID, -1, 0);
2054 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time",
2055 TIMESTAMPTZOID, -1, 0);
2056 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset",
2057 TIMESTAMPTZOID, -1, 0);
2058
2059 BlessTupleDesc(tupdesc);
2060
2061 /* Get statistics about the archiver process */
2062 archiver_stats = pgstat_fetch_stat_archiver();
2063
2064 /* Fill values and NULLs */
2065 values[0] = Int64GetDatum(archiver_stats->archived_count);
2066 if (*(archiver_stats->last_archived_wal) == '\0')
2067 nulls[1] = true;
2068 else
2069 values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal);
2070
2071 if (archiver_stats->last_archived_timestamp == 0)
2072 nulls[2] = true;
2073 else
2075
2076 values[3] = Int64GetDatum(archiver_stats->failed_count);
2077 if (*(archiver_stats->last_failed_wal) == '\0')
2078 nulls[4] = true;
2079 else
2080 values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal);
2081
2082 if (archiver_stats->last_failed_timestamp == 0)
2083 nulls[5] = true;
2084 else
2085 values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp);
2086
2087 if (archiver_stats->stat_reset_timestamp == 0)
2088 nulls[6] = true;
2089 else
2090 values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp);
2091
2092 /* Returns the record as Datum */
2094}
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
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:225
TimestampTz stat_reset_timestamp
Definition: pgstat.h:226
TimestampTz last_archived_timestamp
Definition: pgstat.h:221
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:223
PgStat_Counter failed_count
Definition: pgstat.h:222
PgStat_Counter archived_count
Definition: pgstat.h:218
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:219
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:175
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:835

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

771{
772 int32 procNumber = PG_GETARG_INT32(0);
773 PgBackendStatus *beentry;
774 const char *activity;
775 char *clipped_activity;
776 text *ret;
777
778 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
779 activity = "<backend information not available>";
780 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
781 activity = "<insufficient privilege>";
782 else if (*(beentry->st_activity_raw) == '\0')
783 activity = "<command string not enabled>";
784 else
785 activity = beentry->st_activity_raw;
786
787 clipped_activity = pgstat_clip_activity(activity);
788 ret = cstring_to_text(activity);
789 pfree(clipped_activity);
790
791 PG_RETURN_TEXT_P(ret);
792}
PgBackendStatus * pgstat_get_beentry_by_proc_number(ProcNumber procNumber)
int32_t int32
Definition: c.h:498
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:658
text * cstring_to_text(const char *s)
Definition: varlena.c:192

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

839{
840 int32 procNumber = PG_GETARG_INT32(0);
841 TimestampTz result;
842 PgBackendStatus *beentry;
843
844 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
846
847 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
849
850 result = beentry->st_activity_start_timestamp;
851
852 /*
853 * No time recorded for start of current query -- this is the case if the
854 * user hasn't enabled query-level stats collection.
855 */
856 if (result == 0)
858
859 PG_RETURN_TIMESTAMPTZ(result);
860}
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 908 of file pgstatfuncs.c.

909{
910 int32 procNumber = PG_GETARG_INT32(0);
911 PgBackendStatus *beentry;
912 char remote_host[NI_MAXHOST];
913 int ret;
914
915 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
917
918 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
920
921 /* A zeroed client addr means we don't know */
923 sizeof(beentry->st_clientaddr)))
925
926 switch (beentry->st_clientaddr.addr.ss_family)
927 {
928 case AF_INET:
929 case AF_INET6:
930 break;
931 default:
933 }
934
935 remote_host[0] = '\0';
936 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
937 beentry->st_clientaddr.salen,
938 remote_host, sizeof(remote_host),
939 NULL, 0,
940 NI_NUMERICHOST | NI_NUMERICSERV);
941 if (ret != 0)
943
944 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
945
947 CStringGetDatum(remote_host)));
948}

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

952{
953 int32 procNumber = PG_GETARG_INT32(0);
954 PgBackendStatus *beentry;
955 char remote_port[NI_MAXSERV];
956 int ret;
957
958 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
960
961 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
963
964 /* A zeroed client addr means we don't know */
966 sizeof(beentry->st_clientaddr)))
968
969 switch (beentry->st_clientaddr.addr.ss_family)
970 {
971 case AF_INET:
972 case AF_INET6:
973 break;
974 case AF_UNIX:
975 PG_RETURN_INT32(-1);
976 default:
978 }
979
980 remote_port[0] = '\0';
981 ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
982 beentry->st_clientaddr.salen,
983 NULL, 0,
984 remote_port, sizeof(remote_port),
985 NI_NUMERICHOST | NI_NUMERICSERV);
986 if (ret != 0)
988
990 CStringGetDatum(remote_port)));
991}
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 710 of file pgstatfuncs.c.

711{
712 int32 procNumber = PG_GETARG_INT32(0);
713 PgBackendStatus *beentry;
714
715 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
717
719}
#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 204 of file pgstatfuncs.c.

205{
206 FuncCallContext *funcctx;
207 int *fctx;
208
209 /* stuff done only on the first call of the function */
210 if (SRF_IS_FIRSTCALL())
211 {
212 /* create a function context for cross-call persistence */
213 funcctx = SRF_FIRSTCALL_INIT();
214
216 sizeof(int));
217 funcctx->user_fctx = fctx;
218
219 fctx[0] = 0;
220 }
221
222 /* stuff done on every call of the function */
223 funcctx = SRF_PERCALL_SETUP();
224 fctx = funcctx->user_fctx;
225
226 fctx[0] += 1;
227
228 /*
229 * We recheck pgstat_fetch_stat_numbackends() each time through, just in
230 * case the local status data has been refreshed since we started. It's
231 * plenty cheap enough if not. If a refresh does happen, we'll likely
232 * miss or duplicate some backend IDs, but we're content not to crash.
233 * (Refreshing midway through such a query would be problematic usage
234 * anyway, since the backend IDs we've already returned might no longer
235 * refer to extant sessions.)
236 */
237 if (fctx[0] <= pgstat_fetch_stat_numbackends())
238 {
239 /* do when there is more left to send */
241
242 SRF_RETURN_NEXT(funcctx, Int32GetDatum(local_beentry->proc_number));
243 }
244 else
245 {
246 /* do when there is no more left */
247 SRF_RETURN_DONE(funcctx);
248 }
249}
#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:1256
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 1577 of file pgstatfuncs.c.

1578{
1579 ReturnSetInfo *rsinfo;
1580 BackendType bktype;
1581 int pid;
1582 PgStat_Backend *backend_stats;
1583 PgStat_BktypeIO *bktype_stats;
1584
1585 InitMaterializedSRF(fcinfo, 0);
1586 rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1587
1588 pid = PG_GETARG_INT32(0);
1589 backend_stats = pgstat_fetch_stat_backend_by_pid(pid, &bktype);
1590
1591 if (!backend_stats)
1592 return (Datum) 0;
1593
1594 bktype_stats = &backend_stats->io_stats;
1595
1596 /*
1597 * In Assert builds, we can afford an extra loop through all of the
1598 * counters (in pg_stat_io_build_tuples()), checking that only expected
1599 * stats are non-zero, since it keeps the non-Assert code cleaner.
1600 */
1601 Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype));
1602
1603 /* save tuples with data from this PgStat_BktypeIO */
1604 pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype,
1605 backend_stats->stat_reset_timestamp);
1606 return (Datum) 0;
1607}
Assert(PointerIsAligned(start, uint64))
BackendType
Definition: miscadmin.h:338
PgStat_Backend * pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype)
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:1439
TimestampTz stat_reset_timestamp
Definition: pgstat.h:490
PgStat_BktypeIO io_stats
Definition: pgstat.h:491

References Assert(), InitMaterializedSRF(), PgStat_Backend::io_stats, PG_GETARG_INT32, pg_stat_io_build_tuples(), pgstat_bktype_io_stats_valid(), pgstat_fetch_stat_backend_by_pid(), and PgStat_Backend::stat_reset_timestamp.

◆ pg_stat_get_backend_pid()

Datum pg_stat_get_backend_pid ( PG_FUNCTION_ARGS  )

Definition at line 697 of file pgstatfuncs.c.

698{
699 int32 procNumber = PG_GETARG_INT32(0);
700 PgBackendStatus *beentry;
701
702 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
704
705 PG_RETURN_INT32(beentry->st_procpid);
706}

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

887{
888 int32 procNumber = PG_GETARG_INT32(0);
889 TimestampTz result;
890 PgBackendStatus *beentry;
891
892 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
894
895 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
897
898 result = beentry->st_proc_start_timestamp;
899
900 if (result == 0) /* probably can't happen? */
902
903 PG_RETURN_TIMESTAMPTZ(result);
904}

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

736{
737#define PG_STAT_GET_SUBXACT_COLS 2
738 TupleDesc tupdesc;
740 bool nulls[PG_STAT_GET_SUBXACT_COLS] = {0};
741 int32 procNumber = PG_GETARG_INT32(0);
742 LocalPgBackendStatus *local_beentry;
743
744 /* Initialise attributes information in the tuple descriptor */
746 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subxact_count",
747 INT4OID, -1, 0);
748 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "subxact_overflow",
749 BOOLOID, -1, 0);
750
751 BlessTupleDesc(tupdesc);
752
753 if ((local_beentry = pgstat_get_local_beentry_by_proc_number(procNumber)) != NULL)
754 {
755 /* Fill values and NULLs */
756 values[0] = Int32GetDatum(local_beentry->backend_subxact_count);
758 }
759 else
760 {
761 nulls[0] = true;
762 nulls[1] = true;
763 }
764
765 /* Returns the record as Datum */
767}
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 723 of file pgstatfuncs.c.

724{
725 int32 procNumber = PG_GETARG_INT32(0);
726 PgBackendStatus *beentry;
727
728 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
730
731 PG_RETURN_OID(beentry->st_userid);
732}

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

817{
818 int32 procNumber = PG_GETARG_INT32(0);
819 PgBackendStatus *beentry;
820 PGPROC *proc;
821 const char *wait_event = NULL;
822
823 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
824 wait_event = "<backend information not available>";
825 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
826 wait_event = "<insufficient privilege>";
827 else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
828 wait_event = pgstat_get_wait_event(proc->wait_event_info);
829
830 if (!wait_event)
832
834}

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

796{
797 int32 procNumber = PG_GETARG_INT32(0);
798 PgBackendStatus *beentry;
799 PGPROC *proc;
800 const char *wait_event_type = NULL;
801
802 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
803 wait_event_type = "<backend information not available>";
804 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
805 wait_event_type = "<insufficient privilege>";
806 else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
807 wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
808
809 if (!wait_event_type)
811
812 PG_RETURN_TEXT_P(cstring_to_text(wait_event_type));
813}

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

Datum pg_stat_get_backend_wal ( PG_FUNCTION_ARGS  )

Definition at line 1666 of file pgstatfuncs.c.

1667{
1668 int pid;
1669 PgStat_Backend *backend_stats;
1670 PgStat_WalCounters bktype_stats;
1671
1672 pid = PG_GETARG_INT32(0);
1673 backend_stats = pgstat_fetch_stat_backend_by_pid(pid, NULL);
1674
1675 if (!backend_stats)
1677
1678 bktype_stats = backend_stats->wal_counters;
1679
1680 /* save tuples with data from this PgStat_WalCounters */
1681 return (pg_stat_wal_build_tuple(bktype_stats, backend_stats->stat_reset_timestamp));
1682}
static Datum pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters, TimestampTz stat_reset_timestamp)
Definition: pgstatfuncs.c:1616
PgStat_WalCounters wal_counters
Definition: pgstat.h:492

References PG_GETARG_INT32, PG_RETURN_NULL, pg_stat_wal_build_tuple(), pgstat_fetch_stat_backend_by_pid(), PgStat_Backend::stat_reset_timestamp, and PgStat_Backend::wal_counters.

◆ pg_stat_get_backend_xact_start()

Datum pg_stat_get_backend_xact_start ( PG_FUNCTION_ARGS  )

Definition at line 864 of file pgstatfuncs.c.

865{
866 int32 procNumber = PG_GETARG_INT32(0);
867 TimestampTz result;
868 PgBackendStatus *beentry;
869
870 if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
872
873 else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
875
876 result = beentry->st_xact_start_timestamp;
877
878 if (result == 0) /* not in a transaction */
880
881 PG_RETURN_TIMESTAMPTZ(result);
882}

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

1263{
1264 PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean);
1265}
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 1268 of file pgstatfuncs.c.

1269{
1270 PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean);
1271}

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

1297{
1298 PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp);
1299}

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

1303{
1305}

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

1251{
1253}
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 1226 of file pgstatfuncs.c.

1227{
1229}

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

1221{
1223}

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

1215{
1217}

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

1245{
1246 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_performed);
1247}

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

1239{
1240 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_requested);
1241}

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

1233{
1234 PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_timed);
1235}

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

1257{
1259}

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

1291{
1293}

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

1283{
1284 /* time is already in msec, just convert to double for presentation */
1285 PG_RETURN_FLOAT8((double)
1286 pgstat_fetch_stat_checkpointer()->sync_time);
1287}

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

1275{
1276 /* time is already in msec, just convert to double for presentation */
1277 PG_RETURN_FLOAT8((double)
1278 pgstat_fetch_stat_checkpointer()->write_time);
1279}

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

1144{
1145 Oid dbid = PG_GETARG_OID(0);
1146 int64 result;
1147 PgStat_StatDBEntry *dbentry;
1148
1149 if (!DataChecksumsEnabled())
1151
1152 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1153 result = 0;
1154 else
1155 result = (int64) (dbentry->checksum_failures);
1156
1157 PG_RETURN_INT64(result);
1158}
unsigned int Oid
Definition: postgres_ext.h:30
PgStat_Counter checksum_failures
Definition: pgstat.h:363
bool DataChecksumsEnabled(void)
Definition: xlog.c:4754

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

1162{
1163 Oid dbid = PG_GETARG_OID(0);
1164 TimestampTz result;
1165 PgStat_StatDBEntry *dbentry;
1166
1167 if (!DataChecksumsEnabled())
1169
1170 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1171 result = 0;
1172 else
1173 result = dbentry->last_checksum_failure;
1174
1175 if (result == 0)
1177 else
1178 PG_RETURN_TIMESTAMPTZ(result);
1179}
TimestampTz last_checksum_failure
Definition: pgstat.h:364

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

1124{
1125 Oid dbid = PG_GETARG_OID(0);
1126 int64 result;
1127 PgStat_StatDBEntry *dbentry;
1128
1129 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1130 result = 0;
1131 else
1132 result = (int64) (dbentry->conflict_tablespace +
1133 dbentry->conflict_lock +
1134 dbentry->conflict_snapshot +
1135 dbentry->conflict_logicalslot +
1136 dbentry->conflict_bufferpin +
1137 dbentry->conflict_startup_deadlock);
1138
1139 PG_RETURN_INT64(result);
1140}
PgStat_Counter conflict_startup_deadlock
Definition: pgstat.h:359
PgStat_Counter conflict_lock
Definition: pgstat.h:355
PgStat_Counter conflict_snapshot
Definition: pgstat.h:356
PgStat_Counter conflict_bufferpin
Definition: pgstat.h:358
PgStat_Counter conflict_logicalslot
Definition: pgstat.h:357
PgStat_Counter conflict_tablespace
Definition: pgstat.h:354

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

996{
997 Oid dbid = PG_GETARG_OID(0);
998 int32 result;
999 int tot_backends = pgstat_fetch_stat_numbackends();
1000 int idx;
1001
1002 result = 0;
1003 for (idx = 1; idx <= tot_backends; idx++)
1004 {
1006
1007 if (local_beentry->backendStatus.st_databaseid == dbid)
1008 result++;
1009 }
1010
1011 PG_RETURN_INT32(result);
1012}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262

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

1105{
1106 Oid dbid = PG_GETARG_OID(0);
1107 TimestampTz result;
1108 PgStat_StatDBEntry *dbentry;
1109
1110 if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1111 result = 0;
1112 else
1113 result = dbentry->stat_reset_timestamp;
1114
1115 if (result == 0)
1117 else
1118 PG_RETURN_TIMESTAMPTZ(result);
1119}
TimestampTz stat_reset_timestamp
Definition: pgstat.h:377

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

173{
174 Oid funcid = PG_GETARG_OID(0);
175 PgStat_StatFuncEntry *funcentry;
176
177 if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
179 PG_RETURN_INT64(funcentry->numcalls);
180}
PgStat_Counter numcalls
Definition: pgstat.h:382

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

1537{
1538 ReturnSetInfo *rsinfo;
1539 PgStat_IO *backends_io_stats;
1540
1541 InitMaterializedSRF(fcinfo, 0);
1542 rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1543
1544 backends_io_stats = pgstat_fetch_stat_io();
1545
1546 for (int bktype = 0; bktype < BACKEND_NUM_TYPES; bktype++)
1547 {
1548 PgStat_BktypeIO *bktype_stats = &backends_io_stats->stats[bktype];
1549
1550 /*
1551 * In Assert builds, we can afford an extra loop through all of the
1552 * counters (in pg_stat_io_build_tuples()), checking that only
1553 * expected stats are non-zero, since it keeps the non-Assert code
1554 * cleaner.
1555 */
1556 Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype));
1557
1558 /*
1559 * For those BackendTypes without IO Operation stats, skip
1560 * representing them in the view altogether.
1561 */
1562 if (!pgstat_tracks_io_bktype(bktype))
1563 continue;
1564
1565 /* save tuples with data from this PgStat_BktypeIO */
1566 pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype,
1567 backends_io_stats->stat_reset_timestamp);
1568 }
1569
1570 return (Datum) 0;
1571}
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:377
PgStat_IO * pgstat_fetch_stat_io(void)
Definition: pgstat_io.c:163
bool pgstat_tracks_io_bktype(BackendType bktype)
Definition: pgstat_io.c:359
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:339
TimestampTz stat_reset_timestamp
Definition: pgstat.h:338

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

256{
257#define PG_STAT_GET_PROGRESS_COLS PGSTAT_NUM_PROGRESS_PARAM + 3
258 int num_backends = pgstat_fetch_stat_numbackends();
259 int curr_backend;
260 char *cmd = text_to_cstring(PG_GETARG_TEXT_PP(0));
261 ProgressCommandType cmdtype;
262 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
263
264 /* Translate command name into command type code. */
265 if (pg_strcasecmp(cmd, "VACUUM") == 0)
266 cmdtype = PROGRESS_COMMAND_VACUUM;
267 else if (pg_strcasecmp(cmd, "ANALYZE") == 0)
268 cmdtype = PROGRESS_COMMAND_ANALYZE;
269 else if (pg_strcasecmp(cmd, "CLUSTER") == 0)
270 cmdtype = PROGRESS_COMMAND_CLUSTER;
271 else if (pg_strcasecmp(cmd, "CREATE INDEX") == 0)
273 else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0)
275 else if (pg_strcasecmp(cmd, "COPY") == 0)
276 cmdtype = PROGRESS_COMMAND_COPY;
277 else
279 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
280 errmsg("invalid command name: \"%s\"", cmd)));
281
282 InitMaterializedSRF(fcinfo, 0);
283
284 /* 1-based index */
285 for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
286 {
287 LocalPgBackendStatus *local_beentry;
288 PgBackendStatus *beentry;
290 bool nulls[PG_STAT_GET_PROGRESS_COLS] = {0};
291 int i;
292
293 local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
294 beentry = &local_beentry->backendStatus;
295
296 /*
297 * Report values for only those backends which are running the given
298 * command.
299 */
300 if (beentry->st_progress_command != cmdtype)
301 continue;
302
303 /* Value available to all callers */
304 values[0] = Int32GetDatum(beentry->st_procpid);
306
307 /* show rest of the values including relid only to role members */
308 if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
309 {
311 for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
312 values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
313 }
314 else
315 {
316 nulls[2] = true;
317 for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
318 nulls[i + 3] = true;
319 }
320
321 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
322 }
323
324 return (Datum) 0;
325}
#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:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#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:77
#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:225

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

2102{
2103#define PG_STAT_GET_REPLICATION_SLOT_COLS 10
2104 text *slotname_text = PG_GETARG_TEXT_P(0);
2105 NameData slotname;
2106 TupleDesc tupdesc;
2108 bool nulls[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0};
2109 PgStat_StatReplSlotEntry *slotent;
2111
2112 /* Initialise attributes information in the tuple descriptor */
2114 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name",
2115 TEXTOID, -1, 0);
2116 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns",
2117 INT8OID, -1, 0);
2118 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count",
2119 INT8OID, -1, 0);
2120 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes",
2121 INT8OID, -1, 0);
2122 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns",
2123 INT8OID, -1, 0);
2124 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count",
2125 INT8OID, -1, 0);
2126 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes",
2127 INT8OID, -1, 0);
2128 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns",
2129 INT8OID, -1, 0);
2130 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes",
2131 INT8OID, -1, 0);
2132 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset",
2133 TIMESTAMPTZOID, -1, 0);
2134 BlessTupleDesc(tupdesc);
2135
2136 namestrcpy(&slotname, text_to_cstring(slotname_text));
2137 slotent = pgstat_fetch_replslot(slotname);
2138 if (!slotent)
2139 {
2140 /*
2141 * If the slot is not found, initialise its stats. This is possible if
2142 * the create slot message is lost.
2143 */
2144 memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry));
2145 slotent = &allzero;
2146 }
2147
2148 values[0] = CStringGetTextDatum(NameStr(slotname));
2149 values[1] = Int64GetDatum(slotent->spill_txns);
2150 values[2] = Int64GetDatum(slotent->spill_count);
2151 values[3] = Int64GetDatum(slotent->spill_bytes);
2152 values[4] = Int64GetDatum(slotent->stream_txns);
2153 values[5] = Int64GetDatum(slotent->stream_count);
2154 values[6] = Int64GetDatum(slotent->stream_bytes);
2155 values[7] = Int64GetDatum(slotent->total_txns);
2156 values[8] = Int64GetDatum(slotent->total_bytes);
2157
2158 if (slotent->stat_reset_timestamp == 0)
2159 nulls[9] = true;
2160 else
2162
2163 /* Returns the record as Datum */
2165}
#define NameStr(name)
Definition: c.h:717
#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:398
PgStat_Counter stream_count
Definition: pgstat.h:394
PgStat_Counter total_txns
Definition: pgstat.h:396
PgStat_Counter total_bytes
Definition: pgstat.h:397
PgStat_Counter spill_txns
Definition: pgstat.h:390
PgStat_Counter stream_txns
Definition: pgstat.h:393
PgStat_Counter spill_count
Definition: pgstat.h:391
PgStat_Counter stream_bytes
Definition: pgstat.h:395
PgStat_Counter spill_bytes
Definition: pgstat.h:392
Definition: c.h:712

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

1704{
1705#define PG_STAT_GET_SLRU_COLS 9
1706 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1707 int i;
1708 PgStat_SLRUStats *stats;
1709
1710 InitMaterializedSRF(fcinfo, 0);
1711
1712 /* request SLRU stats from the cumulative stats system */
1713 stats = pgstat_fetch_slru();
1714
1715 for (i = 0;; i++)
1716 {
1717 /* for each row */
1719 bool nulls[PG_STAT_GET_SLRU_COLS] = {0};
1721 const char *name;
1722
1724
1725 if (!name)
1726 break;
1727
1728 stat = stats[i];
1729
1731 values[1] = Int64GetDatum(stat.blocks_zeroed);
1732 values[2] = Int64GetDatum(stat.blocks_hit);
1733 values[3] = Int64GetDatum(stat.blocks_read);
1734 values[4] = Int64GetDatum(stat.blocks_written);
1735 values[5] = Int64GetDatum(stat.blocks_exists);
1736 values[6] = Int64GetDatum(stat.flush);
1737 values[7] = Int64GetDatum(stat.truncate);
1738 values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp);
1739
1740 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
1741 }
1742
1743 return (Datum) 0;
1744}
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 1823 of file pgstatfuncs.c.

1824{
1825 bool have_snapshot;
1826 TimestampTz ts;
1827
1828 ts = pgstat_get_stat_snapshot_timestamp(&have_snapshot);
1829
1830 if (!have_snapshot)
1832
1834}
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:1045

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

2173{
2174#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 11
2175 Oid subid = PG_GETARG_OID(0);
2176 TupleDesc tupdesc;
2178 bool nulls[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0};
2179 PgStat_StatSubEntry *subentry;
2180 PgStat_StatSubEntry allzero;
2181 int i = 0;
2182
2183 /* Get subscription stats */
2184 subentry = pgstat_fetch_stat_subscription(subid);
2185
2186 /* Initialise attributes information in the tuple descriptor */
2188 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subid",
2189 OIDOID, -1, 0);
2190 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "apply_error_count",
2191 INT8OID, -1, 0);
2192 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "sync_error_count",
2193 INT8OID, -1, 0);
2194 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "confl_insert_exists",
2195 INT8OID, -1, 0);
2196 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "confl_update_origin_differs",
2197 INT8OID, -1, 0);
2198 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "confl_update_exists",
2199 INT8OID, -1, 0);
2200 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "confl_update_missing",
2201 INT8OID, -1, 0);
2202 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "confl_delete_origin_differs",
2203 INT8OID, -1, 0);
2204 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "confl_delete_missing",
2205 INT8OID, -1, 0);
2206 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "confl_multiple_unique_conflicts",
2207 INT8OID, -1, 0);
2208 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "stats_reset",
2209 TIMESTAMPTZOID, -1, 0);
2210 BlessTupleDesc(tupdesc);
2211
2212 if (!subentry)
2213 {
2214 /* If the subscription is not found, initialise its stats */
2215 memset(&allzero, 0, sizeof(PgStat_StatSubEntry));
2216 subentry = &allzero;
2217 }
2218
2219 /* subid */
2220 values[i++] = ObjectIdGetDatum(subid);
2221
2222 /* apply_error_count */
2223 values[i++] = Int64GetDatum(subentry->apply_error_count);
2224
2225 /* sync_error_count */
2226 values[i++] = Int64GetDatum(subentry->sync_error_count);
2227
2228 /* conflict count */
2229 for (int nconflict = 0; nconflict < CONFLICT_NUM_TYPES; nconflict++)
2230 values[i++] = Int64GetDatum(subentry->conflict_count[nconflict]);
2231
2232 /* stats_reset */
2233 if (subentry->stat_reset_timestamp == 0)
2234 nulls[i] = true;
2235 else
2237
2239
2240 /* Returns the record as Datum */
2242}
#define CONFLICT_NUM_TYPES
Definition: conflict.h:54
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS
PgStat_Counter apply_error_count
Definition: pgstat.h:415
PgStat_Counter sync_error_count
Definition: pgstat.h:416
PgStat_Counter conflict_count[CONFLICT_NUM_TYPES]
Definition: pgstat.h:417
TimestampTz stat_reset_timestamp
Definition: pgstat.h:418

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

1689{
1690 PgStat_WalStats *wal_stats;
1691
1692 /* Get statistics about WAL activity */
1693 wal_stats = pgstat_fetch_stat_wal();
1694
1695 return (pg_stat_wal_build_tuple(wal_stats->wal_counters,
1696 wal_stats->stat_reset_timestamp));
1697}
PgStat_WalStats * pgstat_fetch_stat_wal(void)
Definition: pgstat_wal.c:67
TimestampTz stat_reset_timestamp
Definition: pgstat.h:481
PgStat_WalCounters wal_counters
Definition: pgstat.h:480

References pg_stat_wal_build_tuple(), pgstat_fetch_stat_wal(), PgStat_WalStats::stat_reset_timestamp, and PgStat_WalStats::wal_counters.

◆ pg_stat_get_xact_function_calls()

Datum pg_stat_get_xact_function_calls ( PG_FUNCTION_ARGS  )

Definition at line 1793 of file pgstatfuncs.c.

1794{
1795 Oid funcid = PG_GETARG_OID(0);
1796 PgStat_FunctionCounts *funcentry;
1797
1798 if ((funcentry = find_funcstat_entry(funcid)) == NULL)
1800 PG_RETURN_INT64(funcentry->numcalls);
1801}
PgStat_Counter numcalls
Definition: pgstat.h:82

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

2253{
2254 char *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0));
2255 Oid dboid = PG_GETARG_OID(1);
2256 uint64 objid = PG_GETARG_INT64(2);
2257 PgStat_Kind kind = pgstat_get_kind_from_str(stats_type);
2258
2259 PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objid));
2260}
uint64_t uint64
Definition: c.h:503
#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:1062
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1419
#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 1439 of file pgstatfuncs.c.

1443{
1444 Datum bktype_desc = CStringGetTextDatum(GetBackendTypeDesc(bktype));
1445
1446 for (int io_obj = 0; io_obj < IOOBJECT_NUM_TYPES; io_obj++)
1447 {
1448 const char *obj_name = pgstat_get_io_object_name(io_obj);
1449
1450 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
1451 {
1452 const char *context_name = pgstat_get_io_context_name(io_context);
1453
1455 bool nulls[IO_NUM_COLUMNS] = {0};
1456
1457 /*
1458 * Some combinations of BackendType, IOObject, and IOContext are
1459 * not valid for any type of IOOp. In such cases, omit the entire
1460 * row from the view.
1461 */
1462 if (!pgstat_tracks_io_object(bktype, io_obj, io_context))
1463 continue;
1464
1465 values[IO_COL_BACKEND_TYPE] = bktype_desc;
1466 values[IO_COL_CONTEXT] = CStringGetTextDatum(context_name);
1468 if (stat_reset_timestamp != 0)
1469 values[IO_COL_RESET_TIME] = TimestampTzGetDatum(stat_reset_timestamp);
1470 else
1471 nulls[IO_COL_RESET_TIME] = true;
1472
1473 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
1474 {
1475 int op_idx = pgstat_get_io_op_index(io_op);
1476 int time_idx = pgstat_get_io_time_index(io_op);
1477 int byte_idx = pgstat_get_io_byte_index(io_op);
1478
1479 /*
1480 * Some combinations of BackendType and IOOp, of IOContext and
1481 * IOOp, and of IOObject and IOOp are not tracked. Set these
1482 * cells in the view NULL.
1483 */
1484 if (pgstat_tracks_io_op(bktype, io_obj, io_context, io_op))
1485 {
1486 PgStat_Counter count =
1487 bktype_stats->counts[io_obj][io_context][io_op];
1488
1489 values[op_idx] = Int64GetDatum(count);
1490 }
1491 else
1492 nulls[op_idx] = true;
1493
1494 if (!nulls[op_idx])
1495 {
1496 /* not every operation is timed */
1497 if (time_idx != IO_COL_INVALID)
1498 {
1499 PgStat_Counter time =
1500 bktype_stats->times[io_obj][io_context][io_op];
1501
1502 values[time_idx] = Float8GetDatum(pg_stat_us_to_ms(time));
1503 }
1504
1505 /* not every IO is tracked in bytes */
1506 if (byte_idx != IO_COL_INVALID)
1507 {
1508 char buf[256];
1509 PgStat_Counter byte =
1510 bktype_stats->bytes[io_obj][io_context][io_op];
1511
1512 /* Convert to numeric */
1513 snprintf(buf, sizeof buf, UINT64_FORMAT, byte);
1517 Int32GetDatum(-1));
1518 }
1519 }
1520 else
1521 {
1522 if (time_idx != IO_COL_INVALID)
1523 nulls[time_idx] = true;
1524 if (byte_idx != IO_COL_INVALID)
1525 nulls[byte_idx] = true;
1526 }
1527 }
1528
1529 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
1530 values, nulls);
1531 }
1532 }
1533}
#define UINT64_FORMAT
Definition: c.h:521
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
static char * buf
Definition: pg_test_fsync.c:72
#define IOOP_NUM_TYPES
Definition: pgstat.h:316
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:290
int64 PgStat_Counter
Definition: pgstat.h:65
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:279
const char * pgstat_get_io_context_name(IOContext io_context)
Definition: pgstat_io.c:248
const char * pgstat_get_io_object_name(IOObject io_object)
Definition: pgstat_io.c:269
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:485
bool pgstat_tracks_io_object(BackendType bktype, IOObject io_object, IOContext io_context)
Definition: pgstat_io.c:401
static double pg_stat_us_to_ms(PgStat_Counter val_ms)
Definition: pgstatfuncs.c:1426
static io_stat_col pgstat_get_io_time_index(IOOp io_op)
Definition: pgstatfuncs.c:1401
static io_stat_col pgstat_get_io_op_index(IOOp io_op)
Definition: pgstatfuncs.c:1343
static io_stat_col pgstat_get_io_byte_index(IOOp io_op)
Definition: pgstatfuncs.c:1374
#define snprintf
Definition: port.h:239
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:326
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:324
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325

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

1859{
1861
1863}
void pgstat_reset_counters(void)
Definition: pgstat.c:850

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

1945{
1946 PGPROC *proc;
1947 PgBackendStatus *beentry;
1948 ProcNumber procNumber;
1949 int backend_pid = PG_GETARG_INT32(0);
1950
1951 proc = BackendPidGetProc(backend_pid);
1952
1953 /* This could be an auxiliary process */
1954 if (!proc)
1955 proc = AuxiliaryPidGetProc(backend_pid);
1956
1957 if (!proc)
1959
1960 procNumber = GetNumberFromPGProc(proc);
1961
1962 beentry = pgstat_get_beentry_by_proc_number(procNumber);
1963 if (!beentry)
1965
1966 /* Check if the backend type tracks statistics */
1969
1971
1973}
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:869
bool pgstat_tracks_backend_bktype(BackendType bktype)
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32
#define GetNumberFromPGProc(proc)
Definition: proc.h:425
int ProcNumber
Definition: procnumber.h:24

References AuxiliaryPidGetProc(), BackendPidGetProc(), GetNumberFromPGProc, InvalidOid, PG_GETARG_INT32, PG_RETURN_VOID, pgstat_get_beentry_by_proc_number(), PGSTAT_KIND_BACKEND, pgstat_reset(), pgstat_tracks_backend_bktype(), and PgBackendStatus::st_backendType.

◆ pg_stat_reset_replication_slot()

Datum pg_stat_reset_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 1994 of file pgstatfuncs.c.

1995{
1996 char *target = NULL;
1997
1998 if (PG_ARGISNULL(0))
2000 else
2001 {
2003 pgstat_reset_replslot(target);
2004 }
2005
2007}
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:891
#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 1872 of file pgstatfuncs.c.

1873{
1874 char *target = NULL;
1875
1876 if (PG_ARGISNULL(0))
1877 {
1878 /* Reset all the statistics when nothing is specified */
1886
1888 }
1889
1891
1892 if (strcmp(target, "archiver") == 0)
1894 else if (strcmp(target, "bgwriter") == 0)
1896 else if (strcmp(target, "checkpointer") == 0)
1898 else if (strcmp(target, "io") == 0)
1900 else if (strcmp(target, "recovery_prefetch") == 0)
1902 else if (strcmp(target, "slru") == 0)
1904 else if (strcmp(target, "wal") == 0)
1906 else
1907 ereport(ERROR,
1908 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1909 errmsg("unrecognized reset target: \"%s\"", target),
1910 errhint("Target must be \"archiver\", \"bgwriter\", \"checkpointer\", \"io\", \"recovery_prefetch\", \"slru\", or \"wal\".")));
1911
1913}
int errhint(const char *fmt,...)
Definition: elog.c:1318
#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 1931 of file pgstatfuncs.c.

1932{
1933 Oid funcoid = PG_GETARG_OID(0);
1934
1936
1938}
Oid MyDatabaseId
Definition: globals.c:95
#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 1920 of file pgstatfuncs.c.

1921{
1922 Oid taboid = PG_GETARG_OID(0);
1923 Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
1924
1925 pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
1926
1928}
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:304
#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 1977 of file pgstatfuncs.c.

1978{
1979 char *target = NULL;
1980
1981 if (PG_ARGISNULL(0))
1983 else
1984 {
1986 pgstat_reset_slru(target);
1987 }
1988
1990}
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 2011 of file pgstatfuncs.c.

2012{
2013 Oid subid;
2014
2015 if (PG_ARGISNULL(0))
2016 {
2017 /* Clear all subscription stats */
2019 }
2020 else
2021 {
2022 subid = PG_GETARG_OID(0);
2023
2024 if (!OidIsValid(subid))
2025 ereport(ERROR,
2026 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2027 errmsg("invalid subscription OID %u", subid)));
2029 }
2030
2032}
#define OidIsValid(objectId)
Definition: c.h:746
#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 1426 of file pgstatfuncs.c.

1427{
1428 return val_ms * (double) 0.001;
1429}

Referenced by pg_stat_io_build_tuples().

◆ pg_stat_wal_build_tuple()

static Datum pg_stat_wal_build_tuple ( PgStat_WalCounters  wal_counters,
TimestampTz  stat_reset_timestamp 
)
static

Definition at line 1616 of file pgstatfuncs.c.

1618{
1619#define PG_STAT_WAL_COLS 5
1620 TupleDesc tupdesc;
1622 bool nulls[PG_STAT_WAL_COLS] = {0};
1623 char buf[256];
1624
1625 /* Initialise attributes information in the tuple descriptor */
1627 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
1628 INT8OID, -1, 0);
1629 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
1630 INT8OID, -1, 0);
1631 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
1632 NUMERICOID, -1, 0);
1633 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
1634 INT8OID, -1, 0);
1635 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stats_reset",
1636 TIMESTAMPTZOID, -1, 0);
1637
1638 BlessTupleDesc(tupdesc);
1639
1640 /* Fill values and NULLs */
1641 values[0] = Int64GetDatum(wal_counters.wal_records);
1642 values[1] = Int64GetDatum(wal_counters.wal_fpi);
1643
1644 /* Convert to numeric. */
1645 snprintf(buf, sizeof buf, UINT64_FORMAT, wal_counters.wal_bytes);
1649 Int32GetDatum(-1));
1650
1651 values[3] = Int64GetDatum(wal_counters.wal_buffers_full);
1652
1653 if (stat_reset_timestamp != 0)
1654 values[4] = TimestampTzGetDatum(stat_reset_timestamp);
1655 else
1656 nulls[4] = true;
1657
1658 /* Returns the record as Datum */
1660}
#define PG_STAT_WAL_COLS
PgStat_Counter wal_fpi
Definition: pgstat.h:469
uint64 wal_bytes
Definition: pgstat.h:470
PgStat_Counter wal_buffers_full
Definition: pgstat.h:471
PgStat_Counter wal_records
Definition: pgstat.h:468

References BlessTupleDesc(), buf, CreateTemplateTupleDesc(), CStringGetDatum(), DirectFunctionCall3, heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), numeric_in(), ObjectIdGetDatum(), PG_RETURN_DATUM, PG_STAT_WAL_COLS, snprintf, TimestampTzGetDatum(), TupleDescInitEntry(), UINT64_FORMAT, values, PgStat_WalCounters::wal_buffers_full, PgStat_WalCounters::wal_bytes, PgStat_WalCounters::wal_fpi, and PgStat_WalCounters::wal_records.

Referenced by pg_stat_get_backend_wal(), and pg_stat_get_wal().

◆ pgstat_get_io_byte_index()

static io_stat_col pgstat_get_io_byte_index ( IOOp  io_op)
static

Definition at line 1374 of file pgstatfuncs.c.

1375{
1376 switch (io_op)
1377 {
1378 case IOOP_EXTEND:
1379 return IO_COL_EXTEND_BYTES;
1380 case IOOP_READ:
1381 return IO_COL_READ_BYTES;
1382 case IOOP_WRITE:
1383 return IO_COL_WRITE_BYTES;
1384 case IOOP_EVICT:
1385 case IOOP_FSYNC:
1386 case IOOP_HIT:
1387 case IOOP_REUSE:
1388 case IOOP_WRITEBACK:
1389 return IO_COL_INVALID;
1390 }
1391
1392 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1394}
#define pg_unreachable()
Definition: c.h:332
#define elog(elevel,...)
Definition: elog.h:226
@ IOOP_EXTEND
Definition: pgstat.h:311
@ IOOP_FSYNC
Definition: pgstat.h:305
@ IOOP_READ
Definition: pgstat.h:312
@ IOOP_WRITEBACK
Definition: pgstat.h:308
@ IOOP_HIT
Definition: pgstat.h:306
@ IOOP_EVICT
Definition: pgstat.h:304
@ IOOP_REUSE
Definition: pgstat.h:307
@ IOOP_WRITE
Definition: pgstat.h:313

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

1344{
1345 switch (io_op)
1346 {
1347 case IOOP_EVICT:
1348 return IO_COL_EVICTIONS;
1349 case IOOP_EXTEND:
1350 return IO_COL_EXTENDS;
1351 case IOOP_FSYNC:
1352 return IO_COL_FSYNCS;
1353 case IOOP_HIT:
1354 return IO_COL_HITS;
1355 case IOOP_READ:
1356 return IO_COL_READS;
1357 case IOOP_REUSE:
1358 return IO_COL_REUSES;
1359 case IOOP_WRITE:
1360 return IO_COL_WRITES;
1361 case IOOP_WRITEBACK:
1362 return IO_COL_WRITEBACKS;
1363 }
1364
1365 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1367}

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

1402{
1403 switch (io_op)
1404 {
1405 case IOOP_READ:
1406 return IO_COL_READ_TIME;
1407 case IOOP_WRITE:
1408 return IO_COL_WRITE_TIME;
1409 case IOOP_WRITEBACK:
1410 return IO_COL_WRITEBACK_TIME;
1411 case IOOP_EXTEND:
1412 return IO_COL_EXTEND_TIME;
1413 case IOOP_FSYNC:
1414 return IO_COL_FSYNC_TIME;
1415 case IOOP_EVICT:
1416 case IOOP_HIT:
1417 case IOOP_REUSE:
1418 return IO_COL_INVALID;
1419 }
1420
1421 elog(ERROR, "unrecognized IOOp value: %d", io_op);
1423}

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