Skip to content

Commit 3e2cfa4

Browse files
committed
Clean up data conversion short-lived memory context.
dblink uses a short-lived data conversion memory context. However it was not deleted when no longer needed, leading to a noticeable memory leak under some circumstances. Plug the hole, along with minor refactoring. Backpatch to 9.2 where the leak was introduced. Report and initial patch by MauMau. Reviewed/modified slightly by Tom Lane and me.
1 parent b568d38 commit 3e2cfa4

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

contrib/dblink/dblink.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,13 @@ materializeQueryResult(FunctionCallInfo fcinfo,
969969

970970
PG_TRY();
971971
{
972+
/* Create short-lived memory context for data conversions */
973+
sinfo.tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
974+
"dblink temporary context",
975+
ALLOCSET_DEFAULT_MINSIZE,
976+
ALLOCSET_DEFAULT_INITSIZE,
977+
ALLOCSET_DEFAULT_MAXSIZE);
978+
972979
/* execute query, collecting any tuples into the tuplestore */
973980
res = storeQueryResult(&sinfo, conn, sql);
974981

@@ -1033,6 +1040,12 @@ materializeQueryResult(FunctionCallInfo fcinfo,
10331040
PQclear(res);
10341041
res = NULL;
10351042
}
1043+
1044+
/* clean up data conversion short-lived memory context */
1045+
if (sinfo.tmpcontext != NULL)
1046+
MemoryContextDelete(sinfo.tmpcontext);
1047+
sinfo.tmpcontext = NULL;
1048+
10361049
PQclear(sinfo.last_res);
10371050
sinfo.last_res = NULL;
10381051
PQclear(sinfo.cur_res);
@@ -1196,15 +1209,6 @@ storeRow(storeInfo *sinfo, PGresult *res, bool first)
11961209
if (sinfo->cstrs)
11971210
pfree(sinfo->cstrs);
11981211
sinfo->cstrs = (char **) palloc(nfields * sizeof(char *));
1199-
1200-
/* Create short-lived memory context for data conversions */
1201-
if (!sinfo->tmpcontext)
1202-
sinfo->tmpcontext =
1203-
AllocSetContextCreate(CurrentMemoryContext,
1204-
"dblink temporary context",
1205-
ALLOCSET_DEFAULT_MINSIZE,
1206-
ALLOCSET_DEFAULT_INITSIZE,
1207-
ALLOCSET_DEFAULT_MAXSIZE);
12081212
}
12091213

12101214
/* Should have a single-row result if we get here */

0 commit comments

Comments
 (0)