Skip to content

Commit

Permalink
support pg12+
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed Feb 8, 2024
1 parent ba1d6c4 commit afe8e00
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/pgmeminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,42 @@ pgmeminfo(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}

/*
* This is analogy of MemoryContextMemConsumed function (PostgreSQL 17+)
* But it is reimplemented for support older pg
*/
static void
pgmeminfo_MemoryContextMemConsumed(MemoryContext context,
MemoryContextCounters *consumed)
{
MemoryContextCounters stat;
MemoryContext child;
int level;

memset(&stat, 0, sizeof(stat));

#if PG_VERSION_NUM >= 140000

(*context->methods->stats) (context, NULL, (void *) &level, &stat, false);

#else

(*context->methods->stats) (context, NULL, (void *) &level, &stat);

#endif


for (child = context->firstchild; child != NULL; child = child->nextchild)
{
pgmeminfo_MemoryContextMemConsumed(child, &stat);
}

consumed->nblocks += stat.nblocks;
consumed->freechunks += stat.freechunks;
consumed->totalspace += stat.totalspace;
consumed->freespace += stat.freespace;
}

static void
PutMemoryContextStatsTupleStore(AccumMode acm, int deep,
Tuplestorestate *tupstore, TupleDesc tupdesc,
Expand All @@ -149,9 +185,21 @@ PutMemoryContextStatsTupleStore(AccumMode acm, int deep,

memset(&stat, 0, sizeof(stat));
if (acm == ACCUM_ALL || (acm == ACCUM_LEAF && level == deep))
MemoryContextMemConsumed(context, &stat);
pgmeminfo_MemoryContextMemConsumed(context, &stat);
else
(*context->methods->stats) (context, NULL, (void *) &level, &stat, true);
{

#if PG_VERSION_NUM >= 140000

(*context->methods->stats) (context, NULL, (void *) &level, &stat, false);

#else

(*context->methods->stats) (context, NULL, (void *) &level, &stat);

#endif

}

memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));
Expand Down

0 comments on commit afe8e00

Please # to comment.