Skip site navigation (1) Skip section navigation (2)

memory leak in ALTER TABLE

From: Neil Conway <neilc(at)samurai(dot)com>
To: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: memory leak in ALTER TABLE
Date: 2005-02-09 04:56:33
Message-ID: 1107924993.1286.93.camel@localhost.localdomain (view raw or flat)
Thread:
Lists: pgsql-patches
ALTER TABLE ADD COLUMN exhibits a significant memory leak when adding a
column with a default expression. In that situation, we need to rewrite
the heap relation. To evaluate the new default expression, we use
ExecEvalExpr(); however, this can allocate memory in the current memory
context, and ATRewriteTable() does not switch out of the active portal's
heap memory context. The end result is a rather large memory leak (on
the order of gigabytes for a reasonably sized table). To repro, just
create a large table (a few hundred megabytes), and add a serial column
to it.

This patch changes ATRewriteTable() to switch to the per-tuple memory
context before beginning the per-tuple loop. It also removes an explicit
heap_freetuple() in the loop, since that is no longer needed.

In an unrelated change, I noticed the code was scanning through the
attributes of the new tuple descriptor for each tuple of the old table.
I changed this to use precomputation.

Barring any objections, I will apply this to HEAD and REL8_0_STABLE
tomorrow.

-Neil


Attachment: alter_table_add_column_leak-2.patch
Description: text/x-patch (3.5 KB)

Responses

pgsql-patches by date

Next:From: Nicolai TufarDate: 2005-02-09 07:59:28
Subject: Repleacement for src/port/snprintf.c
Previous:From: Tom LaneDate: 2005-02-08 21:45:24
Subject: Re: WIP: pl/pgsql cleanup

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group