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.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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
42
42
43
43
44
44
/* GUC variables */
45
- int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN ;
45
+ int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN ;
46
46
47
47
48
48
/* Local function prototypes */
@@ -67,7 +67,7 @@ static void AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event,
67
67
*
68
68
* constraintOid, if nonzero, says that this trigger is being created
69
69
* 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
71
71
* executing a user-entered CREATE TRIGGER command.
72
72
*
73
73
* Note: can return InvalidOid if we decided to not create a trigger at all,
@@ -211,11 +211,11 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
211
211
}
212
212
213
213
/*
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.
219
219
*/
220
220
if (stmt -> isconstraint && !OidIsValid (constraintOid ) &&
221
221
list_length (stmt -> args ) >= 6 &&
@@ -421,8 +421,8 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
421
421
{
422
422
/*
423
423
* 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.
426
426
*/
427
427
referenced .classId = ConstraintRelationId ;
428
428
referenced .objectId = constraintOid ;
@@ -461,21 +461,22 @@ CreateTrigger(CreateTrigStmt *stmt, Oid constraintOid)
461
461
* full-fledged foreign key constraints.
462
462
*
463
463
* 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
465
465
* single trigger on the referencing table adds no new information, we need
466
466
* to know the trigger functions of both of the triggers on the referenced
467
467
* table to build the constraint declaration. Also, due to lack of proper
468
468
* dependency checking pre-7.3, it is possible that the source database had
469
469
* an incomplete set of triggers resulting in an only partially enforced
470
470
* FK constraint. (This would happen if one of the tables had been dropped
471
471
* 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
473
473
* an FK constraint only when we've seen all three triggers of a set. This is
474
474
* implemented by storing unmatched items in a list in TopMemoryContext.
475
475
* We match triggers together by comparing the trigger arguments (which
476
476
* include constraint name, table and column names, so should be good enough).
477
477
*/
478
- typedef struct {
478
+ typedef struct
479
+ {
479
480
List * args ; /* list of (T_String) Values or NIL */
480
481
Oid funcoids [3 ]; /* OIDs of trigger functions */
481
482
/* The three function OIDs are stored in the order update, delete, child */
@@ -486,7 +487,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
486
487
{
487
488
static List * info_list = NIL ;
488
489
489
- static const char * const funcdescr [3 ] = {
490
+ static const char * const funcdescr [3 ] = {
490
491
gettext_noop ("Found referenced table's UPDATE trigger." ),
491
492
gettext_noop ("Found referenced table's DELETE trigger." ),
492
493
gettext_noop ("Found referencing table's trigger." )
@@ -511,7 +512,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
511
512
i = 0 ;
512
513
foreach (l , stmt -> args )
513
514
{
514
- Value * arg = (Value * ) lfirst (l );
515
+ Value * arg = (Value * ) lfirst (l );
515
516
516
517
i ++ ;
517
518
if (i < 4 ) /* skip constraint and table names */
@@ -537,7 +538,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
537
538
i = 0 ;
538
539
foreach (l , fk_attrs )
539
540
{
540
- Value * arg = (Value * ) lfirst (l );
541
+ Value * arg = (Value * ) lfirst (l );
541
542
542
543
if (i ++ > 0 )
543
544
appendStringInfoChar (& buf , ',' );
@@ -548,7 +549,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
548
549
i = 0 ;
549
550
foreach (l , pk_attrs )
550
551
{
551
- Value * arg = (Value * ) lfirst (l );
552
+ Value * arg = (Value * ) lfirst (l );
552
553
553
554
if (i ++ > 0 )
554
555
appendStringInfoChar (& buf , ',' );
@@ -598,9 +599,9 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
598
599
MemoryContext oldContext ;
599
600
600
601
ereport (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 ])));
604
605
oldContext = MemoryContextSwitchTo (TopMemoryContext );
605
606
info = (OldTriggerInfo * ) palloc0 (sizeof (OldTriggerInfo ));
606
607
info -> args = copyObject (stmt -> args );
@@ -614,9 +615,9 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
614
615
{
615
616
/* Second trigger of set */
616
617
ereport (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 ])));
620
621
}
621
622
else
622
623
{
@@ -1184,8 +1185,8 @@ RelationBuildTriggers(Relation relation)
1184
1185
int i ;
1185
1186
1186
1187
val = DatumGetByteaP (fastgetattr (htup ,
1187
- Anum_pg_trigger_tgargs ,
1188
- tgrel -> rd_att , & isnull ));
1188
+ Anum_pg_trigger_tgargs ,
1189
+ tgrel -> rd_att , & isnull ));
1189
1190
if (isnull )
1190
1191
elog (ERROR , "tgargs is null in trigger for relation \"%s\"" ,
1191
1192
RelationGetRelationName (relation ));
@@ -1637,7 +1638,7 @@ ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
1637
1638
trigger -> tgenabled == TRIGGER_DISABLED )
1638
1639
continue ;
1639
1640
}
1640
- else /* ORIGIN or LOCAL role */
1641
+ else /* ORIGIN or LOCAL role */
1641
1642
{
1642
1643
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1643
1644
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1696,7 +1697,7 @@ ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
1696
1697
trigger -> tgenabled == TRIGGER_DISABLED )
1697
1698
continue ;
1698
1699
}
1699
- else /* ORIGIN or LOCAL role */
1700
+ else /* ORIGIN or LOCAL role */
1700
1701
{
1701
1702
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1702
1703
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1768,7 +1769,7 @@ ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
1768
1769
trigger -> tgenabled == TRIGGER_DISABLED )
1769
1770
continue ;
1770
1771
}
1771
- else /* ORIGIN or LOCAL role */
1772
+ else /* ORIGIN or LOCAL role */
1772
1773
{
1773
1774
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1774
1775
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1834,7 +1835,7 @@ ExecBRDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
1834
1835
trigger -> tgenabled == TRIGGER_DISABLED )
1835
1836
continue ;
1836
1837
}
1837
- else /* ORIGIN or LOCAL role */
1838
+ else /* ORIGIN or LOCAL role */
1838
1839
{
1839
1840
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1840
1841
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1919,7 +1920,7 @@ ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
1919
1920
trigger -> tgenabled == TRIGGER_DISABLED )
1920
1921
continue ;
1921
1922
}
1922
- else /* ORIGIN or LOCAL role */
1923
+ else /* ORIGIN or LOCAL role */
1923
1924
{
1924
1925
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1925
1926
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -1990,7 +1991,7 @@ ExecBRUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
1990
1991
trigger -> tgenabled == TRIGGER_DISABLED )
1991
1992
continue ;
1992
1993
}
1993
- else /* ORIGIN or LOCAL role */
1994
+ else /* ORIGIN or LOCAL role */
1994
1995
{
1995
1996
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
1996
1997
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -2669,7 +2670,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
2669
2670
trigdesc = rInfo -> ri_TrigDesc ;
2670
2671
finfo = rInfo -> ri_TrigFunctions ;
2671
2672
instr = rInfo -> ri_TrigInstrument ;
2672
- if (trigdesc == NULL ) /* should not happen */
2673
+ if (trigdesc == NULL ) /* should not happen */
2673
2674
elog (ERROR , "relation %u has no triggers" ,
2674
2675
event -> ate_relid );
2675
2676
}
@@ -2725,7 +2726,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
2725
2726
2726
2727
if (local_estate )
2727
2728
{
2728
- ListCell * l ;
2729
+ ListCell * l ;
2729
2730
2730
2731
foreach (l , estate -> es_trig_target_relations )
2731
2732
{
@@ -2905,8 +2906,8 @@ AfterTriggerFireDeferred(void)
2905
2906
ActiveSnapshot = CopySnapshot (GetTransactionSnapshot ());
2906
2907
2907
2908
/*
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.
2910
2911
*/
2911
2912
while (afterTriggerMarkEvents (events , NULL , false))
2912
2913
{
@@ -2940,13 +2941,13 @@ AfterTriggerEndXact(bool isCommit)
2940
2941
*
2941
2942
* Since all the info is in TopTransactionContext or children thereof, we
2942
2943
* 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!
2946
2947
*
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.)
2950
2951
*/
2951
2952
if (afterTriggers && afterTriggers -> event_cxt )
2952
2953
MemoryContextDelete (afterTriggers -> event_cxt );
@@ -2973,9 +2974,8 @@ AfterTriggerBeginSubXact(void)
2973
2974
2974
2975
/*
2975
2976
* 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.)
2979
2979
*/
2980
2980
while (my_level >= afterTriggers -> maxtransdepth )
2981
2981
{
@@ -3071,16 +3071,17 @@ AfterTriggerEndSubXact(bool isCommit)
3071
3071
afterTriggers -> state_stack [my_level ] = NULL ;
3072
3072
Assert (afterTriggers -> query_depth ==
3073
3073
afterTriggers -> depth_stack [my_level ]);
3074
+
3074
3075
/*
3075
3076
* It's entirely possible that the subxact created an event_cxt but
3076
3077
* 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.)
3084
3085
*/
3085
3086
if (afterTriggers -> cxt_stack [my_level ] &&
3086
3087
afterTriggers -> events .tail == afterTriggers -> events_stack [my_level ].tail )
@@ -3615,7 +3616,7 @@ AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event, bool row_trigger,
3615
3616
trigger -> tgenabled == TRIGGER_DISABLED )
3616
3617
continue ;
3617
3618
}
3618
- else /* ORIGIN or LOCAL role */
3619
+ else /* ORIGIN or LOCAL role */
3619
3620
{
3620
3621
if (trigger -> tgenabled == TRIGGER_FIRES_ON_REPLICA ||
3621
3622
trigger -> tgenabled == TRIGGER_DISABLED )
@@ -3668,10 +3669,10 @@ AfterTriggerSaveEvent(ResultRelInfo *relinfo, int event, bool row_trigger,
3668
3669
3669
3670
/*
3670
3671
* 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
3672
3673
* will go away if the subtransaction aborts.
3673
3674
*/
3674
- if (my_level > 1 ) /* subtransaction? */
3675
+ if (my_level > 1 ) /* subtransaction? */
3675
3676
{
3676
3677
Assert (my_level < afterTriggers -> maxtransdepth );
3677
3678
cxtptr = & afterTriggers -> cxt_stack [my_level ];
0 commit comments