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  TupleDesc tupdesc;
124  Tuplestorestate *tupstore;
125  MemoryContext per_query_ctx;
126  MemoryContext oldcontext;
127 
128  /* check to see if caller supports us returning a tuplestore */
129  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
130  ereport(ERROR,
131  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
132  errmsg("set-valued function called in context that cannot accept a set")));
133  if (!(rsinfo->allowedModes & SFRM_Materialize))
134  ereport(ERROR,
135  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
136  errmsg("materialize mode required, but it is not allowed in this context")));
137 
138  /* Build a tuple descriptor for our result type */
139  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
140  elog(ERROR, "return type must be a row type");
141 
142  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
143  oldcontext = MemoryContextSwitchTo(per_query_ctx);
144 
145  tupstore = tuplestore_begin_heap(true, false, work_mem);
146  rsinfo->returnMode = SFRM_Materialize;
147  rsinfo->setResult = tupstore;
148  rsinfo->setDesc = tupdesc;
149 
150  MemoryContextSwitchTo(oldcontext);
151 
152  PutMemoryContextsStatsTupleStore(tupstore, tupdesc,
153  TopMemoryContext, NULL, 0);
154 
155  /* clean up and return the tuplestore */
156  tuplestore_donestoring(tupstore);
157 
158  return (Datum) 0;
159 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
@ SFRM_Materialize
Definition: execnodes.h:293
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
int work_mem
Definition: globals.c:124
MemoryContext TopMemoryContext
Definition: mcxt.c:48
static void PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, const char *parent, int level)
Definition: mcxtfuncs.c:36
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
uintptr_t Datum
Definition: postgres.h:411
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:238
SetFunctionReturnMode returnMode
Definition: execnodes.h:312
ExprContext * econtext
Definition: execnodes.h:308
TupleDesc setDesc
Definition: execnodes.h:316
Tuplestorestate * setResult
Definition: execnodes.h:315
int allowedModes
Definition: execnodes.h:310
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60

References ReturnSetInfo::allowedModes, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), if(), IsA, MemoryContextSwitchTo(), PutMemoryContextsStatsTupleStore(), ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, TopMemoryContext, tuplestore_begin_heap(), tuplestore_donestoring, TYPEFUNC_COMPOSITE, and work_mem.

◆ pg_log_backend_memory_contexts()

Datum pg_log_backend_memory_contexts ( PG_FUNCTION_ARGS  )

Definition at line 175 of file mcxtfuncs.c.

176 {
177  int pid = PG_GETARG_INT32(0);
178  PGPROC *proc;
179  BackendId backendId = InvalidBackendId;
180 
181  proc = BackendPidGetProc(pid);
182 
183  /*
184  * See if the process with given pid is a backend or an auxiliary process.
185  *
186  * If the given process is a backend, use its backend id in
187  * SendProcSignal() later to speed up the operation. Otherwise, don't do
188  * that because auxiliary processes (except the startup process) don't
189  * have a valid backend id.
190  */
191  if (proc != NULL)
192  backendId = proc->backendId;
193  else
194  proc = AuxiliaryPidGetProc(pid);
195 
196  /*
197  * BackendPidGetProc() and AuxiliaryPidGetProc() return NULL if the pid
198  * isn't valid; but by the time we reach kill(), a process for which we
199  * get a valid proc here might have terminated on its own. There's no way
200  * to acquire a lock on an arbitrary process to prevent that. But since
201  * this mechanism is usually used to debug a backend or an auxiliary
202  * process running and consuming lots of memory, that it might end on its
203  * own first and its memory contexts are not logged is not a problem.
204  */
205  if (proc == NULL)
206  {
207  /*
208  * This is just a warning so a loop-through-resultset will not abort
209  * if one backend terminated on its own during the run.
210  */
212  (errmsg("PID %d is not a PostgreSQL server process", pid)));
213  PG_RETURN_BOOL(false);
214  }
215 
216  if (SendProcSignal(pid, PROCSIG_LOG_MEMORY_CONTEXT, backendId) < 0)
217  {
218  /* Again, just a warning to allow loops */
220  (errmsg("could not send signal to process %d: %m", pid)));
221  PG_RETURN_BOOL(false);
222  }
223 
224  PG_RETURN_BOOL(true);
225 }
int BackendId
Definition: backendid.h:21
#define InvalidBackendId
Definition: backendid.h:23
#define WARNING
Definition: elog.h:30
#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:3146
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:261
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition: procsignal.h:37
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:975
Definition: proc.h:125
BackendId backendId
Definition: proc.h:156

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 
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  */
86  idlen = pg_mbcliplen(ident, idlen, MEMORY_CONTEXT_IDENT_DISPLAY_SIZE - 1);
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:85
#define AssertArg(condition)
Definition: c.h:806
const char * name
Definition: encode.c:561
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1026
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS
#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE
Definition: mcxtfuncs.c:29
#define MemoryContextIsValid(context)
Definition: memnodes.h:104
#define Int32GetDatum(X)
Definition: postgres.h:523
MemoryContext firstchild
Definition: memnodes.h:87
const char * ident
Definition: memnodes.h:91
const MemoryContextMethods * methods
Definition: memnodes.h:85
MemoryContext nextchild
Definition: memnodes.h:89
const char * name
Definition: memnodes.h:90
void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: memnodes.h:68
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define stat
Definition: win32_port.h:283

References AssertArg, CStringGetTextDatum, MemoryContextData::firstchild, MemoryContextData::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().