Avoid memory leak if pgstat_vacuum_stat is interrupted partway through.
authorTom Lane
Sun, 27 Dec 2009 19:40:07 +0000 (19:40 +0000)
committerTom Lane
Sun, 27 Dec 2009 19:40:07 +0000 (19:40 +0000)
The temporary hash tables made by pgstat_collect_oids should be allocated
in a short-term memory context, which is not the default behavior of
hash_create.  Noted while looking through hash_create calls in connection
with Robert Haas' recent complaint.

This is a pre-existing bug, but it doesn't seem important enough to
back-patch.  The hash table is not so large that it would matter unless this
happened many times within a session, which seems quite unlikely.

src/backend/postmaster/pgstat.c

index 4fa2d9fd63763b3546ad6f1ca123fc2752ce73e4..ab88a59eea40179e88b88b93873e6b025d5f6038 100644 (file)
@@ -13,7 +13,7 @@
  *
  * Copyright (c) 2001-2009, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.193 2009/11/28 23:38:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.194 2009/12/27 19:40:07 tgl Exp $
  * ----------
  */
 #include "postgres.h"
@@ -1033,7 +1033,8 @@ pgstat_vacuum_stat(void)
  *
  * Collect the OIDs of all objects listed in the specified system catalog
  * into a temporary hash table.  Caller should hash_destroy the result
- * when done with it.
+ * when done with it.  (However, we make the table in CurrentMemoryContext
+ * so that it will be freed properly in event of an error.)
  * ----------
  */
 static HTAB *
@@ -1049,10 +1050,11 @@ pgstat_collect_oids(Oid catalogid)
    hash_ctl.keysize = sizeof(Oid);
    hash_ctl.entrysize = sizeof(Oid);
    hash_ctl.hash = oid_hash;
+   hash_ctl.hcxt = CurrentMemoryContext;
    htab = hash_create("Temporary table of OIDs",
                       PGSTAT_TAB_HASH_SIZE,
                       &hash_ctl,
-                      HASH_ELEM | HASH_FUNCTION);
+                      HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
 
    rel = heap_open(catalogid, AccessShareLock);
    scan = heap_beginscan(rel, SnapshotNow, 0, NULL);