|
7 | 7 | * Portions Copyright (c) 1994, Regents of the University of California
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.214 2007/03/19 23:38:29 wieck Exp $ |
| 10 | + * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.215 2007/07/01 17:45:42 tgl Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -2814,6 +2814,24 @@ AfterTriggerEndSubXact(bool isCommit)
|
2814 | 2814 | afterTriggers->state_stack[my_level] = NULL;
|
2815 | 2815 | Assert(afterTriggers->query_depth ==
|
2816 | 2816 | afterTriggers->depth_stack[my_level]);
|
| 2817 | + /* |
| 2818 | + * It's entirely possible that the subxact created an event_cxt but |
| 2819 | + * there is not anything left in it (because all the triggers were |
| 2820 | + * fired at end-of-statement). If so, we should release the context |
| 2821 | + * to prevent memory leakage in a long sequence of subtransactions. |
| 2822 | + * We can detect whether there's anything of use in the context by |
| 2823 | + * seeing if anything was added to the global events list since |
| 2824 | + * subxact start. (This test doesn't catch every case where the |
| 2825 | + * context is deletable; for instance maybe the only additions were |
| 2826 | + * from a sub-sub-xact. But it handles the common case.) |
| 2827 | + */ |
| 2828 | + if (afterTriggers->cxt_stack[my_level] && |
| 2829 | + afterTriggers->events.tail == afterTriggers->events_stack[my_level].tail) |
| 2830 | + { |
| 2831 | + MemoryContextDelete(afterTriggers->cxt_stack[my_level]); |
| 2832 | + /* avoid double delete if abort later */ |
| 2833 | + afterTriggers->cxt_stack[my_level] = NULL; |
| 2834 | + } |
2817 | 2835 | }
|
2818 | 2836 | else
|
2819 | 2837 | {
|
|
0 commit comments