Fix an old bug in after-trigger handling: AfterTriggerEndQuery took the address of afterTriggers->query_stack[afterTriggers->query_depth] and hung onto it through all its firings of triggers.  However, if a trigger causes sufficiently many nested query executions, query_stack will get repalloc'd bigger, leaving AfterTriggerEndQuery --- and hence afterTriggerInvokeEvents --- using a stale pointer.
Fix an old bug in after-trigger handling: AfterTriggerEndQuery took the
address of afterTriggers->query_stack[afterTriggers->query_depth] and hung
onto it through all its firings of triggers.  However, if a trigger causes
sufficiently many nested query executions, query_stack will get repalloc'd
bigger, leaving AfterTriggerEndQuery --- and hence afterTriggerInvokeEvents
--- using a stale pointer.

So far as I can find, the only consequence of this error is to stomp on a
couple of words of already-freed memory; which would lead to a failure only if
that chunk had already gotten re-allocated for something else.  So it's hard
to exhibit a simple failure case, but this is surely a bug.

I noticed this while working on my recent patch to reduce pending-trigger
space usage.  The present patch is mighty ugly, because it requires making
afterTriggerInvokeEvents know about all the possible event lists it might get
called on.  Fortunately, this is only needed in back branches because CVS HEAD
avoids the problem in a different way: afterTriggerInvokeEvents only touches
the passed AfterTriggerEventList pointer once at startup.  Back branches are
stable enough that wiring in knowledge of all possible call usages doesn't
seem like a killer problem.

Back-patch to 8.0.  7.4's trigger code is completely different and doesn't
seem to have the problem (it doesn't even use repalloc).

0x0FFF/gpdb,Quikling/gpdb,CraigHarris/gpdb,kaknikhil/gpdb,jmcatamney/gpdb,adam8157/gpdb,0x0FFF/gpdb,xuegang/gpdb,xinzweb/gpdb,ahachete/gpdb,Chibin/gpdb,yuanzhao/gpdb,rvs/gpdb,edespino/gpdb,royc1/gpdb,royc1/gpdb,rvs/gpdb,janebeckman/gpdb,zaksoup/gpdb,janebeckman/gpdb,yuanzhao/gpdb,0x0FFF/gpdb,adam8157/gpdb,royc1/gpdb,Quikling/gpdb,lintzc/gpdb,lisakowen/gpdb,ahachete/gpdb,Quikling/gpdb,Chibin/gpdb,greenplum-db/gpdb,edespino/gpdb,jmcatamney/gpdb,kaknikhil/gpdb,edespino/gpdb,yuanzhao/gpdb,0x0FFF/gpdb,janebeckman/gpdb,xuegang/gpdb,rvs/gpdb,rvs/gpdb,chrishajas/gpdb,greenplum-db/gpdb,chrishajas/gpdb,greenplum-db/gpdb,kaknikhil/gpdb,lintzc/gpdb,Quikling/gpdb,kaknikhil/gpdb,adam8157/gpdb,kaknikhil/gpdb,ashwinstar/gpdb,greenplum-db/gpdb,lintzc/gpdb,CraigHarris/gpdb,zaksoup/gpdb,xinzweb/gpdb,ahachete/gpdb,yuanzhao/gpdb,adam8157/gpdb,jmcatamney/gpdb,royc1/gpdb,kaknikhil/gpdb,50wu/gpdb,chrishajas/gpdb,edespino/gpdb,greenplum-db/gpdb,lisakowen/gpdb,edespino/gpdb,rvs/gpdb,edespino/gpdb,xinzweb/gpdb,janebeckman/gpdb,Chibin/gpdb,ashwinstar/gpdb,ashwinstar/gpdb,xuegang/gpdb,janebeckman/gpdb,chrishajas/gpdb,50wu/gpdb,xinzweb/gpdb,0x0FFF/gpdb,50wu/gpdb,CraigHarris/gpdb,ashwinstar/gpdb,Chibin/gpdb,xuegang/gpdb,kaknikhil/gpdb,ashwinstar/gpdb,ashwinstar/gpdb,cjcjameson/gpdb,chrishajas/gpdb,Quikling/gpdb,lisakowen/gpdb,jmcatamney/gpdb,royc1/gpdb,chrishajas/gpdb,Quikling/gpdb,xinzweb/gpdb,chrishajas/gpdb,CraigHarris/gpdb,royc1/gpdb,yuanzhao/gpdb,edespino/gpdb,adam8157/gpdb,rvs/gpdb,0x0FFF/gpdb,kaknikhil/gpdb,greenplum-db/gpdb,kaknikhil/gpdb,yuanzhao/gpdb,50wu/gpdb,edespino/gpdb,zaksoup/gpdb,cjcjameson/gpdb,lintzc/gpdb,cjcjameson/gpdb,50wu/gpdb,lintzc/gpdb,ahachete/gpdb,janebeckman/gpdb,xinzweb/gpdb,cjcjameson/gpdb,ashwinstar/gpdb,greenplum-db/gpdb,lisakowen/gpdb,zaksoup/gpdb,ahachete/gpdb,Chibin/gpdb,cjcjameson/gpdb,jmcatamney/gpdb,kaknikhil/gpdb,janebeckman/gpdb,Chibin/gpdb,zaksoup/gpdb,yuanzhao/gpdb,jmcatamney/gpdb,CraigHarris/gpdb,50wu/gpdb,xinzweb/gpdb,edespino/gpdb,rvs/gpdb,royc1/gpdb,rvs/gpdb,janebeckman/gpdb,yuanzhao/gpdb,xinzweb/gpdb,Quikling/gpdb,Quikling/gpdb,jmcatamney/gpdb,xuegang/gpdb,lisakowen/gpdb,cjcjameson/gpdb,lintzc/gpdb,janebeckman/gpdb,50wu/gpdb,lisakowen/gpdb,janebeckman/gpdb,lisakowen/gpdb,greenplum-db/gpdb,chrishajas/gpdb,zaksoup/gpdb,0x0FFF/gpdb,ashwinstar/gpdb,lintzc/gpdb,ahachete/gpdb,0x0FFF/gpdb,Chibin/gpdb,yuanzhao/gpdb,adam8157/gpdb,cjcjameson/gpdb,50wu/gpdb,cjcjameson/gpdb,xuegang/gpdb,CraigHarris/gpdb,cjcjameson/gpdb,edespino/gpdb,lintzc/gpdb,Chibin/gpdb,rvs/gpdb,cjcjameson/gpdb,yuanzhao/gpdb,xuegang/gpdb,rvs/gpdb,adam8157/gpdb,lintzc/gpdb,zaksoup/gpdb,Chibin/gpdb,zaksoup/gpdb,CraigHarris/gpdb,xuegang/gpdb,CraigHarris/gpdb,xuegang/gpdb,Quikling/gpdb,ahachete/gpdb,adam8157/gpdb,ahachete/gpdb,royc1/gpdb,jmcatamney/gpdb,CraigHarris/gpdb,lisakowen/gpdb,Chibin/gpdb,Quikling/gpdb