| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pgstatfuncs.c |
| 4 |
* Functions for accessing various forms of statistics data |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/utils/adt/pgstatfuncs.c |
| 12 |
* |
| 13 |
*------------------------------------------------------------------------- |
| 14 |
*/ |
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "access/xlog.h" |
| 19 |
#include "access/xlogprefetcher.h" |
| 20 |
#include "catalog/catalog.h" |
| 21 |
#include "catalog/pg_authid.h" |
| 22 |
#include "catalog/pg_type.h" |
| 23 |
#include "common/ip.h" |
| 24 |
#include "funcapi.h" |
| 25 |
#include "miscadmin.h" |
| 26 |
#include "pgstat.h" |
| 27 |
#include "postmaster/bgworker.h" |
| 28 |
#include "replication/logicallauncher.h" |
| 29 |
#include "storage/proc.h" |
| 30 |
#include "storage/procarray.h" |
| 31 |
#include "utils/acl.h" |
| 32 |
#include "utils/builtins.h" |
| 33 |
#include "utils/timestamp.h" |
| 34 |
|
| 35 |
#define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var)))) |
| 36 |
|
| 37 |
#define HAS_PGSTAT_PERMISSIONS(role) (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role)) |
| 38 |
|
| 39 |
#define PG_STAT_GET_RELENTRY_INT64(stat) \ |
| 40 |
Datum \ |
| 41 |
CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \ |
| 42 |
{ \ |
| 43 |
Oid relid = PG_GETARG_OID(0); \ |
| 44 |
int64 result; \ |
| 45 |
PgStat_StatTabEntry *tabentry; \ |
| 46 |
\ |
| 47 |
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \ |
| 48 |
result = 0; \ |
| 49 |
else \ |
| 50 |
result = (int64) (tabentry->stat); \ |
| 51 |
\ |
| 52 |
PG_RETURN_INT64(result); \ |
| 53 |
} |
| 54 |
|
| 55 |
/* pg_stat_get_analyze_count */ |
| 56 |
PG_STAT_GET_RELENTRY_INT64(analyze_count) |
| 57 |
|
| 58 |
/* pg_stat_get_autoanalyze_count */ |
| 59 |
PG_STAT_GET_RELENTRY_INT64(autoanalyze_count) |
| 60 |
|
| 61 |
/* pg_stat_get_autovacuum_count */ |
| 62 |
PG_STAT_GET_RELENTRY_INT64(autovacuum_count) |
| 63 |
|
| 64 |
/* pg_stat_get_blocks_fetched */ |
| 65 |
PG_STAT_GET_RELENTRY_INT64(blocks_fetched) |
| 66 |
|
| 67 |
/* pg_stat_get_blocks_hit */ |
| 68 |
PG_STAT_GET_RELENTRY_INT64(blocks_hit) |
| 69 |
|
| 70 |
/* pg_stat_get_dead_tuples */ |
| 71 |
PG_STAT_GET_RELENTRY_INT64(dead_tuples) |
| 72 |
|
| 73 |
/* pg_stat_get_ins_since_vacuum */ |
| 74 |
PG_STAT_GET_RELENTRY_INT64(ins_since_vacuum) |
| 75 |
|
| 76 |
/* pg_stat_get_live_tuples */ |
| 77 |
PG_STAT_GET_RELENTRY_INT64(live_tuples) |
| 78 |
|
| 79 |
/* pg_stat_get_mod_since_analyze */ |
| 80 |
PG_STAT_GET_RELENTRY_INT64(mod_since_analyze) |
| 81 |
|
| 82 |
/* pg_stat_get_numscans */ |
| 83 |
PG_STAT_GET_RELENTRY_INT64(numscans) |
| 84 |
|
| 85 |
/* pg_stat_get_tuples_deleted */ |
| 86 |
PG_STAT_GET_RELENTRY_INT64(tuples_deleted) |
| 87 |
|
| 88 |
/* pg_stat_get_tuples_fetched */ |
| 89 |
PG_STAT_GET_RELENTRY_INT64(tuples_fetched) |
| 90 |
|
| 91 |
/* pg_stat_get_tuples_hot_updated */ |
| 92 |
PG_STAT_GET_RELENTRY_INT64(tuples_hot_updated) |
| 93 |
|
| 94 |
/* pg_stat_get_tuples_newpage_updated */ |
| 95 |
PG_STAT_GET_RELENTRY_INT64(tuples_newpage_updated) |
| 96 |
|
| 97 |
/* pg_stat_get_tuples_inserted */ |
| 98 |
PG_STAT_GET_RELENTRY_INT64(tuples_inserted) |
| 99 |
|
| 100 |
/* pg_stat_get_tuples_returned */ |
| 101 |
PG_STAT_GET_RELENTRY_INT64(tuples_returned) |
| 102 |
|
| 103 |
/* pg_stat_get_tuples_updated */ |
| 104 |
PG_STAT_GET_RELENTRY_INT64(tuples_updated) |
| 105 |
|
| 106 |
/* pg_stat_get_vacuum_count */ |
| 107 |
PG_STAT_GET_RELENTRY_INT64(vacuum_count) |
| 108 |
|
| 109 |
#define PG_STAT_GET_RELENTRY_FLOAT8(stat) \ |
| 110 |
Datum \ |
| 111 |
CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \ |
| 112 |
{ \ |
| 113 |
Oid relid = PG_GETARG_OID(0); \ |
| 114 |
double result; \ |
| 115 |
PgStat_StatTabEntry *tabentry; \ |
| 116 |
\ |
| 117 |
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \ |
| 118 |
result = 0; \ |
| 119 |
else \ |
| 120 |
result = (double) (tabentry->stat); \ |
| 121 |
\ |
| 122 |
PG_RETURN_FLOAT8(result); \ |
| 123 |
} |
| 124 |
|
| 125 |
/* pg_stat_get_total_vacuum_time */ |
| 126 |
PG_STAT_GET_RELENTRY_FLOAT8(total_vacuum_time) |
| 127 |
|
| 128 |
/* pg_stat_get_total_autovacuum_time */ |
| 129 |
PG_STAT_GET_RELENTRY_FLOAT8(total_autovacuum_time) |
| 130 |
|
| 131 |
/* pg_stat_get_total_analyze_time */ |
| 132 |
PG_STAT_GET_RELENTRY_FLOAT8(total_analyze_time) |
| 133 |
|
| 134 |
/* pg_stat_get_total_autoanalyze_time */ |
| 135 |
PG_STAT_GET_RELENTRY_FLOAT8(total_autoanalyze_time) |
| 136 |
|
| 137 |
#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat) \ |
| 138 |
Datum \ |
| 139 |
CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \ |
| 140 |
{ \ |
| 141 |
Oid relid = PG_GETARG_OID(0); \ |
| 142 |
TimestampTz result; \ |
| 143 |
PgStat_StatTabEntry *tabentry; \ |
| 144 |
\ |
| 145 |
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \ |
| 146 |
result = 0; \ |
| 147 |
else \ |
| 148 |
result = tabentry->stat; \ |
| 149 |
\ |
| 150 |
if (result == 0) \ |
| 151 |
PG_RETURN_NULL(); \ |
| 152 |
else \ |
| 153 |
PG_RETURN_TIMESTAMPTZ(result); \ |
| 154 |
} |
| 155 |
|
| 156 |
/* pg_stat_get_last_analyze_time */ |
| 157 |
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_analyze_time) |
| 158 |
|
| 159 |
/* pg_stat_get_last_autoanalyze_time */ |
| 160 |
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_autoanalyze_time) |
| 161 |
|
| 162 |
/* pg_stat_get_last_autovacuum_time */ |
| 163 |
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_autovacuum_time) |
| 164 |
|
| 165 |
/* pg_stat_get_last_vacuum_time */ |
| 166 |
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_vacuum_time) |
| 167 |
|
| 168 |
/* pg_stat_get_lastscan */ |
| 169 |
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(lastscan) |
| 170 |
|
| 171 |
Datum |
| 172 |
pg_stat_get_function_calls(PG_FUNCTION_ARGS) |
| 173 |
{ |
| 174 |
Oid funcid = PG_GETARG_OID(0); |
| 175 |
PgStat_StatFuncEntry *funcentry; |
| 176 |
|
| 177 |
if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL) |
| 178 |
PG_RETURN_NULL(); |
| 179 |
PG_RETURN_INT64(funcentry->numcalls); |
| 180 |
} |
| 181 |
|
| 182 |
/* convert counter from microsec to millisec for display */ |
| 183 |
#define PG_STAT_GET_FUNCENTRY_FLOAT8_MS(stat) \ |
| 184 |
Datum \ |
| 185 |
CppConcat(pg_stat_get_function_,stat)(PG_FUNCTION_ARGS) \ |
| 186 |
{ \ |
| 187 |
Oid funcid = PG_GETARG_OID(0); \ |
| 188 |
double result; \ |
| 189 |
PgStat_StatFuncEntry *funcentry; \ |
| 190 |
\ |
| 191 |
if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL) \ |
| 192 |
PG_RETURN_NULL(); \ |
| 193 |
result = ((double) funcentry->stat) / 1000.0; \ |
| 194 |
PG_RETURN_FLOAT8(result); \ |
| 195 |
} |
| 196 |
|
| 197 |
/* pg_stat_get_function_total_time */ |
| 198 |
PG_STAT_GET_FUNCENTRY_FLOAT8_MS(total_time) |
| 199 |
|
| 200 |
/* pg_stat_get_function_self_time */ |
| 201 |
PG_STAT_GET_FUNCENTRY_FLOAT8_MS(self_time) |
| 202 |
|
| 203 |
Datum |
| 204 |
pg_stat_get_backend_idset(PG_FUNCTION_ARGS) |
| 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 |
|
| 215 |
fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx, |
| 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 */ |
| 240 |
LocalPgBackendStatus *local_beentry = pgstat_get_local_beentry_by_index(fctx[0]); |
| 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 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* Returns command progress information for the named command. |
| 253 |
*/ |
| 254 |
Datum |
| 255 |
pg_stat_get_progress_info(PG_FUNCTION_ARGS) |
| 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) |
| 272 |
cmdtype = PROGRESS_COMMAND_CREATE_INDEX; |
| 273 |
else if (pg_strcasecmp(cmd, "BASEBACKUP") == 0) |
| 274 |
cmdtype = PROGRESS_COMMAND_BASEBACKUP; |
| 275 |
else if (pg_strcasecmp(cmd, "COPY") == 0) |
| 276 |
cmdtype = PROGRESS_COMMAND_COPY; |
| 277 |
else |
| 278 |
ereport(ERROR, |
| 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; |
| 289 |
Datum values[PG_STAT_GET_PROGRESS_COLS] = {0}; |
| 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); |
| 305 |
values[1] = ObjectIdGetDatum(beentry->st_databaseid); |
| 306 |
|
| 307 |
/* show rest of the values including relid only to role members */ |
| 308 |
if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 309 |
{ |
| 310 |
values[2] = ObjectIdGetDatum(beentry->st_progress_command_target); |
| 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 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* Returns activity of PG backends. |
| 329 |
*/ |
| 330 |
Datum |
| 331 |
pg_stat_get_activity(PG_FUNCTION_ARGS) |
| 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 */ |
| 345 |
Datum values[PG_STAT_GET_ACTIVITY_COLS] = {0}; |
| 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) |
| 363 |
values[0] = ObjectIdGetDatum(beentry->st_databaseid); |
| 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) |
| 375 |
values[3] = CStringGetTextDatum(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; |
| 405 |
case STATE_IDLEINTRANSACTION: |
| 406 |
values[4] = CStringGetTextDatum("idle in transaction"); |
| 407 |
break; |
| 408 |
case STATE_FASTPATH: |
| 409 |
values[4] = CStringGetTextDatum("fastpath function call"); |
| 410 |
break; |
| 411 |
case STATE_IDLEINTRANSACTION_ABORTED: |
| 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) |
| 496 |
values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp); |
| 497 |
else |
| 498 |
nulls[8] = true; |
| 499 |
|
| 500 |
if (beentry->st_activity_start_timestamp != 0) |
| 501 |
values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp); |
| 502 |
else |
| 503 |
nulls[9] = true; |
| 504 |
|
| 505 |
if (beentry->st_proc_start_timestamp != 0) |
| 506 |
values[10] = TimestampTzGetDatum(beentry->st_proc_start_timestamp); |
| 507 |
else |
| 508 |
nulls[10] = true; |
| 509 |
|
| 510 |
if (beentry->st_state_start_timestamp != 0) |
| 511 |
values[11] = TimestampTzGetDatum(beentry->st_state_start_timestamp); |
| 512 |
else |
| 513 |
nulls[11] = true; |
| 514 |
|
| 515 |
/* A zeroed client addr means we don't know */ |
| 516 |
if (pg_memory_is_all_zeros(&beentry->st_clientaddr, |
| 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); |
| 542 |
values[12] = DirectFunctionCall1(inet_in, |
| 543 |
CStringGetDatum(remote_host)); |
| 544 |
if (beentry->st_clienthostname && |
| 545 |
beentry->st_clienthostname[0]) |
| 546 |
values[13] = CStringGetTextDatum(beentry->st_clienthostname); |
| 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] = |
| 591 |
CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType)); |
| 592 |
|
| 593 |
/* SSL information */ |
| 594 |
if (beentry->st_ssl) |
| 595 |
{ |
| 596 |
values[18] = BoolGetDatum(true); /* ssl */ |
| 597 |
values[19] = CStringGetTextDatum(beentry->st_sslstatus->ssl_version); |
| 598 |
values[20] = CStringGetTextDatum(beentry->st_sslstatus->ssl_cipher); |
| 599 |
values[21] = Int32GetDatum(beentry->st_sslstatus->ssl_bits); |
| 600 |
|
| 601 |
if (beentry->st_sslstatus->ssl_client_dn[0]) |
| 602 |
values[22] = CStringGetTextDatum(beentry->st_sslstatus->ssl_client_dn); |
| 603 |
else |
| 604 |
nulls[22] = true; |
| 605 |
|
| 606 |
if (beentry->st_sslstatus->ssl_client_serial[0]) |
| 607 |
values[23] = DirectFunctionCall3(numeric_in, |
| 608 |
CStringGetDatum(beentry->st_sslstatus->ssl_client_serial), |
| 609 |
ObjectIdGetDatum(InvalidOid), |
| 610 |
Int32GetDatum(-1)); |
| 611 |
else |
| 612 |
nulls[23] = true; |
| 613 |
|
| 614 |
if (beentry->st_sslstatus->ssl_issuer_dn[0]) |
| 615 |
values[24] = CStringGetTextDatum(beentry->st_sslstatus->ssl_issuer_dn); |
| 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 */ |
| 629 |
values[26] = CStringGetTextDatum(beentry->st_gssstatus->gss_princ); |
| 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 == INT64CONST(0)) |
| 644 |
nulls[30] = true; |
| 645 |
else |
| 646 |
values[30] = Int64GetDatum(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 |
} |
| 687 |
|
| 688 |
|
| 689 |
Datum |
| 690 |
pg_backend_pid(PG_FUNCTION_ARGS) |
| 691 |
{ |
| 692 |
PG_RETURN_INT32(MyProcPid); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
Datum |
| 697 |
pg_stat_get_backend_pid(PG_FUNCTION_ARGS) |
| 698 |
{ |
| 699 |
int32 procNumber = PG_GETARG_INT32(0); |
| 700 |
PgBackendStatus *beentry; |
| 701 |
|
| 702 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 703 |
PG_RETURN_NULL(); |
| 704 |
|
| 705 |
PG_RETURN_INT32(beentry->st_procpid); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
Datum |
| 710 |
pg_stat_get_backend_dbid(PG_FUNCTION_ARGS) |
| 711 |
{ |
| 712 |
int32 procNumber = PG_GETARG_INT32(0); |
| 713 |
PgBackendStatus *beentry; |
| 714 |
|
| 715 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 716 |
PG_RETURN_NULL(); |
| 717 |
|
| 718 |
PG_RETURN_OID(beentry->st_databaseid); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
Datum |
| 723 |
pg_stat_get_backend_userid(PG_FUNCTION_ARGS) |
| 724 |
{ |
| 725 |
int32 procNumber = PG_GETARG_INT32(0); |
| 726 |
PgBackendStatus *beentry; |
| 727 |
|
| 728 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 729 |
PG_RETURN_NULL(); |
| 730 |
|
| 731 |
PG_RETURN_OID(beentry->st_userid); |
| 732 |
} |
| 733 |
|
| 734 |
Datum |
| 735 |
pg_stat_get_backend_subxact(PG_FUNCTION_ARGS) |
| 736 |
{ |
| 737 |
#define PG_STAT_GET_SUBXACT_COLS 2 |
| 738 |
TupleDesc tupdesc; |
| 739 |
Datum values[PG_STAT_GET_SUBXACT_COLS] = {0}; |
| 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 */ |
| 745 |
tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_SUBXACT_COLS); |
| 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); |
| 757 |
values[1] = BoolGetDatum(local_beentry->backend_subxact_overflowed); |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
nulls[0] = true; |
| 762 |
nulls[1] = true; |
| 763 |
} |
| 764 |
|
| 765 |
/* Returns the record as Datum */ |
| 766 |
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 767 |
} |
| 768 |
|
| 769 |
Datum |
| 770 |
pg_stat_get_backend_activity(PG_FUNCTION_ARGS) |
| 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(clipped_activity); |
| 789 |
pfree(clipped_activity); |
| 790 |
|
| 791 |
PG_RETURN_TEXT_P(ret); |
| 792 |
} |
| 793 |
|
| 794 |
Datum |
| 795 |
pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS) |
| 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 |
| 807 |
{ |
| 808 |
proc = BackendPidGetProc(beentry->st_procpid); |
| 809 |
if (!proc) |
| 810 |
proc = AuxiliaryPidGetProc(beentry->st_procpid); |
| 811 |
if (proc) |
| 812 |
wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info); |
| 813 |
} |
| 814 |
|
| 815 |
if (!wait_event_type) |
| 816 |
PG_RETURN_NULL(); |
| 817 |
|
| 818 |
PG_RETURN_TEXT_P(cstring_to_text(wait_event_type)); |
| 819 |
} |
| 820 |
|
| 821 |
Datum |
| 822 |
pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS) |
| 823 |
{ |
| 824 |
int32 procNumber = PG_GETARG_INT32(0); |
| 825 |
PgBackendStatus *beentry; |
| 826 |
PGPROC *proc; |
| 827 |
const char *wait_event = NULL; |
| 828 |
|
| 829 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 830 |
wait_event = "<backend information not available>"; |
| 831 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 832 |
wait_event = "<insufficient privilege>"; |
| 833 |
else |
| 834 |
{ |
| 835 |
proc = BackendPidGetProc(beentry->st_procpid); |
| 836 |
if (!proc) |
| 837 |
proc = AuxiliaryPidGetProc(beentry->st_procpid); |
| 838 |
if (proc) |
| 839 |
wait_event = pgstat_get_wait_event(proc->wait_event_info); |
| 840 |
} |
| 841 |
|
| 842 |
if (!wait_event) |
| 843 |
PG_RETURN_NULL(); |
| 844 |
|
| 845 |
PG_RETURN_TEXT_P(cstring_to_text(wait_event)); |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
Datum |
| 850 |
pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS) |
| 851 |
{ |
| 852 |
int32 procNumber = PG_GETARG_INT32(0); |
| 853 |
TimestampTz result; |
| 854 |
PgBackendStatus *beentry; |
| 855 |
|
| 856 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 857 |
PG_RETURN_NULL(); |
| 858 |
|
| 859 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 860 |
PG_RETURN_NULL(); |
| 861 |
|
| 862 |
result = beentry->st_activity_start_timestamp; |
| 863 |
|
| 864 |
/* |
| 865 |
* No time recorded for start of current query -- this is the case if the |
| 866 |
* user hasn't enabled query-level stats collection. |
| 867 |
*/ |
| 868 |
if (result == 0) |
| 869 |
PG_RETURN_NULL(); |
| 870 |
|
| 871 |
PG_RETURN_TIMESTAMPTZ(result); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
Datum |
| 876 |
pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS) |
| 877 |
{ |
| 878 |
int32 procNumber = PG_GETARG_INT32(0); |
| 879 |
TimestampTz result; |
| 880 |
PgBackendStatus *beentry; |
| 881 |
|
| 882 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 883 |
PG_RETURN_NULL(); |
| 884 |
|
| 885 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 886 |
PG_RETURN_NULL(); |
| 887 |
|
| 888 |
result = beentry->st_xact_start_timestamp; |
| 889 |
|
| 890 |
if (result == 0) /* not in a transaction */ |
| 891 |
PG_RETURN_NULL(); |
| 892 |
|
| 893 |
PG_RETURN_TIMESTAMPTZ(result); |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
Datum |
| 898 |
pg_stat_get_backend_start(PG_FUNCTION_ARGS) |
| 899 |
{ |
| 900 |
int32 procNumber = PG_GETARG_INT32(0); |
| 901 |
TimestampTz result; |
| 902 |
PgBackendStatus *beentry; |
| 903 |
|
| 904 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 905 |
PG_RETURN_NULL(); |
| 906 |
|
| 907 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 908 |
PG_RETURN_NULL(); |
| 909 |
|
| 910 |
result = beentry->st_proc_start_timestamp; |
| 911 |
|
| 912 |
if (result == 0) /* probably can't happen? */ |
| 913 |
PG_RETURN_NULL(); |
| 914 |
|
| 915 |
PG_RETURN_TIMESTAMPTZ(result); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
Datum |
| 920 |
pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS) |
| 921 |
{ |
| 922 |
int32 procNumber = PG_GETARG_INT32(0); |
| 923 |
PgBackendStatus *beentry; |
| 924 |
char remote_host[NI_MAXHOST]; |
| 925 |
int ret; |
| 926 |
|
| 927 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 928 |
PG_RETURN_NULL(); |
| 929 |
|
| 930 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 931 |
PG_RETURN_NULL(); |
| 932 |
|
| 933 |
/* A zeroed client addr means we don't know */ |
| 934 |
if (pg_memory_is_all_zeros(&beentry->st_clientaddr, |
| 935 |
sizeof(beentry->st_clientaddr))) |
| 936 |
PG_RETURN_NULL(); |
| 937 |
|
| 938 |
switch (beentry->st_clientaddr.addr.ss_family) |
| 939 |
{ |
| 940 |
case AF_INET: |
| 941 |
case AF_INET6: |
| 942 |
break; |
| 943 |
default: |
| 944 |
PG_RETURN_NULL(); |
| 945 |
} |
| 946 |
|
| 947 |
remote_host[0] = '\0'; |
| 948 |
ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr, |
| 949 |
beentry->st_clientaddr.salen, |
| 950 |
remote_host, sizeof(remote_host), |
| 951 |
NULL, 0, |
| 952 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 953 |
if (ret != 0) |
| 954 |
PG_RETURN_NULL(); |
| 955 |
|
| 956 |
clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host); |
| 957 |
|
| 958 |
PG_RETURN_DATUM(DirectFunctionCall1(inet_in, |
| 959 |
CStringGetDatum(remote_host))); |
| 960 |
} |
| 961 |
|
| 962 |
Datum |
| 963 |
pg_stat_get_backend_client_port(PG_FUNCTION_ARGS) |
| 964 |
{ |
| 965 |
int32 procNumber = PG_GETARG_INT32(0); |
| 966 |
PgBackendStatus *beentry; |
| 967 |
char remote_port[NI_MAXSERV]; |
| 968 |
int ret; |
| 969 |
|
| 970 |
if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL) |
| 971 |
PG_RETURN_NULL(); |
| 972 |
|
| 973 |
else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid)) |
| 974 |
PG_RETURN_NULL(); |
| 975 |
|
| 976 |
/* A zeroed client addr means we don't know */ |
| 977 |
if (pg_memory_is_all_zeros(&beentry->st_clientaddr, |
| 978 |
sizeof(beentry->st_clientaddr))) |
| 979 |
PG_RETURN_NULL(); |
| 980 |
|
| 981 |
switch (beentry->st_clientaddr.addr.ss_family) |
| 982 |
{ |
| 983 |
case AF_INET: |
| 984 |
case AF_INET6: |
| 985 |
break; |
| 986 |
case AF_UNIX: |
| 987 |
PG_RETURN_INT32(-1); |
| 988 |
default: |
| 989 |
PG_RETURN_NULL(); |
| 990 |
} |
| 991 |
|
| 992 |
remote_port[0] = '\0'; |
| 993 |
ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr, |
| 994 |
beentry->st_clientaddr.salen, |
| 995 |
NULL, 0, |
| 996 |
remote_port, sizeof(remote_port), |
| 997 |
NI_NUMERICHOST | NI_NUMERICSERV); |
| 998 |
if (ret != 0) |
| 999 |
PG_RETURN_NULL(); |
| 1000 |
|
| 1001 |
PG_RETURN_DATUM(DirectFunctionCall1(int4in, |
| 1002 |
CStringGetDatum(remote_port))); |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
Datum |
| 1007 |
pg_stat_get_db_numbackends(PG_FUNCTION_ARGS) |
| 1008 |
{ |
| 1009 |
Oid dbid = PG_GETARG_OID(0); |
| 1010 |
int32 result; |
| 1011 |
int tot_backends = pgstat_fetch_stat_numbackends(); |
| 1012 |
int idx; |
| 1013 |
|
| 1014 |
result = 0; |
| 1015 |
for (idx = 1; idx <= tot_backends; idx++) |
| 1016 |
{ |
| 1017 |
LocalPgBackendStatus *local_beentry = pgstat_get_local_beentry_by_index(idx); |
| 1018 |
|
| 1019 |
if (local_beentry->backendStatus.st_databaseid == dbid) |
| 1020 |
result++; |
| 1021 |
} |
| 1022 |
|
| 1023 |
PG_RETURN_INT32(result); |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
#define PG_STAT_GET_DBENTRY_INT64(stat) \ |
| 1028 |
Datum \ |
| 1029 |
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \ |
| 1030 |
{ \ |
| 1031 |
Oid dbid = PG_GETARG_OID(0); \ |
| 1032 |
int64 result; \ |
| 1033 |
PgStat_StatDBEntry *dbentry; \ |
| 1034 |
\ |
| 1035 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \ |
| 1036 |
result = 0; \ |
| 1037 |
else \ |
| 1038 |
result = (int64) (dbentry->stat); \ |
| 1039 |
\ |
| 1040 |
PG_RETURN_INT64(result); \ |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* pg_stat_get_db_blocks_fetched */ |
| 1044 |
PG_STAT_GET_DBENTRY_INT64(blocks_fetched) |
| 1045 |
|
| 1046 |
/* pg_stat_get_db_blocks_hit */ |
| 1047 |
PG_STAT_GET_DBENTRY_INT64(blocks_hit) |
| 1048 |
|
| 1049 |
/* pg_stat_get_db_conflict_bufferpin */ |
| 1050 |
PG_STAT_GET_DBENTRY_INT64(conflict_bufferpin) |
| 1051 |
|
| 1052 |
/* pg_stat_get_db_conflict_lock */ |
| 1053 |
PG_STAT_GET_DBENTRY_INT64(conflict_lock) |
| 1054 |
|
| 1055 |
/* pg_stat_get_db_conflict_snapshot */ |
| 1056 |
PG_STAT_GET_DBENTRY_INT64(conflict_snapshot) |
| 1057 |
|
| 1058 |
/* pg_stat_get_db_conflict_startup_deadlock */ |
| 1059 |
PG_STAT_GET_DBENTRY_INT64(conflict_startup_deadlock) |
| 1060 |
|
| 1061 |
/* pg_stat_get_db_conflict_tablespace */ |
| 1062 |
PG_STAT_GET_DBENTRY_INT64(conflict_tablespace) |
| 1063 |
|
| 1064 |
/* pg_stat_get_db_deadlocks */ |
| 1065 |
PG_STAT_GET_DBENTRY_INT64(deadlocks) |
| 1066 |
|
| 1067 |
/* pg_stat_get_db_sessions */ |
| 1068 |
PG_STAT_GET_DBENTRY_INT64(sessions) |
| 1069 |
|
| 1070 |
/* pg_stat_get_db_sessions_abandoned */ |
| 1071 |
PG_STAT_GET_DBENTRY_INT64(sessions_abandoned) |
| 1072 |
|
| 1073 |
/* pg_stat_get_db_sessions_fatal */ |
| 1074 |
PG_STAT_GET_DBENTRY_INT64(sessions_fatal) |
| 1075 |
|
| 1076 |
/* pg_stat_get_db_sessions_killed */ |
| 1077 |
PG_STAT_GET_DBENTRY_INT64(sessions_killed) |
| 1078 |
|
| 1079 |
/* pg_stat_get_db_parallel_workers_to_launch */ |
| 1080 |
PG_STAT_GET_DBENTRY_INT64(parallel_workers_to_launch) |
| 1081 |
|
| 1082 |
/* pg_stat_get_db_parallel_workers_launched */ |
| 1083 |
PG_STAT_GET_DBENTRY_INT64(parallel_workers_launched) |
| 1084 |
|
| 1085 |
/* pg_stat_get_db_temp_bytes */ |
| 1086 |
PG_STAT_GET_DBENTRY_INT64(temp_bytes) |
| 1087 |
|
| 1088 |
/* pg_stat_get_db_temp_files */ |
| 1089 |
PG_STAT_GET_DBENTRY_INT64(temp_files) |
| 1090 |
|
| 1091 |
/* pg_stat_get_db_tuples_deleted */ |
| 1092 |
PG_STAT_GET_DBENTRY_INT64(tuples_deleted) |
| 1093 |
|
| 1094 |
/* pg_stat_get_db_tuples_fetched */ |
| 1095 |
PG_STAT_GET_DBENTRY_INT64(tuples_fetched) |
| 1096 |
|
| 1097 |
/* pg_stat_get_db_tuples_inserted */ |
| 1098 |
PG_STAT_GET_DBENTRY_INT64(tuples_inserted) |
| 1099 |
|
| 1100 |
/* pg_stat_get_db_tuples_returned */ |
| 1101 |
PG_STAT_GET_DBENTRY_INT64(tuples_returned) |
| 1102 |
|
| 1103 |
/* pg_stat_get_db_tuples_updated */ |
| 1104 |
PG_STAT_GET_DBENTRY_INT64(tuples_updated) |
| 1105 |
|
| 1106 |
/* pg_stat_get_db_xact_commit */ |
| 1107 |
PG_STAT_GET_DBENTRY_INT64(xact_commit) |
| 1108 |
|
| 1109 |
/* pg_stat_get_db_xact_rollback */ |
| 1110 |
PG_STAT_GET_DBENTRY_INT64(xact_rollback) |
| 1111 |
|
| 1112 |
/* pg_stat_get_db_conflict_logicalslot */ |
| 1113 |
PG_STAT_GET_DBENTRY_INT64(conflict_logicalslot) |
| 1114 |
|
| 1115 |
Datum |
| 1116 |
pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS) |
| 1117 |
{ |
| 1118 |
Oid dbid = PG_GETARG_OID(0); |
| 1119 |
TimestampTz result; |
| 1120 |
PgStat_StatDBEntry *dbentry; |
| 1121 |
|
| 1122 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) |
| 1123 |
result = 0; |
| 1124 |
else |
| 1125 |
result = dbentry->stat_reset_timestamp; |
| 1126 |
|
| 1127 |
if (result == 0) |
| 1128 |
PG_RETURN_NULL(); |
| 1129 |
else |
| 1130 |
PG_RETURN_TIMESTAMPTZ(result); |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
Datum |
| 1135 |
pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS) |
| 1136 |
{ |
| 1137 |
Oid dbid = PG_GETARG_OID(0); |
| 1138 |
int64 result; |
| 1139 |
PgStat_StatDBEntry *dbentry; |
| 1140 |
|
| 1141 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) |
| 1142 |
result = 0; |
| 1143 |
else |
| 1144 |
result = (int64) (dbentry->conflict_tablespace + |
| 1145 |
dbentry->conflict_lock + |
| 1146 |
dbentry->conflict_snapshot + |
| 1147 |
dbentry->conflict_logicalslot + |
| 1148 |
dbentry->conflict_bufferpin + |
| 1149 |
dbentry->conflict_startup_deadlock); |
| 1150 |
|
| 1151 |
PG_RETURN_INT64(result); |
| 1152 |
} |
| 1153 |
|
| 1154 |
Datum |
| 1155 |
pg_stat_get_db_checksum_failures(PG_FUNCTION_ARGS) |
| 1156 |
{ |
| 1157 |
Oid dbid = PG_GETARG_OID(0); |
| 1158 |
int64 result; |
| 1159 |
PgStat_StatDBEntry *dbentry; |
| 1160 |
|
| 1161 |
if (!DataChecksumsEnabled()) |
| 1162 |
PG_RETURN_NULL(); |
| 1163 |
|
| 1164 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) |
| 1165 |
result = 0; |
| 1166 |
else |
| 1167 |
result = (int64) (dbentry->checksum_failures); |
| 1168 |
|
| 1169 |
PG_RETURN_INT64(result); |
| 1170 |
} |
| 1171 |
|
| 1172 |
Datum |
| 1173 |
pg_stat_get_db_checksum_last_failure(PG_FUNCTION_ARGS) |
| 1174 |
{ |
| 1175 |
Oid dbid = PG_GETARG_OID(0); |
| 1176 |
TimestampTz result; |
| 1177 |
PgStat_StatDBEntry *dbentry; |
| 1178 |
|
| 1179 |
if (!DataChecksumsEnabled()) |
| 1180 |
PG_RETURN_NULL(); |
| 1181 |
|
| 1182 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) |
| 1183 |
result = 0; |
| 1184 |
else |
| 1185 |
result = dbentry->last_checksum_failure; |
| 1186 |
|
| 1187 |
if (result == 0) |
| 1188 |
PG_RETURN_NULL(); |
| 1189 |
else |
| 1190 |
PG_RETURN_TIMESTAMPTZ(result); |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* convert counter from microsec to millisec for display */ |
| 1194 |
#define PG_STAT_GET_DBENTRY_FLOAT8_MS(stat) \ |
| 1195 |
Datum \ |
| 1196 |
CppConcat(pg_stat_get_db_,stat)(PG_FUNCTION_ARGS) \ |
| 1197 |
{ \ |
| 1198 |
Oid dbid = PG_GETARG_OID(0); \ |
| 1199 |
double result; \ |
| 1200 |
PgStat_StatDBEntry *dbentry; \ |
| 1201 |
\ |
| 1202 |
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) \ |
| 1203 |
result = 0; \ |
| 1204 |
else \ |
| 1205 |
result = ((double) dbentry->stat) / 1000.0; \ |
| 1206 |
\ |
| 1207 |
PG_RETURN_FLOAT8(result); \ |
| 1208 |
} |
| 1209 |
|
| 1210 |
/* pg_stat_get_db_active_time */ |
| 1211 |
PG_STAT_GET_DBENTRY_FLOAT8_MS(active_time) |
| 1212 |
|
| 1213 |
/* pg_stat_get_db_blk_read_time */ |
| 1214 |
PG_STAT_GET_DBENTRY_FLOAT8_MS(blk_read_time) |
| 1215 |
|
| 1216 |
/* pg_stat_get_db_blk_write_time */ |
| 1217 |
PG_STAT_GET_DBENTRY_FLOAT8_MS(blk_write_time) |
| 1218 |
|
| 1219 |
/* pg_stat_get_db_idle_in_transaction_time */ |
| 1220 |
PG_STAT_GET_DBENTRY_FLOAT8_MS(idle_in_transaction_time) |
| 1221 |
|
| 1222 |
/* pg_stat_get_db_session_time */ |
| 1223 |
PG_STAT_GET_DBENTRY_FLOAT8_MS(session_time) |
| 1224 |
|
| 1225 |
Datum |
| 1226 |
pg_stat_get_checkpointer_num_timed(PG_FUNCTION_ARGS) |
| 1227 |
{ |
| 1228 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->num_timed); |
| 1229 |
} |
| 1230 |
|
| 1231 |
Datum |
| 1232 |
pg_stat_get_checkpointer_num_requested(PG_FUNCTION_ARGS) |
| 1233 |
{ |
| 1234 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->num_requested); |
| 1235 |
} |
| 1236 |
|
| 1237 |
Datum |
| 1238 |
pg_stat_get_checkpointer_num_performed(PG_FUNCTION_ARGS) |
| 1239 |
{ |
| 1240 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->num_performed); |
| 1241 |
} |
| 1242 |
|
| 1243 |
Datum |
| 1244 |
pg_stat_get_checkpointer_restartpoints_timed(PG_FUNCTION_ARGS) |
| 1245 |
{ |
| 1246 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_timed); |
| 1247 |
} |
| 1248 |
|
| 1249 |
Datum |
| 1250 |
pg_stat_get_checkpointer_restartpoints_requested(PG_FUNCTION_ARGS) |
| 1251 |
{ |
| 1252 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_requested); |
| 1253 |
} |
| 1254 |
|
| 1255 |
Datum |
| 1256 |
pg_stat_get_checkpointer_restartpoints_performed(PG_FUNCTION_ARGS) |
| 1257 |
{ |
| 1258 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->restartpoints_performed); |
| 1259 |
} |
| 1260 |
|
| 1261 |
Datum |
| 1262 |
pg_stat_get_checkpointer_buffers_written(PG_FUNCTION_ARGS) |
| 1263 |
{ |
| 1264 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->buffers_written); |
| 1265 |
} |
| 1266 |
|
| 1267 |
Datum |
| 1268 |
pg_stat_get_checkpointer_slru_written(PG_FUNCTION_ARGS) |
| 1269 |
{ |
| 1270 |
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->slru_written); |
| 1271 |
} |
| 1272 |
|
| 1273 |
Datum |
| 1274 |
pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS) |
| 1275 |
{ |
| 1276 |
PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_written_clean); |
| 1277 |
} |
| 1278 |
|
| 1279 |
Datum |
| 1280 |
pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS) |
| 1281 |
{ |
| 1282 |
PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->maxwritten_clean); |
| 1283 |
} |
| 1284 |
|
| 1285 |
Datum |
| 1286 |
pg_stat_get_checkpointer_write_time(PG_FUNCTION_ARGS) |
| 1287 |
{ |
| 1288 |
/* time is already in msec, just convert to double for presentation */ |
| 1289 |
PG_RETURN_FLOAT8((double) |
| 1290 |
pgstat_fetch_stat_checkpointer()->write_time); |
| 1291 |
} |
| 1292 |
|
| 1293 |
Datum |
| 1294 |
pg_stat_get_checkpointer_sync_time(PG_FUNCTION_ARGS) |
| 1295 |
{ |
| 1296 |
/* time is already in msec, just convert to double for presentation */ |
| 1297 |
PG_RETURN_FLOAT8((double) |
| 1298 |
pgstat_fetch_stat_checkpointer()->sync_time); |
| 1299 |
} |
| 1300 |
|
| 1301 |
Datum |
| 1302 |
pg_stat_get_checkpointer_stat_reset_time(PG_FUNCTION_ARGS) |
| 1303 |
{ |
| 1304 |
PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_checkpointer()->stat_reset_timestamp); |
| 1305 |
} |
| 1306 |
|
| 1307 |
Datum |
| 1308 |
pg_stat_get_bgwriter_stat_reset_time(PG_FUNCTION_ARGS) |
| 1309 |
{ |
| 1310 |
PG_RETURN_TIMESTAMPTZ(pgstat_fetch_stat_bgwriter()->stat_reset_timestamp); |
| 1311 |
} |
| 1312 |
|
| 1313 |
Datum |
| 1314 |
pg_stat_get_buf_alloc(PG_FUNCTION_ARGS) |
| 1315 |
{ |
| 1316 |
PG_RETURN_INT64(pgstat_fetch_stat_bgwriter()->buf_alloc); |
| 1317 |
} |
| 1318 |
|
| 1319 |
/* |
| 1320 |
* When adding a new column to the pg_stat_io view and the |
| 1321 |
* pg_stat_get_backend_io() function, add a new enum value here above |
| 1322 |
* IO_NUM_COLUMNS. |
| 1323 |
*/ |
| 1324 |
typedef enum io_stat_col |
| 1325 |
{ |
| 1326 |
IO_COL_INVALID = -1, |
| 1327 |
IO_COL_BACKEND_TYPE, |
| 1328 |
IO_COL_OBJECT, |
| 1329 |
IO_COL_CONTEXT, |
| 1330 |
IO_COL_READS, |
| 1331 |
IO_COL_READ_BYTES, |
| 1332 |
IO_COL_READ_TIME, |
| 1333 |
IO_COL_WRITES, |
| 1334 |
IO_COL_WRITE_BYTES, |
| 1335 |
IO_COL_WRITE_TIME, |
| 1336 |
IO_COL_WRITEBACKS, |
| 1337 |
IO_COL_WRITEBACK_TIME, |
| 1338 |
IO_COL_EXTENDS, |
| 1339 |
IO_COL_EXTEND_BYTES, |
| 1340 |
IO_COL_EXTEND_TIME, |
| 1341 |
IO_COL_HITS, |
| 1342 |
IO_COL_EVICTIONS, |
| 1343 |
IO_COL_REUSES, |
| 1344 |
IO_COL_FSYNCS, |
| 1345 |
IO_COL_FSYNC_TIME, |
| 1346 |
IO_COL_RESET_TIME, |
| 1347 |
IO_NUM_COLUMNS, |
| 1348 |
} io_stat_col; |
| 1349 |
|
| 1350 |
/* |
| 1351 |
* When adding a new IOOp, add a new io_stat_col and add a case to this |
| 1352 |
* function returning the corresponding io_stat_col. |
| 1353 |
*/ |
| 1354 |
static io_stat_col |
| 1355 |
pgstat_get_io_op_index(IOOp io_op) |
| 1356 |
{ |
| 1357 |
switch (io_op) |
| 1358 |
{ |
| 1359 |
case IOOP_EVICT: |
| 1360 |
return IO_COL_EVICTIONS; |
| 1361 |
case IOOP_EXTEND: |
| 1362 |
return IO_COL_EXTENDS; |
| 1363 |
case IOOP_FSYNC: |
| 1364 |
return IO_COL_FSYNCS; |
| 1365 |
case IOOP_HIT: |
| 1366 |
return IO_COL_HITS; |
| 1367 |
case IOOP_READ: |
| 1368 |
return IO_COL_READS; |
| 1369 |
case IOOP_REUSE: |
| 1370 |
return IO_COL_REUSES; |
| 1371 |
case IOOP_WRITE: |
| 1372 |
return IO_COL_WRITES; |
| 1373 |
case IOOP_WRITEBACK: |
| 1374 |
return IO_COL_WRITEBACKS; |
| 1375 |
} |
| 1376 |
|
| 1377 |
elog(ERROR, "unrecognized IOOp value: %d", io_op); |
| 1378 |
pg_unreachable(); |
| 1379 |
} |
| 1380 |
|
| 1381 |
/* |
| 1382 |
* Get the number of the column containing IO bytes for the specified IOOp. |
| 1383 |
* If an IOOp is not tracked in bytes, IO_COL_INVALID is returned. |
| 1384 |
*/ |
| 1385 |
static io_stat_col |
| 1386 |
pgstat_get_io_byte_index(IOOp io_op) |
| 1387 |
{ |
| 1388 |
switch (io_op) |
| 1389 |
{ |
| 1390 |
case IOOP_EXTEND: |
| 1391 |
return IO_COL_EXTEND_BYTES; |
| 1392 |
case IOOP_READ: |
| 1393 |
return IO_COL_READ_BYTES; |
| 1394 |
case IOOP_WRITE: |
| 1395 |
return IO_COL_WRITE_BYTES; |
| 1396 |
case IOOP_EVICT: |
| 1397 |
case IOOP_FSYNC: |
| 1398 |
case IOOP_HIT: |
| 1399 |
case IOOP_REUSE: |
| 1400 |
case IOOP_WRITEBACK: |
| 1401 |
return IO_COL_INVALID; |
| 1402 |
} |
| 1403 |
|
| 1404 |
elog(ERROR, "unrecognized IOOp value: %d", io_op); |
| 1405 |
pg_unreachable(); |
| 1406 |
} |
| 1407 |
|
| 1408 |
/* |
| 1409 |
* Get the number of the column containing IO times for the specified IOOp. |
| 1410 |
* If an op has no associated time, IO_COL_INVALID is returned. |
| 1411 |
*/ |
| 1412 |
static io_stat_col |
| 1413 |
pgstat_get_io_time_index(IOOp io_op) |
| 1414 |
{ |
| 1415 |
switch (io_op) |
| 1416 |
{ |
| 1417 |
case IOOP_READ: |
| 1418 |
return IO_COL_READ_TIME; |
| 1419 |
case IOOP_WRITE: |
| 1420 |
return IO_COL_WRITE_TIME; |
| 1421 |
case IOOP_WRITEBACK: |
| 1422 |
return IO_COL_WRITEBACK_TIME; |
| 1423 |
case IOOP_EXTEND: |
| 1424 |
return IO_COL_EXTEND_TIME; |
| 1425 |
case IOOP_FSYNC: |
| 1426 |
return IO_COL_FSYNC_TIME; |
| 1427 |
case IOOP_EVICT: |
| 1428 |
case IOOP_HIT: |
| 1429 |
case IOOP_REUSE: |
| 1430 |
return IO_COL_INVALID; |
| 1431 |
} |
| 1432 |
|
| 1433 |
elog(ERROR, "unrecognized IOOp value: %d", io_op); |
| 1434 |
pg_unreachable(); |
| 1435 |
} |
| 1436 |
|
| 1437 |
static inline double |
| 1438 |
pg_stat_us_to_ms(PgStat_Counter val_ms) |
| 1439 |
{ |
| 1440 |
return val_ms * (double) 0.001; |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* |
| 1444 |
* pg_stat_io_build_tuples |
| 1445 |
* |
| 1446 |
* Helper routine for pg_stat_get_io() and pg_stat_get_backend_io() |
| 1447 |
* filling a result tuplestore with one tuple for each object and each |
| 1448 |
* context supported by the caller, based on the contents of bktype_stats. |
| 1449 |
*/ |
| 1450 |
static void |
| 1451 |
pg_stat_io_build_tuples(ReturnSetInfo *rsinfo, |
| 1452 |
PgStat_BktypeIO *bktype_stats, |
| 1453 |
BackendType bktype, |
| 1454 |
TimestampTz stat_reset_timestamp) |
| 1455 |
{ |
| 1456 |
Datum bktype_desc = CStringGetTextDatum(GetBackendTypeDesc(bktype)); |
| 1457 |
|
| 1458 |
for (int io_obj = 0; io_obj < IOOBJECT_NUM_TYPES; io_obj++) |
| 1459 |
{ |
| 1460 |
const char *obj_name = pgstat_get_io_object_name(io_obj); |
| 1461 |
|
| 1462 |
for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++) |
| 1463 |
{ |
| 1464 |
const char *context_name = pgstat_get_io_context_name(io_context); |
| 1465 |
|
| 1466 |
Datum values[IO_NUM_COLUMNS] = {0}; |
| 1467 |
bool nulls[IO_NUM_COLUMNS] = {0}; |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* Some combinations of BackendType, IOObject, and IOContext are |
| 1471 |
* not valid for any type of IOOp. In such cases, omit the entire |
| 1472 |
* row from the view. |
| 1473 |
*/ |
| 1474 |
if (!pgstat_tracks_io_object(bktype, io_obj, io_context)) |
| 1475 |
continue; |
| 1476 |
|
| 1477 |
values[IO_COL_BACKEND_TYPE] = bktype_desc; |
| 1478 |
values[IO_COL_CONTEXT] = CStringGetTextDatum(context_name); |
| 1479 |
values[IO_COL_OBJECT] = CStringGetTextDatum(obj_name); |
| 1480 |
if (stat_reset_timestamp != 0) |
| 1481 |
values[IO_COL_RESET_TIME] = TimestampTzGetDatum(stat_reset_timestamp); |
| 1482 |
else |
| 1483 |
nulls[IO_COL_RESET_TIME] = true; |
| 1484 |
|
| 1485 |
for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++) |
| 1486 |
{ |
| 1487 |
int op_idx = pgstat_get_io_op_index(io_op); |
| 1488 |
int time_idx = pgstat_get_io_time_index(io_op); |
| 1489 |
int byte_idx = pgstat_get_io_byte_index(io_op); |
| 1490 |
|
| 1491 |
/* |
| 1492 |
* Some combinations of BackendType and IOOp, of IOContext and |
| 1493 |
* IOOp, and of IOObject and IOOp are not tracked. Set these |
| 1494 |
* cells in the view NULL. |
| 1495 |
*/ |
| 1496 |
if (pgstat_tracks_io_op(bktype, io_obj, io_context, io_op)) |
| 1497 |
{ |
| 1498 |
PgStat_Counter count = |
| 1499 |
bktype_stats->counts[io_obj][io_context][io_op]; |
| 1500 |
|
| 1501 |
values[op_idx] = Int64GetDatum(count); |
| 1502 |
} |
| 1503 |
else |
| 1504 |
nulls[op_idx] = true; |
| 1505 |
|
| 1506 |
if (!nulls[op_idx]) |
| 1507 |
{ |
| 1508 |
/* not every operation is timed */ |
| 1509 |
if (time_idx != IO_COL_INVALID) |
| 1510 |
{ |
| 1511 |
PgStat_Counter time = |
| 1512 |
bktype_stats->times[io_obj][io_context][io_op]; |
| 1513 |
|
| 1514 |
values[time_idx] = Float8GetDatum(pg_stat_us_to_ms(time)); |
| 1515 |
} |
| 1516 |
|
| 1517 |
/* not every IO is tracked in bytes */ |
| 1518 |
if (byte_idx != IO_COL_INVALID) |
| 1519 |
{ |
| 1520 |
char buf[256]; |
| 1521 |
PgStat_Counter byte = |
| 1522 |
bktype_stats->bytes[io_obj][io_context][io_op]; |
| 1523 |
|
| 1524 |
/* Convert to numeric */ |
| 1525 |
snprintf(buf, sizeof buf, INT64_FORMAT, byte); |
| 1526 |
values[byte_idx] = DirectFunctionCall3(numeric_in, |
| 1527 |
CStringGetDatum(buf), |
| 1528 |
ObjectIdGetDatum(0), |
| 1529 |
Int32GetDatum(-1)); |
| 1530 |
} |
| 1531 |
} |
| 1532 |
else |
| 1533 |
{ |
| 1534 |
if (time_idx != IO_COL_INVALID) |
| 1535 |
nulls[time_idx] = true; |
| 1536 |
if (byte_idx != IO_COL_INVALID) |
| 1537 |
nulls[byte_idx] = true; |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, |
| 1542 |
values, nulls); |
| 1543 |
} |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
Datum |
| 1548 |
pg_stat_get_io(PG_FUNCTION_ARGS) |
| 1549 |
{ |
| 1550 |
ReturnSetInfo *rsinfo; |
| 1551 |
PgStat_IO *backends_io_stats; |
| 1552 |
|
| 1553 |
InitMaterializedSRF(fcinfo, 0); |
| 1554 |
rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 1555 |
|
| 1556 |
backends_io_stats = pgstat_fetch_stat_io(); |
| 1557 |
|
| 1558 |
for (int bktype = 0; bktype < BACKEND_NUM_TYPES; bktype++) |
| 1559 |
{ |
| 1560 |
PgStat_BktypeIO *bktype_stats = &backends_io_stats->stats[bktype]; |
| 1561 |
|
| 1562 |
/* |
| 1563 |
* In Assert builds, we can afford an extra loop through all of the |
| 1564 |
* counters (in pg_stat_io_build_tuples()), checking that only |
| 1565 |
* expected stats are non-zero, since it keeps the non-Assert code |
| 1566 |
* cleaner. |
| 1567 |
*/ |
| 1568 |
Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype)); |
| 1569 |
|
| 1570 |
/* |
| 1571 |
* For those BackendTypes without IO Operation stats, skip |
| 1572 |
* representing them in the view altogether. |
| 1573 |
*/ |
| 1574 |
if (!pgstat_tracks_io_bktype(bktype)) |
| 1575 |
continue; |
| 1576 |
|
| 1577 |
/* save tuples with data from this PgStat_BktypeIO */ |
| 1578 |
pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype, |
| 1579 |
backends_io_stats->stat_reset_timestamp); |
| 1580 |
} |
| 1581 |
|
| 1582 |
return (Datum) 0; |
| 1583 |
} |
| 1584 |
|
| 1585 |
/* |
| 1586 |
* Returns I/O statistics for a backend with given PID. |
| 1587 |
*/ |
| 1588 |
Datum |
| 1589 |
pg_stat_get_backend_io(PG_FUNCTION_ARGS) |
| 1590 |
{ |
| 1591 |
ReturnSetInfo *rsinfo; |
| 1592 |
BackendType bktype; |
| 1593 |
int pid; |
| 1594 |
PgStat_Backend *backend_stats; |
| 1595 |
PgStat_BktypeIO *bktype_stats; |
| 1596 |
|
| 1597 |
InitMaterializedSRF(fcinfo, 0); |
| 1598 |
rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 1599 |
|
| 1600 |
pid = PG_GETARG_INT32(0); |
| 1601 |
backend_stats = pgstat_fetch_stat_backend_by_pid(pid, &bktype); |
| 1602 |
|
| 1603 |
if (!backend_stats) |
| 1604 |
return (Datum) 0; |
| 1605 |
|
| 1606 |
bktype_stats = &backend_stats->io_stats; |
| 1607 |
|
| 1608 |
/* |
| 1609 |
* In Assert builds, we can afford an extra loop through all of the |
| 1610 |
* counters (in pg_stat_io_build_tuples()), checking that only expected |
| 1611 |
* stats are non-zero, since it keeps the non-Assert code cleaner. |
| 1612 |
*/ |
| 1613 |
Assert(pgstat_bktype_io_stats_valid(bktype_stats, bktype)); |
| 1614 |
|
| 1615 |
/* save tuples with data from this PgStat_BktypeIO */ |
| 1616 |
pg_stat_io_build_tuples(rsinfo, bktype_stats, bktype, |
| 1617 |
backend_stats->stat_reset_timestamp); |
| 1618 |
return (Datum) 0; |
| 1619 |
} |
| 1620 |
|
| 1621 |
/* |
| 1622 |
* pg_stat_wal_build_tuple |
| 1623 |
* |
| 1624 |
* Helper routine for pg_stat_get_wal() and pg_stat_get_backend_wal() |
| 1625 |
* returning one tuple based on the contents of wal_counters. |
| 1626 |
*/ |
| 1627 |
static Datum |
| 1628 |
pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters, |
| 1629 |
TimestampTz stat_reset_timestamp) |
| 1630 |
{ |
| 1631 |
#define PG_STAT_WAL_COLS 5 |
| 1632 |
TupleDesc tupdesc; |
| 1633 |
Datum values[PG_STAT_WAL_COLS] = {0}; |
| 1634 |
bool nulls[PG_STAT_WAL_COLS] = {0}; |
| 1635 |
char buf[256]; |
| 1636 |
|
| 1637 |
/* Initialise attributes information in the tuple descriptor */ |
| 1638 |
tupdesc = CreateTemplateTupleDesc(PG_STAT_WAL_COLS); |
| 1639 |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records", |
| 1640 |
INT8OID, -1, 0); |
| 1641 |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi", |
| 1642 |
INT8OID, -1, 0); |
| 1643 |
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes", |
| 1644 |
NUMERICOID, -1, 0); |
| 1645 |
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full", |
| 1646 |
INT8OID, -1, 0); |
| 1647 |
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stats_reset", |
| 1648 |
TIMESTAMPTZOID, -1, 0); |
| 1649 |
|
| 1650 |
BlessTupleDesc(tupdesc); |
| 1651 |
|
| 1652 |
/* Fill values and NULLs */ |
| 1653 |
values[0] = Int64GetDatum(wal_counters.wal_records); |
| 1654 |
values[1] = Int64GetDatum(wal_counters.wal_fpi); |
| 1655 |
|
| 1656 |
/* Convert to numeric. */ |
| 1657 |
snprintf(buf, sizeof buf, UINT64_FORMAT, wal_counters.wal_bytes); |
| 1658 |
values[2] = DirectFunctionCall3(numeric_in, |
| 1659 |
CStringGetDatum(buf), |
| 1660 |
ObjectIdGetDatum(0), |
| 1661 |
Int32GetDatum(-1)); |
| 1662 |
|
| 1663 |
values[3] = Int64GetDatum(wal_counters.wal_buffers_full); |
| 1664 |
|
| 1665 |
if (stat_reset_timestamp != 0) |
| 1666 |
values[4] = TimestampTzGetDatum(stat_reset_timestamp); |
| 1667 |
else |
| 1668 |
nulls[4] = true; |
| 1669 |
|
| 1670 |
/* Returns the record as Datum */ |
| 1671 |
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 1672 |
} |
| 1673 |
|
| 1674 |
/* |
| 1675 |
* Returns WAL statistics for a backend with given PID. |
| 1676 |
*/ |
| 1677 |
Datum |
| 1678 |
pg_stat_get_backend_wal(PG_FUNCTION_ARGS) |
| 1679 |
{ |
| 1680 |
int pid; |
| 1681 |
PgStat_Backend *backend_stats; |
| 1682 |
PgStat_WalCounters bktype_stats; |
| 1683 |
|
| 1684 |
pid = PG_GETARG_INT32(0); |
| 1685 |
backend_stats = pgstat_fetch_stat_backend_by_pid(pid, NULL); |
| 1686 |
|
| 1687 |
if (!backend_stats) |
| 1688 |
PG_RETURN_NULL(); |
| 1689 |
|
| 1690 |
bktype_stats = backend_stats->wal_counters; |
| 1691 |
|
| 1692 |
/* save tuples with data from this PgStat_WalCounters */ |
| 1693 |
return (pg_stat_wal_build_tuple(bktype_stats, backend_stats->stat_reset_timestamp)); |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* |
| 1697 |
* Returns statistics of WAL activity |
| 1698 |
*/ |
| 1699 |
Datum |
| 1700 |
pg_stat_get_wal(PG_FUNCTION_ARGS) |
| 1701 |
{ |
| 1702 |
PgStat_WalStats *wal_stats; |
| 1703 |
|
| 1704 |
/* Get statistics about WAL activity */ |
| 1705 |
wal_stats = pgstat_fetch_stat_wal(); |
| 1706 |
|
| 1707 |
return (pg_stat_wal_build_tuple(wal_stats->wal_counters, |
| 1708 |
wal_stats->stat_reset_timestamp)); |
| 1709 |
} |
| 1710 |
|
| 1711 |
/* |
| 1712 |
* Returns statistics of SLRU caches. |
| 1713 |
*/ |
| 1714 |
Datum |
| 1715 |
pg_stat_get_slru(PG_FUNCTION_ARGS) |
| 1716 |
{ |
| 1717 |
#define PG_STAT_GET_SLRU_COLS 9 |
| 1718 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 1719 |
int i; |
| 1720 |
PgStat_SLRUStats *stats; |
| 1721 |
|
| 1722 |
InitMaterializedSRF(fcinfo, 0); |
| 1723 |
|
| 1724 |
/* request SLRU stats from the cumulative stats system */ |
| 1725 |
stats = pgstat_fetch_slru(); |
| 1726 |
|
| 1727 |
for (i = 0;; i++) |
| 1728 |
{ |
| 1729 |
/* for each row */ |
| 1730 |
Datum values[PG_STAT_GET_SLRU_COLS] = {0}; |
| 1731 |
bool nulls[PG_STAT_GET_SLRU_COLS] = {0}; |
| 1732 |
PgStat_SLRUStats stat; |
| 1733 |
const char *name; |
| 1734 |
|
| 1735 |
name = pgstat_get_slru_name(i); |
| 1736 |
|
| 1737 |
if (!name) |
| 1738 |
break; |
| 1739 |
|
| 1740 |
stat = stats[i]; |
| 1741 |
|
| 1742 |
values[0] = PointerGetDatum(cstring_to_text(name)); |
| 1743 |
values[1] = Int64GetDatum(stat.blocks_zeroed); |
| 1744 |
values[2] = Int64GetDatum(stat.blocks_hit); |
| 1745 |
values[3] = Int64GetDatum(stat.blocks_read); |
| 1746 |
values[4] = Int64GetDatum(stat.blocks_written); |
| 1747 |
values[5] = Int64GetDatum(stat.blocks_exists); |
| 1748 |
values[6] = Int64GetDatum(stat.flush); |
| 1749 |
values[7] = Int64GetDatum(stat.truncate); |
| 1750 |
values[8] = TimestampTzGetDatum(stat.stat_reset_timestamp); |
| 1751 |
|
| 1752 |
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); |
| 1753 |
} |
| 1754 |
|
| 1755 |
return (Datum) 0; |
| 1756 |
} |
| 1757 |
|
| 1758 |
#define PG_STAT_GET_XACT_RELENTRY_INT64(stat) \ |
| 1759 |
Datum \ |
| 1760 |
CppConcat(pg_stat_get_xact_,stat)(PG_FUNCTION_ARGS) \ |
| 1761 |
{ \ |
| 1762 |
Oid relid = PG_GETARG_OID(0); \ |
| 1763 |
int64 result; \ |
| 1764 |
PgStat_TableStatus *tabentry; \ |
| 1765 |
\ |
| 1766 |
if ((tabentry = find_tabstat_entry(relid)) == NULL) \ |
| 1767 |
result = 0; \ |
| 1768 |
else \ |
| 1769 |
result = (int64) (tabentry->counts.stat); \ |
| 1770 |
\ |
| 1771 |
PG_RETURN_INT64(result); \ |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* pg_stat_get_xact_numscans */ |
| 1775 |
PG_STAT_GET_XACT_RELENTRY_INT64(numscans) |
| 1776 |
|
| 1777 |
/* pg_stat_get_xact_tuples_returned */ |
| 1778 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_returned) |
| 1779 |
|
| 1780 |
/* pg_stat_get_xact_tuples_fetched */ |
| 1781 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_fetched) |
| 1782 |
|
| 1783 |
/* pg_stat_get_xact_tuples_hot_updated */ |
| 1784 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_hot_updated) |
| 1785 |
|
| 1786 |
/* pg_stat_get_xact_tuples_newpage_updated */ |
| 1787 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_newpage_updated) |
| 1788 |
|
| 1789 |
/* pg_stat_get_xact_blocks_fetched */ |
| 1790 |
PG_STAT_GET_XACT_RELENTRY_INT64(blocks_fetched) |
| 1791 |
|
| 1792 |
/* pg_stat_get_xact_blocks_hit */ |
| 1793 |
PG_STAT_GET_XACT_RELENTRY_INT64(blocks_hit) |
| 1794 |
|
| 1795 |
/* pg_stat_get_xact_tuples_inserted */ |
| 1796 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_inserted) |
| 1797 |
|
| 1798 |
/* pg_stat_get_xact_tuples_updated */ |
| 1799 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_updated) |
| 1800 |
|
| 1801 |
/* pg_stat_get_xact_tuples_deleted */ |
| 1802 |
PG_STAT_GET_XACT_RELENTRY_INT64(tuples_deleted) |
| 1803 |
|
| 1804 |
Datum |
| 1805 |
pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS) |
| 1806 |
{ |
| 1807 |
Oid funcid = PG_GETARG_OID(0); |
| 1808 |
PgStat_FunctionCounts *funcentry; |
| 1809 |
|
| 1810 |
if ((funcentry = find_funcstat_entry(funcid)) == NULL) |
| 1811 |
PG_RETURN_NULL(); |
| 1812 |
PG_RETURN_INT64(funcentry->numcalls); |
| 1813 |
} |
| 1814 |
|
| 1815 |
#define PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(stat) \ |
| 1816 |
Datum \ |
| 1817 |
CppConcat(pg_stat_get_xact_function_,stat)(PG_FUNCTION_ARGS) \ |
| 1818 |
{ \ |
| 1819 |
Oid funcid = PG_GETARG_OID(0); \ |
| 1820 |
PgStat_FunctionCounts *funcentry; \ |
| 1821 |
\ |
| 1822 |
if ((funcentry = find_funcstat_entry(funcid)) == NULL) \ |
| 1823 |
PG_RETURN_NULL(); \ |
| 1824 |
PG_RETURN_FLOAT8(INSTR_TIME_GET_MILLISEC(funcentry->stat)); \ |
| 1825 |
} |
| 1826 |
|
| 1827 |
/* pg_stat_get_xact_function_total_time */ |
| 1828 |
PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(total_time) |
| 1829 |
|
| 1830 |
/* pg_stat_get_xact_function_self_time */ |
| 1831 |
PG_STAT_GET_XACT_FUNCENTRY_FLOAT8_MS(self_time) |
| 1832 |
|
| 1833 |
/* Get the timestamp of the current statistics snapshot */ |
| 1834 |
Datum |
| 1835 |
pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS) |
| 1836 |
{ |
| 1837 |
bool have_snapshot; |
| 1838 |
TimestampTz ts; |
| 1839 |
|
| 1840 |
ts = pgstat_get_stat_snapshot_timestamp(&have_snapshot); |
| 1841 |
|
| 1842 |
if (!have_snapshot) |
| 1843 |
PG_RETURN_NULL(); |
| 1844 |
|
| 1845 |
PG_RETURN_TIMESTAMPTZ(ts); |
| 1846 |
} |
| 1847 |
|
| 1848 |
/* Discard the active statistics snapshot */ |
| 1849 |
Datum |
| 1850 |
pg_stat_clear_snapshot(PG_FUNCTION_ARGS) |
| 1851 |
{ |
| 1852 |
pgstat_clear_snapshot(); |
| 1853 |
|
| 1854 |
PG_RETURN_VOID(); |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
/* Force statistics to be reported at the next occasion */ |
| 1859 |
Datum |
| 1860 |
pg_stat_force_next_flush(PG_FUNCTION_ARGS) |
| 1861 |
{ |
| 1862 |
pgstat_force_next_flush(); |
| 1863 |
|
| 1864 |
PG_RETURN_VOID(); |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
/* Reset all counters for the current database */ |
| 1869 |
Datum |
| 1870 |
pg_stat_reset(PG_FUNCTION_ARGS) |
| 1871 |
{ |
| 1872 |
pgstat_reset_counters(); |
| 1873 |
|
| 1874 |
PG_RETURN_VOID(); |
| 1875 |
} |
| 1876 |
|
| 1877 |
/* |
| 1878 |
* Reset some shared cluster-wide counters |
| 1879 |
* |
| 1880 |
* When adding a new reset target, ideally the name should match that in |
| 1881 |
* pgstat_kind_builtin_infos, if relevant. |
| 1882 |
*/ |
| 1883 |
Datum |
| 1884 |
pg_stat_reset_shared(PG_FUNCTION_ARGS) |
| 1885 |
{ |
| 1886 |
char *target = NULL; |
| 1887 |
|
| 1888 |
if (PG_ARGISNULL(0)) |
| 1889 |
{ |
| 1890 |
/* Reset all the statistics when nothing is specified */ |
| 1891 |
pgstat_reset_of_kind(PGSTAT_KIND_ARCHIVER); |
| 1892 |
pgstat_reset_of_kind(PGSTAT_KIND_BGWRITER); |
| 1893 |
pgstat_reset_of_kind(PGSTAT_KIND_CHECKPOINTER); |
| 1894 |
pgstat_reset_of_kind(PGSTAT_KIND_IO); |
| 1895 |
XLogPrefetchResetStats(); |
| 1896 |
pgstat_reset_of_kind(PGSTAT_KIND_SLRU); |
| 1897 |
pgstat_reset_of_kind(PGSTAT_KIND_WAL); |
| 1898 |
|
| 1899 |
PG_RETURN_VOID(); |
| 1900 |
} |
| 1901 |
|
| 1902 |
target = text_to_cstring(PG_GETARG_TEXT_PP(0)); |
| 1903 |
|
| 1904 |
if (strcmp(target, "archiver") == 0) |
| 1905 |
pgstat_reset_of_kind(PGSTAT_KIND_ARCHIVER); |
| 1906 |
else if (strcmp(target, "bgwriter") == 0) |
| 1907 |
pgstat_reset_of_kind(PGSTAT_KIND_BGWRITER); |
| 1908 |
else if (strcmp(target, "checkpointer") == 0) |
| 1909 |
pgstat_reset_of_kind(PGSTAT_KIND_CHECKPOINTER); |
| 1910 |
else if (strcmp(target, "io") == 0) |
| 1911 |
pgstat_reset_of_kind(PGSTAT_KIND_IO); |
| 1912 |
else if (strcmp(target, "recovery_prefetch") == 0) |
| 1913 |
XLogPrefetchResetStats(); |
| 1914 |
else if (strcmp(target, "slru") == 0) |
| 1915 |
pgstat_reset_of_kind(PGSTAT_KIND_SLRU); |
| 1916 |
else if (strcmp(target, "wal") == 0) |
| 1917 |
pgstat_reset_of_kind(PGSTAT_KIND_WAL); |
| 1918 |
else |
| 1919 |
ereport(ERROR, |
| 1920 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1921 |
errmsg("unrecognized reset target: \"%s\"", target), |
| 1922 |
errhint("Target must be \"archiver\", \"bgwriter\", \"checkpointer\", \"io\", \"recovery_prefetch\", \"slru\", or \"wal\"."))); |
| 1923 |
|
| 1924 |
PG_RETURN_VOID(); |
| 1925 |
} |
| 1926 |
|
| 1927 |
/* |
| 1928 |
* Reset a statistics for a single object, which may be of current |
| 1929 |
* database or shared across all databases in the cluster. |
| 1930 |
*/ |
| 1931 |
Datum |
| 1932 |
pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS) |
| 1933 |
{ |
| 1934 |
Oid taboid = PG_GETARG_OID(0); |
| 1935 |
Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId); |
| 1936 |
|
| 1937 |
pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid); |
| 1938 |
|
| 1939 |
PG_RETURN_VOID(); |
| 1940 |
} |
| 1941 |
|
| 1942 |
Datum |
| 1943 |
pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS) |
| 1944 |
{ |
| 1945 |
Oid funcoid = PG_GETARG_OID(0); |
| 1946 |
|
| 1947 |
pgstat_reset(PGSTAT_KIND_FUNCTION, MyDatabaseId, funcoid); |
| 1948 |
|
| 1949 |
PG_RETURN_VOID(); |
| 1950 |
} |
| 1951 |
|
| 1952 |
/* |
| 1953 |
* Reset statistics of backend with given PID. |
| 1954 |
*/ |
| 1955 |
Datum |
| 1956 |
pg_stat_reset_backend_stats(PG_FUNCTION_ARGS) |
| 1957 |
{ |
| 1958 |
PGPROC *proc; |
| 1959 |
PgBackendStatus *beentry; |
| 1960 |
ProcNumber procNumber; |
| 1961 |
int backend_pid = PG_GETARG_INT32(0); |
| 1962 |
|
| 1963 |
proc = BackendPidGetProc(backend_pid); |
| 1964 |
|
| 1965 |
/* This could be an auxiliary process */ |
| 1966 |
if (!proc) |
| 1967 |
proc = AuxiliaryPidGetProc(backend_pid); |
| 1968 |
|
| 1969 |
if (!proc) |
| 1970 |
PG_RETURN_VOID(); |
| 1971 |
|
| 1972 |
procNumber = GetNumberFromPGProc(proc); |
| 1973 |
|
| 1974 |
beentry = pgstat_get_beentry_by_proc_number(procNumber); |
| 1975 |
if (!beentry) |
| 1976 |
PG_RETURN_VOID(); |
| 1977 |
|
| 1978 |
/* Check if the backend type tracks statistics */ |
| 1979 |
if (!pgstat_tracks_backend_bktype(beentry->st_backendType)) |
| 1980 |
PG_RETURN_VOID(); |
| 1981 |
|
| 1982 |
pgstat_reset(PGSTAT_KIND_BACKEND, InvalidOid, procNumber); |
| 1983 |
|
| 1984 |
PG_RETURN_VOID(); |
| 1985 |
} |
| 1986 |
|
| 1987 |
/* Reset SLRU counters (a specific one or all of them). */ |
| 1988 |
Datum |
| 1989 |
pg_stat_reset_slru(PG_FUNCTION_ARGS) |
| 1990 |
{ |
| 1991 |
char *target = NULL; |
| 1992 |
|
| 1993 |
if (PG_ARGISNULL(0)) |
| 1994 |
pgstat_reset_of_kind(PGSTAT_KIND_SLRU); |
| 1995 |
else |
| 1996 |
{ |
| 1997 |
target = text_to_cstring(PG_GETARG_TEXT_PP(0)); |
| 1998 |
pgstat_reset_slru(target); |
| 1999 |
} |
| 2000 |
|
| 2001 |
PG_RETURN_VOID(); |
| 2002 |
} |
| 2003 |
|
| 2004 |
/* Reset replication slots stats (a specific one or all of them). */ |
| 2005 |
Datum |
| 2006 |
pg_stat_reset_replication_slot(PG_FUNCTION_ARGS) |
| 2007 |
{ |
| 2008 |
char *target = NULL; |
| 2009 |
|
| 2010 |
if (PG_ARGISNULL(0)) |
| 2011 |
pgstat_reset_of_kind(PGSTAT_KIND_REPLSLOT); |
| 2012 |
else |
| 2013 |
{ |
| 2014 |
target = text_to_cstring(PG_GETARG_TEXT_PP(0)); |
| 2015 |
pgstat_reset_replslot(target); |
| 2016 |
} |
| 2017 |
|
| 2018 |
PG_RETURN_VOID(); |
| 2019 |
} |
| 2020 |
|
| 2021 |
/* Reset subscription stats (a specific one or all of them) */ |
| 2022 |
Datum |
| 2023 |
pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS) |
| 2024 |
{ |
| 2025 |
Oid subid; |
| 2026 |
|
| 2027 |
if (PG_ARGISNULL(0)) |
| 2028 |
{ |
| 2029 |
/* Clear all subscription stats */ |
| 2030 |
pgstat_reset_of_kind(PGSTAT_KIND_SUBSCRIPTION); |
| 2031 |
} |
| 2032 |
else |
| 2033 |
{ |
| 2034 |
subid = PG_GETARG_OID(0); |
| 2035 |
|
| 2036 |
if (!OidIsValid(subid)) |
| 2037 |
ereport(ERROR, |
| 2038 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 2039 |
errmsg("invalid subscription OID %u", subid))); |
| 2040 |
pgstat_reset(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid); |
| 2041 |
} |
| 2042 |
|
| 2043 |
PG_RETURN_VOID(); |
| 2044 |
} |
| 2045 |
|
| 2046 |
Datum |
| 2047 |
pg_stat_get_archiver(PG_FUNCTION_ARGS) |
| 2048 |
{ |
| 2049 |
TupleDesc tupdesc; |
| 2050 |
Datum values[7] = {0}; |
| 2051 |
bool nulls[7] = {0}; |
| 2052 |
PgStat_ArchiverStats *archiver_stats; |
| 2053 |
|
| 2054 |
/* Initialise attributes information in the tuple descriptor */ |
| 2055 |
tupdesc = CreateTemplateTupleDesc(7); |
| 2056 |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "archived_count", |
| 2057 |
INT8OID, -1, 0); |
| 2058 |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "last_archived_wal", |
| 2059 |
TEXTOID, -1, 0); |
| 2060 |
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "last_archived_time", |
| 2061 |
TIMESTAMPTZOID, -1, 0); |
| 2062 |
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "failed_count", |
| 2063 |
INT8OID, -1, 0); |
| 2064 |
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "last_failed_wal", |
| 2065 |
TEXTOID, -1, 0); |
| 2066 |
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "last_failed_time", |
| 2067 |
TIMESTAMPTZOID, -1, 0); |
| 2068 |
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stats_reset", |
| 2069 |
TIMESTAMPTZOID, -1, 0); |
| 2070 |
|
| 2071 |
BlessTupleDesc(tupdesc); |
| 2072 |
|
| 2073 |
/* Get statistics about the archiver process */ |
| 2074 |
archiver_stats = pgstat_fetch_stat_archiver(); |
| 2075 |
|
| 2076 |
/* Fill values and NULLs */ |
| 2077 |
values[0] = Int64GetDatum(archiver_stats->archived_count); |
| 2078 |
if (*(archiver_stats->last_archived_wal) == '\0') |
| 2079 |
nulls[1] = true; |
| 2080 |
else |
| 2081 |
values[1] = CStringGetTextDatum(archiver_stats->last_archived_wal); |
| 2082 |
|
| 2083 |
if (archiver_stats->last_archived_timestamp == 0) |
| 2084 |
nulls[2] = true; |
| 2085 |
else |
| 2086 |
values[2] = TimestampTzGetDatum(archiver_stats->last_archived_timestamp); |
| 2087 |
|
| 2088 |
values[3] = Int64GetDatum(archiver_stats->failed_count); |
| 2089 |
if (*(archiver_stats->last_failed_wal) == '\0') |
| 2090 |
nulls[4] = true; |
| 2091 |
else |
| 2092 |
values[4] = CStringGetTextDatum(archiver_stats->last_failed_wal); |
| 2093 |
|
| 2094 |
if (archiver_stats->last_failed_timestamp == 0) |
| 2095 |
nulls[5] = true; |
| 2096 |
else |
| 2097 |
values[5] = TimestampTzGetDatum(archiver_stats->last_failed_timestamp); |
| 2098 |
|
| 2099 |
if (archiver_stats->stat_reset_timestamp == 0) |
| 2100 |
nulls[6] = true; |
| 2101 |
else |
| 2102 |
values[6] = TimestampTzGetDatum(archiver_stats->stat_reset_timestamp); |
| 2103 |
|
| 2104 |
/* Returns the record as Datum */ |
| 2105 |
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 2106 |
} |
| 2107 |
|
| 2108 |
/* |
| 2109 |
* Get the statistics for the replication slot. If the slot statistics is not |
| 2110 |
* available, return all-zeroes stats. |
| 2111 |
*/ |
| 2112 |
Datum |
| 2113 |
pg_stat_get_replication_slot(PG_FUNCTION_ARGS) |
| 2114 |
{ |
| 2115 |
#define PG_STAT_GET_REPLICATION_SLOT_COLS 10 |
| 2116 |
text *slotname_text = PG_GETARG_TEXT_P(0); |
| 2117 |
NameData slotname; |
| 2118 |
TupleDesc tupdesc; |
| 2119 |
Datum values[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0}; |
| 2120 |
bool nulls[PG_STAT_GET_REPLICATION_SLOT_COLS] = {0}; |
| 2121 |
PgStat_StatReplSlotEntry *slotent; |
| 2122 |
PgStat_StatReplSlotEntry allzero; |
| 2123 |
|
| 2124 |
/* Initialise attributes information in the tuple descriptor */ |
| 2125 |
tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_REPLICATION_SLOT_COLS); |
| 2126 |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "slot_name", |
| 2127 |
TEXTOID, -1, 0); |
| 2128 |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "spill_txns", |
| 2129 |
INT8OID, -1, 0); |
| 2130 |
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "spill_count", |
| 2131 |
INT8OID, -1, 0); |
| 2132 |
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "spill_bytes", |
| 2133 |
INT8OID, -1, 0); |
| 2134 |
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stream_txns", |
| 2135 |
INT8OID, -1, 0); |
| 2136 |
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "stream_count", |
| 2137 |
INT8OID, -1, 0); |
| 2138 |
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "stream_bytes", |
| 2139 |
INT8OID, -1, 0); |
| 2140 |
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "total_txns", |
| 2141 |
INT8OID, -1, 0); |
| 2142 |
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "total_bytes", |
| 2143 |
INT8OID, -1, 0); |
| 2144 |
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset", |
| 2145 |
TIMESTAMPTZOID, -1, 0); |
| 2146 |
BlessTupleDesc(tupdesc); |
| 2147 |
|
| 2148 |
namestrcpy(&slotname, text_to_cstring(slotname_text)); |
| 2149 |
slotent = pgstat_fetch_replslot(slotname); |
| 2150 |
if (!slotent) |
| 2151 |
{ |
| 2152 |
/* |
| 2153 |
* If the slot is not found, initialise its stats. This is possible if |
| 2154 |
* the create slot message is lost. |
| 2155 |
*/ |
| 2156 |
memset(&allzero, 0, sizeof(PgStat_StatReplSlotEntry)); |
| 2157 |
slotent = &allzero; |
| 2158 |
} |
| 2159 |
|
| 2160 |
values[0] = CStringGetTextDatum(NameStr(slotname)); |
| 2161 |
values[1] = Int64GetDatum(slotent->spill_txns); |
| 2162 |
values[2] = Int64GetDatum(slotent->spill_count); |
| 2163 |
values[3] = Int64GetDatum(slotent->spill_bytes); |
| 2164 |
values[4] = Int64GetDatum(slotent->stream_txns); |
| 2165 |
values[5] = Int64GetDatum(slotent->stream_count); |
| 2166 |
values[6] = Int64GetDatum(slotent->stream_bytes); |
| 2167 |
values[7] = Int64GetDatum(slotent->total_txns); |
| 2168 |
values[8] = Int64GetDatum(slotent->total_bytes); |
| 2169 |
|
| 2170 |
if (slotent->stat_reset_timestamp == 0) |
| 2171 |
nulls[9] = true; |
| 2172 |
else |
| 2173 |
values[9] = TimestampTzGetDatum(slotent->stat_reset_timestamp); |
| 2174 |
|
| 2175 |
/* Returns the record as Datum */ |
| 2176 |
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 2177 |
} |
| 2178 |
|
| 2179 |
/* |
| 2180 |
* Get the subscription statistics for the given subscription. If the |
| 2181 |
* subscription statistics is not available, return all-zeros stats. |
| 2182 |
*/ |
| 2183 |
Datum |
| 2184 |
pg_stat_get_subscription_stats(PG_FUNCTION_ARGS) |
| 2185 |
{ |
| 2186 |
#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 11 |
| 2187 |
Oid subid = PG_GETARG_OID(0); |
| 2188 |
TupleDesc tupdesc; |
| 2189 |
Datum values[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0}; |
| 2190 |
bool nulls[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0}; |
| 2191 |
PgStat_StatSubEntry *subentry; |
| 2192 |
PgStat_StatSubEntry allzero; |
| 2193 |
int i = 0; |
| 2194 |
|
| 2195 |
/* Get subscription stats */ |
| 2196 |
subentry = pgstat_fetch_stat_subscription(subid); |
| 2197 |
|
| 2198 |
/* Initialise attributes information in the tuple descriptor */ |
| 2199 |
tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_SUBSCRIPTION_STATS_COLS); |
| 2200 |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "subid", |
| 2201 |
OIDOID, -1, 0); |
| 2202 |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "apply_error_count", |
| 2203 |
INT8OID, -1, 0); |
| 2204 |
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "sync_error_count", |
| 2205 |
INT8OID, -1, 0); |
| 2206 |
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "confl_insert_exists", |
| 2207 |
INT8OID, -1, 0); |
| 2208 |
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "confl_update_origin_differs", |
| 2209 |
INT8OID, -1, 0); |
| 2210 |
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "confl_update_exists", |
| 2211 |
INT8OID, -1, 0); |
| 2212 |
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "confl_update_missing", |
| 2213 |
INT8OID, -1, 0); |
| 2214 |
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "confl_delete_origin_differs", |
| 2215 |
INT8OID, -1, 0); |
| 2216 |
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "confl_delete_missing", |
| 2217 |
INT8OID, -1, 0); |
| 2218 |
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "confl_multiple_unique_conflicts", |
| 2219 |
INT8OID, -1, 0); |
| 2220 |
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "stats_reset", |
| 2221 |
TIMESTAMPTZOID, -1, 0); |
| 2222 |
BlessTupleDesc(tupdesc); |
| 2223 |
|
| 2224 |
if (!subentry) |
| 2225 |
{ |
| 2226 |
/* If the subscription is not found, initialise its stats */ |
| 2227 |
memset(&allzero, 0, sizeof(PgStat_StatSubEntry)); |
| 2228 |
subentry = &allzero; |
| 2229 |
} |
| 2230 |
|
| 2231 |
/* subid */ |
| 2232 |
values[i++] = ObjectIdGetDatum(subid); |
| 2233 |
|
| 2234 |
/* apply_error_count */ |
| 2235 |
values[i++] = Int64GetDatum(subentry->apply_error_count); |
| 2236 |
|
| 2237 |
/* sync_error_count */ |
| 2238 |
values[i++] = Int64GetDatum(subentry->sync_error_count); |
| 2239 |
|
| 2240 |
/* conflict count */ |
| 2241 |
for (int nconflict = 0; nconflict < CONFLICT_NUM_TYPES; nconflict++) |
| 2242 |
values[i++] = Int64GetDatum(subentry->conflict_count[nconflict]); |
| 2243 |
|
| 2244 |
/* stats_reset */ |
| 2245 |
if (subentry->stat_reset_timestamp == 0) |
| 2246 |
nulls[i] = true; |
| 2247 |
else |
| 2248 |
values[i] = TimestampTzGetDatum(subentry->stat_reset_timestamp); |
| 2249 |
|
| 2250 |
Assert(i + 1 == PG_STAT_GET_SUBSCRIPTION_STATS_COLS); |
| 2251 |
|
| 2252 |
/* Returns the record as Datum */ |
| 2253 |
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 2254 |
} |
| 2255 |
|
| 2256 |
/* |
| 2257 |
* Checks for presence of stats for object with provided kind, database oid, |
| 2258 |
* object oid. |
| 2259 |
* |
| 2260 |
* This is useful for tests, but not really anything else. Therefore not |
| 2261 |
* documented. |
| 2262 |
*/ |
| 2263 |
Datum |
| 2264 |
pg_stat_have_stats(PG_FUNCTION_ARGS) |
| 2265 |
{ |
| 2266 |
char *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0)); |
| 2267 |
Oid dboid = PG_GETARG_OID(1); |
| 2268 |
uint64 objid = PG_GETARG_INT64(2); |
| 2269 |
PgStat_Kind kind = pgstat_get_kind_from_str(stats_type); |
| 2270 |
|
| 2271 |
PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objid)); |
| 2272 |
} |