77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
99 * IDENTIFICATION
10- * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.222 2007/11/05 19:00:25 tgl Exp $
10+ * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.223 2007/11/15 23:23:44 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
4242
4343
4444/* GUC variables */
45- int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN ;
45+ int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN ;
4646
4747
4848/* Local function prototypes */
@@ -67,7 +67,7 @@ static void AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event,
6767 *
6868 * constraintOid, if nonzero, says that this trigger is being created
6969 * internally to implement that constraint. A suitable pg_depend entry will
70- * be made to link the trigger to that constraint. constraintOid is zero when
70+ * be made to link the trigger to that constraint. constraintOid is zero when
7171 * executing a user-entered CREATE TRIGGER command.
7272 *
7373 * Note: can return InvalidOid if we decided to not create a trigger at all,
@@ -211,11 +211,11 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
211211}
212212
213213/*
214- * If the command is a user-entered CREATE CONSTRAINT TRIGGER command
215- *that references one of the built-in RI_FKey trigger functions, assume
216- *it is from a dump of a pre-7.3 foreign key constraint, and take steps
217- *to convert this legacy representation into a regular foreign key
218- * constraint. Ugly, but necessary for loading old dump files.
214+ * If the command is a user-entered CREATE CONSTRAINT TRIGGER command that
215+ * references one of the built-in RI_FKey trigger functions, assume it is
216+ * from a dump of a pre-7.3 foreign key constraint, and take steps to
217+ * convert this legacy representation into a regular foreign key
218+ * constraint. Ugly, but necessary for loading old dump files.
219219 */
220220if (stmt -> isconstraint && !OidIsValid (constraintOid )&&
221221list_length (stmt -> args ) >=6 &&
@@ -421,8 +421,8 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
421421{
422422/*
423423 * It's for a constraint, so make it an internal dependency of the
424- * constraint. We can skip depending on the relations, as there'll
425- *be an indirect dependency via the constraint.
424+ * constraint. We can skip depending on the relations, as there'll be
425+ * an indirect dependency via the constraint.
426426 */
427427referenced .classId = ConstraintRelationId ;
428428referenced .objectId = constraintOid ;
@@ -461,21 +461,22 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
461461 * full-fledged foreign key constraints.
462462 *
463463 * The conversion is complex because a pre-7.3 foreign key involved three
464- * separate triggers, which were reported separately in dumps. While the
464+ * separate triggers, which were reported separately in dumps. While the
465465 * single trigger on the referencing table adds no new information, we need
466466 * to know the trigger functions of both of the triggers on the referenced
467467 * table to build the constraint declaration. Also, due to lack of proper
468468 * dependency checking pre-7.3, it is possible that the source database had
469469 * an incomplete set of triggers resulting in an only partially enforced
470470 * FK constraint. (This would happen if one of the tables had been dropped
471471 * and re-created, but only if the DB had been affected by a 7.0 pg_dump bug
472- * that caused loss of tgconstrrelid information.) We choose to translate to
472+ * that caused loss of tgconstrrelid information.) We choose to translate to
473473 * an FK constraint only when we've seen all three triggers of a set. This is
474474 * implemented by storing unmatched items in a list in TopMemoryContext.
475475 * We match triggers together by comparing the trigger arguments (which
476476 * include constraint name, table and column names, so should be good enough).
477477 */
478- typedef struct {
478+ typedef struct
479+ {
479480List * args ;/* list of (T_String) Values or NIL */
480481Oid funcoids [3 ];/* OIDs of trigger functions */
481482/* The three function OIDs are stored in the order update, delete, child */
@@ -486,7 +487,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
486487{
487488static List * info_list = NIL ;
488489
489- static const char * const funcdescr [3 ]= {
490+ static const char * const funcdescr [3 ]= {
490491gettext_noop ("Found referenced table's UPDATE trigger." ),
491492gettext_noop ("Found referenced table's DELETE trigger." ),
492493gettext_noop ("Found referencing table's trigger." )
@@ -511,7 +512,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
511512i = 0 ;
512513foreach (l ,stmt -> args )
513514{
514- Value * arg = (Value * )lfirst (l );
515+ Value * arg = (Value * )lfirst (l );
515516
516517i ++ ;
517518if (i < 4 )/* skip constraint and table names */
@@ -537,7 +538,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
537538i = 0 ;
538539foreach (l ,fk_attrs )
539540{
540- Value * arg = (Value * )lfirst (l );
541+ Value * arg = (Value * )lfirst (l );
541542
542543if (i ++ > 0 )
543544appendStringInfoChar (& buf ,',' );
@@ -548,7 +549,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
548549i = 0 ;
549550foreach (l ,pk_attrs )
550551{
551- Value * arg = (Value * )lfirst (l );
552+ Value * arg = (Value * )lfirst (l );
552553
553554if (i ++ > 0 )
554555appendStringInfoChar (& buf ,',' );
@@ -598,9 +599,9 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
598599MemoryContext oldContext ;
599600
600601ereport (NOTICE ,
601- (errmsg ("ignoring incomplete trigger group for constraint \"%s\" %s" ,
602- constr_name ,buf .data ),
603- errdetail (funcdescr [funcnum ])));
602+ (errmsg ("ignoring incomplete trigger group for constraint \"%s\" %s" ,
603+ constr_name ,buf .data ),
604+ errdetail (funcdescr [funcnum ])));
604605oldContext = MemoryContextSwitchTo (TopMemoryContext );
605606info = (OldTriggerInfo * )palloc0 (sizeof (OldTriggerInfo ));
606607info -> args = copyObject (stmt -> args );
@@ -614,9 +615,9 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
614615{
615616/* Second trigger of set */
616617ereport (NOTICE ,
617- (errmsg ("ignoring incomplete trigger group for constraint \"%s\" %s" ,
618- constr_name ,buf .data ),
619- errdetail (funcdescr [funcnum ])));
618+ (errmsg ("ignoring incomplete trigger group for constraint \"%s\" %s" ,
619+ constr_name ,buf .data ),
620+ errdetail (funcdescr [funcnum ])));
620621}
621622else
622623{
@@ -1184,8 +1185,8 @@ RelationBuildTriggers(Relation relation)
11841185int i ;
11851186
11861187val = DatumGetByteaP (fastgetattr (htup ,
1187- Anum_pg_trigger_tgargs ,
1188- tgrel -> rd_att ,& isnull ));
1188+ Anum_pg_trigger_tgargs ,
1189+ tgrel -> rd_att ,& isnull ));
11891190if (isnull )
11901191elog (ERROR ,"tgargs is null in trigger for relation \"%s\"" ,
11911192RelationGetRelationName (relation ));
@@ -1637,7 +1638,7 @@ ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
16371638trigger -> tgenabled == TRIGGER_DISABLED )
16381639continue ;
16391640}
1640- else /* ORIGIN or LOCAL role */
1641+ else /* ORIGIN or LOCAL role */
16411642{
16421643if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
16431644trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1696,7 +1697,7 @@ ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
16961697trigger -> tgenabled == TRIGGER_DISABLED )
16971698continue ;
16981699}
1699- else /* ORIGIN or LOCAL role */
1700+ else /* ORIGIN or LOCAL role */
17001701{
17011702if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
17021703trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1768,7 +1769,7 @@ ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
17681769trigger -> tgenabled == TRIGGER_DISABLED )
17691770continue ;
17701771}
1771- else /* ORIGIN or LOCAL role */
1772+ else /* ORIGIN or LOCAL role */
17721773{
17731774if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
17741775trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1834,7 +1835,7 @@ ExecBRDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
18341835trigger -> tgenabled == TRIGGER_DISABLED )
18351836continue ;
18361837}
1837- else /* ORIGIN or LOCAL role */
1838+ else /* ORIGIN or LOCAL role */
18381839{
18391840if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
18401841trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1919,7 +1920,7 @@ ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
19191920trigger -> tgenabled == TRIGGER_DISABLED )
19201921continue ;
19211922}
1922- else /* ORIGIN or LOCAL role */
1923+ else /* ORIGIN or LOCAL role */
19231924{
19241925if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
19251926trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1990,7 +1991,7 @@ ExecBRUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
19901991trigger -> tgenabled == TRIGGER_DISABLED )
19911992continue ;
19921993}
1993- else /* ORIGIN or LOCAL role */
1994+ else /* ORIGIN or LOCAL role */
19941995{
19951996if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
19961997trigger -> tgenabled == TRIGGER_DISABLED )
@@ -2669,7 +2670,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
26692670trigdesc = rInfo -> ri_TrigDesc ;
26702671finfo = rInfo -> ri_TrigFunctions ;
26712672instr = rInfo -> ri_TrigInstrument ;
2672- if (trigdesc == NULL )/* should not happen */
2673+ if (trigdesc == NULL )/* should not happen */
26732674elog (ERROR ,"relation %u has no triggers" ,
26742675event -> ate_relid );
26752676}
@@ -2725,7 +2726,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
27252726
27262727if (local_estate )
27272728{
2728- ListCell * l ;
2729+ ListCell * l ;
27292730
27302731foreach (l ,estate -> es_trig_target_relations )
27312732{
@@ -2905,8 +2906,8 @@ AfterTriggerFireDeferred(void)
29052906ActiveSnapshot = CopySnapshot (GetTransactionSnapshot ());
29062907
29072908/*
2908- * Run all the remaining triggers.Loop until they are all gone, in
2909- *case some trigger queues more for us to do.
2909+ * Run all the remaining triggers.Loop until they are all gone, in case
2910+ * some trigger queues more for us to do.
29102911 */
29112912while (afterTriggerMarkEvents (events ,NULL , false))
29122913{
@@ -2940,13 +2941,13 @@ AfterTriggerEndXact(bool isCommit)
29402941 *
29412942 * Since all the info is in TopTransactionContext or children thereof, we
29422943 * don't really need to do anything to reclaim memory. However, the
2943- * pending-events list could be large, and so it's useful to discard
2944- *it as soon as possible --- especially if we are aborting because we
2945- *ran out of memory for the list!
2944+ * pending-events list could be large, and so it's useful to discard it as
2945+ * soon as possible --- especially if we are aborting because we ran out
2946+ * of memory for the list!
29462947 *
2947- * (Note: any event_cxts of child subtransactions could also be
2948- *deleted here, but we have no convenient way to find them, so we
2949- *leave it to TopTransactionContext reset to clean them up.)
2948+ * (Note: any event_cxts of child subtransactions could also be deleted
2949+ * here, but we have no convenient way to find them, so we leave it to
2950+ * TopTransactionContext reset to clean them up.)
29502951 */
29512952if (afterTriggers && afterTriggers -> event_cxt )
29522953MemoryContextDelete (afterTriggers -> event_cxt );
@@ -2973,9 +2974,8 @@ AfterTriggerBeginSubXact(void)
29732974
29742975/*
29752976 * Allocate more space in the stacks if needed. (Note: because the
2976- * minimum nest level of a subtransaction is 2, we waste the first
2977- * couple entries of each array; not worth the notational effort to
2978- * avoid it.)
2977+ * minimum nest level of a subtransaction is 2, we waste the first couple
2978+ * entries of each array; not worth the notational effort to avoid it.)
29792979 */
29802980while (my_level >=afterTriggers -> maxtransdepth )
29812981{
@@ -3071,16 +3071,17 @@ AfterTriggerEndSubXact(bool isCommit)
30713071afterTriggers -> state_stack [my_level ]= NULL ;
30723072Assert (afterTriggers -> query_depth ==
30733073afterTriggers -> depth_stack [my_level ]);
3074+
30743075/*
30753076 * It's entirely possible that the subxact created an event_cxt but
30763077 * there is not anything left in it (because all the triggers were
3077- * fired at end-of-statement). If so, we should release the context
3078- * to prevent memory leakage in a long sequence of subtransactions.
3079- *We can detect whether there's anything of use in the context by
3080- *seeing if anything was added to the global events list since
3081- *subxact start. (This test doesn't catch every case where the
3082- *context is deletable; for instance maybe the only additions were
3083- *from a sub-sub-xact. But it handles the common case.)
3078+ * fired at end-of-statement). If so, we should release the context
3079+ * to prevent memory leakage in a long sequence of subtransactions. We
3080+ * can detect whether there's anything of use in the context by seeing
3081+ * if anything was added to the global events list since subxact
3082+ * start. (This test doesn't catch every case where the context is
3083+ * deletable; for instance maybe the only additions were from a
3084+ * sub-sub-xact. But it handles the common case.)
30843085 */
30853086if (afterTriggers -> cxt_stack [my_level ]&&
30863087afterTriggers -> events .tail == afterTriggers -> events_stack [my_level ].tail )
@@ -3615,7 +3616,7 @@ AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event, bool row_trigger,
36153616trigger -> tgenabled == TRIGGER_DISABLED )
36163617continue ;
36173618}
3618- else /* ORIGIN or LOCAL role */
3619+ else /* ORIGIN or LOCAL role */
36193620{
36203621if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
36213622trigger -> tgenabled == TRIGGER_DISABLED )
@@ -3668,10 +3669,10 @@ AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event, bool row_trigger,
36683669
36693670/*
36703671 * If we don't yet have an event context for the current (sub)xact,
3671- * create one. Make it a child of CurTransactionContext to ensure it
3672+ * create one. Make it a child of CurTransactionContext to ensure it
36723673 * will go away if the subtransaction aborts.
36733674 */
3674- if (my_level > 1 )/* subtransaction? */
3675+ if (my_level > 1 )/* subtransaction? */
36753676{
36763677Assert (my_level < afterTriggers -> maxtransdepth );
36773678cxtptr = & afterTriggers -> cxt_stack [my_level ];