Fix possible corruption of AfterTriggerEventLists in subtransaction rollback.
authorTom Lane <[email protected]>
Thu, 19 Aug 2010 15:46:30 +0000 (15:46 +0000)
committerTom Lane <[email protected]>
Thu, 19 Aug 2010 15:46:30 +0000 (15:46 +0000)
afterTriggerInvokeEvents failed to adjust events->tailfree when truncating
the last chunk of an event list.  This could result in the data being
"de-truncated" by afterTriggerRestoreEventList during a subsequent
subtransaction abort.  Even that wouldn't kill us, because the re-added data
would just be events marked DONE --- unless the data had been partially
overwritten by new events.  Then we might crash, or in any case misbehave
(perhaps fire triggers twice, or fire triggers with the wrong event data).
Per bug #5622 from Thue Janus Kristensen.

Back-patch to 8.4 where the current trigger list representation was introduced.

src/backend/commands/trigger.c

index 09199ed27be754f7bfbe9adc62401d53aea6b871..15871bbab54d2a0fa63f4264d96d564de0ca8c06 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.248.2.2 2010/01/24 21:49:31 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.248.2.3 2010/08/19 15:46:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2534,6 +2534,7 @@ afterTriggerAddEvent(AfterTriggerEventList *events,
        else
            events->tail->next = chunk;
        events->tail = chunk;
+       /* events->tailfree is now out of sync, but we'll fix it below */
    }
 
    /*
@@ -2935,6 +2936,15 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
        {
            chunk->freeptr = CHUNK_DATA_START(chunk);
            chunk->endfree = chunk->endptr;
+
+           /*
+            * If it's last chunk, must sync event list's tailfree too.  Note
+            * that delete_ok must NOT be passed as true if there could be
+            * stacked AfterTriggerEventList values pointing at this event
+            * list, since we'd fail to fix their copies of tailfree.
+            */
+           if (chunk == events->tail)
+               events->tailfree = chunk->freeptr;
        }
    }