PostgreSQL Source Code  git master
mcxtfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * mcxtfuncs.c
4  * Functions to show backend memory context.
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/mcxtfuncs.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "funcapi.h"
19 #include "miscadmin.h"
20 #include "mb/pg_wchar.h"
21 #include "utils/builtins.h"
22 
23 /* ----------
24  * The max bytes for showing identifiers of MemoryContext.
25  * ----------
26  */
27 #define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE 1024
28 
29 /*
30  * PutMemoryContextsStatsTupleStore
31  * One recursion level for pg_get_backend_memory_contexts.
32  */
33 static void
35  TupleDesc tupdesc, MemoryContext context,
36  const char *parent, int level)
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 }
112 
113 /*
114  * pg_get_backend_memory_contexts
115  * SQL SRF showing backend memory context.
116  */
117 Datum
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 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
const MemoryContextMethods * methods
Definition: memnodes.h:83
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:610
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
#define ERROR
Definition: elog.h:43
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:748
const char * ident
Definition: memnodes.h:89
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:144
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
#define MemoryContextIsValid(context)
Definition: memnodes.h:102
Datum pg_get_backend_memory_contexts(PG_FUNCTION_ARGS)
Definition: mcxtfuncs.c:118
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
const char * name
Definition: encode.c:561
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
#define Int32GetDatum(X)
Definition: postgres.h:479
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
#define CStringGetTextDatum(s)
Definition: builtins.h:86
#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE
Definition: mcxtfuncs.c:27
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
const char * name
Definition: memnodes.h:88
MemoryContext nextchild
Definition: memnodes.h:87
#define stat
Definition: win32_port.h:275