PostgreSQL Source Code  git master
mcxtfuncs.c File Reference
#include "postgres.h"
#include "funcapi.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 28 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 119 of file mcxtfuncs.c.

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

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 144 of file mcxtfuncs.c.

145 {
146  int pid = PG_GETARG_INT32(0);
147  PGPROC *proc;
148  ProcNumber procNumber = INVALID_PROC_NUMBER;
149 
150  /*
151  * See if the process with given pid is a backend or an auxiliary process.
152  */
153  proc = BackendPidGetProc(pid);
154  if (proc == NULL)
155  proc = AuxiliaryPidGetProc(pid);
156 
157  /*
158  * BackendPidGetProc() and AuxiliaryPidGetProc() return NULL if the pid
159  * isn't valid; but by the time we reach kill(), a process for which we
160  * get a valid proc here might have terminated on its own. There's no way
161  * to acquire a lock on an arbitrary process to prevent that. But since
162  * this mechanism is usually used to debug a backend or an auxiliary
163  * process running and consuming lots of memory, that it might end on its
164  * own first and its memory contexts are not logged is not a problem.
165  */
166  if (proc == NULL)
167  {
168  /*
169  * This is just a warning so a loop-through-resultset will not abort
170  * if one backend terminated on its own during the run.
171  */
173  (errmsg("PID %d is not a PostgreSQL server process", pid)));
174  PG_RETURN_BOOL(false);
175  }
176 
177  procNumber = GetNumberFromPGProc(proc);
178  if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, procNumber) < 0)
179  {
180  /* Again, just a warning to allow loops */
182  (errmsg("could not send signal to process %d: %m", pid)));
183  PG_RETURN_BOOL(false);
184  }
185 
186  PG_RETURN_BOOL(true);
187 }
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#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
#define GetNumberFromPGProc(proc)
Definition: proc.h:429
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3183
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition: procsignal.c:257
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition: procsignal.h:37
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:1018
Definition: proc.h:157

References AuxiliaryPidGetProc(), BackendPidGetProc(), ereport, errmsg(), GetNumberFromPGProc, INVALID_PROC_NUMBER, 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 35 of file mcxtfuncs.c.

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

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

Referenced by pg_get_backend_memory_contexts().