PostgreSQL Source Code  git master
mcxtfuncs.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "mb/pg_wchar.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
Include dependency graph for mcxtfuncs.c:

Go to the source code of this file.

Macros

#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE   1024
 
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS   9
 

Functions

static void PutMemoryContextsStatsTupleStore (Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, const char *parent, int level)
 
Datum pg_get_backend_memory_contexts (PG_FUNCTION_ARGS)
 
Datum pg_log_backend_memory_contexts (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MEMORY_CONTEXT_IDENT_DISPLAY_SIZE

#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE   1024

Definition at line 29 of file mcxtfuncs.c.

◆ PG_GET_BACKEND_MEMORY_CONTEXTS_COLS

#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS   9

Function Documentation

◆ pg_get_backend_memory_contexts()

Datum pg_get_backend_memory_contexts ( PG_FUNCTION_ARGS  )

Definition at line 120 of file mcxtfuncs.c.

121 {
122  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
123 
124  InitMaterializedSRF(fcinfo, 0);
126  TopMemoryContext, NULL, 0);
127 
128  return (Datum) 0;
129 }
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
MemoryContext TopMemoryContext
Definition: mcxt.c:141
static void PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, const char *parent, int level)
Definition: mcxtfuncs.c:36
uintptr_t Datum
Definition: postgres.h:64
TupleDesc setDesc
Definition: execnodes.h:334
Tuplestorestate * setResult
Definition: execnodes.h:333

References InitMaterializedSRF(), PutMemoryContextsStatsTupleStore(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, and TopMemoryContext.

◆ pg_log_backend_memory_contexts()

Datum pg_log_backend_memory_contexts ( PG_FUNCTION_ARGS  )

Definition at line 145 of file mcxtfuncs.c.

146 {
147  int pid = PG_GETARG_INT32(0);
148  PGPROC *proc;
149  BackendId backendId = InvalidBackendId;
150 
151  proc = BackendPidGetProc(pid);
152 
153  /*
154  * See if the process with given pid is a backend or an auxiliary process.
155  *
156  * If the given process is a backend, use its backend id in
157  * SendProcSignal() later to speed up the operation. Otherwise, don't do
158  * that because auxiliary processes (except the startup process) don't
159  * have a valid backend id.
160  */
161  if (proc != NULL)
162  backendId = proc->backendId;
163  else
164  proc = AuxiliaryPidGetProc(pid);
165 
166  /*
167  * BackendPidGetProc() and AuxiliaryPidGetProc() return NULL if the pid
168  * isn't valid; but by the time we reach kill(), a process for which we
169  * get a valid proc here might have terminated on its own. There's no way
170  * to acquire a lock on an arbitrary process to prevent that. But since
171  * this mechanism is usually used to debug a backend or an auxiliary
172  * process running and consuming lots of memory, that it might end on its
173  * own first and its memory contexts are not logged is not a problem.
174  */
175  if (proc == NULL)
176  {
177  /*
178  * This is just a warning so a loop-through-resultset will not abort
179  * if one backend terminated on its own during the run.
180  */
182  (errmsg("PID %d is not a PostgreSQL server process", pid)));
183  PG_RETURN_BOOL(false);
184  }
185 
186  if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, backendId) < 0)
187  {
188  /* Again, just a warning to allow loops */
190  (errmsg("could not send signal to process %d: %m", pid)));
191  PG_RETURN_BOOL(false);
192  }
193 
194  PG_RETURN_BOOL(true);
195 }
int BackendId
Definition: backendid.h:21
#define InvalidBackendId
Definition: backendid.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3103
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:262
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition: procsignal.h:37
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:965
Definition: proc.h:162
BackendId backendId
Definition: proc.h:197

References AuxiliaryPidGetProc(), PGPROC::backendId, BackendPidGetProc(), ereport, errmsg(), InvalidBackendId, PG_GETARG_INT32, PG_RETURN_BOOL, PROCSIG_LOG_MEMORY_CONTEXT, SendProcSignal(), and WARNING.

◆ PutMemoryContextsStatsTupleStore()

static void PutMemoryContextsStatsTupleStore ( Tuplestorestate tupstore,
TupleDesc  tupdesc,
MemoryContext  context,
const char *  parent,
int  level 
)
static

Definition at line 36 of file mcxtfuncs.c.

39 {
40 #define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 9
41 
45  MemoryContext child;
46  const char *name;
47  const char *ident;
48 
49  Assert(MemoryContextIsValid(context));
50 
51  name = context->name;
52  ident = context->ident;
53 
54  /*
55  * To be consistent with logging output, we label dynahash contexts with
56  * just the hash table name as with MemoryContextStatsPrint().
57  */
58  if (ident && strcmp(name, "dynahash") == 0)
59  {
60  name = ident;
61  ident = NULL;
62  }
63 
64  /* Examine the context itself */
65  memset(&stat, 0, sizeof(stat));
66  (*context->methods->stats) (context, NULL, (void *) &level, &stat, true);
67 
68  memset(values, 0, sizeof(values));
69  memset(nulls, 0, sizeof(nulls));
70 
71  if (name)
73  else
74  nulls[0] = true;
75 
76  if (ident)
77  {
78  int idlen = strlen(ident);
79  char clipped_ident[MEMORY_CONTEXT_IDENT_DISPLAY_SIZE];
80 
81  /*
82  * Some identifiers such as SQL query string can be very long,
83  * truncate oversize identifiers.
84  */
87 
88  memcpy(clipped_ident, ident, idlen);
89  clipped_ident[idlen] = '\0';
90  values[1] = CStringGetTextDatum(clipped_ident);
91  }
92  else
93  nulls[1] = true;
94 
95  if (parent)
96  values[2] = CStringGetTextDatum(parent);
97  else
98  nulls[2] = true;
99 
100  values[3] = Int32GetDatum(level);
101  values[4] = Int64GetDatum(stat.totalspace);
102  values[5] = Int64GetDatum(stat.nblocks);
103  values[6] = Int64GetDatum(stat.freespace);
104  values[7] = Int64GetDatum(stat.freechunks);
105  values[8] = Int64GetDatum(stat.totalspace - stat.freespace);
106  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
107 
108  for (child = context->firstchild; child != NULL; child = child->nextchild)
109  {
110  PutMemoryContextsStatsTupleStore(tupstore, tupdesc,
111  child, name, level + 1);
112  }
113 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1790
#define ident
Definition: indent_codes.h:47
Assert(fmt[strlen(fmt) - 1] !='\n')
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1084
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS
#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE
Definition: mcxtfuncs.c:29
#define MemoryContextIsValid(context)
Definition: memnodes.h:107
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
MemoryContext firstchild
Definition: memnodes.h:90
const char * ident
Definition: memnodes.h:94
const MemoryContextMethods * methods
Definition: memnodes.h:88
MemoryContext nextchild
Definition: memnodes.h:92
const char * name
Definition: memnodes.h:93
void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: memnodes.h:69
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
const char * name
#define stat
Definition: win32_port.h:284

References Assert(), CStringGetTextDatum, MemoryContextData::firstchild, MemoryContextData::ident, ident, Int32GetDatum(), Int64GetDatum(), MEMORY_CONTEXT_IDENT_DISPLAY_SIZE, MemoryContextIsValid, MemoryContextData::methods, name, MemoryContextData::name, MemoryContextData::nextchild, PG_GET_BACKEND_MEMORY_CONTEXTS_COLS, pg_mbcliplen(), stat, MemoryContextMethods::stats, tuplestore_putvalues(), and values.

Referenced by pg_get_backend_memory_contexts().