1 /*-------------------------------------------------------------------------
4 * PostgreSQL TRIGGERs support code.
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/commands/trigger.c
12 *-------------------------------------------------------------------------
16 #include "access/genam.h"
17 #include "access/htup_details.h"
18 #include "access/relation.h"
19 #include "access/sysattr.h"
20 #include "access/table.h"
21 #include "access/tableam.h"
22 #include "access/xact.h"
23 #include "catalog/catalog.h"
24 #include "catalog/dependency.h"
25 #include "catalog/index.h"
26 #include "catalog/indexing.h"
27 #include "catalog/objectaccess.h"
28 #include "catalog/partition.h"
29 #include "catalog/pg_constraint.h"
30 #include "catalog/pg_inherits.h"
31 #include "catalog/pg_proc.h"
32 #include "catalog/pg_trigger.h"
33 #include "catalog/pg_type.h"
34 #include "commands/dbcommands.h"
35 #include "commands/defrem.h"
36 #include "commands/trigger.h"
37 #include "executor/executor.h"
38 #include "miscadmin.h"
39 #include "nodes/bitmapset.h"
40 #include "nodes/makefuncs.h"
41 #include "optimizer/optimizer.h"
42 #include "parser/parse_clause.h"
43 #include "parser/parse_collate.h"
44 #include "parser/parse_func.h"
45 #include "parser/parse_relation.h"
46 #include "parser/parsetree.h"
47 #include "partitioning/partdesc.h"
49 #include "rewrite/rewriteManip.h"
50 #include "storage/bufmgr.h"
51 #include "storage/lmgr.h"
52 #include "tcop/utility.h"
53 #include "utils/acl.h"
54 #include "utils/builtins.h"
55 #include "utils/bytea.h"
56 #include "utils/fmgroids.h"
57 #include "utils/inval.h"
58 #include "utils/lsyscache.h"
59 #include "utils/memutils.h"
60 #include "utils/rel.h"
61 #include "utils/snapmgr.h"
62 #include "utils/syscache.h"
63 #include "utils/tuplestore.h"
67 int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN;
69 /* How many levels deep into trigger execution are we? */
70 static int MyTriggerDepth = 0;
73 * Note that similar macros also exist in executor/execMain.c. There does not
74 * appear to be any good header to put them into, given the structures that
75 * they use, so we let them be duplicated. Be sure to update all if one needs
76 * to be changed, however.
78 #define GetAllUpdatedColumns(relinfo, estate) \
79 (bms_union(exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols, \
80 exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->extraUpdatedCols))
82 /* Local function prototypes */
83 static void ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid);
84 static void SetTriggerFlags(TriggerDesc *trigdesc, Trigger *trigger);
85 static bool GetTupleForTrigger(EState *estate,
87 ResultRelInfo *relinfo,
89 LockTupleMode lockmode,
90 TupleTableSlot *oldslot,
91 TupleTableSlot **newSlot);
92 static bool TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
93 Trigger *trigger, TriggerEvent event,
94 Bitmapset *modifiedCols,
95 TupleTableSlot *oldslot, TupleTableSlot *newslot);
96 static HeapTuple ExecCallTriggerFunc(TriggerData *trigdata,
99 Instrumentation *instr,
100 MemoryContext per_tuple_context);
101 static void AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
102 int event, bool row_trigger,
103 TupleTableSlot *oldtup, TupleTableSlot *newtup,
104 List *recheckIndexes, Bitmapset *modifiedCols,
105 TransitionCaptureState *transition_capture);
106 static void AfterTriggerEnlargeQueryState(void);
107 static bool before_stmt_triggers_fired(Oid relid, CmdType cmdType);
111 * Create a trigger. Returns the address of the created trigger.
113 * queryString is the source text of the CREATE TRIGGER command.
114 * This must be supplied if a whenClause is specified, else it can be NULL.
116 * relOid, if nonzero, is the relation on which the trigger should be
117 * created. If zero, the name provided in the statement will be looked up.
119 * refRelOid, if nonzero, is the relation to which the constraint trigger
120 * refers. If zero, the constraint relation name provided in the statement
121 * will be looked up as needed.
123 * constraintOid, if nonzero, says that this trigger is being created
124 * internally to implement that constraint. A suitable pg_depend entry will
125 * be made to link the trigger to that constraint. constraintOid is zero when
126 * executing a user-entered CREATE TRIGGER command. (For CREATE CONSTRAINT
127 * TRIGGER, we build a pg_constraint entry internally.)
129 * indexOid, if nonzero, is the OID of an index associated with the constraint.
130 * We do nothing with this except store it into pg_trigger.tgconstrindid;
131 * but when creating a trigger for a deferrable unique constraint on a
132 * partitioned table, its children are looked up. Note we don't cope with
133 * invalid indexes in that case.
135 * funcoid, if nonzero, is the OID of the function to invoke. When this is
136 * given, stmt->funcname is ignored.
138 * parentTriggerOid, if nonzero, is a trigger that begets this one; so that
139 * if that trigger is dropped, this one should be too. (This is passed as
140 * Invalid by most callers; it's set here when recursing on a partition.)
142 * If whenClause is passed, it is an already-transformed expression for
143 * WHEN. In this case, we ignore any that may come in stmt->whenClause.
145 * If isInternal is true then this is an internally-generated trigger.
146 * This argument sets the tgisinternal field of the pg_trigger entry, and
147 * if true causes us to modify the given trigger name to ensure uniqueness.
149 * When isInternal is not true we require ACL_TRIGGER permissions on the
150 * relation, as well as ACL_EXECUTE on the trigger function. For internal
151 * triggers the caller must apply any required permission checks.
153 * When called on partitioned tables, this function recurses to create the
154 * trigger on all the partitions, except if isInternal is true, in which
155 * case caller is expected to execute recursion on its own.
157 * Note: can return InvalidObjectAddress if we decided to not create a trigger
158 * at all, but a foreign-key constraint. This is a kluge for backwards
162 CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
163 Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
164 Oid funcoid, Oid parentTriggerOid, Node *whenClause,
165 bool isInternal, bool in_partition)
173 Datum values[Natts_pg_trigger];
174 bool nulls[Natts_pg_trigger];
182 Oid fargtypes[1]; /* dummy */
185 char internaltrigname[NAMEDATALEN];
187 Oid constrrelid = InvalidOid;
188 ObjectAddress myself,
190 char *oldtablename = NULL;
191 char *newtablename = NULL;
192 bool partition_recurse;
194 if (OidIsValid(relOid))
195 rel = table_open(relOid, ShareRowExclusiveLock);
197 rel = table_openrv(stmt->relation, ShareRowExclusiveLock);
200 * Triggers must be on tables or views, and there are additional
201 * relation-type-specific restrictions.
203 if (rel->rd_rel->relkind == RELKIND_RELATION)
205 /* Tables can't have INSTEAD OF triggers */
206 if (stmt->timing != TRIGGER_TYPE_BEFORE &&
207 stmt->timing != TRIGGER_TYPE_AFTER)
209 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
210 errmsg("\"%s\" is a table",
211 RelationGetRelationName(rel)),
212 errdetail("Tables cannot have INSTEAD OF triggers.")));
214 else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
216 /* Partitioned tables can't have INSTEAD OF triggers */
217 if (stmt->timing != TRIGGER_TYPE_BEFORE &&
218 stmt->timing != TRIGGER_TYPE_AFTER)
220 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
221 errmsg("\"%s\" is a table",
222 RelationGetRelationName(rel)),
223 errdetail("Tables cannot have INSTEAD OF triggers.")));
226 * FOR EACH ROW triggers have further restrictions
231 * BEFORE triggers FOR EACH ROW are forbidden, because they would
232 * allow the user to direct the row to another partition, which
233 * isn't implemented in the executor.
235 if (stmt->timing != TRIGGER_TYPE_AFTER)
237 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
238 errmsg("\"%s\" is a partitioned table",
239 RelationGetRelationName(rel)),
240 errdetail("Partitioned tables cannot have BEFORE / FOR EACH ROW triggers.")));
243 * Disallow use of transition tables.
245 * Note that we have another restriction about transition tables
246 * in partitions; search for 'has_superclass' below for an
247 * explanation. The check here is just to protect from the fact
248 * that if we allowed it here, the creation would succeed for a
249 * partitioned table with no partitions, but would be blocked by
250 * the other restriction when the first partition was created,
251 * which is very unfriendly behavior.
253 if (stmt->transitionRels != NIL)
255 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
256 errmsg("\"%s\" is a partitioned table",
257 RelationGetRelationName(rel)),
258 errdetail("Triggers on partitioned tables cannot have transition tables.")));
261 else if (rel->rd_rel->relkind == RELKIND_VIEW)
264 * Views can have INSTEAD OF triggers (which we check below are
265 * row-level), or statement-level BEFORE/AFTER triggers.
267 if (stmt->timing != TRIGGER_TYPE_INSTEAD && stmt->row)
269 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
270 errmsg("\"%s\" is a view",
271 RelationGetRelationName(rel)),
272 errdetail("Views cannot have row-level BEFORE or AFTER triggers.")));
273 /* Disallow TRUNCATE triggers on VIEWs */
274 if (TRIGGER_FOR_TRUNCATE(stmt->events))
276 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
277 errmsg("\"%s\" is a view",
278 RelationGetRelationName(rel)),
279 errdetail("Views cannot have TRUNCATE triggers.")));
281 else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
283 if (stmt->timing != TRIGGER_TYPE_BEFORE &&
284 stmt->timing != TRIGGER_TYPE_AFTER)
286 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
287 errmsg("\"%s\" is a foreign table",
288 RelationGetRelationName(rel)),
289 errdetail("Foreign tables cannot have INSTEAD OF triggers.")));
291 if (TRIGGER_FOR_TRUNCATE(stmt->events))
293 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
294 errmsg("\"%s\" is a foreign table",
295 RelationGetRelationName(rel)),
296 errdetail("Foreign tables cannot have TRUNCATE triggers.")));
299 * We disallow constraint triggers to protect the assumption that
300 * triggers on FKs can't be deferred. See notes with AfterTriggers
301 * data structures, below.
303 if (stmt->isconstraint)
305 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
306 errmsg("\"%s\" is a foreign table",
307 RelationGetRelationName(rel)),
308 errdetail("Foreign tables cannot have constraint triggers.")));
312 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
313 errmsg("\"%s\" is not a table or view",
314 RelationGetRelationName(rel))));
316 if (!allowSystemTableMods && IsSystemRelation(rel))
318 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
319 errmsg("permission denied: \"%s\" is a system catalog",
320 RelationGetRelationName(rel))));
322 if (stmt->isconstraint)
325 * We must take a lock on the target relation to protect against
326 * concurrent drop. It's not clear that AccessShareLock is strong
327 * enough, but we certainly need at least that much... otherwise, we
328 * might end up creating a pg_constraint entry referencing a
331 if (OidIsValid(refRelOid))
333 LockRelationOid(refRelOid, AccessShareLock);
334 constrrelid = refRelOid;
336 else if (stmt->constrrel != NULL)
337 constrrelid = RangeVarGetRelid(stmt->constrrel, AccessShareLock,
341 /* permission checks */
344 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
346 if (aclresult != ACLCHECK_OK)
347 aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
348 RelationGetRelationName(rel));
350 if (OidIsValid(constrrelid))
352 aclresult = pg_class_aclcheck(constrrelid, GetUserId(),
354 if (aclresult != ACLCHECK_OK)
355 aclcheck_error(aclresult, get_relkind_objtype(get_rel_relkind(constrrelid)),
356 get_rel_name(constrrelid));
361 * When called on a partitioned table to create a FOR EACH ROW trigger
362 * that's not internal, we create one trigger for each partition, too.
364 * For that, we'd better hold lock on all of them ahead of time.
366 partition_recurse = !isInternal && stmt->row &&
367 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
368 if (partition_recurse)
369 list_free(find_all_inheritors(RelationGetRelid(rel),
370 ShareRowExclusiveLock, NULL));
373 TRIGGER_CLEAR_TYPE(tgtype);
375 TRIGGER_SETT_ROW(tgtype);
376 tgtype |= stmt->timing;
377 tgtype |= stmt->events;
379 /* Disallow ROW-level TRUNCATE triggers */
380 if (TRIGGER_FOR_ROW(tgtype) && TRIGGER_FOR_TRUNCATE(tgtype))
382 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
383 errmsg("TRUNCATE FOR EACH ROW triggers are not supported")));
385 /* INSTEAD triggers must be row-level, and can't have WHEN or columns */
386 if (TRIGGER_FOR_INSTEAD(tgtype))
388 if (!TRIGGER_FOR_ROW(tgtype))
390 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
391 errmsg("INSTEAD OF triggers must be FOR EACH ROW")));
392 if (stmt->whenClause)
394 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
395 errmsg("INSTEAD OF triggers cannot have WHEN conditions")));
396 if (stmt->columns != NIL)
398 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
399 errmsg("INSTEAD OF triggers cannot have column lists")));
403 * We don't yet support naming ROW transition variables, but the parser
404 * recognizes the syntax so we can give a nicer message here.
406 * Per standard, REFERENCING TABLE names are only allowed on AFTER
407 * triggers. Per standard, REFERENCING ROW names are not allowed with FOR
408 * EACH STATEMENT. Per standard, each OLD/NEW, ROW/TABLE permutation is
409 * only allowed once. Per standard, OLD may not be specified when
410 * creating a trigger only for INSERT, and NEW may not be specified when
411 * creating a trigger only for DELETE.
413 * Notice that the standard allows an AFTER ... FOR EACH ROW trigger to
414 * reference both ROW and TABLE transition data.
416 if (stmt->transitionRels != NIL)
418 List *varList = stmt->transitionRels;
423 TriggerTransition *tt = lfirst_node(TriggerTransition, lc);
427 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
428 errmsg("ROW variable naming in the REFERENCING clause is not supported"),
429 errhint("Use OLD TABLE or NEW TABLE for naming transition tables.")));
432 * Because of the above test, we omit further ROW-related testing
433 * below. If we later allow naming OLD and NEW ROW variables,
434 * adjustments will be needed below.
437 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
439 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
440 errmsg("\"%s\" is a foreign table",
441 RelationGetRelationName(rel)),
442 errdetail("Triggers on foreign tables cannot have transition tables.")));
444 if (rel->rd_rel->relkind == RELKIND_VIEW)
446 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
447 errmsg("\"%s\" is a view",
448 RelationGetRelationName(rel)),
449 errdetail("Triggers on views cannot have transition tables.")));
452 * We currently don't allow row-level triggers with transition
453 * tables on partition or inheritance children. Such triggers
454 * would somehow need to see tuples converted to the format of the
455 * table they're attached to, and it's not clear which subset of
456 * tuples each child should see. See also the prohibitions in
457 * ATExecAttachPartition() and ATExecAddInherit().
459 if (TRIGGER_FOR_ROW(tgtype) && has_superclass(rel->rd_id))
461 /* Use appropriate error message. */
462 if (rel->rd_rel->relispartition)
464 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
465 errmsg("ROW triggers with transition tables are not supported on partitions")));
468 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
469 errmsg("ROW triggers with transition tables are not supported on inheritance children")));
472 if (stmt->timing != TRIGGER_TYPE_AFTER)
474 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
475 errmsg("transition table name can only be specified for an AFTER trigger")));
477 if (TRIGGER_FOR_TRUNCATE(tgtype))
479 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
480 errmsg("TRUNCATE triggers with transition tables are not supported")));
483 * We currently don't allow multi-event triggers ("INSERT OR
484 * UPDATE") with transition tables, because it's not clear how to
485 * handle INSERT ... ON CONFLICT statements which can fire both
486 * INSERT and UPDATE triggers. We show the inserted tuples to
487 * INSERT triggers and the updated tuples to UPDATE triggers, but
488 * it's not yet clear what INSERT OR UPDATE trigger should see.
489 * This restriction could be lifted if we can decide on the right
490 * semantics in a later release.
492 if (((TRIGGER_FOR_INSERT(tgtype) ? 1 : 0) +
493 (TRIGGER_FOR_UPDATE(tgtype) ? 1 : 0) +
494 (TRIGGER_FOR_DELETE(tgtype) ? 1 : 0)) != 1)
496 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
497 errmsg("transition tables cannot be specified for triggers with more than one event")));
500 * We currently don't allow column-specific triggers with
501 * transition tables. Per spec, that seems to require
502 * accumulating separate transition tables for each combination of
503 * columns, which is a lot of work for a rather marginal feature.
505 if (stmt->columns != NIL)
507 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508 errmsg("transition tables cannot be specified for triggers with column lists")));
511 * We disallow constraint triggers with transition tables, to
512 * protect the assumption that such triggers can't be deferred.
513 * See notes with AfterTriggers data structures, below.
515 * Currently this is enforced by the grammar, so just Assert here.
517 Assert(!stmt->isconstraint);
521 if (!(TRIGGER_FOR_INSERT(tgtype) ||
522 TRIGGER_FOR_UPDATE(tgtype)))
524 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
525 errmsg("NEW TABLE can only be specified for an INSERT or UPDATE trigger")));
527 if (newtablename != NULL)
529 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
530 errmsg("NEW TABLE cannot be specified multiple times")));
532 newtablename = tt->name;
536 if (!(TRIGGER_FOR_DELETE(tgtype) ||
537 TRIGGER_FOR_UPDATE(tgtype)))
539 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
540 errmsg("OLD TABLE can only be specified for a DELETE or UPDATE trigger")));
542 if (oldtablename != NULL)
544 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
545 errmsg("OLD TABLE cannot be specified multiple times")));
547 oldtablename = tt->name;
551 if (newtablename != NULL && oldtablename != NULL &&
552 strcmp(newtablename, oldtablename) == 0)
554 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
555 errmsg("OLD TABLE name and NEW TABLE name cannot be the same")));
559 * Parse the WHEN clause, if any and we weren't passed an already
562 * Note that as a side effect, we fill whenRtable when parsing. If we got
563 * an already parsed clause, this does not occur, which is what we want --
564 * no point in adding redundant dependencies below.
566 if (!whenClause && stmt->whenClause)
573 /* Set up a pstate to parse with */
574 pstate = make_parsestate(NULL);
575 pstate->p_sourcetext = queryString;
578 * Set up RTEs for OLD and NEW references.
580 * 'OLD' must always have varno equal to 1 and 'NEW' equal to 2.
582 rte = addRangeTableEntryForRelation(pstate, rel,
584 makeAlias("old", NIL),
586 addRTEtoQuery(pstate, rte, false, true, true);
587 rte = addRangeTableEntryForRelation(pstate, rel,
589 makeAlias("new", NIL),
591 addRTEtoQuery(pstate, rte, false, true, true);
593 /* Transform expression. Copy to be sure we don't modify original */
594 whenClause = transformWhereClause(pstate,
595 copyObject(stmt->whenClause),
596 EXPR_KIND_TRIGGER_WHEN,
598 /* we have to fix its collations too */
599 assign_expr_collations(pstate, whenClause);
602 * Check for disallowed references to OLD/NEW.
604 * NB: pull_var_clause is okay here only because we don't allow
605 * subselects in WHEN clauses; it would fail to examine the contents
608 varList = pull_var_clause(whenClause, 0);
611 Var *var = (Var *) lfirst(lc);
616 if (!TRIGGER_FOR_ROW(tgtype))
618 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
619 errmsg("statement trigger's WHEN condition cannot reference column values"),
620 parser_errposition(pstate, var->location)));
621 if (TRIGGER_FOR_INSERT(tgtype))
623 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
624 errmsg("INSERT trigger's WHEN condition cannot reference OLD values"),
625 parser_errposition(pstate, var->location)));
626 /* system columns are okay here */
629 if (!TRIGGER_FOR_ROW(tgtype))
631 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
632 errmsg("statement trigger's WHEN condition cannot reference column values"),
633 parser_errposition(pstate, var->location)));
634 if (TRIGGER_FOR_DELETE(tgtype))
636 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
637 errmsg("DELETE trigger's WHEN condition cannot reference NEW values"),
638 parser_errposition(pstate, var->location)));
639 if (var->varattno < 0 && TRIGGER_FOR_BEFORE(tgtype))
641 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
642 errmsg("BEFORE trigger's WHEN condition cannot reference NEW system columns"),
643 parser_errposition(pstate, var->location)));
644 if (TRIGGER_FOR_BEFORE(tgtype) &&
645 var->varattno == 0 &&
646 RelationGetDescr(rel)->constr &&
647 RelationGetDescr(rel)->constr->has_generated_stored)
649 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
650 errmsg("BEFORE trigger's WHEN condition cannot reference NEW generated columns"),
651 errdetail("A whole-row reference is used and the table contains generated columns."),
652 parser_errposition(pstate, var->location)));
653 if (TRIGGER_FOR_BEFORE(tgtype) &&
655 TupleDescAttr(RelationGetDescr(rel), var->varattno - 1)->attgenerated)
657 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
658 errmsg("BEFORE trigger's WHEN condition cannot reference NEW generated columns"),
659 errdetail("Column \"%s\" is a generated column.",
660 NameStr(TupleDescAttr(RelationGetDescr(rel), var->varattno - 1)->attname)),
661 parser_errposition(pstate, var->location)));
664 /* can't happen without add_missing_from, so just elog */
665 elog(ERROR, "trigger WHEN condition cannot contain references to other relations");
670 /* we'll need the rtable for recordDependencyOnExpr */
671 whenRtable = pstate->p_rtable;
673 qual = nodeToString(whenClause);
675 free_parsestate(pstate);
677 else if (!whenClause)
685 qual = nodeToString(whenClause);
690 * Find and validate the trigger function.
692 if (!OidIsValid(funcoid))
693 funcoid = LookupFuncName(stmt->funcname, 0, fargtypes, false);
696 aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
697 if (aclresult != ACLCHECK_OK)
698 aclcheck_error(aclresult, OBJECT_FUNCTION,
699 NameListToString(stmt->funcname));
701 funcrettype = get_func_rettype(funcoid);
702 if (funcrettype != TRIGGEROID)
705 * We allow OPAQUE just so we can load old dump files. When we see a
706 * trigger function declared OPAQUE, change it to TRIGGER.
708 if (funcrettype == OPAQUEOID)
711 (errmsg("changing return type of function %s from %s to %s",
712 NameListToString(stmt->funcname),
713 "opaque", "trigger")));
714 SetFunctionReturnType(funcoid, TRIGGEROID);
718 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
719 errmsg("function %s must return type %s",
720 NameListToString(stmt->funcname), "trigger")));
724 * If the command is a user-entered CREATE CONSTRAINT TRIGGER command that
725 * references one of the built-in RI_FKey trigger functions, assume it is
726 * from a dump of a pre-7.3 foreign key constraint, and take steps to
727 * convert this legacy representation into a regular foreign key
728 * constraint. Ugly, but necessary for loading old dump files.
730 if (stmt->isconstraint && !isInternal &&
731 list_length(stmt->args) >= 6 &&
732 (list_length(stmt->args) % 2) == 0 &&
733 RI_FKey_trigger_type(funcoid) != RI_TRIGGER_NONE)
735 /* Keep lock on target rel until end of xact */
736 table_close(rel, NoLock);
738 ConvertTriggerToFK(stmt, funcoid);
740 return InvalidObjectAddress;
744 * If it's a user-entered CREATE CONSTRAINT TRIGGER command, make a
745 * corresponding pg_constraint entry.
747 if (stmt->isconstraint && !OidIsValid(constraintOid))
749 /* Internal callers should have made their own constraints */
751 constraintOid = CreateConstraintEntry(stmt->trigname,
752 RelationGetNamespace(rel),
757 InvalidOid, /* no parent */
758 RelationGetRelid(rel),
759 NULL, /* no conkey */
762 InvalidOid, /* no domain */
763 InvalidOid, /* no index */
764 InvalidOid, /* no foreign key */
773 NULL, /* no exclusion */
774 NULL, /* no check constraint */
778 true, /* noinherit */
779 isInternal); /* is_internal */
783 * Generate the trigger's OID now, so that we can use it in the name if
786 tgrel = table_open(TriggerRelationId, RowExclusiveLock);
788 trigoid = GetNewOidWithIndex(tgrel, TriggerOidIndexId,
789 Anum_pg_trigger_oid);
792 * If trigger is internally generated, modify the provided trigger name to
793 * ensure uniqueness by appending the trigger OID. (Callers will usually
794 * supply a simple constant trigger name in these cases.)
798 snprintf(internaltrigname, sizeof(internaltrigname),
799 "%s_%u", stmt->trigname, trigoid);
800 trigname = internaltrigname;
804 /* user-defined trigger; use the specified trigger name as-is */
805 trigname = stmt->trigname;
809 * Scan pg_trigger for existing triggers on relation. We do this only to
810 * give a nice error message if there's already a trigger of the same
811 * name. (The unique index on tgrelid/tgname would complain anyway.) We
812 * can skip this for internally generated triggers, since the name
813 * modification above should be sufficient.
815 * NOTE that this is cool only because we have ShareRowExclusiveLock on
816 * the relation, so the trigger set won't be changing underneath us.
821 Anum_pg_trigger_tgrelid,
822 BTEqualStrategyNumber, F_OIDEQ,
823 ObjectIdGetDatum(RelationGetRelid(rel)));
824 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
826 while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
828 Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(tuple);
830 if (namestrcmp(&(pg_trigger->tgname), trigname) == 0)
832 (errcode(ERRCODE_DUPLICATE_OBJECT),
833 errmsg("trigger \"%s\" for relation \"%s\" already exists",
834 trigname, RelationGetRelationName(rel))));
836 systable_endscan(tgscan);
840 * Build the new pg_trigger tuple.
842 * When we're creating a trigger in a partition, we mark it as internal,
843 * even though we don't do the isInternal magic in this function. This
844 * makes the triggers in partitions identical to the ones in the
845 * partitioned tables, except that they are marked internal.
847 memset(nulls, false, sizeof(nulls));
849 values[Anum_pg_trigger_oid - 1] = ObjectIdGetDatum(trigoid);
850 values[Anum_pg_trigger_tgrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel));
851 values[Anum_pg_trigger_tgname - 1] = DirectFunctionCall1(namein,
852 CStringGetDatum(trigname));
853 values[Anum_pg_trigger_tgfoid - 1] = ObjectIdGetDatum(funcoid);
854 values[Anum_pg_trigger_tgtype - 1] = Int16GetDatum(tgtype);
855 values[Anum_pg_trigger_tgenabled - 1] = CharGetDatum(TRIGGER_FIRES_ON_ORIGIN);
856 values[Anum_pg_trigger_tgisinternal - 1] = BoolGetDatum(isInternal || in_partition);
857 values[Anum_pg_trigger_tgconstrrelid - 1] = ObjectIdGetDatum(constrrelid);
858 values[Anum_pg_trigger_tgconstrindid - 1] = ObjectIdGetDatum(indexOid);
859 values[Anum_pg_trigger_tgconstraint - 1] = ObjectIdGetDatum(constraintOid);
860 values[Anum_pg_trigger_tgdeferrable - 1] = BoolGetDatum(stmt->deferrable);
861 values[Anum_pg_trigger_tginitdeferred - 1] = BoolGetDatum(stmt->initdeferred);
867 int16 nargs = list_length(stmt->args);
870 foreach(le, stmt->args)
872 char *ar = strVal(lfirst(le));
874 len += strlen(ar) + 4;
881 args = (char *) palloc(len + 1);
883 foreach(le, stmt->args)
885 char *s = strVal(lfirst(le));
886 char *d = args + strlen(args);
896 values[Anum_pg_trigger_tgnargs - 1] = Int16GetDatum(nargs);
897 values[Anum_pg_trigger_tgargs - 1] = DirectFunctionCall1(byteain,
898 CStringGetDatum(args));
902 values[Anum_pg_trigger_tgnargs - 1] = Int16GetDatum(0);
903 values[Anum_pg_trigger_tgargs - 1] = DirectFunctionCall1(byteain,
904 CStringGetDatum(""));
907 /* build column number array if it's a column-specific trigger */
908 ncolumns = list_length(stmt->columns);
916 columns = (int16 *) palloc(ncolumns * sizeof(int16));
917 foreach(cell, stmt->columns)
919 char *name = strVal(lfirst(cell));
923 /* Lookup column name. System columns are not allowed */
924 attnum = attnameAttNum(rel, name, false);
925 if (attnum == InvalidAttrNumber)
927 (errcode(ERRCODE_UNDEFINED_COLUMN),
928 errmsg("column \"%s\" of relation \"%s\" does not exist",
929 name, RelationGetRelationName(rel))));
931 /* Check for duplicates */
932 for (j = i - 1; j >= 0; j--)
934 if (columns[j] == attnum)
936 (errcode(ERRCODE_DUPLICATE_COLUMN),
937 errmsg("column \"%s\" specified more than once",
941 columns[i++] = attnum;
944 tgattr = buildint2vector(columns, ncolumns);
945 values[Anum_pg_trigger_tgattr - 1] = PointerGetDatum(tgattr);
947 /* set tgqual if trigger has WHEN clause */
949 values[Anum_pg_trigger_tgqual - 1] = CStringGetTextDatum(qual);
951 nulls[Anum_pg_trigger_tgqual - 1] = true;
954 values[Anum_pg_trigger_tgoldtable - 1] = DirectFunctionCall1(namein,
955 CStringGetDatum(oldtablename));
957 nulls[Anum_pg_trigger_tgoldtable - 1] = true;
959 values[Anum_pg_trigger_tgnewtable - 1] = DirectFunctionCall1(namein,
960 CStringGetDatum(newtablename));
962 nulls[Anum_pg_trigger_tgnewtable - 1] = true;
964 tuple = heap_form_tuple(tgrel->rd_att, values, nulls);
967 * Insert tuple into pg_trigger.
969 CatalogTupleInsert(tgrel, tuple);
971 heap_freetuple(tuple);
972 table_close(tgrel, RowExclusiveLock);
974 pfree(DatumGetPointer(values[Anum_pg_trigger_tgname - 1]));
975 pfree(DatumGetPointer(values[Anum_pg_trigger_tgargs - 1]));
976 pfree(DatumGetPointer(values[Anum_pg_trigger_tgattr - 1]));
978 pfree(DatumGetPointer(values[Anum_pg_trigger_tgoldtable - 1]));
980 pfree(DatumGetPointer(values[Anum_pg_trigger_tgnewtable - 1]));
983 * Update relation's pg_class entry; if necessary; and if not, send an SI
984 * message to make other backends (and this one) rebuild relcache entries.
986 pgrel = table_open(RelationRelationId, RowExclusiveLock);
987 tuple = SearchSysCacheCopy1(RELOID,
988 ObjectIdGetDatum(RelationGetRelid(rel)));
989 if (!HeapTupleIsValid(tuple))
990 elog(ERROR, "cache lookup failed for relation %u",
991 RelationGetRelid(rel));
992 if (!((Form_pg_class) GETSTRUCT(tuple))->relhastriggers)
994 ((Form_pg_class) GETSTRUCT(tuple))->relhastriggers = true;
996 CatalogTupleUpdate(pgrel, &tuple->t_self, tuple);
998 CommandCounterIncrement();
1001 CacheInvalidateRelcacheByTuple(tuple);
1003 heap_freetuple(tuple);
1004 table_close(pgrel, RowExclusiveLock);
1007 * Record dependencies for trigger. Always place a normal dependency on
1010 myself.classId = TriggerRelationId;
1011 myself.objectId = trigoid;
1012 myself.objectSubId = 0;
1014 referenced.classId = ProcedureRelationId;
1015 referenced.objectId = funcoid;
1016 referenced.objectSubId = 0;
1017 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1019 if (isInternal && OidIsValid(constraintOid))
1022 * Internally-generated trigger for a constraint, so make it an
1023 * internal dependency of the constraint. We can skip depending on
1024 * the relation(s), as there'll be an indirect dependency via the
1027 referenced.classId = ConstraintRelationId;
1028 referenced.objectId = constraintOid;
1029 referenced.objectSubId = 0;
1030 recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
1035 * User CREATE TRIGGER, so place dependencies. We make trigger be
1036 * auto-dropped if its relation is dropped or if the FK relation is
1037 * dropped. (Auto drop is compatible with our pre-7.3 behavior.)
1039 referenced.classId = RelationRelationId;
1040 referenced.objectId = RelationGetRelid(rel);
1041 referenced.objectSubId = 0;
1042 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
1044 if (OidIsValid(constrrelid))
1046 referenced.classId = RelationRelationId;
1047 referenced.objectId = constrrelid;
1048 referenced.objectSubId = 0;
1049 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
1051 /* Not possible to have an index dependency in this case */
1052 Assert(!OidIsValid(indexOid));
1055 * If it's a user-specified constraint trigger, make the constraint
1056 * internally dependent on the trigger instead of vice versa.
1058 if (OidIsValid(constraintOid))
1060 referenced.classId = ConstraintRelationId;
1061 referenced.objectId = constraintOid;
1062 referenced.objectSubId = 0;
1063 recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL);
1067 * If it's a partition trigger, create the partition dependencies.
1069 if (OidIsValid(parentTriggerOid))
1071 ObjectAddressSet(referenced, TriggerRelationId, parentTriggerOid);
1072 recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_PRI);
1073 ObjectAddressSet(referenced, RelationRelationId, RelationGetRelid(rel));
1074 recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_SEC);
1078 /* If column-specific trigger, add normal dependencies on columns */
1079 if (columns != NULL)
1083 referenced.classId = RelationRelationId;
1084 referenced.objectId = RelationGetRelid(rel);
1085 for (i = 0; i < ncolumns; i++)
1087 referenced.objectSubId = columns[i];
1088 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1093 * If it has a WHEN clause, add dependencies on objects mentioned in the
1094 * expression (eg, functions, as well as any columns used).
1096 if (whenRtable != NIL)
1097 recordDependencyOnExpr(&myself, whenClause, whenRtable,
1100 /* Post creation hook for new trigger */
1101 InvokeObjectPostCreateHookArg(TriggerRelationId, trigoid, 0,
1105 * Lastly, create the trigger on child relations, if needed.
1107 if (partition_recurse)
1109 PartitionDesc partdesc = RelationGetPartitionDesc(rel);
1111 List *childTbls = NIL;
1114 MemoryContext oldcxt,
1117 perChildCxt = AllocSetContextCreate(CurrentMemoryContext,
1119 ALLOCSET_SMALL_SIZES);
1122 * When a trigger is being created associated with an index, we'll
1123 * need to associate the trigger in each child partition with the
1124 * corresponding index on it.
1126 if (OidIsValid(indexOid))
1131 idxs = find_inheritance_children(indexOid, ShareRowExclusiveLock);
1133 childTbls = lappend_oid(childTbls,
1134 IndexGetRelation(lfirst_oid(l),
1138 oldcxt = MemoryContextSwitchTo(perChildCxt);
1140 /* Iterate to create the trigger on each existing partition */
1141 for (i = 0; i < partdesc->nparts; i++)
1143 Oid indexOnChild = InvalidOid;
1145 CreateTrigStmt *childStmt;
1148 bool found_whole_row;
1150 childTbl = table_open(partdesc->oids[i], ShareRowExclusiveLock);
1152 /* Find which of the child indexes is the one on this partition */
1153 if (OidIsValid(indexOid))
1155 forboth(l, idxs, l2, childTbls)
1157 if (lfirst_oid(l2) == partdesc->oids[i])
1159 indexOnChild = lfirst_oid(l);
1163 if (!OidIsValid(indexOnChild))
1164 elog(ERROR, "failed to find index matching index \"%s\" in partition \"%s\"",
1165 get_rel_name(indexOid),
1166 get_rel_name(partdesc->oids[i]));
1170 * Initialize our fabricated parse node by copying the original
1171 * one, then resetting fields that we pass separately.
1173 childStmt = (CreateTrigStmt *) copyObject(stmt);
1174 childStmt->funcname = NIL;
1175 childStmt->whenClause = NULL;
1177 /* If there is a WHEN clause, create a modified copy of it */
1178 qual = copyObject(whenClause);
1180 map_partition_varattnos((List *) qual, PRS2_OLD_VARNO,
1183 if (found_whole_row)
1184 elog(ERROR, "unexpected whole-row reference found in trigger WHEN clause");
1186 map_partition_varattnos((List *) qual, PRS2_NEW_VARNO,
1189 if (found_whole_row)
1190 elog(ERROR, "unexpected whole-row reference found in trigger WHEN clause");
1192 CreateTrigger(childStmt, queryString,
1193 partdesc->oids[i], refRelOid,
1194 InvalidOid, indexOnChild,
1195 funcoid, trigoid, qual,
1198 table_close(childTbl, NoLock);
1200 MemoryContextReset(perChildCxt);
1203 MemoryContextSwitchTo(oldcxt);
1204 MemoryContextDelete(perChildCxt);
1206 list_free(childTbls);
1209 /* Keep lock on target rel until end of xact */
1210 table_close(rel, NoLock);
1217 * Convert legacy (pre-7.3) CREATE CONSTRAINT TRIGGER commands into
1218 * full-fledged foreign key constraints.
1220 * The conversion is complex because a pre-7.3 foreign key involved three
1221 * separate triggers, which were reported separately in dumps. While the
1222 * single trigger on the referencing table adds no new information, we need
1223 * to know the trigger functions of both of the triggers on the referenced
1224 * table to build the constraint declaration. Also, due to lack of proper
1225 * dependency checking pre-7.3, it is possible that the source database had
1226 * an incomplete set of triggers resulting in an only partially enforced
1227 * FK constraint. (This would happen if one of the tables had been dropped
1228 * and re-created, but only if the DB had been affected by a 7.0 pg_dump bug
1229 * that caused loss of tgconstrrelid information.) We choose to translate to
1230 * an FK constraint only when we've seen all three triggers of a set. This is
1231 * implemented by storing unmatched items in a list in TopMemoryContext.
1232 * We match triggers together by comparing the trigger arguments (which
1233 * include constraint name, table and column names, so should be good enough).
1237 List *args; /* list of (T_String) Values or NIL */
1238 Oid funcoids[3]; /* OIDs of trigger functions */
1239 /* The three function OIDs are stored in the order update, delete, child */
1243 ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
1245 static List *info_list = NIL;
1247 static const char *const funcdescr[3] = {
1248 gettext_noop("Found referenced table's UPDATE trigger."),
1249 gettext_noop("Found referenced table's DELETE trigger."),
1250 gettext_noop("Found referencing table's trigger.")
1254 char *fk_table_name;
1255 char *pk_table_name;
1256 char fk_matchtype = FKCONSTR_MATCH_SIMPLE;
1257 List *fk_attrs = NIL;
1258 List *pk_attrs = NIL;
1261 OldTriggerInfo *info = NULL;
1265 /* Parse out the trigger arguments */
1266 constr_name = strVal(linitial(stmt->args));
1267 fk_table_name = strVal(lsecond(stmt->args));
1268 pk_table_name = strVal(lthird(stmt->args));
1270 foreach(l, stmt->args)
1272 Value *arg = (Value *) lfirst(l);
1275 if (i < 4) /* skip constraint and table names */
1277 if (i == 4) /* handle match type */
1279 if (strcmp(strVal(arg), "FULL") == 0)
1280 fk_matchtype = FKCONSTR_MATCH_FULL;
1282 fk_matchtype = FKCONSTR_MATCH_SIMPLE;
1286 fk_attrs = lappend(fk_attrs, arg);
1288 pk_attrs = lappend(pk_attrs, arg);
1291 /* Prepare description of constraint for use in messages */
1292 initStringInfo(&buf);
1293 appendStringInfo(&buf, "FOREIGN KEY %s(",
1294 quote_identifier(fk_table_name));
1296 foreach(l, fk_attrs)
1298 Value *arg = (Value *) lfirst(l);
1301 appendStringInfoChar(&buf, ',');
1302 appendStringInfoString(&buf, quote_identifier(strVal(arg)));
1304 appendStringInfo(&buf, ") REFERENCES %s(",
1305 quote_identifier(pk_table_name));
1307 foreach(l, pk_attrs)
1309 Value *arg = (Value *) lfirst(l);
1312 appendStringInfoChar(&buf, ',');
1313 appendStringInfoString(&buf, quote_identifier(strVal(arg)));
1315 appendStringInfoChar(&buf, ')');
1317 /* Identify class of trigger --- update, delete, or referencing-table */
1320 case F_RI_FKEY_CASCADE_UPD:
1321 case F_RI_FKEY_RESTRICT_UPD:
1322 case F_RI_FKEY_SETNULL_UPD:
1323 case F_RI_FKEY_SETDEFAULT_UPD:
1324 case F_RI_FKEY_NOACTION_UPD:
1328 case F_RI_FKEY_CASCADE_DEL:
1329 case F_RI_FKEY_RESTRICT_DEL:
1330 case F_RI_FKEY_SETNULL_DEL:
1331 case F_RI_FKEY_SETDEFAULT_DEL:
1332 case F_RI_FKEY_NOACTION_DEL:
1341 /* See if we have a match to this trigger */
1342 foreach(l, info_list)
1344 info = (OldTriggerInfo *) lfirst(l);
1345 if (info->funcoids[funcnum] == InvalidOid &&
1346 equal(info->args, stmt->args))
1348 info->funcoids[funcnum] = funcoid;
1355 /* First trigger of set, so create a new list entry */
1356 MemoryContext oldContext;
1359 (errmsg("ignoring incomplete trigger group for constraint \"%s\" %s",
1360 constr_name, buf.data),
1361 errdetail_internal("%s", _(funcdescr[funcnum]))));
1362 oldContext = MemoryContextSwitchTo(TopMemoryContext);
1363 info = (OldTriggerInfo *) palloc0(sizeof(OldTriggerInfo));
1364 info->args = copyObject(stmt->args);
1365 info->funcoids[funcnum] = funcoid;
1366 info_list = lappend(info_list, info);
1367 MemoryContextSwitchTo(oldContext);
1369 else if (info->funcoids[0] == InvalidOid ||
1370 info->funcoids[1] == InvalidOid ||
1371 info->funcoids[2] == InvalidOid)
1373 /* Second trigger of set */
1375 (errmsg("ignoring incomplete trigger group for constraint \"%s\" %s",
1376 constr_name, buf.data),
1377 errdetail_internal("%s", _(funcdescr[funcnum]))));
1381 /* OK, we have a set, so make the FK constraint ALTER TABLE cmd */
1382 AlterTableStmt *atstmt = makeNode(AlterTableStmt);
1383 AlterTableCmd *atcmd = makeNode(AlterTableCmd);
1384 Constraint *fkcon = makeNode(Constraint);
1385 PlannedStmt *wrapper = makeNode(PlannedStmt);
1388 (errmsg("converting trigger group into constraint \"%s\" %s",
1389 constr_name, buf.data),
1390 errdetail_internal("%s", _(funcdescr[funcnum]))));
1391 fkcon->contype = CONSTR_FOREIGN;
1392 fkcon->location = -1;
1395 /* This trigger is on the FK table */
1396 atstmt->relation = stmt->relation;
1397 if (stmt->constrrel)
1398 fkcon->pktable = stmt->constrrel;
1401 /* Work around ancient pg_dump bug that omitted constrrel */
1402 fkcon->pktable = makeRangeVar(NULL, pk_table_name, -1);
1407 /* This trigger is on the PK table */
1408 fkcon->pktable = stmt->relation;
1409 if (stmt->constrrel)
1410 atstmt->relation = stmt->constrrel;
1413 /* Work around ancient pg_dump bug that omitted constrrel */
1414 atstmt->relation = makeRangeVar(NULL, fk_table_name, -1);
1417 atstmt->cmds = list_make1(atcmd);
1418 atstmt->relkind = OBJECT_TABLE;
1419 atcmd->subtype = AT_AddConstraint;
1420 atcmd->def = (Node *) fkcon;
1421 if (strcmp(constr_name, "<unnamed>") == 0)
1422 fkcon->conname = NULL;
1424 fkcon->conname = constr_name;
1425 fkcon->fk_attrs = fk_attrs;
1426 fkcon->pk_attrs = pk_attrs;
1427 fkcon->fk_matchtype = fk_matchtype;
1428 switch (info->funcoids[0])
1430 case F_RI_FKEY_NOACTION_UPD:
1431 fkcon->fk_upd_action = FKCONSTR_ACTION_NOACTION;
1433 case F_RI_FKEY_CASCADE_UPD:
1434 fkcon->fk_upd_action = FKCONSTR_ACTION_CASCADE;
1436 case F_RI_FKEY_RESTRICT_UPD:
1437 fkcon->fk_upd_action = FKCONSTR_ACTION_RESTRICT;
1439 case F_RI_FKEY_SETNULL_UPD:
1440 fkcon->fk_upd_action = FKCONSTR_ACTION_SETNULL;
1442 case F_RI_FKEY_SETDEFAULT_UPD:
1443 fkcon->fk_upd_action = FKCONSTR_ACTION_SETDEFAULT;
1446 /* can't get here because of earlier checks */
1447 elog(ERROR, "confused about RI update function");
1449 switch (info->funcoids[1])
1451 case F_RI_FKEY_NOACTION_DEL:
1452 fkcon->fk_del_action = FKCONSTR_ACTION_NOACTION;
1454 case F_RI_FKEY_CASCADE_DEL:
1455 fkcon->fk_del_action = FKCONSTR_ACTION_CASCADE;
1457 case F_RI_FKEY_RESTRICT_DEL:
1458 fkcon->fk_del_action = FKCONSTR_ACTION_RESTRICT;
1460 case F_RI_FKEY_SETNULL_DEL:
1461 fkcon->fk_del_action = FKCONSTR_ACTION_SETNULL;
1463 case F_RI_FKEY_SETDEFAULT_DEL:
1464 fkcon->fk_del_action = FKCONSTR_ACTION_SETDEFAULT;
1467 /* can't get here because of earlier checks */
1468 elog(ERROR, "confused about RI delete function");
1470 fkcon->deferrable = stmt->deferrable;
1471 fkcon->initdeferred = stmt->initdeferred;
1472 fkcon->skip_validation = false;
1473 fkcon->initially_valid = true;
1475 /* finally, wrap it in a dummy PlannedStmt */
1476 wrapper->commandType = CMD_UTILITY;
1477 wrapper->canSetTag = false;
1478 wrapper->utilityStmt = (Node *) atstmt;
1479 wrapper->stmt_location = -1;
1480 wrapper->stmt_len = -1;
1482 /* ... and execute it */
1483 ProcessUtility(wrapper,
1484 "(generated ALTER TABLE ADD FOREIGN KEY command)",
1485 PROCESS_UTILITY_SUBCOMMAND, NULL, NULL,
1486 None_Receiver, NULL);
1488 /* Remove the matched item from the list */
1489 info_list = list_delete_ptr(info_list, info);
1491 /* We leak the copied args ... not worth worrying about */
1496 * Guts of trigger deletion.
1499 RemoveTriggerById(Oid trigOid)
1503 ScanKeyData skey[1];
1508 tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1511 * Find the trigger to delete.
1513 ScanKeyInit(&skey[0],
1514 Anum_pg_trigger_oid,
1515 BTEqualStrategyNumber, F_OIDEQ,
1516 ObjectIdGetDatum(trigOid));
1518 tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
1521 tup = systable_getnext(tgscan);
1522 if (!HeapTupleIsValid(tup))
1523 elog(ERROR, "could not find tuple for trigger %u", trigOid);
1526 * Open and exclusive-lock the relation the trigger belongs to.
1528 relid = ((Form_pg_trigger) GETSTRUCT(tup))->tgrelid;
1530 rel = table_open(relid, AccessExclusiveLock);
1532 if (rel->rd_rel->relkind != RELKIND_RELATION &&
1533 rel->rd_rel->relkind != RELKIND_VIEW &&
1534 rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
1535 rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1537 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1538 errmsg("\"%s\" is not a table, view, or foreign table",
1539 RelationGetRelationName(rel))));
1541 if (!allowSystemTableMods && IsSystemRelation(rel))
1543 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1544 errmsg("permission denied: \"%s\" is a system catalog",
1545 RelationGetRelationName(rel))));
1548 * Delete the pg_trigger tuple.
1550 CatalogTupleDelete(tgrel, &tup->t_self);
1552 systable_endscan(tgscan);
1553 table_close(tgrel, RowExclusiveLock);
1556 * We do not bother to try to determine whether any other triggers remain,
1557 * which would be needed in order to decide whether it's safe to clear the
1558 * relation's relhastriggers. (In any case, there might be a concurrent
1559 * process adding new triggers.) Instead, just force a relcache inval to
1560 * make other backends (and this one too!) rebuild their relcache entries.
1561 * There's no great harm in leaving relhastriggers true even if there are
1564 CacheInvalidateRelcache(rel);
1566 /* Keep lock on trigger's rel until end of xact */
1567 table_close(rel, NoLock);
1571 * get_trigger_oid - Look up a trigger by name to find its OID.
1573 * If missing_ok is false, throw an error if trigger not found. If
1574 * true, just return InvalidOid.
1577 get_trigger_oid(Oid relid, const char *trigname, bool missing_ok)
1580 ScanKeyData skey[2];
1586 * Find the trigger, verify permissions, set up object address
1588 tgrel = table_open(TriggerRelationId, AccessShareLock);
1590 ScanKeyInit(&skey[0],
1591 Anum_pg_trigger_tgrelid,
1592 BTEqualStrategyNumber, F_OIDEQ,
1593 ObjectIdGetDatum(relid));
1594 ScanKeyInit(&skey[1],
1595 Anum_pg_trigger_tgname,
1596 BTEqualStrategyNumber, F_NAMEEQ,
1597 CStringGetDatum(trigname));
1599 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1602 tup = systable_getnext(tgscan);
1604 if (!HeapTupleIsValid(tup))
1608 (errcode(ERRCODE_UNDEFINED_OBJECT),
1609 errmsg("trigger \"%s\" for table \"%s\" does not exist",
1610 trigname, get_rel_name(relid))));
1615 oid = ((Form_pg_trigger) GETSTRUCT(tup))->oid;
1618 systable_endscan(tgscan);
1619 table_close(tgrel, AccessShareLock);
1624 * Perform permissions and integrity checks before acquiring a relation lock.
1627 RangeVarCallbackForRenameTrigger(const RangeVar *rv, Oid relid, Oid oldrelid,
1633 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1634 if (!HeapTupleIsValid(tuple))
1635 return; /* concurrently dropped */
1636 form = (Form_pg_class) GETSTRUCT(tuple);
1638 /* only tables and views can have triggers */
1639 if (form->relkind != RELKIND_RELATION && form->relkind != RELKIND_VIEW &&
1640 form->relkind != RELKIND_FOREIGN_TABLE &&
1641 form->relkind != RELKIND_PARTITIONED_TABLE)
1643 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1644 errmsg("\"%s\" is not a table, view, or foreign table",
1647 /* you must own the table to rename one of its triggers */
1648 if (!pg_class_ownercheck(relid, GetUserId()))
1649 aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname);
1650 if (!allowSystemTableMods && IsSystemClass(relid, form))
1652 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1653 errmsg("permission denied: \"%s\" is a system catalog",
1656 ReleaseSysCache(tuple);
1660 * renametrig - changes the name of a trigger on a relation
1662 * trigger name is changed in trigger catalog.
1663 * No record of the previous name is kept.
1665 * get proper relrelation from relation catalog (if not arg)
1666 * scan trigger catalog
1667 * for name conflict (within rel)
1668 * for original trigger (if not arg)
1669 * modify tgname in trigger tuple
1670 * update row in catalog
1673 renametrig(RenameStmt *stmt)
1682 ObjectAddress address;
1685 * Look up name, check permissions, and acquire lock (which we will NOT
1686 * release until end of transaction).
1688 relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
1690 RangeVarCallbackForRenameTrigger,
1693 /* Have lock already, so just need to build relcache entry. */
1694 targetrel = relation_open(relid, NoLock);
1697 * Scan pg_trigger twice for existing triggers on relation. We do this in
1698 * order to ensure a trigger does not exist with newname (The unique index
1699 * on tgrelid/tgname would complain anyway) and to ensure a trigger does
1700 * exist with oldname.
1702 * NOTE that this is cool only because we have AccessExclusiveLock on the
1703 * relation, so the trigger set won't be changing underneath us.
1705 tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1708 * First pass -- look for name conflict
1710 ScanKeyInit(&key[0],
1711 Anum_pg_trigger_tgrelid,
1712 BTEqualStrategyNumber, F_OIDEQ,
1713 ObjectIdGetDatum(relid));
1714 ScanKeyInit(&key[1],
1715 Anum_pg_trigger_tgname,
1716 BTEqualStrategyNumber, F_NAMEEQ,
1717 PointerGetDatum(stmt->newname));
1718 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1720 if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1722 (errcode(ERRCODE_DUPLICATE_OBJECT),
1723 errmsg("trigger \"%s\" for relation \"%s\" already exists",
1724 stmt->newname, RelationGetRelationName(targetrel))));
1725 systable_endscan(tgscan);
1728 * Second pass -- look for trigger existing with oldname and update
1730 ScanKeyInit(&key[0],
1731 Anum_pg_trigger_tgrelid,
1732 BTEqualStrategyNumber, F_OIDEQ,
1733 ObjectIdGetDatum(relid));
1734 ScanKeyInit(&key[1],
1735 Anum_pg_trigger_tgname,
1736 BTEqualStrategyNumber, F_NAMEEQ,
1737 PointerGetDatum(stmt->subname));
1738 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1740 if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1742 Form_pg_trigger trigform;
1745 * Update pg_trigger tuple with new tgname.
1747 tuple = heap_copytuple(tuple); /* need a modifiable copy */
1748 trigform = (Form_pg_trigger) GETSTRUCT(tuple);
1749 tgoid = trigform->oid;
1751 namestrcpy(&trigform->tgname,
1754 CatalogTupleUpdate(tgrel, &tuple->t_self, tuple);
1756 InvokeObjectPostAlterHook(TriggerRelationId,
1760 * Invalidate relation's relcache entry so that other backends (and
1761 * this one too!) are sent SI message to make them rebuild relcache
1762 * entries. (Ideally this should happen automatically...)
1764 CacheInvalidateRelcache(targetrel);
1769 (errcode(ERRCODE_UNDEFINED_OBJECT),
1770 errmsg("trigger \"%s\" for table \"%s\" does not exist",
1771 stmt->subname, RelationGetRelationName(targetrel))));
1774 ObjectAddressSet(address, TriggerRelationId, tgoid);
1776 systable_endscan(tgscan);
1778 table_close(tgrel, RowExclusiveLock);
1781 * Close rel, but keep exclusive lock!
1783 relation_close(targetrel, NoLock);
1790 * EnableDisableTrigger()
1792 * Called by ALTER TABLE ENABLE/DISABLE [ REPLICA | ALWAYS ] TRIGGER
1793 * to change 'tgenabled' field for the specified trigger(s)
1795 * rel: relation to process (caller must hold suitable lock on it)
1796 * tgname: trigger to process, or NULL to scan all triggers
1797 * fires_when: new value for tgenabled field. In addition to generic
1798 * enablement/disablement, this also defines when the trigger
1799 * should be fired in session replication roles.
1800 * skip_system: if true, skip "system" triggers (constraint triggers)
1802 * Caller should have checked permissions for the table; here we also
1803 * enforce that superuser privilege is required to alter the state of
1807 EnableDisableTrigger(Relation rel, const char *tgname,
1808 char fires_when, bool skip_system, LOCKMODE lockmode)
1812 ScanKeyData keys[2];
1818 /* Scan the relevant entries in pg_triggers */
1819 tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1821 ScanKeyInit(&keys[0],
1822 Anum_pg_trigger_tgrelid,
1823 BTEqualStrategyNumber, F_OIDEQ,
1824 ObjectIdGetDatum(RelationGetRelid(rel)));
1827 ScanKeyInit(&keys[1],
1828 Anum_pg_trigger_tgname,
1829 BTEqualStrategyNumber, F_NAMEEQ,
1830 CStringGetDatum(tgname));
1836 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1839 found = changed = false;
1841 while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1843 Form_pg_trigger oldtrig = (Form_pg_trigger) GETSTRUCT(tuple);
1845 if (oldtrig->tgisinternal)
1847 /* system trigger ... ok to process? */
1852 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1853 errmsg("permission denied: \"%s\" is a system trigger",
1854 NameStr(oldtrig->tgname))));
1859 if (oldtrig->tgenabled != fires_when)
1861 /* need to change this one ... make a copy to scribble on */
1862 HeapTuple newtup = heap_copytuple(tuple);
1863 Form_pg_trigger newtrig = (Form_pg_trigger) GETSTRUCT(newtup);
1865 newtrig->tgenabled = fires_when;
1867 CatalogTupleUpdate(tgrel, &newtup->t_self, newtup);
1869 heap_freetuple(newtup);
1872 * When altering FOR EACH ROW triggers on a partitioned table, do
1873 * the same on the partitions as well.
1875 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
1876 (TRIGGER_FOR_ROW(oldtrig->tgtype)))
1878 PartitionDesc partdesc = RelationGetPartitionDesc(rel);
1881 for (i = 0; i < partdesc->nparts; i++)
1885 part = relation_open(partdesc->oids[i], lockmode);
1886 EnableDisableTrigger(part, NameStr(oldtrig->tgname),
1887 fires_when, skip_system, lockmode);
1888 table_close(part, NoLock); /* keep lock till commit */
1895 InvokeObjectPostAlterHook(TriggerRelationId,
1899 systable_endscan(tgscan);
1901 table_close(tgrel, RowExclusiveLock);
1903 if (tgname && !found)
1905 (errcode(ERRCODE_UNDEFINED_OBJECT),
1906 errmsg("trigger \"%s\" for table \"%s\" does not exist",
1907 tgname, RelationGetRelationName(rel))));
1910 * If we changed anything, broadcast a SI inval message to force each
1911 * backend (including our own!) to rebuild relation's relcache entry.
1912 * Otherwise they will fail to apply the change promptly.
1915 CacheInvalidateRelcache(rel);
1920 * Build trigger data to attach to the given relcache entry.
1922 * Note that trigger data attached to a relcache entry must be stored in
1923 * CacheMemoryContext to ensure it survives as long as the relcache entry.
1924 * But we should be running in a less long-lived working context. To avoid
1925 * leaking cache memory if this routine fails partway through, we build a
1926 * temporary TriggerDesc in working memory and then copy the completed
1927 * structure into cache memory.
1930 RelationBuildTriggers(Relation relation)
1932 TriggerDesc *trigdesc;
1940 MemoryContext oldContext;
1944 * Allocate a working array to hold the triggers (the array is extended if
1948 triggers = (Trigger *) palloc(maxtrigs * sizeof(Trigger));
1952 * Note: since we scan the triggers using TriggerRelidNameIndexId, we will
1953 * be reading the triggers in name order, except possibly during
1954 * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
1955 * ensures that triggers will be fired in name order.
1958 Anum_pg_trigger_tgrelid,
1959 BTEqualStrategyNumber, F_OIDEQ,
1960 ObjectIdGetDatum(RelationGetRelid(relation)));
1962 tgrel = table_open(TriggerRelationId, AccessShareLock);
1963 tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1966 while (HeapTupleIsValid(htup = systable_getnext(tgscan)))
1968 Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(htup);
1973 if (numtrigs >= maxtrigs)
1976 triggers = (Trigger *) repalloc(triggers, maxtrigs * sizeof(Trigger));
1978 build = &(triggers[numtrigs]);
1980 build->tgoid = pg_trigger->oid;
1981 build->tgname = DatumGetCString(DirectFunctionCall1(nameout,
1982 NameGetDatum(&pg_trigger->tgname)));
1983 build->tgfoid = pg_trigger->tgfoid;
1984 build->tgtype = pg_trigger->tgtype;
1985 build->tgenabled = pg_trigger->tgenabled;
1986 build->tgisinternal = pg_trigger->tgisinternal;
1987 build->tgconstrrelid = pg_trigger->tgconstrrelid;
1988 build->tgconstrindid = pg_trigger->tgconstrindid;
1989 build->tgconstraint = pg_trigger->tgconstraint;
1990 build->tgdeferrable = pg_trigger->tgdeferrable;
1991 build->tginitdeferred = pg_trigger->tginitdeferred;
1992 build->tgnargs = pg_trigger->tgnargs;
1993 /* tgattr is first var-width field, so OK to access directly */
1994 build->tgnattr = pg_trigger->tgattr.dim1;
1995 if (build->tgnattr > 0)
1997 build->tgattr = (int16 *) palloc(build->tgnattr * sizeof(int16));
1998 memcpy(build->tgattr, &(pg_trigger->tgattr.values),
1999 build->tgnattr * sizeof(int16));
2002 build->tgattr = NULL;
2003 if (build->tgnargs > 0)
2008 val = DatumGetByteaPP(fastgetattr(htup,
2009 Anum_pg_trigger_tgargs,
2010 tgrel->rd_att, &isnull));
2012 elog(ERROR, "tgargs is null in trigger for relation \"%s\"",
2013 RelationGetRelationName(relation));
2014 p = (char *) VARDATA_ANY(val);
2015 build->tgargs = (char **) palloc(build->tgnargs * sizeof(char *));
2016 for (i = 0; i < build->tgnargs; i++)
2018 build->tgargs[i] = pstrdup(p);
2023 build->tgargs = NULL;
2025 datum = fastgetattr(htup, Anum_pg_trigger_tgoldtable,
2026 tgrel->rd_att, &isnull);
2029 DatumGetCString(DirectFunctionCall1(nameout, datum));
2031 build->tgoldtable = NULL;
2033 datum = fastgetattr(htup, Anum_pg_trigger_tgnewtable,
2034 tgrel->rd_att, &isnull);
2037 DatumGetCString(DirectFunctionCall1(nameout, datum));
2039 build->tgnewtable = NULL;
2041 datum = fastgetattr(htup, Anum_pg_trigger_tgqual,
2042 tgrel->rd_att, &isnull);
2044 build->tgqual = TextDatumGetCString(datum);
2046 build->tgqual = NULL;
2051 systable_endscan(tgscan);
2052 table_close(tgrel, AccessShareLock);
2054 /* There might not be any triggers */
2061 /* Build trigdesc */
2062 trigdesc = (TriggerDesc *) palloc0(sizeof(TriggerDesc));
2063 trigdesc->triggers = triggers;
2064 trigdesc->numtriggers = numtrigs;
2065 for (i = 0; i < numtrigs; i++)
2066 SetTriggerFlags(trigdesc, &(triggers[i]));
2068 /* Copy completed trigdesc into cache storage */
2069 oldContext = MemoryContextSwitchTo(CacheMemoryContext);
2070 relation->trigdesc = CopyTriggerDesc(trigdesc);
2071 MemoryContextSwitchTo(oldContext);
2073 /* Release working memory */
2074 FreeTriggerDesc(trigdesc);
2078 * Update the TriggerDesc's hint flags to include the specified trigger
2081 SetTriggerFlags(TriggerDesc *trigdesc, Trigger *trigger)
2083 int16 tgtype = trigger->tgtype;
2085 trigdesc->trig_insert_before_row |=
2086 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2087 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_INSERT);
2088 trigdesc->trig_insert_after_row |=
2089 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2090 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_INSERT);
2091 trigdesc->trig_insert_instead_row |=
2092 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2093 TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_INSERT);
2094 trigdesc->trig_insert_before_statement |=
2095 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2096 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_INSERT);
2097 trigdesc->trig_insert_after_statement |=
2098 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2099 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_INSERT);
2100 trigdesc->trig_update_before_row |=
2101 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2102 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_UPDATE);
2103 trigdesc->trig_update_after_row |=
2104 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2105 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_UPDATE);
2106 trigdesc->trig_update_instead_row |=
2107 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2108 TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_UPDATE);
2109 trigdesc->trig_update_before_statement |=
2110 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2111 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_UPDATE);
2112 trigdesc->trig_update_after_statement |=
2113 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2114 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_UPDATE);
2115 trigdesc->trig_delete_before_row |=
2116 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2117 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_DELETE);
2118 trigdesc->trig_delete_after_row |=
2119 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2120 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_DELETE);
2121 trigdesc->trig_delete_instead_row |=
2122 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2123 TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_DELETE);
2124 trigdesc->trig_delete_before_statement |=
2125 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2126 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_DELETE);
2127 trigdesc->trig_delete_after_statement |=
2128 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2129 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_DELETE);
2130 /* there are no row-level truncate triggers */
2131 trigdesc->trig_truncate_before_statement |=
2132 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2133 TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_TRUNCATE);
2134 trigdesc->trig_truncate_after_statement |=
2135 TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2136 TRIGGER_TYPE_AFTER, TRIGGER_TYPE_TRUNCATE);
2138 trigdesc->trig_insert_new_table |=
2139 (TRIGGER_FOR_INSERT(tgtype) &&
2140 TRIGGER_USES_TRANSITION_TABLE(trigger->tgnewtable));
2141 trigdesc->trig_update_old_table |=
2142 (TRIGGER_FOR_UPDATE(tgtype) &&
2143 TRIGGER_USES_TRANSITION_TABLE(trigger->tgoldtable));
2144 trigdesc->trig_update_new_table |=
2145 (TRIGGER_FOR_UPDATE(tgtype) &&
2146 TRIGGER_USES_TRANSITION_TABLE(trigger->tgnewtable));
2147 trigdesc->trig_delete_old_table |=
2148 (TRIGGER_FOR_DELETE(tgtype) &&
2149 TRIGGER_USES_TRANSITION_TABLE(trigger->tgoldtable));
2153 * Copy a TriggerDesc data structure.
2155 * The copy is allocated in the current memory context.
2158 CopyTriggerDesc(TriggerDesc *trigdesc)
2160 TriggerDesc *newdesc;
2164 if (trigdesc == NULL || trigdesc->numtriggers <= 0)
2167 newdesc = (TriggerDesc *) palloc(sizeof(TriggerDesc));
2168 memcpy(newdesc, trigdesc, sizeof(TriggerDesc));
2170 trigger = (Trigger *) palloc(trigdesc->numtriggers * sizeof(Trigger));
2171 memcpy(trigger, trigdesc->triggers,
2172 trigdesc->numtriggers * sizeof(Trigger));
2173 newdesc->triggers = trigger;
2175 for (i = 0; i < trigdesc->numtriggers; i++)
2177 trigger->tgname = pstrdup(trigger->tgname);
2178 if (trigger->tgnattr > 0)
2182 newattr = (int16 *) palloc(trigger->tgnattr * sizeof(int16));
2183 memcpy(newattr, trigger->tgattr,
2184 trigger->tgnattr * sizeof(int16));
2185 trigger->tgattr = newattr;
2187 if (trigger->tgnargs > 0)
2192 newargs = (char **) palloc(trigger->tgnargs * sizeof(char *));
2193 for (j = 0; j < trigger->tgnargs; j++)
2194 newargs[j] = pstrdup(trigger->tgargs[j]);
2195 trigger->tgargs = newargs;
2197 if (trigger->tgqual)
2198 trigger->tgqual = pstrdup(trigger->tgqual);
2199 if (trigger->tgoldtable)
2200 trigger->tgoldtable = pstrdup(trigger->tgoldtable);
2201 if (trigger->tgnewtable)
2202 trigger->tgnewtable = pstrdup(trigger->tgnewtable);
2210 * Free a TriggerDesc data structure.
2213 FreeTriggerDesc(TriggerDesc *trigdesc)
2218 if (trigdesc == NULL)
2221 trigger = trigdesc->triggers;
2222 for (i = 0; i < trigdesc->numtriggers; i++)
2224 pfree(trigger->tgname);
2225 if (trigger->tgnattr > 0)
2226 pfree(trigger->tgattr);
2227 if (trigger->tgnargs > 0)
2229 while (--(trigger->tgnargs) >= 0)
2230 pfree(trigger->tgargs[trigger->tgnargs]);
2231 pfree(trigger->tgargs);
2233 if (trigger->tgqual)
2234 pfree(trigger->tgqual);
2235 if (trigger->tgoldtable)
2236 pfree(trigger->tgoldtable);
2237 if (trigger->tgnewtable)
2238 pfree(trigger->tgnewtable);
2241 pfree(trigdesc->triggers);
2246 * Compare two TriggerDesc structures for logical equality.
2250 equalTriggerDescs(TriggerDesc *trigdesc1, TriggerDesc *trigdesc2)
2256 * We need not examine the hint flags, just the trigger array itself; if
2257 * we have the same triggers with the same types, the flags should match.
2259 * As of 7.3 we assume trigger set ordering is significant in the
2260 * comparison; so we just compare corresponding slots of the two sets.
2262 * Note: comparing the stringToNode forms of the WHEN clauses means that
2263 * parse column locations will affect the result. This is okay as long as
2264 * this function is only used for detecting exact equality, as for example
2265 * in checking for staleness of a cache entry.
2267 if (trigdesc1 != NULL)
2269 if (trigdesc2 == NULL)
2271 if (trigdesc1->numtriggers != trigdesc2->numtriggers)
2273 for (i = 0; i < trigdesc1->numtriggers; i++)
2275 Trigger *trig1 = trigdesc1->triggers + i;
2276 Trigger *trig2 = trigdesc2->triggers + i;
2278 if (trig1->tgoid != trig2->tgoid)
2280 if (strcmp(trig1->tgname, trig2->tgname) != 0)
2282 if (trig1->tgfoid != trig2->tgfoid)
2284 if (trig1->tgtype != trig2->tgtype)
2286 if (trig1->tgenabled != trig2->tgenabled)
2288 if (trig1->tgisinternal != trig2->tgisinternal)
2290 if (trig1->tgconstrrelid != trig2->tgconstrrelid)
2292 if (trig1->tgconstrindid != trig2->tgconstrindid)
2294 if (trig1->tgconstraint != trig2->tgconstraint)
2296 if (trig1->tgdeferrable != trig2->tgdeferrable)
2298 if (trig1->tginitdeferred != trig2->tginitdeferred)
2300 if (trig1->tgnargs != trig2->tgnargs)
2302 if (trig1->tgnattr != trig2->tgnattr)
2304 if (trig1->tgnattr > 0 &&
2305 memcmp(trig1->tgattr, trig2->tgattr,
2306 trig1->tgnattr * sizeof(int16)) != 0)
2308 for (j = 0; j < trig1->tgnargs; j++)
2309 if (strcmp(trig1->tgargs[j], trig2->tgargs[j]) != 0)
2311 if (trig1->tgqual == NULL && trig2->tgqual == NULL)
2313 else if (trig1->tgqual == NULL || trig2->tgqual == NULL)
2315 else if (strcmp(trig1->tgqual, trig2->tgqual) != 0)
2317 if (trig1->tgoldtable == NULL && trig2->tgoldtable == NULL)
2319 else if (trig1->tgoldtable == NULL || trig2->tgoldtable == NULL)
2321 else if (strcmp(trig1->tgoldtable, trig2->tgoldtable) != 0)
2323 if (trig1->tgnewtable == NULL && trig2->tgnewtable == NULL)
2325 else if (trig1->tgnewtable == NULL || trig2->tgnewtable == NULL)
2327 else if (strcmp(trig1->tgnewtable, trig2->tgnewtable) != 0)
2331 else if (trigdesc2 != NULL)
2335 #endif /* NOT_USED */
2338 * Check if there is a row-level trigger with transition tables that prevents
2339 * a table from becoming an inheritance child or partition. Return the name
2340 * of the first such incompatible trigger, or NULL if there is none.
2343 FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
2345 if (trigdesc != NULL)
2349 for (i = 0; i < trigdesc->numtriggers; ++i)
2351 Trigger *trigger = &trigdesc->triggers[i];
2353 if (trigger->tgoldtable != NULL || trigger->tgnewtable != NULL)
2354 return trigger->tgname;
2362 * Call a trigger function.
2364 * trigdata: trigger descriptor.
2365 * tgindx: trigger's index in finfo and instr arrays.
2366 * finfo: array of cached trigger function call information.
2367 * instr: optional array of EXPLAIN ANALYZE instrumentation state.
2368 * per_tuple_context: memory context to execute the function in.
2370 * Returns the tuple (or NULL) as returned by the function.
2373 ExecCallTriggerFunc(TriggerData *trigdata,
2376 Instrumentation *instr,
2377 MemoryContext per_tuple_context)
2379 LOCAL_FCINFO(fcinfo, 0);
2380 PgStat_FunctionCallUsage fcusage;
2382 MemoryContext oldContext;
2385 * Protect against code paths that may fail to initialize transition table
2388 Assert(((TRIGGER_FIRED_BY_INSERT(trigdata->tg_event) ||
2389 TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event) ||
2390 TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)) &&
2391 TRIGGER_FIRED_AFTER(trigdata->tg_event) &&
2392 !(trigdata->tg_event & AFTER_TRIGGER_DEFERRABLE) &&
2393 !(trigdata->tg_event & AFTER_TRIGGER_INITDEFERRED)) ||
2394 (trigdata->tg_oldtable == NULL && trigdata->tg_newtable == NULL));
2399 * We cache fmgr lookup info, to avoid making the lookup again on each
2402 if (finfo->fn_oid == InvalidOid)
2403 fmgr_info(trigdata->tg_trigger->tgfoid, finfo);
2405 Assert(finfo->fn_oid == trigdata->tg_trigger->tgfoid);
2408 * If doing EXPLAIN ANALYZE, start charging time to this trigger.
2411 InstrStartNode(instr + tgindx);
2414 * Do the function evaluation in the per-tuple memory context, so that
2415 * leaked memory will be reclaimed once per tuple. Note in particular that
2416 * any new tuple created by the trigger function will live till the end of
2419 oldContext = MemoryContextSwitchTo(per_tuple_context);
2422 * Call the function, passing no arguments but setting a context.
2424 InitFunctionCallInfoData(*fcinfo, finfo, 0,
2425 InvalidOid, (Node *) trigdata, NULL);
2427 pgstat_init_function_usage(fcinfo, &fcusage);
2432 result = FunctionCallInvoke(fcinfo);
2442 pgstat_end_function_usage(&fcusage, true);
2444 MemoryContextSwitchTo(oldContext);
2447 * Trigger protocol allows function to return a null pointer, but NOT to
2448 * set the isnull result flag.
2452 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2453 errmsg("trigger function %u returned null value",
2454 fcinfo->flinfo->fn_oid)));
2457 * If doing EXPLAIN ANALYZE, stop charging time to this trigger, and count
2458 * one "tuple returned" (really the number of firings).
2461 InstrStopNode(instr + tgindx, 1);
2463 return (HeapTuple) DatumGetPointer(result);
2467 ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
2469 TriggerDesc *trigdesc;
2471 TriggerData LocTriggerData;
2473 trigdesc = relinfo->ri_TrigDesc;
2475 if (trigdesc == NULL)
2477 if (!trigdesc->trig_insert_before_statement)
2480 /* no-op if we already fired BS triggers in this context */
2481 if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2485 LocTriggerData.type = T_TriggerData;
2486 LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2487 TRIGGER_EVENT_BEFORE;
2488 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2489 LocTriggerData.tg_trigtuple = NULL;
2490 LocTriggerData.tg_newtuple = NULL;
2491 LocTriggerData.tg_trigslot = NULL;
2492 LocTriggerData.tg_newslot = NULL;
2493 LocTriggerData.tg_oldtable = NULL;
2494 LocTriggerData.tg_newtable = NULL;
2495 for (i = 0; i < trigdesc->numtriggers; i++)
2497 Trigger *trigger = &trigdesc->triggers[i];
2500 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2501 TRIGGER_TYPE_STATEMENT,
2502 TRIGGER_TYPE_BEFORE,
2503 TRIGGER_TYPE_INSERT))
2505 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2509 LocTriggerData.tg_trigger = trigger;
2510 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2512 relinfo->ri_TrigFunctions,
2513 relinfo->ri_TrigInstrument,
2514 GetPerTupleMemoryContext(estate));
2518 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2519 errmsg("BEFORE STATEMENT trigger cannot return a value")));
2524 ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2525 TransitionCaptureState *transition_capture)
2527 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2529 if (trigdesc && trigdesc->trig_insert_after_statement)
2530 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_INSERT,
2531 false, NULL, NULL, NIL, NULL, transition_capture);
2535 ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2536 TupleTableSlot *slot)
2538 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2539 HeapTuple newtuple = false;
2541 TriggerData LocTriggerData;
2544 LocTriggerData.type = T_TriggerData;
2545 LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2547 TRIGGER_EVENT_BEFORE;
2548 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2549 LocTriggerData.tg_trigtuple = NULL;
2550 LocTriggerData.tg_newtuple = NULL;
2551 LocTriggerData.tg_trigslot = NULL;
2552 LocTriggerData.tg_newslot = NULL;
2553 LocTriggerData.tg_oldtable = NULL;
2554 LocTriggerData.tg_newtable = NULL;
2555 for (i = 0; i < trigdesc->numtriggers; i++)
2557 Trigger *trigger = &trigdesc->triggers[i];
2560 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2562 TRIGGER_TYPE_BEFORE,
2563 TRIGGER_TYPE_INSERT))
2565 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2570 newtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2572 LocTriggerData.tg_trigslot = slot;
2573 LocTriggerData.tg_trigtuple = oldtuple = newtuple;
2574 LocTriggerData.tg_trigger = trigger;
2575 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2577 relinfo->ri_TrigFunctions,
2578 relinfo->ri_TrigInstrument,
2579 GetPerTupleMemoryContext(estate));
2580 if (newtuple == NULL)
2583 heap_freetuple(oldtuple);
2584 return false; /* "do nothing" */
2586 else if (newtuple != oldtuple)
2588 ExecForceStoreHeapTuple(newtuple, slot, false);
2591 heap_freetuple(oldtuple);
2593 /* signal tuple should be re-fetched if used */
2602 ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2603 TupleTableSlot *slot, List *recheckIndexes,
2604 TransitionCaptureState *transition_capture)
2606 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2608 if ((trigdesc && trigdesc->trig_insert_after_row) ||
2609 (transition_capture && transition_capture->tcs_insert_new_table))
2610 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_INSERT,
2612 recheckIndexes, NULL,
2613 transition_capture);
2617 ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2618 TupleTableSlot *slot)
2620 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2621 HeapTuple newtuple = NULL;
2623 TriggerData LocTriggerData;
2626 LocTriggerData.type = T_TriggerData;
2627 LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2629 TRIGGER_EVENT_INSTEAD;
2630 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2631 LocTriggerData.tg_trigtuple = NULL;
2632 LocTriggerData.tg_newtuple = NULL;
2633 LocTriggerData.tg_trigslot = NULL;
2634 LocTriggerData.tg_newslot = NULL;
2635 LocTriggerData.tg_oldtable = NULL;
2636 LocTriggerData.tg_newtable = NULL;
2637 for (i = 0; i < trigdesc->numtriggers; i++)
2639 Trigger *trigger = &trigdesc->triggers[i];
2642 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2644 TRIGGER_TYPE_INSTEAD,
2645 TRIGGER_TYPE_INSERT))
2647 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2652 newtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2654 LocTriggerData.tg_trigslot = slot;
2655 LocTriggerData.tg_trigtuple = oldtuple = newtuple;
2656 LocTriggerData.tg_trigger = trigger;
2657 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2659 relinfo->ri_TrigFunctions,
2660 relinfo->ri_TrigInstrument,
2661 GetPerTupleMemoryContext(estate));
2662 if (newtuple == NULL)
2665 heap_freetuple(oldtuple);
2666 return false; /* "do nothing" */
2668 else if (newtuple != oldtuple)
2670 ExecForceStoreHeapTuple(newtuple, slot, false);
2673 heap_freetuple(oldtuple);
2675 /* signal tuple should be re-fetched if used */
2684 ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
2686 TriggerDesc *trigdesc;
2688 TriggerData LocTriggerData;
2690 trigdesc = relinfo->ri_TrigDesc;
2692 if (trigdesc == NULL)
2694 if (!trigdesc->trig_delete_before_statement)
2697 /* no-op if we already fired BS triggers in this context */
2698 if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2702 LocTriggerData.type = T_TriggerData;
2703 LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2704 TRIGGER_EVENT_BEFORE;
2705 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2706 LocTriggerData.tg_trigtuple = NULL;
2707 LocTriggerData.tg_newtuple = NULL;
2708 LocTriggerData.tg_trigslot = NULL;
2709 LocTriggerData.tg_newslot = NULL;
2710 LocTriggerData.tg_oldtable = NULL;
2711 LocTriggerData.tg_newtable = NULL;
2712 for (i = 0; i < trigdesc->numtriggers; i++)
2714 Trigger *trigger = &trigdesc->triggers[i];
2717 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2718 TRIGGER_TYPE_STATEMENT,
2719 TRIGGER_TYPE_BEFORE,
2720 TRIGGER_TYPE_DELETE))
2722 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2726 LocTriggerData.tg_trigger = trigger;
2727 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2729 relinfo->ri_TrigFunctions,
2730 relinfo->ri_TrigInstrument,
2731 GetPerTupleMemoryContext(estate));
2735 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2736 errmsg("BEFORE STATEMENT trigger cannot return a value")));
2741 ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
2742 TransitionCaptureState *transition_capture)
2744 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2746 if (trigdesc && trigdesc->trig_delete_after_statement)
2747 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_DELETE,
2748 false, NULL, NULL, NIL, NULL, transition_capture);
2752 * Execute BEFORE ROW DELETE triggers.
2754 * True indicates caller can proceed with the delete. False indicates caller
2755 * need to suppress the delete and additionally if requested, we need to pass
2756 * back the concurrently updated tuple if any.
2759 ExecBRDeleteTriggers(EState *estate, EPQState *epqstate,
2760 ResultRelInfo *relinfo,
2761 ItemPointer tupleid,
2762 HeapTuple fdw_trigtuple,
2763 TupleTableSlot **epqslot)
2765 TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
2766 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2768 TriggerData LocTriggerData;
2769 HeapTuple trigtuple;
2770 bool should_free = false;
2773 Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
2774 if (fdw_trigtuple == NULL)
2776 TupleTableSlot *epqslot_candidate = NULL;
2778 if (!GetTupleForTrigger(estate, epqstate, relinfo, tupleid,
2779 LockTupleExclusive, slot, &epqslot_candidate))
2783 * If the tuple was concurrently updated and the caller of this
2784 * function requested for the updated tuple, skip the trigger
2787 if (epqslot_candidate != NULL && epqslot != NULL)
2789 *epqslot = epqslot_candidate;
2793 trigtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2798 trigtuple = fdw_trigtuple;
2799 ExecForceStoreHeapTuple(trigtuple, slot, false);
2802 LocTriggerData.type = T_TriggerData;
2803 LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2805 TRIGGER_EVENT_BEFORE;
2806 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2807 LocTriggerData.tg_trigtuple = NULL;
2808 LocTriggerData.tg_newtuple = NULL;
2809 LocTriggerData.tg_trigslot = NULL;
2810 LocTriggerData.tg_newslot = NULL;
2811 LocTriggerData.tg_oldtable = NULL;
2812 LocTriggerData.tg_newtable = NULL;
2813 for (i = 0; i < trigdesc->numtriggers; i++)
2816 Trigger *trigger = &trigdesc->triggers[i];
2818 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2820 TRIGGER_TYPE_BEFORE,
2821 TRIGGER_TYPE_DELETE))
2823 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2827 LocTriggerData.tg_trigslot = slot;
2828 LocTriggerData.tg_trigtuple = trigtuple;
2829 LocTriggerData.tg_trigger = trigger;
2830 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2832 relinfo->ri_TrigFunctions,
2833 relinfo->ri_TrigInstrument,
2834 GetPerTupleMemoryContext(estate));
2835 if (newtuple == NULL)
2837 result = false; /* tell caller to suppress delete */
2840 if (newtuple != trigtuple)
2841 heap_freetuple(newtuple);
2844 heap_freetuple(trigtuple);
2850 ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
2851 ItemPointer tupleid,
2852 HeapTuple fdw_trigtuple,
2853 TransitionCaptureState *transition_capture)
2855 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2856 TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
2858 if ((trigdesc && trigdesc->trig_delete_after_row) ||
2859 (transition_capture && transition_capture->tcs_delete_old_table))
2861 Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
2862 if (fdw_trigtuple == NULL)
2863 GetTupleForTrigger(estate,
2871 ExecForceStoreHeapTuple(fdw_trigtuple, slot, false);
2873 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_DELETE,
2874 true, slot, NULL, NIL, NULL,
2875 transition_capture);
2880 ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
2881 HeapTuple trigtuple)
2883 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2884 TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
2885 TriggerData LocTriggerData;
2888 LocTriggerData.type = T_TriggerData;
2889 LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2891 TRIGGER_EVENT_INSTEAD;
2892 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2893 LocTriggerData.tg_trigtuple = NULL;
2894 LocTriggerData.tg_newtuple = NULL;
2895 LocTriggerData.tg_trigslot = NULL;
2896 LocTriggerData.tg_newslot = NULL;
2897 LocTriggerData.tg_oldtable = NULL;
2898 LocTriggerData.tg_newtable = NULL;
2900 ExecForceStoreHeapTuple(trigtuple, slot, false);
2902 for (i = 0; i < trigdesc->numtriggers; i++)
2905 Trigger *trigger = &trigdesc->triggers[i];
2907 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2909 TRIGGER_TYPE_INSTEAD,
2910 TRIGGER_TYPE_DELETE))
2912 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2916 LocTriggerData.tg_trigslot = slot;
2917 LocTriggerData.tg_trigtuple = trigtuple;
2918 LocTriggerData.tg_trigger = trigger;
2919 rettuple = ExecCallTriggerFunc(&LocTriggerData,
2921 relinfo->ri_TrigFunctions,
2922 relinfo->ri_TrigInstrument,
2923 GetPerTupleMemoryContext(estate));
2924 if (rettuple == NULL)
2925 return false; /* Delete was suppressed */
2926 if (rettuple != trigtuple)
2927 heap_freetuple(rettuple);
2933 ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
2935 TriggerDesc *trigdesc;
2937 TriggerData LocTriggerData;
2938 Bitmapset *updatedCols;
2940 trigdesc = relinfo->ri_TrigDesc;
2942 if (trigdesc == NULL)
2944 if (!trigdesc->trig_update_before_statement)
2947 /* no-op if we already fired BS triggers in this context */
2948 if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2952 updatedCols = GetAllUpdatedColumns(relinfo, estate);
2954 LocTriggerData.type = T_TriggerData;
2955 LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
2956 TRIGGER_EVENT_BEFORE;
2957 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2958 LocTriggerData.tg_trigtuple = NULL;
2959 LocTriggerData.tg_newtuple = NULL;
2960 LocTriggerData.tg_trigslot = NULL;
2961 LocTriggerData.tg_newslot = NULL;
2962 LocTriggerData.tg_oldtable = NULL;
2963 LocTriggerData.tg_newtable = NULL;
2964 for (i = 0; i < trigdesc->numtriggers; i++)
2966 Trigger *trigger = &trigdesc->triggers[i];
2969 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2970 TRIGGER_TYPE_STATEMENT,
2971 TRIGGER_TYPE_BEFORE,
2972 TRIGGER_TYPE_UPDATE))
2974 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2975 updatedCols, NULL, NULL))
2978 LocTriggerData.tg_trigger = trigger;
2979 newtuple = ExecCallTriggerFunc(&LocTriggerData,
2981 relinfo->ri_TrigFunctions,
2982 relinfo->ri_TrigInstrument,
2983 GetPerTupleMemoryContext(estate));
2987 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2988 errmsg("BEFORE STATEMENT trigger cannot return a value")));
2993 ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
2994 TransitionCaptureState *transition_capture)
2996 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2998 if (trigdesc && trigdesc->trig_update_after_statement)
2999 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_UPDATE,
3000 false, NULL, NULL, NIL,
3001 GetAllUpdatedColumns(relinfo, estate),
3002 transition_capture);
3006 ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
3007 ResultRelInfo *relinfo,
3008 ItemPointer tupleid,
3009 HeapTuple fdw_trigtuple,
3010 TupleTableSlot *newslot)
3012 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3013 TupleTableSlot *oldslot = ExecGetTriggerOldSlot(estate, relinfo);
3014 HeapTuple newtuple = NULL;
3015 HeapTuple trigtuple;
3016 bool should_free_trig = false;
3017 bool should_free_new = false;
3018 TriggerData LocTriggerData;
3020 Bitmapset *updatedCols;
3021 LockTupleMode lockmode;
3023 /* Determine lock mode to use */
3024 lockmode = ExecUpdateLockMode(estate, relinfo);
3026 Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
3027 if (fdw_trigtuple == NULL)
3029 TupleTableSlot *epqslot_candidate = NULL;
3031 /* get a copy of the on-disk tuple we are planning to update */
3032 if (!GetTupleForTrigger(estate, epqstate, relinfo, tupleid,
3033 lockmode, oldslot, &epqslot_candidate))
3034 return false; /* cancel the update action */
3037 * In READ COMMITTED isolation level it's possible that target tuple
3038 * was changed due to concurrent update. In that case we have a raw
3039 * subplan output tuple in newSlot, and need to run it through the
3040 * junk filter to produce an insertable tuple.
3042 * Caution: more than likely, the passed-in slot is the same as the
3043 * junkfilter's output slot, so we are clobbering the original value
3044 * of slottuple by doing the filtering. This is OK since neither we
3045 * nor our caller have any more interest in the prior contents of that
3048 if (epqslot_candidate != NULL)
3050 TupleTableSlot *epqslot_clean;
3052 epqslot_clean = ExecFilterJunk(relinfo->ri_junkFilter, epqslot_candidate);
3054 if (newslot != epqslot_clean)
3055 ExecCopySlot(newslot, epqslot_clean);
3058 trigtuple = ExecFetchSlotHeapTuple(oldslot, true, &should_free_trig);
3062 ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false);
3063 trigtuple = fdw_trigtuple;
3066 LocTriggerData.type = T_TriggerData;
3067 LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
3069 TRIGGER_EVENT_BEFORE;
3070 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
3071 LocTriggerData.tg_oldtable = NULL;
3072 LocTriggerData.tg_newtable = NULL;
3073 updatedCols = GetAllUpdatedColumns(relinfo, estate);
3074 for (i = 0; i < trigdesc->numtriggers; i++)
3076 Trigger *trigger = &trigdesc->triggers[i];
3079 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3081 TRIGGER_TYPE_BEFORE,
3082 TRIGGER_TYPE_UPDATE))
3084 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3085 updatedCols, oldslot, newslot))
3089 newtuple = ExecFetchSlotHeapTuple(newslot, true, &should_free_new);
3091 LocTriggerData.tg_trigslot = oldslot;
3092 LocTriggerData.tg_trigtuple = trigtuple;
3093 LocTriggerData.tg_newtuple = oldtuple = newtuple;
3094 LocTriggerData.tg_newslot = newslot;
3095 LocTriggerData.tg_trigger = trigger;
3096 newtuple = ExecCallTriggerFunc(&LocTriggerData,
3098 relinfo->ri_TrigFunctions,
3099 relinfo->ri_TrigInstrument,
3100 GetPerTupleMemoryContext(estate));
3102 if (newtuple == NULL)
3104 if (should_free_trig)
3105 heap_freetuple(trigtuple);
3106 if (should_free_new)
3107 heap_freetuple(oldtuple);
3108 return false; /* "do nothing" */
3110 else if (newtuple != oldtuple)
3112 ExecForceStoreHeapTuple(newtuple, newslot, false);
3115 * If the tuple returned by the trigger / being stored, is the old
3116 * row version, and the heap tuple passed to the trigger was
3117 * allocated locally, materialize the slot. Otherwise we might
3118 * free it while still referenced by the slot.
3120 if (should_free_trig && newtuple == trigtuple)
3121 ExecMaterializeSlot(newslot);
3123 if (should_free_new)
3124 heap_freetuple(oldtuple);
3126 /* signal tuple should be re-fetched if used */
3130 if (should_free_trig)
3131 heap_freetuple(trigtuple);
3137 ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
3138 ItemPointer tupleid,
3139 HeapTuple fdw_trigtuple,
3140 TupleTableSlot *newslot,
3141 List *recheckIndexes,
3142 TransitionCaptureState *transition_capture)
3144 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3145 TupleTableSlot *oldslot = ExecGetTriggerOldSlot(estate, relinfo);
3147 ExecClearTuple(oldslot);
3149 if ((trigdesc && trigdesc->trig_update_after_row) ||
3150 (transition_capture &&
3151 (transition_capture->tcs_update_old_table ||
3152 transition_capture->tcs_update_new_table)))
3155 * Note: if the UPDATE is converted into a DELETE+INSERT as part of
3156 * update-partition-key operation, then this function is also called
3157 * separately for DELETE and INSERT to capture transition table rows.
3158 * In such case, either old tuple or new tuple can be NULL.
3160 if (fdw_trigtuple == NULL && ItemPointerIsValid(tupleid))
3161 GetTupleForTrigger(estate,
3168 else if (fdw_trigtuple != NULL)
3169 ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false);
3171 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_UPDATE,
3172 true, oldslot, newslot, recheckIndexes,
3173 GetAllUpdatedColumns(relinfo, estate),
3174 transition_capture);
3179 ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
3180 HeapTuple trigtuple, TupleTableSlot *newslot)
3182 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3183 TupleTableSlot *oldslot = ExecGetTriggerOldSlot(estate, relinfo);
3184 HeapTuple newtuple = false;
3186 TriggerData LocTriggerData;
3189 LocTriggerData.type = T_TriggerData;
3190 LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
3192 TRIGGER_EVENT_INSTEAD;
3193 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
3194 LocTriggerData.tg_oldtable = NULL;
3195 LocTriggerData.tg_newtable = NULL;
3197 ExecForceStoreHeapTuple(trigtuple, oldslot, false);
3199 for (i = 0; i < trigdesc->numtriggers; i++)
3201 Trigger *trigger = &trigdesc->triggers[i];
3204 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3206 TRIGGER_TYPE_INSTEAD,
3207 TRIGGER_TYPE_UPDATE))
3209 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3210 NULL, oldslot, newslot))
3214 newtuple = ExecFetchSlotHeapTuple(newslot, true, &should_free);
3216 LocTriggerData.tg_trigslot = oldslot;
3217 LocTriggerData.tg_trigtuple = trigtuple;
3218 LocTriggerData.tg_newslot = newslot;
3219 LocTriggerData.tg_newtuple = oldtuple = newtuple;
3221 LocTriggerData.tg_trigger = trigger;
3222 newtuple = ExecCallTriggerFunc(&LocTriggerData,
3224 relinfo->ri_TrigFunctions,
3225 relinfo->ri_TrigInstrument,
3226 GetPerTupleMemoryContext(estate));
3227 if (newtuple == NULL)
3229 return false; /* "do nothing" */
3231 else if (newtuple != oldtuple)
3233 ExecForceStoreHeapTuple(newtuple, newslot, false);
3236 heap_freetuple(oldtuple);
3238 /* signal tuple should be re-fetched if used */
3247 ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
3249 TriggerDesc *trigdesc;
3251 TriggerData LocTriggerData;
3253 trigdesc = relinfo->ri_TrigDesc;
3255 if (trigdesc == NULL)
3257 if (!trigdesc->trig_truncate_before_statement)
3260 LocTriggerData.type = T_TriggerData;
3261 LocTriggerData.tg_event = TRIGGER_EVENT_TRUNCATE |
3262 TRIGGER_EVENT_BEFORE;
3263 LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
3264 LocTriggerData.tg_trigtuple = NULL;
3265 LocTriggerData.tg_newtuple = NULL;
3266 LocTriggerData.tg_trigslot = NULL;
3267 LocTriggerData.tg_newslot = NULL;
3268 LocTriggerData.tg_oldtable = NULL;
3269 LocTriggerData.tg_newtable = NULL;
3271 for (i = 0; i < trigdesc->numtriggers; i++)
3273 Trigger *trigger = &trigdesc->triggers[i];
3276 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3277 TRIGGER_TYPE_STATEMENT,
3278 TRIGGER_TYPE_BEFORE,
3279 TRIGGER_TYPE_TRUNCATE))
3281 if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3285 LocTriggerData.tg_trigger = trigger;
3286 newtuple = ExecCallTriggerFunc(&LocTriggerData,
3288 relinfo->ri_TrigFunctions,
3289 relinfo->ri_TrigInstrument,
3290 GetPerTupleMemoryContext(estate));
3294 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
3295 errmsg("BEFORE STATEMENT trigger cannot return a value")));
3300 ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
3302 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3304 if (trigdesc && trigdesc->trig_truncate_after_statement)
3305 AfterTriggerSaveEvent(estate, relinfo, TRIGGER_EVENT_TRUNCATE,
3306 false, NULL, NULL, NIL, NULL, NULL);
3311 GetTupleForTrigger(EState *estate,
3313 ResultRelInfo *relinfo,
3315 LockTupleMode lockmode,
3316 TupleTableSlot *oldslot,
3317 TupleTableSlot **epqslot)
3319 Relation relation = relinfo->ri_RelationDesc;
3321 if (epqslot != NULL)
3324 TM_FailureData tmfd;
3329 /* caller must pass an epqstate if EvalPlanQual is possible */
3330 Assert(epqstate != NULL);
3333 * lock tuple for update
3335 if (!IsolationUsesXactSnapshot())
3336 lockflags |= TUPLE_LOCK_FLAG_FIND_LAST_VERSION;
3337 test = table_tuple_lock(relation, tid, estate->es_snapshot, oldslot,
3338 estate->es_output_cid,
3339 lockmode, LockWaitBlock,
3345 case TM_SelfModified:
3348 * The target tuple was already updated or deleted by the
3349 * current command, or by a later command in the current
3350 * transaction. We ignore the tuple in the former case, and
3351 * throw error in the latter case, for the same reasons
3352 * enumerated in ExecUpdate and ExecDelete in
3353 * nodeModifyTable.c.
3355 if (tmfd.cmax != estate->es_output_cid)
3357 (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3358 errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
3359 errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3361 /* treat it as deleted; do not process */
3367 *epqslot = EvalPlanQual(epqstate,
3369 relinfo->ri_RangeTableIndex,
3373 * If PlanQual failed for updated tuple - we must not
3374 * process this tuple!
3376 if (TupIsNull(*epqslot))
3385 if (IsolationUsesXactSnapshot())
3387 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3388 errmsg("could not serialize access due to concurrent update")));
3389 elog(ERROR, "unexpected table_tuple_lock status: %u", test);
3393 if (IsolationUsesXactSnapshot())
3395 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3396 errmsg("could not serialize access due to concurrent delete")));
3397 /* tuple was deleted */
3401 elog(ERROR, "attempted to lock invisible tuple");
3405 elog(ERROR, "unrecognized table_tuple_lock status: %u", test);
3406 return false; /* keep compiler quiet */
3412 * We expect the tuple to be present, thus very simple error handling
3415 if (!table_tuple_fetch_row_version(relation, tid, SnapshotAny,
3417 elog(ERROR, "failed to fetch tuple for trigger");
3424 * Is trigger enabled to fire?
3427 TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
3428 Trigger *trigger, TriggerEvent event,
3429 Bitmapset *modifiedCols,
3430 TupleTableSlot *oldslot, TupleTableSlot *newslot)
3432 /* Check replication-role-dependent enable state */
3433 if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
3435 if (trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN ||
3436 trigger->tgenabled == TRIGGER_DISABLED)
3439 else /* ORIGIN or LOCAL role */
3441 if (trigger->tgenabled == TRIGGER_FIRES_ON_REPLICA ||
3442 trigger->tgenabled == TRIGGER_DISABLED)
3447 * Check for column-specific trigger (only possible for UPDATE, and in
3448 * fact we *must* ignore tgattr for other event types)
3450 if (trigger->tgnattr > 0 && TRIGGER_FIRED_BY_UPDATE(event))
3456 for (i = 0; i < trigger->tgnattr; i++)
3458 if (bms_is_member(trigger->tgattr[i] - FirstLowInvalidHeapAttributeNumber,
3469 /* Check for WHEN clause */
3470 if (trigger->tgqual)
3472 ExprState **predicate;
3473 ExprContext *econtext;
3474 MemoryContext oldContext;
3477 Assert(estate != NULL);
3480 * trigger is an element of relinfo->ri_TrigDesc->triggers[]; find the
3481 * matching element of relinfo->ri_TrigWhenExprs[]
3483 i = trigger - relinfo->ri_TrigDesc->triggers;
3484 predicate = &relinfo->ri_TrigWhenExprs[i];
3487 * If first time through for this WHEN expression, build expression
3488 * nodetrees for it. Keep them in the per-query memory context so
3489 * they'll survive throughout the query.
3491 if (*predicate == NULL)
3495 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
3496 tgqual = stringToNode(trigger->tgqual);
3497 /* Change references to OLD and NEW to INNER_VAR and OUTER_VAR */
3498 ChangeVarNodes(tgqual, PRS2_OLD_VARNO, INNER_VAR, 0);
3499 ChangeVarNodes(tgqual, PRS2_NEW_VARNO, OUTER_VAR, 0);
3500 /* ExecPrepareQual wants implicit-AND form */
3501 tgqual = (Node *) make_ands_implicit((Expr *) tgqual);
3502 *predicate = ExecPrepareQual((List *) tgqual, estate);
3503 MemoryContextSwitchTo(oldContext);
3507 * We will use the EState's per-tuple context for evaluating WHEN
3508 * expressions (creating it if it's not already there).
3510 econtext = GetPerTupleExprContext(estate);
3513 * Finally evaluate the expression, making the old and/or new tuples
3514 * available as INNER_VAR/OUTER_VAR respectively.
3516 econtext->ecxt_innertuple = oldslot;
3517 econtext->ecxt_outertuple = newslot;
3518 if (!ExecQual(*predicate, econtext))
3527 * After-trigger stuff
3529 * The AfterTriggersData struct holds data about pending AFTER trigger events
3530 * during the current transaction tree. (BEFORE triggers are fired
3531 * immediately so we don't need any persistent state about them.) The struct
3532 * and most of its subsidiary data are kept in TopTransactionContext; however
3533 * some data that can be discarded sooner appears in the CurTransactionContext
3534 * of the relevant subtransaction. Also, the individual event records are
3535 * kept in a separate sub-context of TopTransactionContext. This is done
3536 * mainly so that it's easy to tell from a memory context dump how much space
3537 * is being eaten by trigger events.
3539 * Because the list of pending events can grow large, we go to some
3540 * considerable effort to minimize per-event memory consumption. The event
3541 * records are grouped into chunks and common data for similar events in the
3542 * same chunk is only stored once.
3544 * XXX We need to be able to save the per-event data in a file if it grows too
3549 /* Per-trigger SET CONSTRAINT status */
3550 typedef struct SetConstraintTriggerData
3553 bool sct_tgisdeferred;
3554 } SetConstraintTriggerData;
3556 typedef struct SetConstraintTriggerData *SetConstraintTrigger;
3559 * SET CONSTRAINT intra-transaction status.
3561 * We make this a single palloc'd object so it can be copied and freed easily.
3563 * all_isset and all_isdeferred are used to keep track
3564 * of SET CONSTRAINTS ALL {DEFERRED, IMMEDIATE}.
3566 * trigstates[] stores per-trigger tgisdeferred settings.
3568 typedef struct SetConstraintStateData
3571 bool all_isdeferred;
3572 int numstates; /* number of trigstates[] entries in use */
3573 int numalloc; /* allocated size of trigstates[] */
3574 SetConstraintTriggerData trigstates[FLEXIBLE_ARRAY_MEMBER];
3575 } SetConstraintStateData;
3577 typedef SetConstraintStateData *SetConstraintState;
3581 * Per-trigger-event data
3583 * The actual per-event data, AfterTriggerEventData, includes DONE/IN_PROGRESS
3584 * status bits and up to two tuple CTIDs. Each event record also has an
3585 * associated AfterTriggerSharedData that is shared across all instances of
3586 * similar events within a "chunk".
3588 * For row-level triggers, we arrange not to waste storage on unneeded ctid
3589 * fields. Updates of regular tables use two; inserts and deletes of regular
3590 * tables use one; foreign tables always use zero and save the tuple(s) to a
3591 * tuplestore. AFTER_TRIGGER_FDW_FETCH directs AfterTriggerExecute() to
3592 * retrieve a fresh tuple or pair of tuples from that tuplestore, while
3593 * AFTER_TRIGGER_FDW_REUSE directs it to use the most-recently-retrieved
3594 * tuple(s). This permits storing tuples once regardless of the number of
3595 * row-level triggers on a foreign table.
3597 * Note that we need triggers on foreign tables to be fired in exactly the
3598 * order they were queued, so that the tuples come out of the tuplestore in
3599 * the right order. To ensure that, we forbid deferrable (constraint)
3600 * triggers on foreign tables. This also ensures that such triggers do not
3601 * get deferred into outer trigger query levels, meaning that it's okay to
3602 * destroy the tuplestore at the end of the query level.
3604 * Statement-level triggers always bear AFTER_TRIGGER_1CTID, though they
3605 * require no ctid field. We lack the flag bit space to neatly represent that
3606 * distinct case, and it seems unlikely to be worth much trouble.
3608 * Note: ats_firing_id is initially zero and is set to something else when
3609 * AFTER_TRIGGER_IN_PROGRESS is set. It indicates which trigger firing
3610 * cycle the trigger will be fired in (or was fired in, if DONE is set).
3611 * Although this is mutable state, we can keep it in AfterTriggerSharedData
3612 * because all instances of the same type of event in a given event list will
3613 * be fired at the same time, if they were queued between the same firing
3614 * cycles. So we need only ensure that ats_firing_id is zero when attaching
3615 * a new event to an existing AfterTriggerSharedData record.
3617 typedef uint32 TriggerFlags;
3619 #define AFTER_TRIGGER_OFFSET 0x0FFFFFFF /* must be low-order bits */
3620 #define AFTER_TRIGGER_DONE 0x10000000
3621 #define AFTER_TRIGGER_IN_PROGRESS 0x20000000
3622 /* bits describing the size and tuple sources of this event */
3623 #define AFTER_TRIGGER_FDW_REUSE 0x00000000
3624 #define AFTER_TRIGGER_FDW_FETCH 0x80000000
3625 #define AFTER_TRIGGER_1CTID 0x40000000
3626 #define AFTER_TRIGGER_2CTID 0xC0000000
3627 #define AFTER_TRIGGER_TUP_BITS 0xC0000000
3629 typedef struct AfterTriggerSharedData *AfterTriggerShared;
3631 typedef struct AfterTriggerSharedData
3633 TriggerEvent ats_event; /* event type indicator, see trigger.h */
3634 Oid ats_tgoid; /* the trigger's ID */
3635 Oid ats_relid; /* the relation it's on */
3636 CommandId ats_firing_id; /* ID for firing cycle */
3637 struct AfterTriggersTableData *ats_table; /* transition table access */
3638 } AfterTriggerSharedData;
3640 typedef struct AfterTriggerEventData *AfterTriggerEvent;
3642 typedef struct AfterTriggerEventData
3644 TriggerFlags ate_flags; /* status bits and offset to shared data */
3645 ItemPointerData ate_ctid1; /* inserted, deleted, or old updated tuple */
3646 ItemPointerData ate_ctid2; /* new updated tuple */
3647 } AfterTriggerEventData;
3649 /* AfterTriggerEventData, minus ate_ctid2 */
3650 typedef struct AfterTriggerEventDataOneCtid
3652 TriggerFlags ate_flags; /* status bits and offset to shared data */
3653 ItemPointerData ate_ctid1; /* inserted, deleted, or old updated tuple */
3654 } AfterTriggerEventDataOneCtid;
3656 /* AfterTriggerEventData, minus ate_ctid1 and ate_ctid2 */
3657 typedef struct AfterTriggerEventDataZeroCtids
3659 TriggerFlags ate_flags; /* status bits and offset to shared data */
3660 } AfterTriggerEventDataZeroCtids;
3662 #define SizeofTriggerEvent(evt) \
3663 (((evt)->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_2CTID ? \
3664 sizeof(AfterTriggerEventData) : \
3665 ((evt)->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_1CTID ? \
3666 sizeof(AfterTriggerEventDataOneCtid) : \
3667 sizeof(AfterTriggerEventDataZeroCtids))
3669 #define GetTriggerSharedData(evt) \
3670 ((AfterTriggerShared) ((char *) (evt) + ((evt)->ate_flags & AFTER_TRIGGER_OFFSET)))
3673 * To avoid palloc overhead, we keep trigger events in arrays in successively-
3674 * larger chunks (a slightly more sophisticated version of an expansible
3675 * array). The space between CHUNK_DATA_START and freeptr is occupied by
3676 * AfterTriggerEventData records; the space between endfree and endptr is
3677 * occupied by AfterTriggerSharedData records.
3679 typedef struct AfterTriggerEventChunk
3681 struct AfterTriggerEventChunk *next; /* list link */
3682 char *freeptr; /* start of free space in chunk */
3683 char *endfree; /* end of free space in chunk */
3684 char *endptr; /* end of chunk */
3685 /* event data follows here */
3686 } AfterTriggerEventChunk;
3688 #define CHUNK_DATA_START(cptr) ((char *) (cptr) + MAXALIGN(sizeof(AfterTriggerEventChunk)))
3690 /* A list of events */
3691 typedef struct AfterTriggerEventList
3693 AfterTriggerEventChunk *head;
3694 AfterTriggerEventChunk *tail;
3695 char *tailfree; /* freeptr of tail chunk */
3696 } AfterTriggerEventList;
3698 /* Macros to help in iterating over a list of events */
3699 #define for_each_chunk(cptr, evtlist) \
3700 for (cptr = (evtlist).head; cptr != NULL; cptr = cptr->next)
3701 #define for_each_event(eptr, cptr) \
3702 for (eptr = (AfterTriggerEvent) CHUNK_DATA_START(cptr); \
3703 (char *) eptr < (cptr)->freeptr; \
3704 eptr = (AfterTriggerEvent) (((char *) eptr) + SizeofTriggerEvent(eptr)))
3705 /* Use this if no special per-chunk processing is needed */
3706 #define for_each_event_chunk(eptr, cptr, evtlist) \
3707 for_each_chunk(cptr, evtlist) for_each_event(eptr, cptr)
3709 /* Macros for iterating from a start point that might not be list start */
3710 #define for_each_chunk_from(cptr) \
3711 for (; cptr != NULL; cptr = cptr->next)
3712 #define for_each_event_from(eptr, cptr) \
3714 (char *) eptr < (cptr)->freeptr; \
3715 eptr = (AfterTriggerEvent) (((char *) eptr) + SizeofTriggerEvent(eptr)))
3719 * All per-transaction data for the AFTER TRIGGERS module.
3721 * AfterTriggersData has the following fields:
3723 * firing_counter is incremented for each call of afterTriggerInvokeEvents.
3724 * We mark firable events with the current firing cycle's ID so that we can
3725 * tell which ones to work on. This ensures sane behavior if a trigger
3726 * function chooses to do SET CONSTRAINTS: the inner SET CONSTRAINTS will
3727 * only fire those events that weren't already scheduled for firing.
3729 * state keeps track of the transaction-local effects of SET CONSTRAINTS.
3730 * This is saved and restored across failed subtransactions.
3732 * events is the current list of deferred events. This is global across
3733 * all subtransactions of the current transaction. In a subtransaction
3734 * abort, we know that the events added by the subtransaction are at the
3735 * end of the list, so it is relatively easy to discard them. The event
3736 * list chunks themselves are stored in event_cxt.
3738 * query_depth is the current depth of nested AfterTriggerBeginQuery calls
3739 * (-1 when the stack is empty).
3741 * query_stack[query_depth] is the per-query-level data, including these fields:
3743 * events is a list of AFTER trigger events queued by the current query.
3744 * None of these are valid until the matching AfterTriggerEndQuery call
3745 * occurs. At that point we fire immediate-mode triggers, and append any
3746 * deferred events to the main events list.
3748 * fdw_tuplestore is a tuplestore containing the foreign-table tuples
3749 * needed by events queued by the current query. (Note: we use just one
3750 * tuplestore even though more than one foreign table might be involved.
3751 * This is okay because tuplestores don't really care what's in the tuples
3752 * they store; but it's possible that someday it'd break.)
3754 * tables is a List of AfterTriggersTableData structs for target tables
3755 * of the current query (see below).
3757 * maxquerydepth is just the allocated length of query_stack.
3759 * trans_stack holds per-subtransaction data, including these fields:
3761 * state is NULL or a pointer to a saved copy of the SET CONSTRAINTS
3762 * state data. Each subtransaction level that modifies that state first
3763 * saves a copy, which we use to restore the state if we abort.
3765 * events is a copy of the events head/tail pointers,
3766 * which we use to restore those values during subtransaction abort.
3768 * query_depth is the subtransaction-start-time value of query_depth,
3769 * which we similarly use to clean up at subtransaction abort.
3771 * firing_counter is the subtransaction-start-time value of firing_counter.
3772 * We use this to recognize which deferred triggers were fired (or marked
3773 * for firing) within an aborted subtransaction.
3775 * We use GetCurrentTransactionNestLevel() to determine the correct array
3776 * index in trans_stack. maxtransdepth is the number of allocated entries in
3777 * trans_stack. (By not keeping our own stack pointer, we can avoid trouble
3778 * in cases where errors during subxact abort cause multiple invocations
3779 * of AfterTriggerEndSubXact() at the same nesting depth.)
3781 * We create an AfterTriggersTableData struct for each target table of the
3782 * current query, and each operation mode (INSERT/UPDATE/DELETE), that has
3783 * either transition tables or statement-level triggers. This is used to
3784 * hold the relevant transition tables, as well as info tracking whether
3785 * we already queued the statement triggers. (We use that info to prevent
3786 * firing the same statement triggers more than once per statement, or really
3787 * once per transition table set.) These structs, along with the transition
3788 * table tuplestores, live in the (sub)transaction's CurTransactionContext.
3789 * That's sufficient lifespan because we don't allow transition tables to be
3790 * used by deferrable triggers, so they only need to survive until
3791 * AfterTriggerEndQuery.
3793 typedef struct AfterTriggersQueryData AfterTriggersQueryData;
3794 typedef struct AfterTriggersTransData AfterTriggersTransData;
3795 typedef struct AfterTriggersTableData AfterTriggersTableData;
3797 typedef struct AfterTriggersData
3799 CommandId firing_counter; /* next firing ID to assign */
3800 SetConstraintState state; /* the active S C state */
3801 AfterTriggerEventList events; /* deferred-event list */
3802 MemoryContext event_cxt; /* memory context for events, if any */
3804 /* per-query-level data: */
3805 AfterTriggersQueryData *query_stack; /* array of structs shown below */
3806 int query_depth; /* current index in above array */
3807 int maxquerydepth; /* allocated len of above array */
3809 /* per-subtransaction-level data: */
3810 AfterTriggersTransData *trans_stack; /* array of structs shown below */
3811 int maxtransdepth; /* allocated len of above array */
3812 } AfterTriggersData;
3814 struct AfterTriggersQueryData
3816 AfterTriggerEventList events; /* events pending from this query */
3817 Tuplestorestate *fdw_tuplestore; /* foreign tuples for said events */
3818 List *tables; /* list of AfterTriggersTableData, see below */
3821 struct AfterTriggersTransData
3823 /* these fields are just for resetting at subtrans abort: */
3824 SetConstraintState state; /* saved S C state, or NULL if not yet saved */
3825 AfterTriggerEventList events; /* saved list pointer */
3826 int query_depth; /* saved query_depth */
3827 CommandId firing_counter; /* saved firing_counter */
3830 struct AfterTriggersTableData
3832 /* relid + cmdType form the lookup key for these structs: */
3833 Oid relid; /* target table's OID */
3834 CmdType cmdType; /* event type, CMD_INSERT/UPDATE/DELETE */
3835 bool closed; /* true when no longer OK to add tuples */
3836 bool before_trig_done; /* did we already queue BS triggers? */
3837 bool after_trig_done; /* did we already queue AS triggers? */
3838 AfterTriggerEventList after_trig_events; /* if so, saved list pointer */
3839 Tuplestorestate *old_tuplestore; /* "old" transition table, if any */
3840 Tuplestorestate *new_tuplestore; /* "new" transition table, if any */
3841 TupleTableSlot *storeslot; /* for converting to tuplestore's format */
3844 static AfterTriggersData afterTriggers;
3846 static void AfterTriggerExecute(EState *estate,
3847 AfterTriggerEvent event,
3848 ResultRelInfo *relInfo,
3849 TriggerDesc *trigdesc,
3851 Instrumentation *instr,
3852 MemoryContext per_tuple_context,
3853 TupleTableSlot *trig_tuple_slot1,
3854 TupleTableSlot *trig_tuple_slot2);
3855 static AfterTriggersTableData *GetAfterTriggersTableData(Oid relid,
3857 static void AfterTriggerFreeQuery(AfterTriggersQueryData *qs);
3858 static SetConstraintState SetConstraintStateCreate(int numalloc);
3859 static SetConstraintState SetConstraintStateCopy(SetConstraintState state);
3860 static SetConstraintState SetConstraintStateAddItem(SetConstraintState state,
3861 Oid tgoid, bool tgisdeferred);
3862 static void cancel_prior_stmt_triggers(Oid relid, CmdType cmdType, int tgevent);
3866 * Get the FDW tuplestore for the current trigger query level, creating it
3869 static Tuplestorestate *
3870 GetCurrentFDWTuplestore(void)
3872 Tuplestorestate *ret;
3874 ret = afterTriggers.query_stack[afterTriggers.query_depth].fdw_tuplestore;
3877 MemoryContext oldcxt;
3878 ResourceOwner saveResourceOwner;
3881 * Make the tuplestore valid until end of subtransaction. We really
3882 * only need it until AfterTriggerEndQuery().
3884 oldcxt = MemoryContextSwitchTo(CurTransactionContext);
3885 saveResourceOwner = CurrentResourceOwner;
3886 CurrentResourceOwner = CurTransactionResourceOwner;
3888 ret = tuplestore_begin_heap(false, false, work_mem);
3890 CurrentResourceOwner = saveResourceOwner;
3891 MemoryContextSwitchTo(oldcxt);
3893 afterTriggers.query_stack[afterTriggers.query_depth].fdw_tuplestore = ret;
3900 * afterTriggerCheckState()
3902 * Returns true if the trigger event is actually in state DEFERRED.
3906 afterTriggerCheckState(AfterTriggerShared evtshared)
3908 Oid tgoid = evtshared->ats_tgoid;
3909 SetConstraintState state = afterTriggers.state;
3913 * For not-deferrable triggers (i.e. normal AFTER ROW triggers and
3914 * constraints declared NOT DEFERRABLE), the state is always false.
3916 if ((evtshared->ats_event & AFTER_TRIGGER_DEFERRABLE) == 0)
3920 * If constraint state exists, SET CONSTRAINTS might have been executed
3921 * either for this trigger or for all triggers.
3925 /* Check for SET CONSTRAINTS for this specific trigger. */
3926 for (i = 0; i < state->numstates; i++)
3928 if (state->trigstates[i].sct_tgoid == tgoid)
3929 return state->trigstates[i].sct_tgisdeferred;
3932 /* Check for SET CONSTRAINTS ALL. */
3933 if (state->all_isset)
3934 return state->all_isdeferred;
3938 * Otherwise return the default state for the trigger.
3940 return ((evtshared->ats_event & AFTER_TRIGGER_INITDEFERRED) != 0);
3945 * afterTriggerAddEvent()
3947 * Add a new trigger event to the specified queue.
3948 * The passed-in event data is copied.
3952 afterTriggerAddEvent(AfterTriggerEventList *events,
3953 AfterTriggerEvent event, AfterTriggerShared evtshared)
3955 Size eventsize = SizeofTriggerEvent(event);
3956 Size needed = eventsize + sizeof(AfterTriggerSharedData);
3957 AfterTriggerEventChunk *chunk;
3958 AfterTriggerShared newshared;
3959 AfterTriggerEvent newevent;
3962 * If empty list or not enough room in the tail chunk, make a new chunk.
3963 * We assume here that a new shared record will always be needed.
3965 chunk = events->tail;
3966 if (chunk == NULL ||
3967 chunk->endfree - chunk->freeptr < needed)
3971 /* Create event context if we didn't already */
3972 if (afterTriggers.event_cxt == NULL)
3973 afterTriggers.event_cxt =
3974 AllocSetContextCreate(TopTransactionContext,
3975 "AfterTriggerEvents",
3976 ALLOCSET_DEFAULT_SIZES);
3979 * Chunk size starts at 1KB and is allowed to increase up to 1MB.
3980 * These numbers are fairly arbitrary, though there is a hard limit at
3981 * AFTER_TRIGGER_OFFSET; else we couldn't link event records to their
3982 * shared records using the available space in ate_flags. Another
3983 * constraint is that if the chunk size gets too huge, the search loop
3984 * below would get slow given a (not too common) usage pattern with
3985 * many distinct event types in a chunk. Therefore, we double the
3986 * preceding chunk size only if there weren't too many shared records
3987 * in the preceding chunk; otherwise we halve it. This gives us some
3988 * ability to adapt to the actual usage pattern of the current query
3989 * while still having large chunk sizes in typical usage. All chunk
3990 * sizes used should be MAXALIGN multiples, to ensure that the shared
3991 * records will be aligned safely.
3993 #define MIN_CHUNK_SIZE 1024
3994 #define MAX_CHUNK_SIZE (1024*1024)
3996 #if MAX_CHUNK_SIZE > (AFTER_TRIGGER_OFFSET+1)
3997 #error MAX_CHUNK_SIZE must not exceed AFTER_TRIGGER_OFFSET
4001 chunksize = MIN_CHUNK_SIZE;
4004 /* preceding chunk size... */
4005 chunksize = chunk->endptr - (char *) chunk;
4006 /* check number of shared records in preceding chunk */
4007 if ((chunk->endptr - chunk->endfree) <=
4008 (100 * sizeof(AfterTriggerSharedData)))
4009 chunksize *= 2; /* okay, double it */
4011 chunksize /= 2; /* too many shared records */
4012 chunksize = Min(chunksize, MAX_CHUNK_SIZE);
4014 chunk = MemoryContextAlloc(afterTriggers.event_cxt, chunksize);
4016 chunk->freeptr = CHUNK_DATA_START(chunk);
4017 chunk->endptr = chunk->endfree = (char *) chunk + chunksize;
4018 Assert(chunk->endfree - chunk->freeptr >= needed);
4020 if (events->head == NULL)
4021 events->head = chunk;
4023 events->tail->next = chunk;
4024 events->tail = chunk;
4025 /* events->tailfree is now out of sync, but we'll fix it below */
4029 * Try to locate a matching shared-data record already in the chunk. If
4030 * none, make a new one.
4032 for (newshared = ((AfterTriggerShared) chunk->endptr) - 1;
4033 (char *) newshared >= chunk->endfree;
4036 if (newshared->ats_tgoid == evtshared->ats_tgoid &&
4037 newshared->ats_relid == evtshared->ats_relid &&
4038 newshared->ats_event == evtshared->ats_event &&
4039 newshared->ats_table == evtshared->ats_table &&
4040 newshared->ats_firing_id == 0)
4043 if ((char *) newshared < chunk->endfree)
4045 *newshared = *evtshared;
4046 newshared->ats_firing_id = 0; /* just to be sure */
4047 chunk->endfree = (char *) newshared;
4050 /* Insert the data */
4051 newevent = (AfterTriggerEvent) chunk->freeptr;
4052 memcpy(newevent, event, eventsize);
4053 /* ... and link the new event to its shared record */
4054 newevent->ate_flags &= ~AFTER_TRIGGER_OFFSET;
4055 newevent->ate_flags |= (char *) newshared - (char *) newevent;
4057 chunk->freeptr += eventsize;
4058 events->tailfree = chunk->freeptr;
4062 * afterTriggerFreeEventList()
4064 * Free all the event storage in the given list.
4068 afterTriggerFreeEventList(AfterTriggerEventList *events)
4070 AfterTriggerEventChunk *chunk;
4072 while ((chunk = events->head) != NULL)
4074 events->head = chunk->next;
4077 events->tail = NULL;
4078 events->tailfree = NULL;
4082 * afterTriggerRestoreEventList()
4084 * Restore an event list to its prior length, removing all the events
4085 * added since it had the value old_events.
4089 afterTriggerRestoreEventList(AfterTriggerEventList *events,
4090 const AfterTriggerEventList *old_events)
4092 AfterTriggerEventChunk *chunk;
4093 AfterTriggerEventChunk *next_chunk;
4095 if (old_events->tail == NULL)
4097 /* restoring to a completely empty state, so free everything */
4098 afterTriggerFreeEventList(events);
4102 *events = *old_events;
4103 /* free any chunks after the last one we want to keep */
4104 for (chunk = events->tail->next; chunk != NULL; chunk = next_chunk)
4106 next_chunk = chunk->next;
4109 /* and clean up the tail chunk to be the right length */
4110 events->tail->next = NULL;
4111 events->tail->freeptr = events->tailfree;
4114 * We don't make any effort to remove now-unused shared data records.
4115 * They might still be useful, anyway.
4121 * afterTriggerDeleteHeadEventChunk()
4123 * Remove the first chunk of events from the query level's event list.
4124 * Keep any event list pointers elsewhere in the query level's data
4125 * structures in sync.
4129 afterTriggerDeleteHeadEventChunk(AfterTriggersQueryData *qs)
4131 AfterTriggerEventChunk *target = qs->events.head;
4134 Assert(target && target->next);
4137 * First, update any pointers in the per-table data, so that they won't be
4138 * dangling. Resetting obsoleted pointers to NULL will make
4139 * cancel_prior_stmt_triggers start from the list head, which is fine.
4141 foreach(lc, qs->tables)
4143 AfterTriggersTableData *table = (AfterTriggersTableData *) lfirst(lc);
4145 if (table->after_trig_done &&
4146 table->after_trig_events.tail == target)
4148 table->after_trig_events.head = NULL;
4149 table->after_trig_events.tail = NULL;
4150 table->after_trig_events.tailfree = NULL;
4154 /* Now we can flush the head chunk */
4155 qs->events.head = target->next;
4161 * AfterTriggerExecute()
4163 * Fetch the required tuples back from the heap and fire one
4164 * single trigger function.
4166 * Frequently, this will be fired many times in a row for triggers of
4167 * a single relation. Therefore, we cache the open relation and provide
4168 * fmgr lookup cache space at the caller level. (For triggers fired at
4169 * the end of a query, we can even piggyback on the executor's state.)
4171 * event: event currently being fired.
4172 * rel: open relation for event.
4173 * trigdesc: working copy of rel's trigger info.
4174 * finfo: array of fmgr lookup cache entries (one per trigger in trigdesc).
4175 * instr: array of EXPLAIN ANALYZE instrumentation nodes (one per trigger),
4176 * or NULL if no instrumentation is wanted.
4177 * per_tuple_context: memory context to call trigger function in.
4178 * trig_tuple_slot1: scratch slot for tg_trigtuple (foreign tables only)
4179 * trig_tuple_slot2: scratch slot for tg_newtuple (foreign tables only)
4183 AfterTriggerExecute(EState *estate,
4184 AfterTriggerEvent event,
4185 ResultRelInfo *relInfo,
4186 TriggerDesc *trigdesc,
4187 FmgrInfo *finfo, Instrumentation *instr,
4188 MemoryContext per_tuple_context,
4189 TupleTableSlot *trig_tuple_slot1,
4190 TupleTableSlot *trig_tuple_slot2)
4192 Relation rel = relInfo->ri_RelationDesc;
4193 AfterTriggerShared evtshared = GetTriggerSharedData(event);
4194 Oid tgoid = evtshared->ats_tgoid;
4195 TriggerData LocTriggerData;
4198 bool should_free_trig = false;
4199 bool should_free_new = false;
4202 * Locate trigger in trigdesc.
4204 LocTriggerData.tg_trigger = NULL;
4205 LocTriggerData.tg_trigslot = NULL;
4206 LocTriggerData.tg_newslot = NULL;
4208 for (tgindx = 0; tgindx < trigdesc->numtriggers; tgindx++)
4210 if (trigdesc->triggers[tgindx].tgoid == tgoid)
4212 LocTriggerData.tg_trigger = &(trigdesc->triggers[tgindx]);
4216 if (LocTriggerData.tg_trigger == NULL)
4217 elog(ERROR, "could not find trigger %u", tgoid);
4220 * If doing EXPLAIN ANALYZE, start charging time to this trigger. We want
4221 * to include time spent re-fetching tuples in the trigger cost.
4224 InstrStartNode(instr + tgindx);
4227 * Fetch the required tuple(s).
4229 switch (event->ate_flags & AFTER_TRIGGER_TUP_BITS)
4231 case AFTER_TRIGGER_FDW_FETCH:
4233 Tuplestorestate *fdw_tuplestore = GetCurrentFDWTuplestore();
4235 if (!tuplestore_gettupleslot(fdw_tuplestore, true, false,
4237 elog(ERROR, "failed to fetch tuple1 for AFTER trigger");
4239 if ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) ==
4240 TRIGGER_EVENT_UPDATE &&
4241 !tuplestore_gettupleslot(fdw_tuplestore, true, false,
4243 elog(ERROR, "failed to fetch tuple2 for AFTER trigger");
4246 case AFTER_TRIGGER_FDW_REUSE:
4249 * Store tuple in the slot so that tg_trigtuple does not reference
4250 * tuplestore memory. (It is formally possible for the trigger
4251 * function to queue trigger events that add to the same
4252 * tuplestore, which can push other tuples out of memory.) The
4253 * distinction is academic, because we start with a minimal tuple
4254 * that is stored as a heap tuple, constructed in different memory
4255 * context, in the slot anyway.
4257 LocTriggerData.tg_trigslot = trig_tuple_slot1;
4258 LocTriggerData.tg_trigtuple =
4259 ExecFetchSlotHeapTuple(trig_tuple_slot1, true, &should_free_trig);
4261 LocTriggerData.tg_newslot = trig_tuple_slot2;
4262 LocTriggerData.tg_newtuple =
4263 ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) ==
4264 TRIGGER_EVENT_UPDATE) ?
4265 ExecFetchSlotHeapTuple(trig_tuple_slot2, true, &should_free_new) : NULL;
4270 if (ItemPointerIsValid(&(event->ate_ctid1)))
4272 LocTriggerData.tg_trigslot = ExecGetTriggerOldSlot(estate, relInfo);
4274 if (!table_tuple_fetch_row_version(rel, &(event->ate_ctid1),
4276 LocTriggerData.tg_trigslot))
4277 elog(ERROR, "failed to fetch tuple1 for AFTER trigger");
4278 LocTriggerData.tg_trigtuple =
4279 ExecFetchSlotHeapTuple(LocTriggerData.tg_trigslot, false, &should_free_trig);
4283 LocTriggerData.tg_trigtuple = NULL;
4286 /* don't touch ctid2 if not there */
4287 if ((event->ate_flags & AFTER_TRIGGER_TUP_BITS) ==
4288 AFTER_TRIGGER_2CTID &&
4289 ItemPointerIsValid(&(event->ate_ctid2)))
4291 LocTriggerData.tg_newslot = ExecGetTriggerNewSlot(estate, relInfo);
4293 if (!table_tuple_fetch_row_version(rel, &(event->ate_ctid2),
4295 LocTriggerData.tg_newslot))
4296 elog(ERROR, "failed to fetch tuple2 for AFTER trigger");
4297 LocTriggerData.tg_newtuple =
4298 ExecFetchSlotHeapTuple(LocTriggerData.tg_newslot, false, &should_free_new);
4302 LocTriggerData.tg_newtuple = NULL;
4307 * Set up the tuplestore information to let the trigger have access to
4308 * transition tables. When we first make a transition table available to
4309 * a trigger, mark it "closed" so that it cannot change anymore. If any
4310 * additional events of the same type get queued in the current trigger
4311 * query level, they'll go into new transition tables.
4313 LocTriggerData.tg_oldtable = LocTriggerData.tg_newtable = NULL;
4314 if (evtshared->ats_table)
4316 if (LocTriggerData.tg_trigger->tgoldtable)
4318 LocTriggerData.tg_oldtable = evtshared->ats_table->old_tuplestore;
4319 evtshared->ats_table->closed = true;
4322 if (LocTriggerData.tg_trigger->tgnewtable)
4324 LocTriggerData.tg_newtable = evtshared->ats_table->new_tuplestore;
4325 evtshared->ats_table->closed = true;
4330 * Setup the remaining trigger information
4332 LocTriggerData.type = T_TriggerData;
4333 LocTriggerData.tg_event =
4334 evtshared->ats_event & (TRIGGER_EVENT_OPMASK | TRIGGER_EVENT_ROW);
4335 LocTriggerData.tg_relation = rel;
4337 MemoryContextReset(per_tuple_context);
4340 * Call the trigger and throw away any possibly returned updated tuple.
4341 * (Don't let ExecCallTriggerFunc measure EXPLAIN time.)
4343 rettuple = ExecCallTriggerFunc(&LocTriggerData,
4348 if (rettuple != NULL &&
4349 rettuple != LocTriggerData.tg_trigtuple &&
4350 rettuple != LocTriggerData.tg_newtuple)
4351 heap_freetuple(rettuple);
4356 if (should_free_trig)
4357 heap_freetuple(LocTriggerData.tg_trigtuple);
4358 if (should_free_new)
4359 heap_freetuple(LocTriggerData.tg_newtuple);
4361 if (LocTriggerData.tg_trigslot)
4362 ExecClearTuple(LocTriggerData.tg_trigslot);
4363 if (LocTriggerData.tg_newslot)
4364 ExecClearTuple(LocTriggerData.tg_newslot);
4367 * If doing EXPLAIN ANALYZE, stop charging time to this trigger, and count
4368 * one "tuple returned" (really the number of firings).
4371 InstrStopNode(instr + tgindx, 1);
4376 * afterTriggerMarkEvents()
4378 * Scan the given event list for not yet invoked events. Mark the ones
4379 * that can be invoked now with the current firing ID.
4381 * If move_list isn't NULL, events that are not to be invoked now are
4382 * transferred to move_list.
4384 * When immediate_only is true, do not invoke currently-deferred triggers.
4385 * (This will be false only at main transaction exit.)
4387 * Returns true if any invokable events were found.
4390 afterTriggerMarkEvents(AfterTriggerEventList *events,
4391 AfterTriggerEventList *move_list,
4392 bool immediate_only)
4395 AfterTriggerEvent event;
4396 AfterTriggerEventChunk *chunk;
4398 for_each_event_chunk(event, chunk, *events)
4400 AfterTriggerShared evtshared = GetTriggerSharedData(event);
4401 bool defer_it = false;
4403 if (!(event->ate_flags &
4404 (AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS)))
4407 * This trigger hasn't been called or scheduled yet. Check if we
4408 * should call it now.
4410 if (immediate_only && afterTriggerCheckState(evtshared))
4417 * Mark it as to be fired in this firing cycle.
4419 evtshared->ats_firing_id = afterTriggers.firing_counter;
4420 event->ate_flags |= AFTER_TRIGGER_IN_PROGRESS;
4426 * If it's deferred, move it to move_list, if requested.
4428 if (defer_it && move_list != NULL)
4430 /* add it to move_list */
4431 afterTriggerAddEvent(move_list, event, evtshared);
4432 /* mark original copy "done" so we don't do it again */
4433 event->ate_flags |= AFTER_TRIGGER_DONE;
4441 * afterTriggerInvokeEvents()
4443 * Scan the given event list for events that are marked as to be fired
4444 * in the current firing cycle, and fire them.
4446 * If estate isn't NULL, we use its result relation info to avoid repeated
4447 * openings and closing of trigger target relations. If it is NULL, we
4448 * make one locally to cache the info in case there are multiple trigger
4451 * When delete_ok is true, it's safe to delete fully-processed events.
4452 * (We are not very tense about that: we simply reset a chunk to be empty
4453 * if all its events got fired. The objective here is just to avoid useless
4454 * rescanning of events when a trigger queues new events during transaction
4455 * end, so it's not necessary to worry much about the case where only
4456 * some events are fired.)
4458 * Returns true if no unfired events remain in the list (this allows us
4459 * to avoid repeating afterTriggerMarkEvents).
4462 afterTriggerInvokeEvents(AfterTriggerEventList *events,
4463 CommandId firing_id,
4467 bool all_fired = true;
4468 AfterTriggerEventChunk *chunk;
4469 MemoryContext per_tuple_context;
4470 bool local_estate = false;
4471 ResultRelInfo *rInfo = NULL;
4472 Relation rel = NULL;
4473 TriggerDesc *trigdesc = NULL;
4474 FmgrInfo *finfo = NULL;
4475 Instrumentation *instr = NULL;
4476 TupleTableSlot *slot1 = NULL,
4479 /* Make a local EState if need be */
4482 estate = CreateExecutorState();
4483 local_estate = true;
4486 /* Make a per-tuple memory context for trigger function calls */
4488 AllocSetContextCreate(CurrentMemoryContext,
4489 "AfterTriggerTupleContext",
4490 ALLOCSET_DEFAULT_SIZES);
4492 for_each_chunk(chunk, *events)
4494 AfterTriggerEvent event;
4495 bool all_fired_in_chunk = true;
4497 for_each_event(event, chunk)
4499 AfterTriggerShared evtshared = GetTriggerSharedData(event);
4502 * Is it one for me to fire?
4504 if ((event->ate_flags & AFTER_TRIGGER_IN_PROGRESS) &&
4505 evtshared->ats_firing_id == firing_id)
4508 * So let's fire it... but first, find the correct relation if
4509 * this is not the same relation as before.
4511 if (rel == NULL || RelationGetRelid(rel) != evtshared->ats_relid)
4513 rInfo = ExecGetTriggerResultRel(estate, evtshared->ats_relid);
4514 rel = rInfo->ri_RelationDesc;
4515 trigdesc = rInfo->ri_TrigDesc;
4516 finfo = rInfo->ri_TrigFunctions;
4517 instr = rInfo->ri_TrigInstrument;
4518 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
4522 ExecDropSingleTupleTableSlot(slot1);
4523 ExecDropSingleTupleTableSlot(slot2);
4525 slot1 = MakeSingleTupleTableSlot(rel->rd_att,
4526 &TTSOpsMinimalTuple);
4527 slot2 = MakeSingleTupleTableSlot(rel->rd_att,
4528 &TTSOpsMinimalTuple);
4530 if (trigdesc == NULL) /* should not happen */
4531 elog(ERROR, "relation %u has no triggers",
4532 evtshared->ats_relid);
4536 * Fire it. Note that the AFTER_TRIGGER_IN_PROGRESS flag is
4537 * still set, so recursive examinations of the event list
4538 * won't try to re-fire it.
4540 AfterTriggerExecute(estate, event, rInfo, trigdesc, finfo, instr,
4541 per_tuple_context, slot1, slot2);
4544 * Mark the event as done.
4546 event->ate_flags &= ~AFTER_TRIGGER_IN_PROGRESS;
4547 event->ate_flags |= AFTER_TRIGGER_DONE;
4549 else if (!(event->ate_flags & AFTER_TRIGGER_DONE))
4551 /* something remains to be done */
4552 all_fired = all_fired_in_chunk = false;
4556 /* Clear the chunk if delete_ok and nothing left of interest */
4557 if (delete_ok && all_fired_in_chunk)
4559 chunk->freeptr = CHUNK_DATA_START(chunk);
4560 chunk->endfree = chunk->endptr;
4563 * If it's last chunk, must sync event list's tailfree too. Note
4564 * that delete_ok must NOT be passed as true if there could be
4565 * additional AfterTriggerEventList values pointing at this event
4566 * list, since we'd fail to fix their copies of tailfree.
4568 if (chunk == events->tail)
4569 events->tailfree = chunk->freeptr;
4574 ExecDropSingleTupleTableSlot(slot1);
4575 ExecDropSingleTupleTableSlot(slot2);
4578 /* Release working resources */
4579 MemoryContextDelete(per_tuple_context);
4583 ExecCleanUpTriggerState(estate);
4584 ExecResetTupleTable(estate->es_tupleTable, false);
4585 FreeExecutorState(estate);
4593 * GetAfterTriggersTableData
4595 * Find or create an AfterTriggersTableData struct for the specified
4596 * trigger event (relation + operation type). Ignore existing structs
4597 * marked "closed"; we don't want to put any additional tuples into them,
4598 * nor change their stmt-triggers-fired state.
4600 * Note: the AfterTriggersTableData list is allocated in the current
4601 * (sub)transaction's CurTransactionContext. This is OK because
4602 * we don't need it to live past AfterTriggerEndQuery.
4604 static AfterTriggersTableData *
4605 GetAfterTriggersTableData(Oid relid, CmdType cmdType)
4607 AfterTriggersTableData *table;
4608 AfterTriggersQueryData *qs;
4609 MemoryContext oldcxt;
4612 /* Caller should have ensured query_depth is OK. */
4613 Assert(afterTriggers.query_depth >= 0 &&
4614 afterTriggers.query_depth < afterTriggers.maxquerydepth);
4615 qs = &afterTriggers.query_stack[afterTriggers.query_depth];
4617 foreach(lc, qs->tables)
4619 table = (AfterTriggersTableData *) lfirst(lc);
4620 if (table->relid == relid && table->cmdType == cmdType &&
4625 oldcxt = MemoryContextSwitchTo(CurTransactionContext);
4627 table = (AfterTriggersTableData *) palloc0(sizeof(AfterTriggersTableData));
4628 table->relid = relid;
4629 table->cmdType = cmdType;
4630 qs->tables = lappend(qs->tables, table);
4632 MemoryContextSwitchTo(oldcxt);
4639 * MakeTransitionCaptureState
4641 * Make a TransitionCaptureState object for the given TriggerDesc, target
4642 * relation, and operation type. The TCS object holds all the state needed
4643 * to decide whether to capture tuples in transition tables.
4645 * If there are no triggers in 'trigdesc' that request relevant transition
4646 * tables, then return NULL.
4648 * The resulting object can be passed to the ExecAR* functions. The caller
4649 * should set tcs_map or tcs_original_insert_tuple as appropriate when dealing
4650 * with child tables.
4652 * Note that we copy the flags from a parent table into this struct (rather
4653 * than subsequently using the relation's TriggerDesc directly) so that we can
4654 * use it to control collection of transition tuples from child tables.
4656 * Per SQL spec, all operations of the same kind (INSERT/UPDATE/DELETE)
4657 * on the same table during one query should share one transition table.
4658 * Therefore, the Tuplestores are owned by an AfterTriggersTableData struct
4659 * looked up using the table OID + CmdType, and are merely referenced by
4660 * the TransitionCaptureState objects we hand out to callers.
4662 TransitionCaptureState *
4663 MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
4665 TransitionCaptureState *state;
4668 AfterTriggersTableData *table;
4669 MemoryContext oldcxt;
4670 ResourceOwner saveResourceOwner;
4672 if (trigdesc == NULL)
4675 /* Detect which table(s) we need. */
4680 need_new = trigdesc->trig_insert_new_table;
4683 need_old = trigdesc->trig_update_old_table;
4684 need_new = trigdesc->trig_update_new_table;
4687 need_old = trigdesc->trig_delete_old_table;
4691 elog(ERROR, "unexpected CmdType: %d", (int) cmdType);
4692 need_old = need_new = false; /* keep compiler quiet */
4695 if (!need_old && !need_new)
4698 /* Check state, like AfterTriggerSaveEvent. */
4699 if (afterTriggers.query_depth < 0)
4700 elog(ERROR, "MakeTransitionCaptureState() called outside of query");
4702 /* Be sure we have enough space to record events at this query depth. */
4703 if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
4704 AfterTriggerEnlargeQueryState();
4707 * Find or create an AfterTriggersTableData struct to hold the
4708 * tuplestore(s). If there's a matching struct but it's marked closed,
4709 * ignore it; we need a newer one.
4711 * Note: the AfterTriggersTableData list, as well as the tuplestores, are
4712 * allocated in the current (sub)transaction's CurTransactionContext, and
4713 * the tuplestores are managed by the (sub)transaction's resource owner.
4714 * This is sufficient lifespan because we do not allow triggers using
4715 * transition tables to be deferrable; they will be fired during
4716 * AfterTriggerEndQuery, after which it's okay to delete the data.
4718 table = GetAfterTriggersTableData(relid, cmdType);
4720 /* Now create required tuplestore(s), if we don't have them already. */
4721 oldcxt = MemoryContextSwitchTo(CurTransactionContext);
4722 saveResourceOwner = CurrentResourceOwner;
4723 CurrentResourceOwner = CurTransactionResourceOwner;
4725 if (need_old && table->old_tuplestore == NULL)
4726 table->old_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4727 if (need_new && table->new_tuplestore == NULL)
4728 table->new_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4730 CurrentResourceOwner = saveResourceOwner;
4731 MemoryContextSwitchTo(oldcxt);
4733 /* Now build the TransitionCaptureState struct, in caller's context */
4734 state = (TransitionCaptureState *) palloc0(sizeof(TransitionCaptureState));
4735 state->tcs_delete_old_table = trigdesc->trig_delete_old_table;
4736 state->tcs_update_old_table = trigdesc->trig_update_old_table;
4737 state->tcs_update_new_table = trigdesc->trig_update_new_table;
4738 state->tcs_insert_new_table = trigdesc->trig_insert_new_table;
4739 state->tcs_private = table;
4746 * AfterTriggerBeginXact()
4748 * Called at transaction start (either BEGIN or implicit for single
4749 * statement outside of transaction block).
4753 AfterTriggerBeginXact(void)
4756 * Initialize after-trigger state structure to empty
4758 afterTriggers.firing_counter = (CommandId) 1; /* mustn't be 0 */
4759 afterTriggers.query_depth = -1;
4762 * Verify that there is no leftover state remaining. If these assertions
4763 * trip, it means that AfterTriggerEndXact wasn't called or didn't clean
4766 Assert(afterTriggers.state == NULL);
4767 Assert(afterTriggers.query_stack == NULL);
4768 Assert(afterTriggers.maxquerydepth == 0);
4769 Assert(afterTriggers.event_cxt == NULL);
4770 Assert(afterTriggers.events.head == NULL);
4771 Assert(afterTriggers.trans_stack == NULL);
4772 Assert(afterTriggers.maxtransdepth == 0);
4777 * AfterTriggerBeginQuery()
4779 * Called just before we start processing a single query within a
4780 * transaction (or subtransaction). Most of the real work gets deferred
4781 * until somebody actually tries to queue a trigger event.
4785 AfterTriggerBeginQuery(void)
4787 /* Increase the query stack depth */
4788 afterTriggers.query_depth++;
4793 * AfterTriggerEndQuery()
4795 * Called after one query has been completely processed. At this time
4796 * we invoke all AFTER IMMEDIATE trigger events queued by the query, and
4797 * transfer deferred trigger events to the global deferred-trigger list.
4799 * Note that this must be called BEFORE closing down the executor
4800 * with ExecutorEnd, because we make use of the EState's info about
4801 * target relations. Normally it is called from ExecutorFinish.
4805 AfterTriggerEndQuery(EState *estate)
4807 AfterTriggersQueryData *qs;
4809 /* Must be inside a query, too */
4810 Assert(afterTriggers.query_depth >= 0);
4813 * If we never even got as far as initializing the event stack, there
4814 * certainly won't be any events, so exit quickly.
4816 if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
4818 afterTriggers.query_depth--;
4823 * Process all immediate-mode triggers queued by the query, and move the
4824 * deferred ones to the main list of deferred events.
4826 * Notice that we decide which ones will be fired, and put the deferred
4827 * ones on the main list, before anything is actually fired. This ensures
4828 * reasonably sane behavior if a trigger function does SET CONSTRAINTS ...
4829 * IMMEDIATE: all events we have decided to defer will be available for it
4832 * We loop in case a trigger queues more events at the same query level.
4833 * Ordinary trigger functions, including all PL/pgSQL trigger functions,
4834 * will instead fire any triggers in a dedicated query level. Foreign key
4835 * enforcement triggers do add to the current query level, thanks to their
4836 * passing fire_triggers = false to SPI_execute_snapshot(). Other
4837 * C-language triggers might do likewise.
4839 * If we find no firable events, we don't have to increment
4842 qs = &afterTriggers.query_stack[afterTriggers.query_depth];
4846 if (afterTriggerMarkEvents(&qs->events, &afterTriggers.events, true))
4848 CommandId firing_id = afterTriggers.firing_counter++;
4849 AfterTriggerEventChunk *oldtail = qs->events.tail;
4851 if (afterTriggerInvokeEvents(&qs->events, firing_id, estate, false))
4852 break; /* all fired */
4855 * Firing a trigger could result in query_stack being repalloc'd,
4856 * so we must recalculate qs after each afterTriggerInvokeEvents
4857 * call. Furthermore, it's unsafe to pass delete_ok = true here,
4858 * because that could cause afterTriggerInvokeEvents to try to
4859 * access qs->events after the stack has been repalloc'd.
4861 qs = &afterTriggers.query_stack[afterTriggers.query_depth];
4864 * We'll need to scan the events list again. To reduce the cost
4865 * of doing so, get rid of completely-fired chunks. We know that
4866 * all events were marked IN_PROGRESS or DONE at the conclusion of
4867 * afterTriggerMarkEvents, so any still-interesting events must
4868 * have been added after that, and so must be in the chunk that
4869 * was then the tail chunk, or in later chunks. So, zap all
4870 * chunks before oldtail. This is approximately the same set of
4871 * events we would have gotten rid of by passing delete_ok = true.
4873 Assert(oldtail != NULL);
4874 while (qs->events.head != oldtail)
4875 afterTriggerDeleteHeadEventChunk(qs);
4881 /* Release query-level-local storage, including tuplestores if any */
4882 AfterTriggerFreeQuery(&afterTriggers.query_stack[afterTriggers.query_depth]);
4884 afterTriggers.query_depth--;
4889 * AfterTriggerFreeQuery
4890 * Release subsidiary storage for a trigger query level.
4891 * This includes closing down tuplestores.
4892 * Note: it's important for this to be safe if interrupted by an error
4893 * and then called again for the same query level.
4896 AfterTriggerFreeQuery(AfterTriggersQueryData *qs)
4898 Tuplestorestate *ts;
4902 /* Drop the trigger events */
4903 afterTriggerFreeEventList(&qs->events);
4905 /* Drop FDW tuplestore if any */
4906 ts = qs->fdw_tuplestore;
4907 qs->fdw_tuplestore = NULL;
4911 /* Release per-table subsidiary storage */
4912 tables = qs->tables;
4915 AfterTriggersTableData *table = (AfterTriggersTableData *) lfirst(lc);
4917 ts = table->old_tuplestore;
4918 table->old_tuplestore = NULL;
4921 ts = table->new_tuplestore;
4922 table->new_tuplestore = NULL;
4928 * Now free the AfterTriggersTableData structs and list cells. Reset list
4929 * pointer first; if list_free_deep somehow gets an error, better to leak
4930 * that storage than have an infinite loop.
4933 list_free_deep(tables);
4938 * AfterTriggerFireDeferred()
4940 * Called just before the current transaction is committed. At this
4941 * time we invoke all pending DEFERRED triggers.
4943 * It is possible for other modules to queue additional deferred triggers
4944 * during pre-commit processing; therefore xact.c may have to call this
4949 AfterTriggerFireDeferred(void)
4951 AfterTriggerEventList *events;
4952 bool snap_pushed = false;
4954 /* Must not be inside a query */
4955 Assert(afterTriggers.query_depth == -1);
4958 * If there are any triggers to fire, make sure we have set a snapshot for
4959 * them to use. (Since PortalRunUtility doesn't set a snap for COMMIT, we
4960 * can't assume ActiveSnapshot is valid on entry.)
4962 events = &afterTriggers.events;
4963 if (events->head != NULL)
4965 PushActiveSnapshot(GetTransactionSnapshot());
4970 * Run all the remaining triggers. Loop until they are all gone, in case
4971 * some trigger queues more for us to do.
4973 while (afterTriggerMarkEvents(events, NULL, false))
4975 CommandId firing_id = afterTriggers.firing_counter++;
4977 if (afterTriggerInvokeEvents(events, firing_id, NULL, true))
4978 break; /* all fired */
4982 * We don't bother freeing the event list, since it will go away anyway
4983 * (and more efficiently than via pfree) in AfterTriggerEndXact.
4987 PopActiveSnapshot();
4992 * AfterTriggerEndXact()
4994 * The current transaction is finishing.
4996 * Any unfired triggers are canceled so we simply throw
4997 * away anything we know.
4999 * Note: it is possible for this to be called repeatedly in case of
5000 * error during transaction abort; therefore, do not complain if
5001 * already closed down.
5005 AfterTriggerEndXact(bool isCommit)
5008 * Forget the pending-events list.
5010 * Since all the info is in TopTransactionContext or children thereof, we
5011 * don't really need to do anything to reclaim memory. However, the
5012 * pending-events list could be large, and so it's useful to discard it as
5013 * soon as possible --- especially if we are aborting because we ran out
5014 * of memory for the list!
5016 if (afterTriggers.event_cxt)
5018 MemoryContextDelete(afterTriggers.event_cxt);
5019 afterTriggers.event_cxt = NULL;
5020 afterTriggers.events.head = NULL;
5021 afterTriggers.events.tail = NULL;
5022 afterTriggers.events.tailfree = NULL;
5026 * Forget any subtransaction state as well. Since this can't be very
5027 * large, we let the eventual reset of TopTransactionContext free the
5028 * memory instead of doing it here.
5030 afterTriggers.trans_stack = NULL;
5031 afterTriggers.maxtransdepth = 0;
5035 * Forget the query stack and constraint-related state information. As
5036 * with the subtransaction state information, we don't bother freeing the
5039 afterTriggers.query_stack = NULL;
5040 afterTriggers.maxquerydepth = 0;
5041 afterTriggers.state = NULL;
5043 /* No more afterTriggers manipulation until next transaction starts. */
5044 afterTriggers.query_depth = -1;
5048 * AfterTriggerBeginSubXact()
5050 * Start a subtransaction.
5053 AfterTriggerBeginSubXact(void)
5055 int my_level = GetCurrentTransactionNestLevel();
5058 * Allocate more space in the trans_stack if needed. (Note: because the
5059 * minimum nest level of a subtransaction is 2, we waste the first couple
5060 * entries of the array; not worth the notational effort to avoid it.)
5062 while (my_level >= afterTriggers.maxtransdepth)
5064 if (afterTriggers.maxtransdepth == 0)
5066 /* Arbitrarily initialize for max of 8 subtransaction levels */
5067 afterTriggers.trans_stack = (AfterTriggersTransData *)
5068 MemoryContextAlloc(TopTransactionContext,
5069 8 * sizeof(AfterTriggersTransData));
5070 afterTriggers.maxtransdepth = 8;
5074 /* repalloc will keep the stack in the same context */
5075 int new_alloc = afterTriggers.maxtransdepth * 2;
5077 afterTriggers.trans_stack = (AfterTriggersTransData *)
5078 repalloc(afterTriggers.trans_stack,
5079 new_alloc * sizeof(AfterTriggersTransData));
5080 afterTriggers.maxtransdepth = new_alloc;
5085 * Push the current information into the stack. The SET CONSTRAINTS state
5086 * is not saved until/unless changed. Likewise, we don't make a
5087 * per-subtransaction event context until needed.
5089 afterTriggers.trans_stack[my_level].state = NULL;
5090 afterTriggers.trans_stack[my_level].events = afterTriggers.events;
5091 afterTriggers.trans_stack[my_level].query_depth = afterTriggers.query_depth;
5092 afterTriggers.trans_stack[my_level].firing_counter = afterTriggers.firing_counter;
5096 * AfterTriggerEndSubXact()
5098 * The current subtransaction is ending.
5101 AfterTriggerEndSubXact(bool isCommit)
5103 int my_level = GetCurrentTransactionNestLevel();
5104 SetConstraintState state;
5105 AfterTriggerEvent event;
5106 AfterTriggerEventChunk *chunk;
5107 CommandId subxact_firing_id;
5110 * Pop the prior state if needed.
5114 Assert(my_level < afterTriggers.maxtransdepth);
5115 /* If we saved a prior state, we don't need it anymore */
5116 state = afterTriggers.trans_stack[my_level].state;
5119 /* this avoids double pfree if error later: */
5120 afterTriggers.trans_stack[my_level].state = NULL;
5121 Assert(afterTriggers.query_depth ==
5122 afterTriggers.trans_stack[my_level].query_depth);
5127 * Aborting. It is possible subxact start failed before calling
5128 * AfterTriggerBeginSubXact, in which case we mustn't risk touching
5129 * trans_stack levels that aren't there.
5131 if (my_level >= afterTriggers.maxtransdepth)
5135 * Release query-level storage for queries being aborted, and restore
5136 * query_depth to its pre-subxact value. This assumes that a
5137 * subtransaction will not add events to query levels started in a
5138 * earlier transaction state.
5140 while (afterTriggers.query_depth > afterTriggers.trans_stack[my_level].query_depth)
5142 if (afterTriggers.query_depth < afterTriggers.maxquerydepth)
5143 AfterTriggerFreeQuery(&afterTriggers.query_stack[afterTriggers.query_depth]);
5144 afterTriggers.query_depth--;
5146 Assert(afterTriggers.query_depth ==
5147 afterTriggers.trans_stack[my_level].query_depth);
5150 * Restore the global deferred-event list to its former length,
5151 * discarding any events queued by the subxact.
5153 afterTriggerRestoreEventList(&afterTriggers.events,
5154 &afterTriggers.trans_stack[my_level].events);
5157 * Restore the trigger state. If the saved state is NULL, then this
5158 * subxact didn't save it, so it doesn't need restoring.
5160 state = afterTriggers.trans_stack[my_level].state;
5163 pfree(afterTriggers.state);
5164 afterTriggers.state = state;
5166 /* this avoids double pfree if error later: */
5167 afterTriggers.trans_stack[my_level].state = NULL;
5170 * Scan for any remaining deferred events that were marked DONE or IN
5171 * PROGRESS by this subxact or a child, and un-mark them. We can
5172 * recognize such events because they have a firing ID greater than or
5173 * equal to the firing_counter value we saved at subtransaction start.
5174 * (This essentially assumes that the current subxact includes all
5175 * subxacts started after it.)
5177 subxact_firing_id = afterTriggers.trans_stack[my_level].firing_counter;
5178 for_each_event_chunk(event, chunk, afterTriggers.events)
5180 AfterTriggerShared evtshared = GetTriggerSharedData(event);
5182 if (event->ate_flags &
5183 (AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS))
5185 if (evtshared->ats_firing_id >= subxact_firing_id)
5187 ~(AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS);
5194 * AfterTriggerEnlargeQueryState()
5196 * Prepare the necessary state so that we can record AFTER trigger events
5197 * queued by a query. It is allowed to have nested queries within a
5198 * (sub)transaction, so we need to have separate state for each query
5203 AfterTriggerEnlargeQueryState(void)
5205 int init_depth = afterTriggers.maxquerydepth;
5207 Assert(afterTriggers.query_depth >= afterTriggers.maxquerydepth);
5209 if (afterTriggers.maxquerydepth == 0)
5211 int new_alloc = Max(afterTriggers.query_depth + 1, 8);
5213 afterTriggers.query_stack = (AfterTriggersQueryData *)
5214 MemoryContextAlloc(TopTransactionContext,
5215 new_alloc * sizeof(AfterTriggersQueryData));
5216 afterTriggers.maxquerydepth = new_alloc;
5220 /* repalloc will keep the stack in the same context */
5221 int old_alloc = afterTriggers.maxquerydepth;
5222 int new_alloc = Max(afterTriggers.query_depth + 1,
5225 afterTriggers.query_stack = (AfterTriggersQueryData *)
5226 repalloc(afterTriggers.query_stack,
5227 new_alloc * sizeof(AfterTriggersQueryData));
5228 afterTriggers.maxquerydepth = new_alloc;
5231 /* Initialize new array entries to empty */
5232 while (init_depth < afterTriggers.maxquerydepth)
5234 AfterTriggersQueryData *qs = &afterTriggers.query_stack[init_depth];
5236 qs->events.head = NULL;
5237 qs->events.tail = NULL;
5238 qs->events.tailfree = NULL;
5239 qs->fdw_tuplestore = NULL;
5247 * Create an empty SetConstraintState with room for numalloc trigstates
5249 static SetConstraintState
5250 SetConstraintStateCreate(int numalloc)
5252 SetConstraintState state;
5254 /* Behave sanely with numalloc == 0 */
5259 * We assume that zeroing will correctly initialize the state values.
5261 state = (SetConstraintState)
5262 MemoryContextAllocZero(TopTransactionContext,
5263 offsetof(SetConstraintStateData, trigstates) +
5264 numalloc * sizeof(SetConstraintTriggerData));
5266 state->numalloc = numalloc;
5272 * Copy a SetConstraintState
5274 static SetConstraintState
5275 SetConstraintStateCopy(SetConstraintState origstate)
5277 SetConstraintState state;
5279 state = SetConstraintStateCreate(origstate->numstates);
5281 state->all_isset = origstate->all_isset;
5282 state->all_isdeferred = origstate->all_isdeferred;
5283 state->numstates = origstate->numstates;
5284 memcpy(state->trigstates, origstate->trigstates,
5285 origstate->numstates * sizeof(SetConstraintTriggerData));
5291 * Add a per-trigger item to a SetConstraintState. Returns possibly-changed
5292 * pointer to the state object (it will change if we have to repalloc).
5294 static SetConstraintState
5295 SetConstraintStateAddItem(SetConstraintState state,
5296 Oid tgoid, bool tgisdeferred)
5298 if (state->numstates >= state->numalloc)
5300 int newalloc = state->numalloc * 2;
5302 newalloc = Max(newalloc, 8); /* in case original has size 0 */
5303 state = (SetConstraintState)
5305 offsetof(SetConstraintStateData, trigstates) +
5306 newalloc * sizeof(SetConstraintTriggerData));
5307 state->numalloc = newalloc;
5308 Assert(state->numstates < state->numalloc);
5311 state->trigstates[state->numstates].sct_tgoid = tgoid;
5312 state->trigstates[state->numstates].sct_tgisdeferred = tgisdeferred;
5319 * AfterTriggerSetState()
5321 * Execute the SET CONSTRAINTS ... utility command.
5325 AfterTriggerSetState(ConstraintsSetStmt *stmt)
5327 int my_level = GetCurrentTransactionNestLevel();
5329 /* If we haven't already done so, initialize our state. */
5330 if (afterTriggers.state == NULL)
5331 afterTriggers.state = SetConstraintStateCreate(8);
5334 * If in a subtransaction, and we didn't save the current state already,
5335 * save it so it can be restored if the subtransaction aborts.
5338 afterTriggers.trans_stack[my_level].state == NULL)
5340 afterTriggers.trans_stack[my_level].state =
5341 SetConstraintStateCopy(afterTriggers.state);
5345 * Handle SET CONSTRAINTS ALL ...
5347 if (stmt->constraints == NIL)
5350 * Forget any previous SET CONSTRAINTS commands in this transaction.
5352 afterTriggers.state->numstates = 0;
5355 * Set the per-transaction ALL state to known.
5357 afterTriggers.state->all_isset = true;
5358 afterTriggers.state->all_isdeferred = stmt->deferred;
5364 List *conoidlist = NIL;
5365 List *tgoidlist = NIL;
5369 * Handle SET CONSTRAINTS constraint-name [, ...]
5371 * First, identify all the named constraints and make a list of their
5372 * OIDs. Since, unlike the SQL spec, we allow multiple constraints of
5373 * the same name within a schema, the specifications are not
5374 * necessarily unique. Our strategy is to target all matching
5375 * constraints within the first search-path schema that has any
5376 * matches, but disregard matches in schemas beyond the first match.
5377 * (This is a bit odd but it's the historical behavior.)
5379 * A constraint in a partitioned table may have corresponding
5380 * constraints in the partitions. Grab those too.
5382 conrel = table_open(ConstraintRelationId, AccessShareLock);
5384 foreach(lc, stmt->constraints)
5386 RangeVar *constraint = lfirst(lc);
5388 List *namespacelist;
5391 if (constraint->catalogname)
5393 if (strcmp(constraint->catalogname, get_database_name(MyDatabaseId)) != 0)
5395 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5396 errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
5397 constraint->catalogname, constraint->schemaname,
5398 constraint->relname)));
5402 * If we're given the schema name with the constraint, look only
5403 * in that schema. If given a bare constraint name, use the
5404 * search path to find the first matching constraint.
5406 if (constraint->schemaname)
5408 Oid namespaceId = LookupExplicitNamespace(constraint->schemaname,
5411 namespacelist = list_make1_oid(namespaceId);
5415 namespacelist = fetch_search_path(true);
5419 foreach(nslc, namespacelist)
5421 Oid namespaceId = lfirst_oid(nslc);
5422 SysScanDesc conscan;
5423 ScanKeyData skey[2];
5426 ScanKeyInit(&skey[0],
5427 Anum_pg_constraint_conname,
5428 BTEqualStrategyNumber, F_NAMEEQ,
5429 CStringGetDatum(constraint->relname));
5430 ScanKeyInit(&skey[1],
5431 Anum_pg_constraint_connamespace,
5432 BTEqualStrategyNumber, F_OIDEQ,
5433 ObjectIdGetDatum(namespaceId));
5435 conscan = systable_beginscan(conrel, ConstraintNameNspIndexId,
5436 true, NULL, 2, skey);
5438 while (HeapTupleIsValid(tup = systable_getnext(conscan)))
5440 Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tup);
5442 if (con->condeferrable)
5443 conoidlist = lappend_oid(conoidlist, con->oid);
5444 else if (stmt->deferred)
5446 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
5447 errmsg("constraint \"%s\" is not deferrable",
5448 constraint->relname)));
5452 systable_endscan(conscan);
5455 * Once we've found a matching constraint we do not search
5456 * later parts of the search path.
5462 list_free(namespacelist);
5469 (errcode(ERRCODE_UNDEFINED_OBJECT),
5470 errmsg("constraint \"%s\" does not exist",
5471 constraint->relname)));
5475 * Scan for any possible descendants of the constraints. We append
5476 * whatever we find to the same list that we're scanning; this has the
5477 * effect that we create new scans for those, too, so if there are
5478 * further descendents, we'll also catch them.
5480 foreach(lc, conoidlist)
5482 Oid parent = lfirst_oid(lc);
5488 Anum_pg_constraint_conparentid,
5489 BTEqualStrategyNumber, F_OIDEQ,
5490 ObjectIdGetDatum(parent));
5492 scan = systable_beginscan(conrel, ConstraintParentIndexId, true, NULL, 1, &key);
5494 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
5496 Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple);
5498 conoidlist = lappend_oid(conoidlist, con->oid);
5501 systable_endscan(scan);
5504 table_close(conrel, AccessShareLock);
5507 * Now, locate the trigger(s) implementing each of these constraints,
5508 * and make a list of their OIDs.
5510 tgrel = table_open(TriggerRelationId, AccessShareLock);
5512 foreach(lc, conoidlist)
5514 Oid conoid = lfirst_oid(lc);
5523 Anum_pg_trigger_tgconstraint,
5524 BTEqualStrategyNumber, F_OIDEQ,
5525 ObjectIdGetDatum(conoid));
5527 tgscan = systable_beginscan(tgrel, TriggerConstraintIndexId, true,
5530 while (HeapTupleIsValid(htup = systable_getnext(tgscan)))
5532 Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(htup);
5535 * Silently skip triggers that are marked as non-deferrable in
5536 * pg_trigger. This is not an error condition, since a
5537 * deferrable RI constraint may have some non-deferrable
5540 if (pg_trigger->tgdeferrable)
5541 tgoidlist = lappend_oid(tgoidlist, pg_trigger->oid);
5546 systable_endscan(tgscan);
5548 /* Safety check: a deferrable constraint should have triggers */
5550 elog(ERROR, "no triggers found for constraint with OID %u",
5554 table_close(tgrel, AccessShareLock);
5557 * Now we can set the trigger states of individual triggers for this
5560 foreach(lc, tgoidlist)
5562 Oid tgoid = lfirst_oid(lc);
5563 SetConstraintState state = afterTriggers.state;
5567 for (i = 0; i < state->numstates; i++)
5569 if (state->trigstates[i].sct_tgoid == tgoid)
5571 state->trigstates[i].sct_tgisdeferred = stmt->deferred;
5578 afterTriggers.state =
5579 SetConstraintStateAddItem(state, tgoid, stmt->deferred);
5585 * SQL99 requires that when a constraint is set to IMMEDIATE, any deferred
5586 * checks against that constraint must be made when the SET CONSTRAINTS
5587 * command is executed -- i.e. the effects of the SET CONSTRAINTS command
5588 * apply retroactively. We've updated the constraints state, so scan the
5589 * list of previously deferred events to fire any that have now become
5592 * Obviously, if this was SET ... DEFERRED then it can't have converted
5593 * any unfired events to immediate, so we need do nothing in that case.
5595 if (!stmt->deferred)
5597 AfterTriggerEventList *events = &afterTriggers.events;
5598 bool snapshot_set = false;
5600 while (afterTriggerMarkEvents(events, NULL, true))
5602 CommandId firing_id = afterTriggers.firing_counter++;
5605 * Make sure a snapshot has been established in case trigger
5606 * functions need one. Note that we avoid setting a snapshot if
5607 * we don't find at least one trigger that has to be fired now.
5608 * This is so that BEGIN; SET CONSTRAINTS ...; SET TRANSACTION
5609 * ISOLATION LEVEL SERIALIZABLE; ... works properly. (If we are
5610 * at the start of a transaction it's not possible for any trigger
5611 * events to be queued yet.)
5615 PushActiveSnapshot(GetTransactionSnapshot());
5616 snapshot_set = true;
5620 * We can delete fired events if we are at top transaction level,
5621 * but we'd better not if inside a subtransaction, since the
5622 * subtransaction could later get rolled back.
5624 if (afterTriggerInvokeEvents(events, firing_id, NULL,
5625 !IsSubTransaction()))
5626 break; /* all fired */
5630 PopActiveSnapshot();
5635 * AfterTriggerPendingOnRel()
5636 * Test to see if there are any pending after-trigger events for rel.
5638 * This is used by TRUNCATE, CLUSTER, ALTER TABLE, etc to detect whether
5639 * it is unsafe to perform major surgery on a relation. Note that only
5640 * local pending events are examined. We assume that having exclusive lock
5641 * on a rel guarantees there are no unserviced events in other backends ---
5642 * but having a lock does not prevent there being such events in our own.
5644 * In some scenarios it'd be reasonable to remove pending events (more
5645 * specifically, mark them DONE by the current subxact) but without a lot
5646 * of knowledge of the trigger semantics we can't do this in general.
5650 AfterTriggerPendingOnRel(Oid relid)
5652 AfterTriggerEvent event;
5653 AfterTriggerEventChunk *chunk;
5656 /* Scan queued events */
5657 for_each_event_chunk(event, chunk, afterTriggers.events)
5659 AfterTriggerShared evtshared = GetTriggerSharedData(event);
5662 * We can ignore completed events. (Even if a DONE flag is rolled
5663 * back by subxact abort, it's OK because the effects of the TRUNCATE
5664 * or whatever must get rolled back too.)
5666 if (event->ate_flags & AFTER_TRIGGER_DONE)
5669 if (evtshared->ats_relid == relid)
5674 * Also scan events queued by incomplete queries. This could only matter
5675 * if TRUNCATE/etc is executed by a function or trigger within an updating
5676 * query on the same relation, which is pretty perverse, but let's check.
5678 for (depth = 0; depth <= afterTriggers.query_depth && depth < afterTriggers.maxquerydepth; depth++)
5680 for_each_event_chunk(event, chunk, afterTriggers.query_stack[depth].events)
5682 AfterTriggerShared evtshared = GetTriggerSharedData(event);
5684 if (event->ate_flags & AFTER_TRIGGER_DONE)
5687 if (evtshared->ats_relid == relid)
5697 * AfterTriggerSaveEvent()
5699 * Called by ExecA[RS]...Triggers() to queue up the triggers that should
5700 * be fired for an event.
5702 * NOTE: this is called whenever there are any triggers associated with
5703 * the event (even if they are disabled). This function decides which
5704 * triggers actually need to be queued. It is also called after each row,
5705 * even if there are no triggers for that event, if there are any AFTER
5706 * STATEMENT triggers for the statement which use transition tables, so that
5707 * the transition tuplestores can be built. Furthermore, if the transition
5708 * capture is happening for UPDATEd rows being moved to another partition due
5709 * to the partition-key being changed, then this function is called once when
5710 * the row is deleted (to capture OLD row), and once when the row is inserted
5711 * into another partition (to capture NEW row). This is done separately because
5712 * DELETE and INSERT happen on different tables.
5714 * Transition tuplestores are built now, rather than when events are pulled
5715 * off of the queue because AFTER ROW triggers are allowed to select from the
5716 * transition tables for the statement.
5720 AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
5721 int event, bool row_trigger,
5722 TupleTableSlot *oldslot, TupleTableSlot *newslot,
5723 List *recheckIndexes, Bitmapset *modifiedCols,
5724 TransitionCaptureState *transition_capture)
5726 Relation rel = relinfo->ri_RelationDesc;
5727 TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
5728 AfterTriggerEventData new_event;
5729 AfterTriggerSharedData new_shared;
5730 char relkind = rel->rd_rel->relkind;
5734 Tuplestorestate *fdw_tuplestore = NULL;
5737 * Check state. We use a normal test not Assert because it is possible to
5738 * reach here in the wrong state given misconfigured RI triggers, in
5739 * particular deferring a cascade action trigger.
5741 if (afterTriggers.query_depth < 0)
5742 elog(ERROR, "AfterTriggerSaveEvent() called outside of query");
5744 /* Be sure we have enough space to record events at this query depth. */
5745 if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
5746 AfterTriggerEnlargeQueryState();
5749 * If the directly named relation has any triggers with transition tables,
5750 * then we need to capture transition tuples.
5752 if (row_trigger && transition_capture != NULL)
5754 TupleTableSlot *original_insert_tuple = transition_capture->tcs_original_insert_tuple;
5755 TupleConversionMap *map = transition_capture->tcs_map;
5756 bool delete_old_table = transition_capture->tcs_delete_old_table;
5757 bool update_old_table = transition_capture->tcs_update_old_table;
5758 bool update_new_table = transition_capture->tcs_update_new_table;
5759 bool insert_new_table = transition_capture->tcs_insert_new_table;
5762 * For INSERT events NEW should be non-NULL, for DELETE events OLD
5763 * should be non-NULL, whereas for UPDATE events normally both OLD and
5764 * NEW are non-NULL. But for UPDATE events fired for capturing
5765 * transition tuples during UPDATE partition-key row movement, OLD is
5766 * NULL when the event is for a row being inserted, whereas NEW is
5767 * NULL when the event is for a row being deleted.
5769 Assert(!(event == TRIGGER_EVENT_DELETE && delete_old_table &&
5770 TupIsNull(oldslot)));
5771 Assert(!(event == TRIGGER_EVENT_INSERT && insert_new_table &&
5772 TupIsNull(newslot)));
5774 if (!TupIsNull(oldslot) &&
5775 ((event == TRIGGER_EVENT_DELETE && delete_old_table) ||
5776 (event == TRIGGER_EVENT_UPDATE && update_old_table)))
5778 Tuplestorestate *old_tuplestore;
5780 old_tuplestore = transition_capture->tcs_private->old_tuplestore;
5784 TupleTableSlot *storeslot;
5786 storeslot = transition_capture->tcs_private->storeslot;
5789 storeslot = ExecAllocTableSlot(&estate->es_tupleTable,
5792 transition_capture->tcs_private->storeslot = storeslot;
5795 execute_attr_map_slot(map->attrMap, oldslot, storeslot);
5796 tuplestore_puttupleslot(old_tuplestore, storeslot);
5799 tuplestore_puttupleslot(old_tuplestore, oldslot);
5801 if (!TupIsNull(newslot) &&
5802 ((event == TRIGGER_EVENT_INSERT && insert_new_table) ||
5803 (event == TRIGGER_EVENT_UPDATE && update_new_table)))
5805 Tuplestorestate *new_tuplestore;
5807 new_tuplestore = transition_capture->tcs_private->new_tuplestore;
5809 if (original_insert_tuple != NULL)
5810 tuplestore_puttupleslot(new_tuplestore,
5811 original_insert_tuple);
5812 else if (map != NULL)
5814 TupleTableSlot *storeslot;
5816 storeslot = transition_capture->tcs_private->storeslot;
5820 storeslot = ExecAllocTableSlot(&estate->es_tupleTable,
5823 transition_capture->tcs_private->storeslot = storeslot;
5826 execute_attr_map_slot(map->attrMap, newslot, storeslot);
5827 tuplestore_puttupleslot(new_tuplestore, storeslot);
5830 tuplestore_puttupleslot(new_tuplestore, newslot);
5834 * If transition tables are the only reason we're here, return. As
5835 * mentioned above, we can also be here during update tuple routing in
5836 * presence of transition tables, in which case this function is
5837 * called separately for oldtup and newtup, so we expect exactly one
5838 * of them to be NULL.
5840 if (trigdesc == NULL ||
5841 (event == TRIGGER_EVENT_DELETE && !trigdesc->trig_delete_after_row) ||
5842 (event == TRIGGER_EVENT_INSERT && !trigdesc->trig_insert_after_row) ||
5843 (event == TRIGGER_EVENT_UPDATE && !trigdesc->trig_update_after_row) ||
5844 (event == TRIGGER_EVENT_UPDATE && (TupIsNull(oldslot) ^ TupIsNull(newslot))))
5849 * Validate the event code and collect the associated tuple CTIDs.
5851 * The event code will be used both as a bitmask and an array offset, so
5852 * validation is important to make sure we don't walk off the edge of our
5855 * Also, if we're considering statement-level triggers, check whether we
5856 * already queued a set of them for this event, and cancel the prior set
5857 * if so. This preserves the behavior that statement-level triggers fire
5858 * just once per statement and fire after row-level triggers.
5862 case TRIGGER_EVENT_INSERT:
5863 tgtype_event = TRIGGER_TYPE_INSERT;
5866 Assert(oldslot == NULL);
5867 Assert(newslot != NULL);
5868 ItemPointerCopy(&(newslot->tts_tid), &(new_event.ate_ctid1));
5869 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5873 Assert(oldslot == NULL);
5874 Assert(newslot == NULL);
5875 ItemPointerSetInvalid(&(new_event.ate_ctid1));
5876 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5877 cancel_prior_stmt_triggers(RelationGetRelid(rel),
5881 case TRIGGER_EVENT_DELETE:
5882 tgtype_event = TRIGGER_TYPE_DELETE;
5885 Assert(oldslot != NULL);
5886 Assert(newslot == NULL);
5887 ItemPointerCopy(&(oldslot->tts_tid), &(new_event.ate_ctid1));
5888 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5892 Assert(oldslot == NULL);
5893 Assert(newslot == NULL);
5894 ItemPointerSetInvalid(&(new_event.ate_ctid1));
5895 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5896 cancel_prior_stmt_triggers(RelationGetRelid(rel),
5900 case TRIGGER_EVENT_UPDATE:
5901 tgtype_event = TRIGGER_TYPE_UPDATE;
5904 Assert(oldslot != NULL);
5905 Assert(newslot != NULL);
5906 ItemPointerCopy(&(oldslot->tts_tid), &(new_event.ate_ctid1));
5907 ItemPointerCopy(&(newslot->tts_tid), &(new_event.ate_ctid2));
5911 Assert(oldslot == NULL);
5912 Assert(newslot == NULL);
5913 ItemPointerSetInvalid(&(new_event.ate_ctid1));
5914 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5915 cancel_prior_stmt_triggers(RelationGetRelid(rel),
5919 case TRIGGER_EVENT_TRUNCATE:
5920 tgtype_event = TRIGGER_TYPE_TRUNCATE;
5921 Assert(oldslot == NULL);
5922 Assert(newslot == NULL);
5923 ItemPointerSetInvalid(&(new_event.ate_ctid1));
5924 ItemPointerSetInvalid(&(new_event.ate_ctid2));
5927 elog(ERROR, "invalid after-trigger event code: %d", event);
5928 tgtype_event = 0; /* keep compiler quiet */
5932 if (!(relkind == RELKIND_FOREIGN_TABLE && row_trigger))
5933 new_event.ate_flags = (row_trigger && event == TRIGGER_EVENT_UPDATE) ?
5934 AFTER_TRIGGER_2CTID : AFTER_TRIGGER_1CTID;
5935 /* else, we'll initialize ate_flags for each trigger */
5937 tgtype_level = (row_trigger ? TRIGGER_TYPE_ROW : TRIGGER_TYPE_STATEMENT);
5939 for (i = 0; i < trigdesc->numtriggers; i++)
5941 Trigger *trigger = &trigdesc->triggers[i];
5943 if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
5948 if (!TriggerEnabled(estate, relinfo, trigger, event,
5949 modifiedCols, oldslot, newslot))
5952 if (relkind == RELKIND_FOREIGN_TABLE && row_trigger)
5954 if (fdw_tuplestore == NULL)
5956 fdw_tuplestore = GetCurrentFDWTuplestore();
5957 new_event.ate_flags = AFTER_TRIGGER_FDW_FETCH;
5960 /* subsequent event for the same tuple */
5961 new_event.ate_flags = AFTER_TRIGGER_FDW_REUSE;
5965 * If the trigger is a foreign key enforcement trigger, there are
5966 * certain cases where we can skip queueing the event because we can
5967 * tell by inspection that the FK constraint will still pass.
5969 if (TRIGGER_FIRED_BY_UPDATE(event) || TRIGGER_FIRED_BY_DELETE(event))
5971 switch (RI_FKey_trigger_type(trigger->tgfoid))
5974 /* Update or delete on trigger's PK table */
5975 if (!RI_FKey_pk_upd_check_required(trigger, rel,
5978 /* skip queuing this event */
5984 /* Update on trigger's FK table */
5985 if (!RI_FKey_fk_upd_check_required(trigger, rel,
5988 /* skip queuing this event */
5993 case RI_TRIGGER_NONE:
5994 /* Not an FK trigger */
6000 * If the trigger is a deferred unique constraint check trigger, only
6001 * queue it if the unique constraint was potentially violated, which
6002 * we know from index insertion time.
6004 if (trigger->tgfoid == F_UNIQUE_KEY_RECHECK)
6006 if (!list_member_oid(recheckIndexes, trigger->tgconstrindid))
6007 continue; /* Uniqueness definitely not violated */
6011 * Fill in event structure and add it to the current query's queue.
6012 * Note we set ats_table to NULL whenever this trigger doesn't use
6013 * transition tables, to improve sharability of the shared event data.
6015 new_shared.ats_event =
6016 (event & TRIGGER_EVENT_OPMASK) |
6017 (row_trigger ? TRIGGER_EVENT_ROW : 0) |
6018 (trigger->tgdeferrable ? AFTER_TRIGGER_DEFERRABLE : 0) |
6019 (trigger->tginitdeferred ? AFTER_TRIGGER_INITDEFERRED : 0);
6020 new_shared.ats_tgoid = trigger->tgoid;
6021 new_shared.ats_relid = RelationGetRelid(rel);
6022 new_shared.ats_firing_id = 0;
6023 if ((trigger->tgoldtable || trigger->tgnewtable) &&
6024 transition_capture != NULL)
6025 new_shared.ats_table = transition_capture->tcs_private;
6027 new_shared.ats_table = NULL;
6029 afterTriggerAddEvent(&afterTriggers.query_stack[afterTriggers.query_depth].events,
6030 &new_event, &new_shared);
6034 * Finally, spool any foreign tuple(s). The tuplestore squashes them to
6035 * minimal tuples, so this loses any system columns. The executor lost
6036 * those columns before us, for an unrelated reason, so this is fine.
6040 if (oldslot != NULL)
6041 tuplestore_puttupleslot(fdw_tuplestore, oldslot);
6042 if (newslot != NULL)
6043 tuplestore_puttupleslot(fdw_tuplestore, newslot);
6048 * Detect whether we already queued BEFORE STATEMENT triggers for the given
6049 * relation + operation, and set the flag so the next call will report "true".
6052 before_stmt_triggers_fired(Oid relid, CmdType cmdType)
6055 AfterTriggersTableData *table;
6057 /* Check state, like AfterTriggerSaveEvent. */
6058 if (afterTriggers.query_depth < 0)
6059 elog(ERROR, "before_stmt_triggers_fired() called outside of query");
6061 /* Be sure we have enough space to record events at this query depth. */
6062 if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
6063 AfterTriggerEnlargeQueryState();
6066 * We keep this state in the AfterTriggersTableData that also holds
6067 * transition tables for the relation + operation. In this way, if we are
6068 * forced to make a new set of transition tables because more tuples get
6069 * entered after we've already fired triggers, we will allow a new set of
6070 * statement triggers to get queued.
6072 table = GetAfterTriggersTableData(relid, cmdType);
6073 result = table->before_trig_done;
6074 table->before_trig_done = true;
6079 * If we previously queued a set of AFTER STATEMENT triggers for the given
6080 * relation + operation, and they've not been fired yet, cancel them. The
6081 * caller will queue a fresh set that's after any row-level triggers that may
6082 * have been queued by the current sub-statement, preserving (as much as
6083 * possible) the property that AFTER ROW triggers fire before AFTER STATEMENT
6084 * triggers, and that the latter only fire once. This deals with the
6085 * situation where several FK enforcement triggers sequentially queue triggers
6086 * for the same table into the same trigger query level. We can't fully
6087 * prevent odd behavior though: if there are AFTER ROW triggers taking
6088 * transition tables, we don't want to change the transition tables once the
6089 * first such trigger has seen them. In such a case, any additional events
6090 * will result in creating new transition tables and allowing new firings of
6091 * statement triggers.
6093 * This also saves the current event list location so that a later invocation
6094 * of this function can cheaply find the triggers we're about to queue and
6098 cancel_prior_stmt_triggers(Oid relid, CmdType cmdType, int tgevent)
6100 AfterTriggersTableData *table;
6101 AfterTriggersQueryData *qs = &afterTriggers.query_stack[afterTriggers.query_depth];
6104 * We keep this state in the AfterTriggersTableData that also holds
6105 * transition tables for the relation + operation. In this way, if we are
6106 * forced to make a new set of transition tables because more tuples get
6107 * entered after we've already fired triggers, we will allow a new set of
6108 * statement triggers to get queued without canceling the old ones.
6110 table = GetAfterTriggersTableData(relid, cmdType);
6112 if (table->after_trig_done)
6115 * We want to start scanning from the tail location that existed just
6116 * before we inserted any statement triggers. But the events list
6117 * might've been entirely empty then, in which case scan from the
6120 AfterTriggerEvent event;
6121 AfterTriggerEventChunk *chunk;
6123 if (table->after_trig_events.tail)
6125 chunk = table->after_trig_events.tail;
6126 event = (AfterTriggerEvent) table->after_trig_events.tailfree;
6130 chunk = qs->events.head;
6134 for_each_chunk_from(chunk)
6137 event = (AfterTriggerEvent) CHUNK_DATA_START(chunk);
6138 for_each_event_from(event, chunk)
6140 AfterTriggerShared evtshared = GetTriggerSharedData(event);
6143 * Exit loop when we reach events that aren't AS triggers for
6144 * the target relation.
6146 if (evtshared->ats_relid != relid)
6148 if ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) != tgevent)
6150 if (!TRIGGER_FIRED_FOR_STATEMENT(evtshared->ats_event))
6152 if (!TRIGGER_FIRED_AFTER(evtshared->ats_event))
6154 /* OK, mark it DONE */
6155 event->ate_flags &= ~AFTER_TRIGGER_IN_PROGRESS;
6156 event->ate_flags |= AFTER_TRIGGER_DONE;
6158 /* signal we must reinitialize event ptr for next chunk */
6164 /* In any case, save current insertion point for next time */
6165 table->after_trig_done = true;
6166 table->after_trig_events = qs->events;
6170 * SQL function pg_trigger_depth()
6173 pg_trigger_depth(PG_FUNCTION_ARGS)
6175 PG_RETURN_INT32(MyTriggerDepth);