bdr: Fix various memory leaks.
authorAndres Freund <andres@anarazel.de>
Wed, 25 Jun 2014 23:08:23 +0000 (01:08 +0200)
committerAndres Freund <andres@anarazel.de>
Thu, 3 Jul 2014 15:55:41 +0000 (17:55 +0200)
Found by Petr.

contrib/bdr/bdr.c
contrib/bdr/bdr_apply.c

index 75cef72c1d3592cffd657148548d3d1883546200..7ce6962c08ef3c65f02f52e0799fa71349adf479 100644 (file)
@@ -534,6 +534,7 @@ bdr_apply_main(Datum main_arg)
 {
    PGconn     *streamConn;
    PGresult   *res;
+   char       *copybuf = NULL;
    int         fd;
    StringInfoData query;
    XLogRecPtr  last_received = InvalidXLogRecPtr;
@@ -664,7 +665,6 @@ bdr_apply_main(Datum main_arg)
        /* int       ret; */
        int         rc;
        int         r;
-       char       *copybuf = NULL;
 
        /*
         * Background workers mustn't call usleep() or any direct equivalent:
@@ -679,6 +679,8 @@ bdr_apply_main(Datum main_arg)
 
        ResetLatch(&MyProc->procLatch);
 
+       MemoryContextSwitchTo(MessageContext);
+
        /* emergency bailout if postmaster has died */
        if (rc & WL_POSTMASTER_DEATH)
            proc_exit(1);
@@ -697,6 +699,12 @@ bdr_apply_main(Datum main_arg)
            if (exit_worker)
                break;
 
+           if (copybuf != NULL)
+           {
+               PQfreemem(copybuf);
+               copybuf = NULL;
+           }
+
            r = PQgetCopyData(streamConn, &copybuf, 1);
 
            if (r == -1)
@@ -720,9 +728,12 @@ bdr_apply_main(Datum main_arg)
                int c;
                StringInfoData s;
 
+               MemoryContextSwitchTo(MessageContext);
+
                initStringInfo(&s);
                s.data = copybuf;
                s.len = r;
+               s.maxlen = -1;
 
                c = pq_getmsgbyte(&s);
 
@@ -755,7 +766,6 @@ bdr_apply_main(Datum main_arg)
                /* other message types are purposefully ignored */
            }
 
-           MemoryContextResetAndDeleteChildren(MessageContext);
        }
 
        /* confirm all writes at once */
@@ -783,6 +793,7 @@ bdr_apply_main(Datum main_arg)
            ResetLatch(&MyProc->procLatch);
            rc = WaitLatch(&MyProc->procLatch, WL_TIMEOUT, 1000L);
        }
+       MemoryContextResetAndDeleteChildren(MessageContext);
    }
 
    proc_exit(0);
index 2e621452a495e6b4d6c449af7d2164fad0f79845..cad257d0a454b66c659998e053c1416e7378ea99 100644 (file)
@@ -392,11 +392,6 @@ process_remote_insert(StringInfo s)
 
    Assert(bdr_apply_worker != NULL);
 
-   /*
-    * Read tuple into a context that's long lived enough for CONCURRENTLY
-    * processing.
-    */
-   MemoryContextSwitchTo(MessageContext);
    rel = read_rel(s, RowExclusiveLock);
 
    action = pq_getmsgbyte(s);
@@ -1594,7 +1589,11 @@ static bool
 bdr_performing_work(void)
 {
    if (started_transaction)
+   {
+       if (CurrentMemoryContext != TopTransactionContext)
+           MemoryContextSwitchTo(TopTransactionContext);
        return false;
+   }
 
    started_transaction = true;
    StartTransactionCommand();