7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.86 2001/01/27 05:16:58 tgl Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.87 2001/03/12 23:02:00 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -1152,14 +1152,16 @@ static bool deftrig_all_isdeferred;
1152
1152
static List * deftrig_trigstates ;
1153
1153
1154
1154
/* ----------
1155
- * The list of events during the entire transaction.
1155
+ * The list of events during the entire transaction. deftrig_events
1156
+ * is the head, deftrig_event_tail is the last entry.
1156
1157
*
1157
- * XXX This must finally be held in a file because of the huge
1158
- * number of events that could occur in the real world .
1158
+ * XXX Need to be able to shove this data out to a file if it grows too
1159
+ * large.. .
1159
1160
* ----------
1160
1161
*/
1161
1162
static int deftrig_n_events ;
1162
1163
static List * deftrig_events ;
1164
+ static List * deftrig_event_tail ;
1163
1165
1164
1166
1165
1167
/* ----------
@@ -1235,7 +1237,22 @@ deferredTriggerCheckState(Oid tgoid, int32 itemstate)
1235
1237
static void
1236
1238
deferredTriggerAddEvent (DeferredTriggerEvent event )
1237
1239
{
1238
- deftrig_events = lappend (deftrig_events , event );
1240
+ /*
1241
+ * Since the event list could grow quite long, we keep track of the
1242
+ * list tail and append there, rather than just doing a stupid "lappend".
1243
+ * This avoids O(N^2) behavior for large numbers of events.
1244
+ */
1245
+ if (deftrig_event_tail == NIL )
1246
+ {
1247
+ /* first list entry */
1248
+ deftrig_events = makeList1 (event );
1249
+ deftrig_event_tail = deftrig_events ;
1250
+ }
1251
+ else
1252
+ {
1253
+ lnext (deftrig_event_tail ) = makeList1 (event );
1254
+ deftrig_event_tail = lnext (deftrig_event_tail );
1255
+ }
1239
1256
deftrig_n_events ++ ;
1240
1257
}
1241
1258
@@ -1397,7 +1414,6 @@ deferredTriggerInvokeEvents(bool immediate_only)
1397
1414
List * el ;
1398
1415
DeferredTriggerEvent event ;
1399
1416
int still_deferred_ones ;
1400
- int eventno = -1 ;
1401
1417
int i ;
1402
1418
MemoryContext per_tuple_context ;
1403
1419
@@ -1421,8 +1437,6 @@ deferredTriggerInvokeEvents(bool immediate_only)
1421
1437
1422
1438
foreach (el , deftrig_events )
1423
1439
{
1424
- eventno ++ ;
1425
-
1426
1440
MemoryContextReset (per_tuple_context );
1427
1441
1428
1442
/* ----------
@@ -1548,6 +1562,7 @@ DeferredTriggerBeginXact(void)
1548
1562
1549
1563
deftrig_n_events = 0 ;
1550
1564
deftrig_events = NIL ;
1565
+ deftrig_event_tail = NIL ;
1551
1566
}
1552
1567
1553
1568
0 commit comments