Skip to content

Commit b16a8f4

Browse files
committed
pg_stat_get_backend_idset should reset fmgr_info->fn_extra at end of
execution, so that it restarts correctly if query tree is used again.
1 parent f00da6d commit b16a8f4

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,33 +179,29 @@ Datum
179179
pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
180180
{
181181
FmgrInfo *fmgr_info = fcinfo->flinfo;
182-
MemoryContext oldcxt;
183182
int32 result;
184183

185-
if (fcinfo->resultinfo == NULL)
186-
elog(ERROR, "called in context that does not accept a set result (1)");
187-
if (!IsA(fcinfo->resultinfo, ReturnSetInfo))
188-
elog(ERROR, "called in context that does not accept a set result (2)");
184+
if (fcinfo->resultinfo == NULL ||
185+
!IsA(fcinfo->resultinfo, ReturnSetInfo))
186+
elog(ERROR, "pg_stat_get_backend_idset: called in context that does not accept a set result");
189187

190188
if (fmgr_info->fn_extra == NULL)
191189
{
192190
if (fmgr_info->fn_mcxt == NULL)
193191
elog(ERROR, "No function memory context in set-function");
194-
195-
oldcxt = MemoryContextSwitchTo(fmgr_info->fn_mcxt);
196-
fmgr_info->fn_extra = palloc(sizeof(int32) * 2);
197-
((int32 *)(fmgr_info->fn_extra))[0] = 0;
198-
((int32 *)(fmgr_info->fn_extra))[1] =
199-
(int32)pgstat_fetch_stat_numbackends();
200-
MemoryContextSwitchTo(oldcxt);
192+
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
193+
2 * sizeof(int));
194+
((int *)(fmgr_info->fn_extra))[0] = 0;
195+
((int *)(fmgr_info->fn_extra))[1] = pgstat_fetch_stat_numbackends();
201196
}
202197

203-
((int32 *)(fmgr_info->fn_extra))[0] += 1;
204-
result = ((int32 *)(fmgr_info->fn_extra))[0];
205-
198+
((int *)(fmgr_info->fn_extra))[0] += 1;
199+
result = ((int *)(fmgr_info->fn_extra))[0];
206200

207-
if (result > ((int32 *)(fmgr_info->fn_extra))[1])
201+
if (result > ((int *)(fmgr_info->fn_extra))[1])
208202
{
203+
pfree(fmgr_info->fn_extra);
204+
fmgr_info->fn_extra = NULL;
209205
((ReturnSetInfo *)(fcinfo->resultinfo))->isDone = ExprEndResult;
210206
PG_RETURN_NULL();
211207
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy