Skip to content

Commit afe8e00

Browse files
committed
support pg12+
1 parent ba1d6c4 commit afe8e00

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/pgmeminfo.c

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,42 @@ pgmeminfo(PG_FUNCTION_ARGS)
127127
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
128128
}
129129

130+
/*
131+
* This is analogy of MemoryContextMemConsumed function (PostgreSQL 17+)
132+
* But it is reimplemented for support older pg
133+
*/
134+
static void
135+
pgmeminfo_MemoryContextMemConsumed(MemoryContext context,
136+
MemoryContextCounters *consumed)
137+
{
138+
MemoryContextCounters stat;
139+
MemoryContext child;
140+
int level;
141+
142+
memset(&stat, 0, sizeof(stat));
143+
144+
#if PG_VERSION_NUM >= 140000
145+
146+
(*context->methods->stats) (context, NULL, (void *) &level, &stat, false);
147+
148+
#else
149+
150+
(*context->methods->stats) (context, NULL, (void *) &level, &stat);
151+
152+
#endif
153+
154+
155+
for (child = context->firstchild; child != NULL; child = child->nextchild)
156+
{
157+
pgmeminfo_MemoryContextMemConsumed(child, &stat);
158+
}
159+
160+
consumed->nblocks += stat.nblocks;
161+
consumed->freechunks += stat.freechunks;
162+
consumed->totalspace += stat.totalspace;
163+
consumed->freespace += stat.freespace;
164+
}
165+
130166
static void
131167
PutMemoryContextStatsTupleStore(AccumMode acm, int deep,
132168
Tuplestorestate *tupstore, TupleDesc tupdesc,
@@ -149,9 +185,21 @@ PutMemoryContextStatsTupleStore(AccumMode acm, int deep,
149185

150186
memset(&stat, 0, sizeof(stat));
151187
if (acm == ACCUM_ALL || (acm == ACCUM_LEAF && level == deep))
152-
MemoryContextMemConsumed(context, &stat);
188+
pgmeminfo_MemoryContextMemConsumed(context, &stat);
153189
else
154-
(*context->methods->stats) (context, NULL, (void *) &level, &stat, true);
190+
{
191+
192+
#if PG_VERSION_NUM >= 140000
193+
194+
(*context->methods->stats) (context, NULL, (void *) &level, &stat, false);
195+
196+
#else
197+
198+
(*context->methods->stats) (context, NULL, (void *) &level, &stat);
199+
200+
#endif
201+
202+
}
155203

156204
memset(values, 0, sizeof(values));
157205
memset(nulls, 0, sizeof(nulls));

0 commit comments

Comments
 (0)