PostgreSQL Source Code  git master
mcxtfuncs.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "mb/pg_wchar.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)
 

Macro Definition Documentation

◆ MEMORY_CONTEXT_IDENT_DISPLAY_SIZE

#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE   1024

Definition at line 27 of file mcxtfuncs.c.

Referenced by PutMemoryContextsStatsTupleStore().

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

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

119 {
120  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
121  TupleDesc tupdesc;
122  Tuplestorestate *tupstore;
123  MemoryContext per_query_ctx;
124  MemoryContext oldcontext;
125 
126  /* check to see if caller supports us returning a tuplestore */
127  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
128  ereport(ERROR,
129  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
130  errmsg("set-valued function called in context that cannot accept a set")));
131  if (!(rsinfo->allowedModes & SFRM_Materialize))
132  ereport(ERROR,
133  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
134  errmsg("materialize mode required, but it is not allowed in this context")));
135 
136  /* Build a tuple descriptor for our result type */
137  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
138  elog(ERROR, "return type must be a row type");
139 
140  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
141  oldcontext = MemoryContextSwitchTo(per_query_ctx);
142 
143  tupstore = tuplestore_begin_heap(true, false, work_mem);
144  rsinfo->returnMode = SFRM_Materialize;
145  rsinfo->setResult = tupstore;
146  rsinfo->setDesc = tupdesc;
147 
148  MemoryContextSwitchTo(oldcontext);
149 
150  PutMemoryContextsStatsTupleStore(tupstore, tupdesc,
151  TopMemoryContext, NULL, 0);
152 
153  /* clean up and return the tuplestore */
154  tuplestore_donestoring(tupstore);
155 
156  return (Datum) 0;
157 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:691
static void PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, const char *parent, int level)
Definition: mcxtfuncs.c:34
#define ERROR
Definition: elog.h:43
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
Tuplestorestate * setResult
Definition: execnodes.h:309
ExprContext * econtext
Definition: execnodes.h:302
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228

◆ PutMemoryContextsStatsTupleStore()

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

Definition at line 34 of file mcxtfuncs.c.

References AssertArg, CStringGetTextDatum, MemoryContextData::firstchild, MemoryContextCounters::freechunks, MemoryContextCounters::freespace, MemoryContextData::ident, Int32GetDatum, Int64GetDatum(), MEMORY_CONTEXT_IDENT_DISPLAY_SIZE, MemoryContextIsValid, MemoryContextData::methods, MemoryContextData::name, name, MemoryContextCounters::nblocks, MemoryContextData::nextchild, PG_GET_BACKEND_MEMORY_CONTEXTS_COLS, pg_mbcliplen(), stat, MemoryContextMethods::stats, MemoryContextCounters::totalspace, tuplestore_putvalues(), and values.

Referenced by pg_get_backend_memory_contexts().

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