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).
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).

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, which should slightly speed up
the loop.

Thanks to steve@deefs.net for reporting the leak.

0x0FFF/gpdb,edespino/gpdb,yazun/postgres-xl,ashwinstar/gpdb,ovr/postgres-xl,50wu/gpdb,tangp3/gpdb,xuegang/gpdb,foyzur/gpdb,Chibin/gpdb,rubikloud/gpdb,cjcjameson/gpdb,cjcjameson/gpdb,ashwinstar/gpdb,yuanzhao/gpdb,foyzur/gpdb,royc1/gpdb,snaga/postgres-xl,lisakowen/gpdb,oberstet/postgres-xl,atris/gpdb,snaga/postgres-xl,greenplum-db/gpdb,CraigHarris/gpdb,Chibin/gpdb,chrishajas/gpdb,ashwinstar/gpdb,rubikloud/gpdb,CraigHarris/gpdb,zaksoup/gpdb,zaksoup/gpdb,ahachete/gpdb,greenplum-db/gpdb,atris/gpdb,cjcjameson/gpdb,50wu/gpdb,tangp3/gpdb,royc1/gpdb,foyzur/gpdb,pavanvd/postgres-xl,techdragon/Postgres-XL,techdragon/Postgres-XL,Chibin/gpdb,jmcatamney/gpdb,adam8157/gpdb,ashwinstar/gpdb,chrishajas/gpdb,lisakowen/gpdb,Chibin/gpdb,jmcatamney/gpdb,kmjungersen/PostgresXL,janebeckman/gpdb,oberstet/postgres-xl,atris/gpdb,rubikloud/gpdb,rvs/gpdb,randomtask1155/gpdb,rvs/gpdb,ashwinstar/gpdb,lisakowen/gpdb,yuanzhao/gpdb,royc1/gpdb,lintzc/gpdb,zeroae/postgres-xl,CraigHarris/gpdb,chrishajas/gpdb,atris/gpdb,royc1/gpdb,kaknikhil/gpdb,rvs/gpdb,rvs/gpdb,jmcatamney/gpdb,lintzc/gpdb,randomtask1155/gpdb,tangp3/gpdb,tpostgres-projects/tPostgres,chrishajas/gpdb,cjcjameson/gpdb,lisakowen/gpdb,xuegang/gpdb,tpostgres-projects/tPostgres,Quikling/gpdb,tangp3/gpdb,rvs/gpdb,tangp3/gpdb,Chibin/gpdb,lintzc/gpdb,kaknikhil/gpdb,Postgres-XL/Postgres-XL,janebeckman/gpdb,cjcjameson/gpdb,greenplum-db/gpdb,jmcatamney/gpdb,zeroae/postgres-xl,0x0FFF/gpdb,atris/gpdb,Quikling/gpdb,ashwinstar/gpdb,foyzur/gpdb,arcivanov/postgres-xl,atris/gpdb,ahachete/gpdb,pavanvd/postgres-xl,lpetrov-pivotal/gpdb,lpetrov-pivotal/gpdb,yuanzhao/gpdb,lpetrov-pivotal/gpdb,oberstet/postgres-xl,Postgres-XL/Postgres-XL,adam8157/gpdb,snaga/postgres-xl,edespino/gpdb,Postgres-XL/Postgres-XL,lintzc/gpdb,yuanzhao/gpdb,foyzur/gpdb,cjcjameson/gpdb,edespino/gpdb,rvs/gpdb,yazun/postgres-xl,kaknikhil/gpdb,Quikling/gpdb,yuanzhao/gpdb,50wu/gpdb,kaknikhil/gpdb,arcivanov/postgres-xl,xinzweb/gpdb,CraigHarris/gpdb,oberstet/postgres-xl,techdragon/Postgres-XL,Quikling/gpdb,lpetrov-pivotal/gpdb,zeroae/postgres-xl,xuegang/gpdb,tpostgres-projects/tPostgres,edespino/gpdb,adam8157/gpdb,postmind-net/postgres-xl,kmjungersen/PostgresXL,Postgres-XL/Postgres-XL,techdragon/Postgres-XL,pavanvd/postgres-xl,lisakowen/gpdb,royc1/gpdb,foyzur/gpdb,cjcjameson/gpdb,kaknikhil/gpdb,janebeckman/gpdb,xuegang/gpdb,janebeckman/gpdb,greenplum-db/gpdb,lintzc/gpdb,atris/gpdb,postmind-net/postgres-xl,postmind-net/postgres-xl,zaksoup/gpdb,xuegang/gpdb,xinzweb/gpdb,jmcatamney/gpdb,CraigHarris/gpdb,ahachete/gpdb,50wu/gpdb,janebeckman/gpdb,xuegang/gpdb,randomtask1155/gpdb,tpostgres-projects/tPostgres,rubikloud/gpdb,yazun/postgres-xl,xuegang/gpdb,postmind-net/postgres-xl,rubikloud/gpdb,greenplum-db/gpdb,edespino/gpdb,edespino/gpdb,Quikling/gpdb,arcivanov/postgres-xl,yuanzhao/gpdb,cjcjameson/gpdb,lisakowen/gpdb,0x0FFF/gpdb,lpetrov-pivotal/gpdb,Quikling/gpdb,tangp3/gpdb,adam8157/gpdb,postmind-net/postgres-xl,arcivanov/postgres-xl,lintzc/gpdb,zaksoup/gpdb,rubikloud/gpdb,ovr/postgres-xl,kaknikhil/gpdb,ahachete/gpdb,zeroae/postgres-xl,arcivanov/postgres-xl,adam8157/gpdb,xinzweb/gpdb,adam8157/gpdb,chrishajas/gpdb,atris/gpdb,pavanvd/postgres-xl,Chibin/gpdb,xuegang/gpdb,rvs/gpdb,yuanzhao/gpdb,ahachete/gpdb,adam8157/gpdb,ovr/postgres-xl,0x0FFF/gpdb,foyzur/gpdb,chrishajas/gpdb,ashwinstar/gpdb,oberstet/postgres-xl,kmjungersen/PostgresXL,Quikling/gpdb,zaksoup/gpdb,cjcjameson/gpdb,royc1/gpdb,cjcjameson/gpdb,rubikloud/gpdb,jmcatamney/gpdb,janebeckman/gpdb,lpetrov-pivotal/gpdb,lisakowen/gpdb,kaknikhil/gpdb,ashwinstar/gpdb,snaga/postgres-xl,adam8157/gpdb,kmjungersen/PostgresXL,Postgres-XL/Postgres-XL,50wu/gpdb,greenplum-db/gpdb,snaga/postgres-xl,chrishajas/gpdb,arcivanov/postgres-xl,randomtask1155/gpdb,ahachete/gpdb,CraigHarris/gpdb,lpetrov-pivotal/gpdb,royc1/gpdb,ovr/postgres-xl,ahachete/gpdb,randomtask1155/gpdb,janebeckman/gpdb,ahachete/gpdb,edespino/gpdb,lpetrov-pivotal/gpdb,greenplum-db/gpdb,xinzweb/gpdb,zaksoup/gpdb,rvs/gpdb,rvs/gpdb,Chibin/gpdb,xinzweb/gpdb,50wu/gpdb,randomtask1155/gpdb,yuanzhao/gpdb,kaknikhil/gpdb,rubikloud/gpdb,Chibin/gpdb,randomtask1155/gpdb,tpostgres-projects/tPostgres,Chibin/gpdb,techdragon/Postgres-XL,Chibin/gpdb,edespino/gpdb,edespino/gpdb,xinzweb/gpdb,lintzc/gpdb,xinzweb/gpdb,randomtask1155/gpdb,yuanzhao/gpdb,tangp3/gpdb,lisakowen/gpdb,yuanzhao/gpdb,pavanvd/postgres-xl,kmjungersen/PostgresXL,tangp3/gpdb,0x0FFF/gpdb,zaksoup/gpdb,50wu/gpdb,lintzc/gpdb,yazun/postgres-xl,0x0FFF/gpdb,foyzur/gpdb,0x0FFF/gpdb,ovr/postgres-xl,kaknikhil/gpdb,lintzc/gpdb,Quikling/gpdb,xinzweb/gpdb,jmcatamney/gpdb,jmcatamney/gpdb,janebeckman/gpdb,greenplum-db/gpdb,CraigHarris/gpdb,Quikling/gpdb,yazun/postgres-xl,zaksoup/gpdb,50wu/gpdb,kaknikhil/gpdb,xuegang/gpdb,CraigHarris/gpdb,Quikling/gpdb,edespino/gpdb,0x0FFF/gpdb,zeroae/postgres-xl,chrishajas/gpdb,CraigHarris/gpdb,janebeckman/gpdb,rvs/gpdb,janebeckman/gpdb,royc1/gpdb