Skip to content

Commit 1dde578

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 ecac0e2 commit 1dde578

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
@@ -977,6 +977,13 @@ materializeQueryResult(FunctionCallInfo fcinfo,
977977

978978
PG_TRY();
979979
{
980+
/* Create short-lived memory context for data conversions */
981+
sinfo.tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
982+
"dblink temporary context",
983+
ALLOCSET_DEFAULT_MINSIZE,
984+
ALLOCSET_DEFAULT_INITSIZE,
985+
ALLOCSET_DEFAULT_MAXSIZE);
986+
980987
/* execute query, collecting any tuples into the tuplestore */
981988
res = storeQueryResult(&sinfo, conn, sql);
982989

@@ -1041,6 +1048,12 @@ materializeQueryResult(FunctionCallInfo fcinfo,
10411048
PQclear(res);
10421049
res = NULL;
10431050
}
1051+
1052+
/* clean up data conversion short-lived memory context */
1053+
if (sinfo.tmpcontext != NULL)
1054+
MemoryContextDelete(sinfo.tmpcontext);
1055+
sinfo.tmpcontext = NULL;
1056+
10441057
PQclear(sinfo.last_res);
10451058
sinfo.last_res = NULL;
10461059
PQclear(sinfo.cur_res);
@@ -1204,15 +1217,6 @@ storeRow(storeInfo *sinfo, PGresult *res, bool first)
12041217
if (sinfo->cstrs)
12051218
pfree(sinfo->cstrs);
12061219
sinfo->cstrs = (char **) palloc(nfields * sizeof(char *));
1207-
1208-
/* Create short-lived memory context for data conversions */
1209-
if (!sinfo->tmpcontext)
1210-
sinfo->tmpcontext =
1211-
AllocSetContextCreate(CurrentMemoryContext,
1212-
"dblink temporary context",
1213-
ALLOCSET_DEFAULT_MINSIZE,
1214-
ALLOCSET_DEFAULT_INITSIZE,
1215-
ALLOCSET_DEFAULT_MAXSIZE);
12161220
}
12171221

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

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