Fix erroneous memory context switch in autovacuum, which was returning to a
authorAlvaro Herrera <[email protected]>
Tue, 20 Jan 2009 12:17:29 +0000 (12:17 +0000)
committerAlvaro Herrera <[email protected]>
Tue, 20 Jan 2009 12:17:29 +0000 (12:17 +0000)
context long after it had been destroyed.

Per problem report from Justin Pasher.  Patch by Tom Lane and me.

8.3 and later do not have this bug, because this code has been restructured for
unrelated reasons.  In 8.2 it does not manifest as a crash, but it still seems
safer fixing it nonetheless.

src/backend/postmaster/autovacuum.c

index f30dd9ee8d9b9cc8129fdfddf880496ef71b834a..3325faf6a685ad09f78a2565ce11e598c9f2693f 100644 (file)
@@ -925,13 +925,12 @@ autovacuum_do_vac_analyze(List *relids, bool dovacuum, bool doanalyze,
                                                  bool freeze)
 {
        VacuumStmt *vacstmt;
-       MemoryContext old_cxt;
 
        /*
         * The node must survive transaction boundaries, so make sure we create it
         * in a long-lived context
         */
-       old_cxt = MemoryContextSwitchTo(AutovacMemCxt);
+       MemoryContextSwitchTo(AutovacMemCxt);
 
        vacstmt = makeNode(VacuumStmt);
 
@@ -957,7 +956,9 @@ autovacuum_do_vac_analyze(List *relids, bool dovacuum, bool doanalyze,
        vacuum(vacstmt, relids);
 
        pfree(vacstmt);
-       MemoryContextSwitchTo(old_cxt);
+
+       /* Make sure we end up pointing to the long-lived context at exit */
+       MemoryContextSwitchTo(AutovacMemCxt);
 }
 
 /*