]> granicus.if.org Git - postgresql/commitdiff
Fix memory leak coming from simple lists built in reindexdb
authorMichael Paquier <michael@paquier.xyz>
Tue, 30 Jul 2019 01:54:48 +0000 (10:54 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 30 Jul 2019 01:54:48 +0000 (10:54 +0900)
When building a list of relations for a parallel processing of a schema
or a database (or just a single-entry list for the non-parallel case
with the database name), the list is allocated and built on-the-fly for
each database processed, leaking after one database-level reindex is
done.  This accumulates leaks when processing all databases, and could
become a visible issue with thousands of relations.

This is fixed by introducing a new routine in simple_list.c to free all
the elements in a simple list made of strings or OIDs.  The header of
the list may be using a variable declaration or an allocated pointer,
so we don't have a routine to free this part to keep the interface
simple.

Per report from coverity for an issue introduced by 5ab892c, and
valgrind complains about the leak as well.  The idea to introduce a new
routine in simple_list.c is from Tom Lane.

Author: Michael Paquier
Reviewed-by: Tom Lane
src/bin/scripts/reindexdb.c
src/fe_utils/simple_list.c
src/include/fe_utils/simple_list.h

index b2c0400cb9c962350ec3567d95e2ff7dc8c719d9..14a4f4a91c708dd1e7112ebff982155760ca198d 100644 (file)
@@ -473,6 +473,12 @@ reindex_one_database(const char *dbname, ReindexType type,
                failed = true;
 
 finish:
+       if (process_list != user_list)
+       {
+               simple_string_list_destroy(process_list);
+               pg_free(process_list);
+       }
+
        ParallelSlotsTerminate(slots, concurrentCons);
        pfree(slots);
 
index 8d605140a742e14fdca2203631042643829e70ca..cfdb7dc87af55f6c7e3f875e3496eab1e91e350a 100644 (file)
@@ -99,6 +99,44 @@ simple_string_list_member(SimpleStringList *list, const char *val)
        return false;
 }
 
+/*
+ * Destroy an OID list
+ */
+void
+simple_oid_list_destroy(SimpleOidList *list)
+{
+       SimpleOidListCell *cell;
+
+       cell = list->head;
+       while (cell != NULL)
+       {
+               SimpleOidListCell *next;
+
+               next = cell->next;
+               pg_free(cell);
+               cell = next;
+       }
+}
+
+/*
+ * Destroy a string list
+ */
+void
+simple_string_list_destroy(SimpleStringList *list)
+{
+       SimpleStringListCell *cell;
+
+       cell = list->head;
+       while (cell != NULL)
+       {
+               SimpleStringListCell *next;
+
+               next = cell->next;
+               pg_free(cell);
+               cell = next;
+       }
+}
+
 /*
  * Find first not-touched list entry, if there is one.
  */
index 8a95cbb3a85b7ba696b9170aa464b0621d96b9e3..75738becf42d84b4d8b4b3d7e71c7fcccea4620c 100644 (file)
@@ -46,9 +46,11 @@ typedef struct SimpleStringList
 
 extern void simple_oid_list_append(SimpleOidList *list, Oid val);
 extern bool simple_oid_list_member(SimpleOidList *list, Oid val);
+extern void simple_oid_list_destroy(SimpleOidList *list);
 
 extern void simple_string_list_append(SimpleStringList *list, const char *val);
 extern bool simple_string_list_member(SimpleStringList *list, const char *val);
+extern void simple_string_list_destroy(SimpleStringList *list);
 
 extern const char *simple_string_list_not_touched(SimpleStringList *list);