aboutsummaryrefslogtreecommitdiff
path: root/contrib/dblink/dblink.c
diff options
context:
space:
mode:
authorJoe Conway <mail@joeconway.com>2014-06-20 12:22:13 -0700
committerJoe Conway <mail@joeconway.com>2014-06-20 12:24:59 -0700
commit1dde5782e34a1c5ef2ce9d97cf18007fed5fde92 (patch)
tree56744eb73882df20bd8d3a4f06259ad792d57262 /contrib/dblink/dblink.c
parentecac0e2b9e8e8e78d771b20fe441e95bb02db2fa (diff)
downloadpostgresql-1dde5782e34a1c5ef2ce9d97cf18007fed5fde92.tar.gz
postgresql-1dde5782e34a1c5ef2ce9d97cf18007fed5fde92.zip
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.
Diffstat (limited to 'contrib/dblink/dblink.c')
-rw-r--r--contrib/dblink/dblink.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index a81853fa911..d77b3ee34ba 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -977,6 +977,13 @@ materializeQueryResult(FunctionCallInfo fcinfo,
PG_TRY();
{
+ /* Create short-lived memory context for data conversions */
+ sinfo.tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
+ "dblink temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
/* execute query, collecting any tuples into the tuplestore */
res = storeQueryResult(&sinfo, conn, sql);
@@ -1041,6 +1048,12 @@ materializeQueryResult(FunctionCallInfo fcinfo,
PQclear(res);
res = NULL;
}
+
+ /* clean up data conversion short-lived memory context */
+ if (sinfo.tmpcontext != NULL)
+ MemoryContextDelete(sinfo.tmpcontext);
+ sinfo.tmpcontext = NULL;
+
PQclear(sinfo.last_res);
sinfo.last_res = NULL;
PQclear(sinfo.cur_res);
@@ -1204,15 +1217,6 @@ storeRow(storeInfo *sinfo, PGresult *res, bool first)
if (sinfo->cstrs)
pfree(sinfo->cstrs);
sinfo->cstrs = (char **) palloc(nfields * sizeof(char *));
-
- /* Create short-lived memory context for data conversions */
- if (!sinfo->tmpcontext)
- sinfo->tmpcontext =
- AllocSetContextCreate(CurrentMemoryContext,
- "dblink temporary context",
- ALLOCSET_DEFAULT_MINSIZE,
- ALLOCSET_DEFAULT_INITSIZE,
- ALLOCSET_DEFAULT_MAXSIZE);
}
/* Should have a single-row result if we get here */