]> granicus.if.org Git - postgresql/commitdiff
Clean up data conversion short-lived memory context.
authorJoe Conway <mail@joeconway.com>
Fri, 20 Jun 2014 19:23:05 +0000 (12:23 -0700)
committerJoe Conway <mail@joeconway.com>
Fri, 20 Jun 2014 19:27:04 +0000 (12:27 -0700)
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.

contrib/dblink/dblink.c

index 1b54d29744f703fd2a675833abe4101fd8b826c0..6880eb9cac3e5975eb2ed021c5d1382e96054190 100644 (file)
@@ -969,6 +969,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);
 
@@ -1033,6 +1040,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);
@@ -1196,15 +1209,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 */