From 0b392314315a55aa201f53b2ba71986be91ebc69 Mon Sep 17 00:00:00 2001 From: Tom Lane <tgl@sss.pgh.pa.us> Date: Sun, 27 Dec 2009 19:40:07 +0000 Subject: [PATCH] Avoid memory leak if pgstat_vacuum_stat is interrupted partway through. 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 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 4fa2d9fd637..ab88a59eea4 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -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); -- GitLab