Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * trigger.c
4 : : * PostgreSQL TRIGGERs support code.
5 : : *
6 : : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/commands/trigger.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
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/indexing.h"
26 : : #include "catalog/objectaccess.h"
27 : : #include "catalog/partition.h"
28 : : #include "catalog/pg_constraint.h"
29 : : #include "catalog/pg_inherits.h"
30 : : #include "catalog/pg_proc.h"
31 : : #include "catalog/pg_trigger.h"
32 : : #include "catalog/pg_type.h"
33 : : #include "commands/dbcommands.h"
34 : : #include "commands/trigger.h"
35 : : #include "executor/executor.h"
36 : : #include "miscadmin.h"
37 : : #include "nodes/bitmapset.h"
38 : : #include "nodes/makefuncs.h"
39 : : #include "optimizer/optimizer.h"
40 : : #include "parser/parse_clause.h"
41 : : #include "parser/parse_collate.h"
42 : : #include "parser/parse_func.h"
43 : : #include "parser/parse_relation.h"
44 : : #include "partitioning/partdesc.h"
45 : : #include "pgstat.h"
46 : : #include "rewrite/rewriteManip.h"
47 : : #include "storage/lmgr.h"
48 : : #include "utils/acl.h"
49 : : #include "utils/builtins.h"
50 : : #include "utils/fmgroids.h"
51 : : #include "utils/guc_hooks.h"
52 : : #include "utils/inval.h"
53 : : #include "utils/lsyscache.h"
54 : : #include "utils/memutils.h"
55 : : #include "utils/plancache.h"
56 : : #include "utils/rel.h"
57 : : #include "utils/snapmgr.h"
58 : : #include "utils/syscache.h"
59 : : #include "utils/tuplestore.h"
60 : :
61 : :
62 : : /* GUC variables */
63 : : int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN;
64 : :
65 : : /* How many levels deep into trigger execution are we? */
66 : : static int MyTriggerDepth = 0;
67 : :
68 : : /* Local function prototypes */
69 : : static void renametrig_internal(Relation tgrel, Relation targetrel,
70 : : HeapTuple trigtup, const char *newname,
71 : : const char *expected_name);
72 : : static void renametrig_partition(Relation tgrel, Oid partitionId,
73 : : Oid parentTriggerOid, const char *newname,
74 : : const char *expected_name);
75 : : static void SetTriggerFlags(TriggerDesc *trigdesc, Trigger *trigger);
76 : : static bool GetTupleForTrigger(EState *estate,
77 : : EPQState *epqstate,
78 : : ResultRelInfo *relinfo,
79 : : ItemPointer tid,
80 : : LockTupleMode lockmode,
81 : : TupleTableSlot *oldslot,
82 : : TupleTableSlot **epqslot,
83 : : TM_Result *tmresultp,
84 : : TM_FailureData *tmfdp);
85 : : static bool TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
86 : : Trigger *trigger, TriggerEvent event,
87 : : Bitmapset *modifiedCols,
88 : : TupleTableSlot *oldslot, TupleTableSlot *newslot);
89 : : static HeapTuple ExecCallTriggerFunc(TriggerData *trigdata,
90 : : int tgindx,
91 : : FmgrInfo *finfo,
92 : : Instrumentation *instr,
93 : : MemoryContext per_tuple_context);
94 : : static void AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
95 : : ResultRelInfo *src_partinfo,
96 : : ResultRelInfo *dst_partinfo,
97 : : int event, bool row_trigger,
98 : : TupleTableSlot *oldslot, TupleTableSlot *newslot,
99 : : List *recheckIndexes, Bitmapset *modifiedCols,
100 : : TransitionCaptureState *transition_capture,
101 : : bool is_crosspart_update);
102 : : static void AfterTriggerEnlargeQueryState(void);
103 : : static bool before_stmt_triggers_fired(Oid relid, CmdType cmdType);
104 : :
105 : :
106 : : /*
107 : : * Create a trigger. Returns the address of the created trigger.
108 : : *
109 : : * queryString is the source text of the CREATE TRIGGER command.
110 : : * This must be supplied if a whenClause is specified, else it can be NULL.
111 : : *
112 : : * relOid, if nonzero, is the relation on which the trigger should be
113 : : * created. If zero, the name provided in the statement will be looked up.
114 : : *
115 : : * refRelOid, if nonzero, is the relation to which the constraint trigger
116 : : * refers. If zero, the constraint relation name provided in the statement
117 : : * will be looked up as needed.
118 : : *
119 : : * constraintOid, if nonzero, says that this trigger is being created
120 : : * internally to implement that constraint. A suitable pg_depend entry will
121 : : * be made to link the trigger to that constraint. constraintOid is zero when
122 : : * executing a user-entered CREATE TRIGGER command. (For CREATE CONSTRAINT
123 : : * TRIGGER, we build a pg_constraint entry internally.)
124 : : *
125 : : * indexOid, if nonzero, is the OID of an index associated with the constraint.
126 : : * We do nothing with this except store it into pg_trigger.tgconstrindid;
127 : : * but when creating a trigger for a deferrable unique constraint on a
128 : : * partitioned table, its children are looked up. Note we don't cope with
129 : : * invalid indexes in that case.
130 : : *
131 : : * funcoid, if nonzero, is the OID of the function to invoke. When this is
132 : : * given, stmt->funcname is ignored.
133 : : *
134 : : * parentTriggerOid, if nonzero, is a trigger that begets this one; so that
135 : : * if that trigger is dropped, this one should be too. There are two cases
136 : : * when a nonzero value is passed for this: 1) when this function recurses to
137 : : * create the trigger on partitions, 2) when creating child foreign key
138 : : * triggers; see CreateFKCheckTrigger() and createForeignKeyActionTriggers().
139 : : *
140 : : * If whenClause is passed, it is an already-transformed expression for
141 : : * WHEN. In this case, we ignore any that may come in stmt->whenClause.
142 : : *
143 : : * If isInternal is true then this is an internally-generated trigger.
144 : : * This argument sets the tgisinternal field of the pg_trigger entry, and
145 : : * if true causes us to modify the given trigger name to ensure uniqueness.
146 : : *
147 : : * When isInternal is not true we require ACL_TRIGGER permissions on the
148 : : * relation, as well as ACL_EXECUTE on the trigger function. For internal
149 : : * triggers the caller must apply any required permission checks.
150 : : *
151 : : * When called on partitioned tables, this function recurses to create the
152 : : * trigger on all the partitions, except if isInternal is true, in which
153 : : * case caller is expected to execute recursion on its own. in_partition
154 : : * indicates such a recursive call; outside callers should pass "false"
155 : : * (but see CloneRowTriggersToPartition).
156 : : */
157 : : ObjectAddress
5259 tgl@sss.pgh.pa.us 158 :CBC 7131 : CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
159 : : Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
160 : : Oid funcoid, Oid parentTriggerOid, Node *whenClause,
161 : : bool isInternal, bool in_partition)
162 : : {
163 : : return
1003 alvherre@alvh.no-ip. 164 : 7131 : CreateTriggerFiringOn(stmt, queryString, relOid, refRelOid,
165 : : constraintOid, indexOid, funcoid,
166 : : parentTriggerOid, whenClause, isInternal,
167 : : in_partition, TRIGGER_FIRES_ON_ORIGIN);
168 : : }
169 : :
170 : : /*
171 : : * Like the above; additionally the firing condition
172 : : * (always/origin/replica/disabled) can be specified.
173 : : */
174 : : ObjectAddress
175 : 7569 : CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
176 : : Oid relOid, Oid refRelOid, Oid constraintOid,
177 : : Oid indexOid, Oid funcoid, Oid parentTriggerOid,
178 : : Node *whenClause, bool isInternal, bool in_partition,
179 : : char trigger_fires_when)
180 : : {
181 : : int16 tgtype;
182 : : int ncolumns;
183 : : int16 *columns;
184 : : int2vector *tgattr;
185 : : List *whenRtable;
186 : : char *qual;
187 : : Datum values[Natts_pg_trigger];
188 : : bool nulls[Natts_pg_trigger];
189 : : Relation rel;
190 : : AclResult aclresult;
191 : : Relation tgrel;
192 : : Relation pgrel;
1247 tgl@sss.pgh.pa.us 193 : 7569 : HeapTuple tuple = NULL;
194 : : Oid funcrettype;
195 : 7569 : Oid trigoid = InvalidOid;
196 : : char internaltrigname[NAMEDATALEN];
197 : : char *trigname;
7864 198 : 7569 : Oid constrrelid = InvalidOid;
199 : : ObjectAddress myself,
200 : : referenced;
2718 kgrittn@postgresql.o 201 : 7569 : char *oldtablename = NULL;
202 : 7569 : char *newtablename = NULL;
203 : : bool partition_recurse;
1247 tgl@sss.pgh.pa.us 204 : 7569 : bool trigger_exists = false;
205 : 7569 : Oid existing_constraint_oid = InvalidOid;
206 : 7569 : bool existing_isInternal = false;
830 alvherre@alvh.no-ip. 207 : 7569 : bool existing_isClone = false;
208 : :
3709 rhaas@postgresql.org 209 [ + + ]: 7569 : if (OidIsValid(relOid))
1910 andres@anarazel.de 210 : 6015 : rel = table_open(relOid, ShareRowExclusiveLock);
211 : : else
212 : 1554 : rel = table_openrv(stmt->relation, ShareRowExclusiveLock);
213 : :
214 : : /*
215 : : * Triggers must be on tables or views, and there are additional
216 : : * relation-type-specific restrictions.
217 : : */
2214 alvherre@alvh.no-ip. 218 [ + + ]: 7569 : if (rel->rd_rel->relkind == RELKIND_RELATION)
219 : : {
220 : : /* Tables can't have INSTEAD OF triggers */
4935 tgl@sss.pgh.pa.us 221 [ + + ]: 6284 : if (stmt->timing != TRIGGER_TYPE_BEFORE &&
222 [ + + ]: 5641 : stmt->timing != TRIGGER_TYPE_AFTER)
223 [ + - ]: 9 : ereport(ERROR,
224 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
225 : : errmsg("\"%s\" is a table",
226 : : RelationGetRelationName(rel)),
227 : : errdetail("Tables cannot have INSTEAD OF triggers.")));
228 : : }
2214 alvherre@alvh.no-ip. 229 [ + + ]: 1285 : else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
230 : : {
231 : : /* Partitioned tables can't have INSTEAD OF triggers */
232 [ + + ]: 1131 : if (stmt->timing != TRIGGER_TYPE_BEFORE &&
233 [ + + ]: 1080 : stmt->timing != TRIGGER_TYPE_AFTER)
2685 rhaas@postgresql.org 234 [ + - ]: 3 : ereport(ERROR,
235 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
236 : : errmsg("\"%s\" is a table",
237 : : RelationGetRelationName(rel)),
238 : : errdetail("Tables cannot have INSTEAD OF triggers.")));
239 : :
240 : : /*
241 : : * FOR EACH ROW triggers have further restrictions
242 : : */
2214 alvherre@alvh.no-ip. 243 [ + + ]: 1128 : if (stmt->row)
244 : : {
245 : : /*
246 : : * Disallow use of transition tables.
247 : : *
248 : : * Note that we have another restriction about transition tables
249 : : * in partitions; search for 'has_superclass' below for an
250 : : * explanation. The check here is just to protect from the fact
251 : : * that if we allowed it here, the creation would succeed for a
252 : : * partitioned table with no partitions, but would be blocked by
253 : : * the other restriction when the first partition was created,
254 : : * which is very unfriendly behavior.
255 : : */
256 [ + + ]: 1019 : if (stmt->transitionRels != NIL)
257 [ + - ]: 3 : ereport(ERROR,
258 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259 : : errmsg("\"%s\" is a partitioned table",
260 : : RelationGetRelationName(rel)),
261 : : errdetail("ROW triggers with transition tables are not supported on partitioned tables.")));
262 : : }
263 : : }
4935 tgl@sss.pgh.pa.us 264 [ + + ]: 154 : else if (rel->rd_rel->relkind == RELKIND_VIEW)
265 : : {
266 : : /*
267 : : * Views can have INSTEAD OF triggers (which we check below are
268 : : * row-level), or statement-level BEFORE/AFTER triggers.
269 : : */
270 [ + + + + ]: 102 : if (stmt->timing != TRIGGER_TYPE_INSTEAD && stmt->row)
271 [ + - ]: 18 : ereport(ERROR,
272 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
273 : : errmsg("\"%s\" is a view",
274 : : RelationGetRelationName(rel)),
275 : : errdetail("Views cannot have row-level BEFORE or AFTER triggers.")));
276 : : /* Disallow TRUNCATE triggers on VIEWs */
277 [ + + ]: 84 : if (TRIGGER_FOR_TRUNCATE(stmt->events))
278 [ + - ]: 6 : ereport(ERROR,
279 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
280 : : errmsg("\"%s\" is a view",
281 : : RelationGetRelationName(rel)),
282 : : errdetail("Views cannot have TRUNCATE triggers.")));
283 : : }
3675 noah@leadboat.com 284 [ + - ]: 52 : else if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
285 : : {
286 [ + + ]: 52 : if (stmt->timing != TRIGGER_TYPE_BEFORE &&
287 [ - + ]: 27 : stmt->timing != TRIGGER_TYPE_AFTER)
3675 noah@leadboat.com 288 [ # # ]:UBC 0 : ereport(ERROR,
289 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
290 : : errmsg("\"%s\" is a foreign table",
291 : : RelationGetRelationName(rel)),
292 : : errdetail("Foreign tables cannot have INSTEAD OF triggers.")));
293 : :
294 : : /*
295 : : * We disallow constraint triggers to protect the assumption that
296 : : * triggers on FKs can't be deferred. See notes with AfterTriggers
297 : : * data structures, below.
298 : : */
3675 noah@leadboat.com 299 [ + + ]:CBC 52 : if (stmt->isconstraint)
300 [ + - ]: 3 : ereport(ERROR,
301 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
302 : : errmsg("\"%s\" is a foreign table",
303 : : RelationGetRelationName(rel)),
304 : : errdetail("Foreign tables cannot have constraint triggers.")));
305 : : }
306 : : else
7574 tgl@sss.pgh.pa.us 307 [ # # ]:UBC 0 : ereport(ERROR,
308 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
309 : : errmsg("relation \"%s\" cannot have triggers",
310 : : RelationGetRelationName(rel)),
311 : : errdetail_relkind_not_supported(rel->rd_rel->relkind)));
312 : :
8038 tgl@sss.pgh.pa.us 313 [ + + + + ]:CBC 7527 : if (!allowSystemTableMods && IsSystemRelation(rel))
7574 314 [ + - ]: 1 : ereport(ERROR,
315 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
316 : : errmsg("permission denied: \"%s\" is a system catalog",
317 : : RelationGetRelationName(rel))));
318 : :
3709 rhaas@postgresql.org 319 [ + + ]: 7526 : if (stmt->isconstraint)
320 : : {
321 : : /*
322 : : * We must take a lock on the target relation to protect against
323 : : * concurrent drop. It's not clear that AccessShareLock is strong
324 : : * enough, but we certainly need at least that much... otherwise, we
325 : : * might end up creating a pg_constraint entry referencing a
326 : : * nonexistent table.
327 : : */
328 [ + + ]: 5652 : if (OidIsValid(refRelOid))
329 : : {
330 : 5517 : LockRelationOid(refRelOid, AccessShareLock);
331 : 5517 : constrrelid = refRelOid;
332 : : }
333 [ + + ]: 135 : else if (stmt->constrrel != NULL)
334 : 12 : constrrelid = RangeVarGetRelid(stmt->constrrel, AccessShareLock,
335 : : false);
336 : : }
337 : :
338 : : /* permission checks */
5201 tgl@sss.pgh.pa.us 339 [ + + ]: 7526 : if (!isInternal)
340 : : {
7813 bruce@momjian.us 341 : 1949 : aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
342 : : ACL_TRIGGER);
7910 peter_e@gmx.net 343 [ - + ]: 1949 : if (aclresult != ACLCHECK_OK)
2325 peter_e@gmx.net 344 :UBC 0 : aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
7562 tgl@sss.pgh.pa.us 345 : 0 : RelationGetRelationName(rel));
346 : :
5561 tgl@sss.pgh.pa.us 347 [ + + ]:CBC 1949 : if (OidIsValid(constrrelid))
348 : : {
7813 bruce@momjian.us 349 : 21 : aclresult = pg_class_aclcheck(constrrelid, GetUserId(),
350 : : ACL_TRIGGER);
7910 peter_e@gmx.net 351 [ - + ]: 21 : if (aclresult != ACLCHECK_OK)
2325 peter_e@gmx.net 352 :UBC 0 : aclcheck_error(aclresult, get_relkind_objtype(get_rel_relkind(constrrelid)),
7562 tgl@sss.pgh.pa.us 353 : 0 : get_rel_name(constrrelid));
354 : : }
355 : : }
356 : :
357 : : /*
358 : : * When called on a partitioned table to create a FOR EACH ROW trigger
359 : : * that's not internal, we create one trigger for each partition, too.
360 : : *
361 : : * For that, we'd better hold lock on all of them ahead of time.
362 : : */
2214 alvherre@alvh.no-ip. 363 [ + + + + ]:CBC 8981 : partition_recurse = !isInternal && stmt->row &&
364 [ + + ]: 1455 : rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
365 [ + + ]: 7526 : if (partition_recurse)
366 : 205 : list_free(find_all_inheritors(RelationGetRelid(rel),
367 : : ShareRowExclusiveLock, NULL));
368 : :
369 : : /* Compute tgtype */
9716 bruce@momjian.us 370 : 7526 : TRIGGER_CLEAR_TYPE(tgtype);
371 [ + + ]: 7526 : if (stmt->row)
372 : 7032 : TRIGGER_SETT_ROW(tgtype);
4935 tgl@sss.pgh.pa.us 373 : 7526 : tgtype |= stmt->timing;
5414 374 : 7526 : tgtype |= stmt->events;
375 : :
376 : : /* Disallow ROW-level TRUNCATE triggers */
377 [ + + - + ]: 7526 : if (TRIGGER_FOR_ROW(tgtype) && TRIGGER_FOR_TRUNCATE(tgtype))
5414 tgl@sss.pgh.pa.us 378 [ # # ]:UBC 0 : ereport(ERROR,
379 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
380 : : errmsg("TRUNCATE FOR EACH ROW triggers are not supported")));
381 : :
382 : : /* INSTEAD triggers must be row-level, and can't have WHEN or columns */
4935 tgl@sss.pgh.pa.us 383 [ + + ]:CBC 7526 : if (TRIGGER_FOR_INSTEAD(tgtype))
384 : : {
385 [ + + ]: 57 : if (!TRIGGER_FOR_ROW(tgtype))
386 [ + - ]: 3 : ereport(ERROR,
387 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
388 : : errmsg("INSTEAD OF triggers must be FOR EACH ROW")));
389 [ + + ]: 54 : if (stmt->whenClause)
390 [ + - ]: 3 : ereport(ERROR,
391 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
392 : : errmsg("INSTEAD OF triggers cannot have WHEN conditions")));
393 [ + + ]: 51 : if (stmt->columns != NIL)
394 [ + - ]: 3 : ereport(ERROR,
395 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
396 : : errmsg("INSTEAD OF triggers cannot have column lists")));
397 : : }
398 : :
399 : : /*
400 : : * We don't yet support naming ROW transition variables, but the parser
401 : : * recognizes the syntax so we can give a nicer message here.
402 : : *
403 : : * Per standard, REFERENCING TABLE names are only allowed on AFTER
404 : : * triggers. Per standard, REFERENCING ROW names are not allowed with FOR
405 : : * EACH STATEMENT. Per standard, each OLD/NEW, ROW/TABLE permutation is
406 : : * only allowed once. Per standard, OLD may not be specified when
407 : : * creating a trigger only for INSERT, and NEW may not be specified when
408 : : * creating a trigger only for DELETE.
409 : : *
410 : : * Notice that the standard allows an AFTER ... FOR EACH ROW trigger to
411 : : * reference both ROW and TABLE transition data.
412 : : */
2718 kgrittn@postgresql.o 413 [ + + ]: 7517 : if (stmt->transitionRels != NIL)
414 : : {
415 : 209 : List *varList = stmt->transitionRels;
416 : : ListCell *lc;
417 : :
418 [ + - + + : 457 : foreach(lc, varList)
+ + ]
419 : : {
2524 bruce@momjian.us 420 : 272 : TriggerTransition *tt = lfirst_node(TriggerTransition, lc);
421 : :
2718 kgrittn@postgresql.o 422 [ - + ]: 272 : if (!(tt->isTable))
2718 kgrittn@postgresql.o 423 [ # # ]:UBC 0 : ereport(ERROR,
424 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
425 : : errmsg("ROW variable naming in the REFERENCING clause is not supported"),
426 : : errhint("Use OLD TABLE or NEW TABLE for naming transition tables.")));
427 : :
428 : : /*
429 : : * Because of the above test, we omit further ROW-related testing
430 : : * below. If we later allow naming OLD and NEW ROW variables,
431 : : * adjustments will be needed below.
432 : : */
433 : :
2532 rhaas@postgresql.org 434 [ + + ]:CBC 272 : if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
435 [ + - ]: 3 : ereport(ERROR,
436 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
437 : : errmsg("\"%s\" is a foreign table",
438 : : RelationGetRelationName(rel)),
439 : : errdetail("Triggers on foreign tables cannot have transition tables.")));
440 : :
441 [ + + ]: 269 : if (rel->rd_rel->relkind == RELKIND_VIEW)
442 [ + - ]: 3 : ereport(ERROR,
443 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
444 : : errmsg("\"%s\" is a view",
445 : : RelationGetRelationName(rel)),
446 : : errdetail("Triggers on views cannot have transition tables.")));
447 : :
448 : : /*
449 : : * We currently don't allow row-level triggers with transition
450 : : * tables on partition or inheritance children. Such triggers
451 : : * would somehow need to see tuples converted to the format of the
452 : : * table they're attached to, and it's not clear which subset of
453 : : * tuples each child should see. See also the prohibitions in
454 : : * ATExecAttachPartition() and ATExecAddInherit().
455 : : */
2482 rhodiumtoad@postgres 456 [ + + + + ]: 266 : if (TRIGGER_FOR_ROW(tgtype) && has_superclass(rel->rd_id))
457 : : {
458 : : /* Use appropriate error message. */
459 [ + + ]: 6 : if (rel->rd_rel->relispartition)
460 [ + - ]: 3 : ereport(ERROR,
461 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
462 : : errmsg("ROW triggers with transition tables are not supported on partitions")));
463 : : else
464 [ + - ]: 3 : ereport(ERROR,
465 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
466 : : errmsg("ROW triggers with transition tables are not supported on inheritance children")));
467 : : }
468 : :
2718 kgrittn@postgresql.o 469 [ - + ]: 260 : if (stmt->timing != TRIGGER_TYPE_AFTER)
2718 kgrittn@postgresql.o 470 [ # # ]:UBC 0 : ereport(ERROR,
471 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
472 : : errmsg("transition table name can only be specified for an AFTER trigger")));
473 : :
2532 rhaas@postgresql.org 474 [ + + ]:CBC 260 : if (TRIGGER_FOR_TRUNCATE(tgtype))
475 [ + - ]: 3 : ereport(ERROR,
476 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
477 : : errmsg("TRUNCATE triggers with transition tables are not supported")));
478 : :
479 : : /*
480 : : * We currently don't allow multi-event triggers ("INSERT OR
481 : : * UPDATE") with transition tables, because it's not clear how to
482 : : * handle INSERT ... ON CONFLICT statements which can fire both
483 : : * INSERT and UPDATE triggers. We show the inserted tuples to
484 : : * INSERT triggers and the updated tuples to UPDATE triggers, but
485 : : * it's not yet clear what INSERT OR UPDATE trigger should see.
486 : : * This restriction could be lifted if we can decide on the right
487 : : * semantics in a later release.
488 : : */
2482 rhodiumtoad@postgres 489 : 257 : if (((TRIGGER_FOR_INSERT(tgtype) ? 1 : 0) +
490 : 257 : (TRIGGER_FOR_UPDATE(tgtype) ? 1 : 0) +
491 [ + + ]: 257 : (TRIGGER_FOR_DELETE(tgtype) ? 1 : 0)) != 1)
492 [ + - ]: 3 : ereport(ERROR,
493 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
494 : : errmsg("transition tables cannot be specified for triggers with more than one event")));
495 : :
496 : : /*
497 : : * We currently don't allow column-specific triggers with
498 : : * transition tables. Per spec, that seems to require
499 : : * accumulating separate transition tables for each combination of
500 : : * columns, which is a lot of work for a rather marginal feature.
501 : : */
2402 tgl@sss.pgh.pa.us 502 [ + + ]: 254 : if (stmt->columns != NIL)
503 [ + - ]: 3 : ereport(ERROR,
504 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
505 : : errmsg("transition tables cannot be specified for triggers with column lists")));
506 : :
507 : : /*
508 : : * We disallow constraint triggers with transition tables, to
509 : : * protect the assumption that such triggers can't be deferred.
510 : : * See notes with AfterTriggers data structures, below.
511 : : *
512 : : * Currently this is enforced by the grammar, so just Assert here.
513 : : */
514 [ - + ]: 251 : Assert(!stmt->isconstraint);
515 : :
2718 kgrittn@postgresql.o 516 [ + + ]: 251 : if (tt->isNew)
517 : : {
518 [ + + ]: 132 : if (!(TRIGGER_FOR_INSERT(tgtype) ||
519 [ - + ]: 73 : TRIGGER_FOR_UPDATE(tgtype)))
2718 kgrittn@postgresql.o 520 [ # # ]:UBC 0 : ereport(ERROR,
521 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
522 : : errmsg("NEW TABLE can only be specified for an INSERT or UPDATE trigger")));
523 : :
2718 kgrittn@postgresql.o 524 [ - + ]:CBC 132 : if (newtablename != NULL)
2718 kgrittn@postgresql.o 525 [ # # ]:UBC 0 : ereport(ERROR,
526 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
527 : : errmsg("NEW TABLE cannot be specified multiple times")));
528 : :
2718 kgrittn@postgresql.o 529 :CBC 132 : newtablename = tt->name;
530 : : }
531 : : else
532 : : {
533 [ + + ]: 119 : if (!(TRIGGER_FOR_DELETE(tgtype) ||
534 [ + + ]: 70 : TRIGGER_FOR_UPDATE(tgtype)))
535 [ + - ]: 3 : ereport(ERROR,
536 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
537 : : errmsg("OLD TABLE can only be specified for a DELETE or UPDATE trigger")));
538 : :
539 [ - + ]: 116 : if (oldtablename != NULL)
2718 kgrittn@postgresql.o 540 [ # # ]:UBC 0 : ereport(ERROR,
541 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
542 : : errmsg("OLD TABLE cannot be specified multiple times")));
543 : :
2718 kgrittn@postgresql.o 544 :CBC 116 : oldtablename = tt->name;
545 : : }
546 : : }
547 : :
548 [ + + + + ]: 185 : if (newtablename != NULL && oldtablename != NULL &&
549 [ - + ]: 63 : strcmp(newtablename, oldtablename) == 0)
2718 kgrittn@postgresql.o 550 [ # # ]:UBC 0 : ereport(ERROR,
551 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
552 : : errmsg("OLD TABLE name and NEW TABLE name cannot be the same")));
553 : : }
554 : :
555 : : /*
556 : : * Parse the WHEN clause, if any and we weren't passed an already
557 : : * transformed one.
558 : : *
559 : : * Note that as a side effect, we fill whenRtable when parsing. If we got
560 : : * an already parsed clause, this does not occur, which is what we want --
561 : : * no point in adding redundant dependencies below.
562 : : */
2214 alvherre@alvh.no-ip. 563 [ + + + + ]:CBC 7493 : if (!whenClause && stmt->whenClause)
5259 tgl@sss.pgh.pa.us 564 : 55 : {
565 : : ParseState *pstate;
566 : : ParseNamespaceItem *nsitem;
567 : : List *varList;
568 : : ListCell *lc;
569 : :
570 : : /* Set up a pstate to parse with */
571 : 73 : pstate = make_parsestate(NULL);
572 : 73 : pstate->p_sourcetext = queryString;
573 : :
574 : : /*
575 : : * Set up nsitems for OLD and NEW references.
576 : : *
577 : : * 'OLD' must always have varno equal to 1 and 'NEW' equal to 2.
578 : : */
1564 579 : 73 : nsitem = addRangeTableEntryForRelation(pstate, rel,
580 : : AccessShareLock,
581 : : makeAlias("old", NIL),
582 : : false, false);
583 : 73 : addNSItemToQuery(pstate, nsitem, false, true, true);
584 : 73 : nsitem = addRangeTableEntryForRelation(pstate, rel,
585 : : AccessShareLock,
586 : : makeAlias("new", NIL),
587 : : false, false);
588 : 73 : addNSItemToQuery(pstate, nsitem, false, true, true);
589 : :
590 : : /* Transform expression. Copy to be sure we don't modify original */
5259 591 : 73 : whenClause = transformWhereClause(pstate,
592 : 73 : copyObject(stmt->whenClause),
593 : : EXPR_KIND_TRIGGER_WHEN,
594 : : "WHEN");
595 : : /* we have to fix its collations too */
4756 596 : 73 : assign_expr_collations(pstate, whenClause);
597 : :
598 : : /*
599 : : * Check for disallowed references to OLD/NEW.
600 : : *
601 : : * NB: pull_var_clause is okay here only because we don't allow
602 : : * subselects in WHEN clauses; it would fail to examine the contents
603 : : * of subselects.
604 : : */
2957 605 : 73 : varList = pull_var_clause(whenClause, 0);
5259 606 [ + + + + : 150 : foreach(lc, varList)
+ + ]
607 : : {
608 : 95 : Var *var = (Var *) lfirst(lc);
609 : :
610 [ + + - ]: 95 : switch (var->varno)
611 : : {
612 : 37 : case PRS2_OLD_VARNO:
613 [ + + ]: 37 : if (!TRIGGER_FOR_ROW(tgtype))
614 [ + - ]: 3 : ereport(ERROR,
615 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
616 : : errmsg("statement trigger's WHEN condition cannot reference column values"),
617 : : parser_errposition(pstate, var->location)));
618 [ + + ]: 34 : if (TRIGGER_FOR_INSERT(tgtype))
619 [ + - ]: 3 : ereport(ERROR,
620 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
621 : : errmsg("INSERT trigger's WHEN condition cannot reference OLD values"),
622 : : parser_errposition(pstate, var->location)));
623 : : /* system columns are okay here */
624 : 31 : break;
625 : 58 : case PRS2_NEW_VARNO:
626 [ - + ]: 58 : if (!TRIGGER_FOR_ROW(tgtype))
5259 tgl@sss.pgh.pa.us 627 [ # # ]:UBC 0 : ereport(ERROR,
628 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
629 : : errmsg("statement trigger's WHEN condition cannot reference column values"),
630 : : parser_errposition(pstate, var->location)));
5259 tgl@sss.pgh.pa.us 631 [ + + ]:CBC 58 : if (TRIGGER_FOR_DELETE(tgtype))
632 [ + - ]: 3 : ereport(ERROR,
633 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
634 : : errmsg("DELETE trigger's WHEN condition cannot reference NEW values"),
635 : : parser_errposition(pstate, var->location)));
636 [ + + + + ]: 55 : if (var->varattno < 0 && TRIGGER_FOR_BEFORE(tgtype))
637 [ + - ]: 3 : ereport(ERROR,
638 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
639 : : errmsg("BEFORE trigger's WHEN condition cannot reference NEW system columns"),
640 : : parser_errposition(pstate, var->location)));
1842 peter@eisentraut.org 641 [ + + ]: 52 : if (TRIGGER_FOR_BEFORE(tgtype) &&
642 [ + + ]: 17 : var->varattno == 0 &&
643 [ + + ]: 6 : RelationGetDescr(rel)->constr &&
644 [ + - ]: 3 : RelationGetDescr(rel)->constr->has_generated_stored)
645 [ + - ]: 3 : ereport(ERROR,
646 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
647 : : errmsg("BEFORE trigger's WHEN condition cannot reference NEW generated columns"),
648 : : errdetail("A whole-row reference is used and the table contains generated columns."),
649 : : parser_errposition(pstate, var->location)));
650 [ + + ]: 49 : if (TRIGGER_FOR_BEFORE(tgtype) &&
651 [ + + ]: 14 : var->varattno > 0 &&
652 [ + + ]: 11 : TupleDescAttr(RelationGetDescr(rel), var->varattno - 1)->attgenerated)
653 [ + - ]: 3 : ereport(ERROR,
654 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
655 : : errmsg("BEFORE trigger's WHEN condition cannot reference NEW generated columns"),
656 : : errdetail("Column \"%s\" is a generated column.",
657 : : NameStr(TupleDescAttr(RelationGetDescr(rel), var->varattno - 1)->attname)),
658 : : parser_errposition(pstate, var->location)));
5259 tgl@sss.pgh.pa.us 659 : 46 : break;
5259 tgl@sss.pgh.pa.us 660 :UBC 0 : default:
661 : : /* can't happen without add_missing_from, so just elog */
662 [ # # ]: 0 : elog(ERROR, "trigger WHEN condition cannot contain references to other relations");
663 : : break;
664 : : }
665 : : }
666 : :
667 : : /* we'll need the rtable for recordDependencyOnExpr */
5259 tgl@sss.pgh.pa.us 668 :CBC 55 : whenRtable = pstate->p_rtable;
669 : :
670 : 55 : qual = nodeToString(whenClause);
671 : :
672 : 55 : free_parsestate(pstate);
673 : : }
2214 alvherre@alvh.no-ip. 674 [ + + ]: 7420 : else if (!whenClause)
675 : : {
5259 tgl@sss.pgh.pa.us 676 : 7399 : whenClause = NULL;
677 : 7399 : whenRtable = NIL;
678 : 7399 : qual = NULL;
679 : : }
680 : : else
681 : : {
2214 alvherre@alvh.no-ip. 682 : 21 : qual = nodeToString(whenClause);
683 : 21 : whenRtable = NIL;
684 : : }
685 : :
686 : : /*
687 : : * Find and validate the trigger function.
688 : : */
689 [ + + ]: 7475 : if (!OidIsValid(funcoid))
1615 690 : 7037 : funcoid = LookupFuncName(stmt->funcname, 0, NULL, false);
4434 tgl@sss.pgh.pa.us 691 [ + + ]: 7475 : if (!isInternal)
692 : : {
518 peter@eisentraut.org 693 : 1898 : aclresult = object_aclcheck(ProcedureRelationId, funcoid, GetUserId(), ACL_EXECUTE);
4434 tgl@sss.pgh.pa.us 694 [ - + ]: 1898 : if (aclresult != ACLCHECK_OK)
2325 peter_e@gmx.net 695 :UBC 0 : aclcheck_error(aclresult, OBJECT_FUNCTION,
4434 tgl@sss.pgh.pa.us 696 : 0 : NameListToString(stmt->funcname));
697 : : }
6006 tgl@sss.pgh.pa.us 698 :CBC 7475 : funcrettype = get_func_rettype(funcoid);
699 [ - + ]: 7475 : if (funcrettype != TRIGGEROID)
1501 tgl@sss.pgh.pa.us 700 [ # # ]:UBC 0 : ereport(ERROR,
701 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
702 : : errmsg("function %s must return type %s",
703 : : NameListToString(stmt->funcname), "trigger")));
704 : :
705 : : /*
706 : : * Scan pg_trigger to see if there is already a trigger of the same name.
707 : : * Skip this for internally generated triggers, since we'll modify the
708 : : * name to be unique below.
709 : : *
710 : : * NOTE that this is cool only because we have ShareRowExclusiveLock on
711 : : * the relation, so the trigger set won't be changing underneath us.
712 : : */
1247 tgl@sss.pgh.pa.us 713 :CBC 7475 : tgrel = table_open(TriggerRelationId, RowExclusiveLock);
714 [ + + ]: 7475 : if (!isInternal)
715 : : {
716 : : ScanKeyData skeys[2];
717 : : SysScanDesc tgscan;
718 : :
719 : 1898 : ScanKeyInit(&skeys[0],
720 : : Anum_pg_trigger_tgrelid,
721 : : BTEqualStrategyNumber, F_OIDEQ,
722 : : ObjectIdGetDatum(RelationGetRelid(rel)));
723 : :
724 : 1898 : ScanKeyInit(&skeys[1],
725 : : Anum_pg_trigger_tgname,
726 : : BTEqualStrategyNumber, F_NAMEEQ,
727 : 1898 : CStringGetDatum(stmt->trigname));
728 : :
729 : 1898 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
730 : : NULL, 2, skeys);
731 : :
732 : : /* There should be at most one matching tuple */
733 [ + + ]: 1898 : if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
734 : : {
735 : 51 : Form_pg_trigger oldtrigger = (Form_pg_trigger) GETSTRUCT(tuple);
736 : :
737 : 51 : trigoid = oldtrigger->oid;
738 : 51 : existing_constraint_oid = oldtrigger->tgconstraint;
739 : 51 : existing_isInternal = oldtrigger->tgisinternal;
830 alvherre@alvh.no-ip. 740 : 51 : existing_isClone = OidIsValid(oldtrigger->tgparentid);
1247 tgl@sss.pgh.pa.us 741 : 51 : trigger_exists = true;
742 : : /* copy the tuple to use in CatalogTupleUpdate() */
743 : 51 : tuple = heap_copytuple(tuple);
744 : : }
745 : 1898 : systable_endscan(tgscan);
746 : : }
747 : :
748 [ + + ]: 7475 : if (!trigger_exists)
749 : : {
750 : : /* Generate the OID for the new trigger. */
751 : 7424 : trigoid = GetNewOidWithIndex(tgrel, TriggerOidIndexId,
752 : : Anum_pg_trigger_oid);
753 : : }
754 : : else
755 : : {
756 : : /*
757 : : * If OR REPLACE was specified, we'll replace the old trigger;
758 : : * otherwise complain about the duplicate name.
759 : : */
760 [ + + ]: 51 : if (!stmt->replace)
761 [ + - ]: 9 : ereport(ERROR,
762 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
763 : : errmsg("trigger \"%s\" for relation \"%s\" already exists",
764 : : stmt->trigname, RelationGetRelationName(rel))));
765 : :
766 : : /*
767 : : * An internal trigger or a child trigger (isClone) cannot be replaced
768 : : * by a user-defined trigger. However, skip this test when
769 : : * in_partition, because then we're recursing from a partitioned table
770 : : * and the check was made at the parent level.
771 : : */
830 alvherre@alvh.no-ip. 772 [ + - + + ]: 42 : if ((existing_isInternal || existing_isClone) &&
773 [ + - + + ]: 30 : !isInternal && !in_partition)
1247 tgl@sss.pgh.pa.us 774 [ + - ]: 3 : ereport(ERROR,
775 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
776 : : errmsg("trigger \"%s\" for relation \"%s\" is an internal or a child trigger",
777 : : stmt->trigname, RelationGetRelationName(rel))));
778 : :
779 : : /*
780 : : * It is not allowed to replace with a constraint trigger; gram.y
781 : : * should have enforced this already.
782 : : */
783 [ - + ]: 39 : Assert(!stmt->isconstraint);
784 : :
785 : : /*
786 : : * It is not allowed to replace an existing constraint trigger,
787 : : * either. (The reason for these restrictions is partly that it seems
788 : : * difficult to deal with pending trigger events in such cases, and
789 : : * partly that the command might imply changing the constraint's
790 : : * properties as well, which doesn't seem nice.)
791 : : */
792 [ - + ]: 39 : if (OidIsValid(existing_constraint_oid))
1247 tgl@sss.pgh.pa.us 793 [ # # ]:UBC 0 : ereport(ERROR,
794 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
795 : : errmsg("trigger \"%s\" for relation \"%s\" is a constraint trigger",
796 : : stmt->trigname, RelationGetRelationName(rel))));
797 : : }
798 : :
799 : : /*
800 : : * If it's a user-entered CREATE CONSTRAINT TRIGGER command, make a
801 : : * corresponding pg_constraint entry.
802 : : */
5201 tgl@sss.pgh.pa.us 803 [ + + + + ]:CBC 7463 : if (stmt->isconstraint && !OidIsValid(constraintOid))
804 : : {
805 : : /* Internal callers should have made their own constraints */
806 [ - + ]: 75 : Assert(!isInternal);
807 : 75 : constraintOid = CreateConstraintEntry(stmt->trigname,
808 : 75 : RelationGetNamespace(rel),
809 : : CONSTRAINT_TRIGGER,
810 : 75 : stmt->deferrable,
811 : 75 : stmt->initdeferred,
812 : : true,
813 : : InvalidOid, /* no parent */
814 : : RelationGetRelid(rel),
815 : : NULL, /* no conkey */
816 : : 0,
817 : : 0,
818 : : InvalidOid, /* no domain */
819 : : InvalidOid, /* no index */
820 : : InvalidOid, /* no foreign key */
821 : : NULL,
822 : : NULL,
823 : : NULL,
824 : : NULL,
825 : : 0,
826 : : ' ',
827 : : ' ',
828 : : NULL,
829 : : 0,
830 : : ' ',
831 : : NULL, /* no exclusion */
832 : : NULL, /* no check constraint */
833 : : NULL,
834 : : true, /* islocal */
835 : : 0, /* inhcount */
836 : : true, /* noinherit */
837 : : false, /* conperiod */
838 : : isInternal); /* is_internal */
839 : : }
840 : :
841 : : /*
842 : : * If trigger is internally generated, modify the provided trigger name to
843 : : * ensure uniqueness by appending the trigger OID. (Callers will usually
844 : : * supply a simple constant trigger name in these cases.)
845 : : */
846 [ + + ]: 7463 : if (isInternal)
847 : : {
848 : 5577 : snprintf(internaltrigname, sizeof(internaltrigname),
849 : : "%s_%u", stmt->trigname, trigoid);
850 : 5577 : trigname = internaltrigname;
851 : : }
852 : : else
853 : : {
854 : : /* user-defined trigger; use the specified trigger name as-is */
6006 855 : 1886 : trigname = stmt->trigname;
856 : : }
857 : :
858 : : /*
859 : : * Build the new pg_trigger tuple.
860 : : */
5642 861 : 7463 : memset(nulls, false, sizeof(nulls));
862 : :
1972 andres@anarazel.de 863 : 7463 : values[Anum_pg_trigger_oid - 1] = ObjectIdGetDatum(trigoid);
9370 bruce@momjian.us 864 : 7463 : values[Anum_pg_trigger_tgrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel));
1508 alvherre@alvh.no-ip. 865 : 7463 : values[Anum_pg_trigger_tgparentid - 1] = ObjectIdGetDatum(parentTriggerOid);
8655 tgl@sss.pgh.pa.us 866 : 7463 : values[Anum_pg_trigger_tgname - 1] = DirectFunctionCall1(namein,
867 : : CStringGetDatum(trigname));
8686 868 : 7463 : values[Anum_pg_trigger_tgfoid - 1] = ObjectIdGetDatum(funcoid);
9716 bruce@momjian.us 869 : 7463 : values[Anum_pg_trigger_tgtype - 1] = Int16GetDatum(tgtype);
1003 alvherre@alvh.no-ip. 870 : 7463 : values[Anum_pg_trigger_tgenabled - 1] = trigger_fires_when;
830 871 : 7463 : values[Anum_pg_trigger_tgisinternal - 1] = BoolGetDatum(isInternal);
8686 tgl@sss.pgh.pa.us 872 : 7463 : values[Anum_pg_trigger_tgconstrrelid - 1] = ObjectIdGetDatum(constrrelid);
5374 873 : 7463 : values[Anum_pg_trigger_tgconstrindid - 1] = ObjectIdGetDatum(indexOid);
6269 874 : 7463 : values[Anum_pg_trigger_tgconstraint - 1] = ObjectIdGetDatum(constraintOid);
8686 875 : 7463 : values[Anum_pg_trigger_tgdeferrable - 1] = BoolGetDatum(stmt->deferrable);
876 : 7463 : values[Anum_pg_trigger_tginitdeferred - 1] = BoolGetDatum(stmt->initdeferred);
877 : :
9716 bruce@momjian.us 878 [ + + ]: 7463 : if (stmt->args)
879 : : {
880 : : ListCell *le;
881 : : char *args;
7263 neilc@samurai.com 882 : 284 : int16 nargs = list_length(stmt->args);
9715 bruce@momjian.us 883 : 284 : int len = 0;
884 : :
9716 885 [ + - + + : 709 : foreach(le, stmt->args)
+ + ]
886 : : {
7903 tgl@sss.pgh.pa.us 887 : 425 : char *ar = strVal(lfirst(le));
888 : :
8686 889 : 425 : len += strlen(ar) + 4;
9665 vadim4o@yahoo.com 890 [ + + ]: 3541 : for (; *ar; ar++)
891 : : {
9691 892 [ - + ]: 3116 : if (*ar == '\\')
9691 vadim4o@yahoo.com 893 :UBC 0 : len++;
894 : : }
895 : : }
9716 bruce@momjian.us 896 :CBC 284 : args = (char *) palloc(len + 1);
8647 tgl@sss.pgh.pa.us 897 : 284 : args[0] = '\0';
9716 bruce@momjian.us 898 [ + - + + : 709 : foreach(le, stmt->args)
+ + ]
899 : : {
7903 tgl@sss.pgh.pa.us 900 : 425 : char *s = strVal(lfirst(le));
9665 vadim4o@yahoo.com 901 : 425 : char *d = args + strlen(args);
902 : :
9691 903 [ + + ]: 3541 : while (*s)
904 : : {
905 [ - + ]: 3116 : if (*s == '\\')
9691 vadim4o@yahoo.com 906 :UBC 0 : *d++ = '\\';
9691 vadim4o@yahoo.com 907 :CBC 3116 : *d++ = *s++;
908 : : }
8647 tgl@sss.pgh.pa.us 909 : 425 : strcpy(d, "\\000");
910 : : }
9716 bruce@momjian.us 911 : 284 : values[Anum_pg_trigger_tgnargs - 1] = Int16GetDatum(nargs);
8660 tgl@sss.pgh.pa.us 912 : 284 : values[Anum_pg_trigger_tgargs - 1] = DirectFunctionCall1(byteain,
913 : : CStringGetDatum(args));
914 : : }
915 : : else
916 : : {
9716 bruce@momjian.us 917 : 7179 : values[Anum_pg_trigger_tgnargs - 1] = Int16GetDatum(0);
8660 tgl@sss.pgh.pa.us 918 : 7179 : values[Anum_pg_trigger_tgargs - 1] = DirectFunctionCall1(byteain,
919 : : CStringGetDatum(""));
920 : : }
921 : :
922 : : /* build column number array if it's a column-specific trigger */
5296 923 : 7463 : ncolumns = list_length(stmt->columns);
924 [ + + ]: 7463 : if (ncolumns == 0)
925 : 7413 : columns = NULL;
926 : : else
927 : : {
928 : : ListCell *cell;
929 : 50 : int i = 0;
930 : :
4311 peter_e@gmx.net 931 : 50 : columns = (int16 *) palloc(ncolumns * sizeof(int16));
5296 tgl@sss.pgh.pa.us 932 [ + - + + : 104 : foreach(cell, stmt->columns)
+ + ]
933 : : {
5161 bruce@momjian.us 934 : 57 : char *name = strVal(lfirst(cell));
935 : : int16 attnum;
936 : : int j;
937 : :
938 : : /* Lookup column name. System columns are not allowed */
5296 tgl@sss.pgh.pa.us 939 : 57 : attnum = attnameAttNum(rel, name, false);
940 [ - + ]: 57 : if (attnum == InvalidAttrNumber)
5296 tgl@sss.pgh.pa.us 941 [ # # ]:UBC 0 : ereport(ERROR,
942 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
943 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
944 : : name, RelationGetRelationName(rel))));
945 : :
946 : : /* Check for duplicates */
5296 tgl@sss.pgh.pa.us 947 [ + + ]:CBC 61 : for (j = i - 1; j >= 0; j--)
948 : : {
949 [ + + ]: 7 : if (columns[j] == attnum)
950 [ + - ]: 3 : ereport(ERROR,
951 : : (errcode(ERRCODE_DUPLICATE_COLUMN),
952 : : errmsg("column \"%s\" specified more than once",
953 : : name)));
954 : : }
955 : :
956 : 54 : columns[i++] = attnum;
957 : : }
958 : : }
959 : 7460 : tgattr = buildint2vector(columns, ncolumns);
9716 bruce@momjian.us 960 : 7460 : values[Anum_pg_trigger_tgattr - 1] = PointerGetDatum(tgattr);
961 : :
962 : : /* set tgqual if trigger has WHEN clause */
5259 tgl@sss.pgh.pa.us 963 [ + + ]: 7460 : if (qual)
964 : 76 : values[Anum_pg_trigger_tgqual - 1] = CStringGetTextDatum(qual);
965 : : else
966 : 7384 : nulls[Anum_pg_trigger_tgqual - 1] = true;
967 : :
2718 kgrittn@postgresql.o 968 [ + + ]: 7460 : if (oldtablename)
969 : 116 : values[Anum_pg_trigger_tgoldtable - 1] = DirectFunctionCall1(namein,
970 : : CStringGetDatum(oldtablename));
971 : : else
972 : 7344 : nulls[Anum_pg_trigger_tgoldtable - 1] = true;
973 [ + + ]: 7460 : if (newtablename)
974 : 132 : values[Anum_pg_trigger_tgnewtable - 1] = DirectFunctionCall1(namein,
975 : : CStringGetDatum(newtablename));
976 : : else
977 : 7328 : nulls[Anum_pg_trigger_tgnewtable - 1] = true;
978 : :
979 : : /*
980 : : * Insert or replace tuple in pg_trigger.
981 : : */
1247 tgl@sss.pgh.pa.us 982 [ + + ]: 7460 : if (!trigger_exists)
983 : : {
984 : 7421 : tuple = heap_form_tuple(tgrel->rd_att, values, nulls);
985 : 7421 : CatalogTupleInsert(tgrel, tuple);
986 : : }
987 : : else
988 : : {
989 : : HeapTuple newtup;
990 : :
991 : 39 : newtup = heap_form_tuple(tgrel->rd_att, values, nulls);
992 : 39 : CatalogTupleUpdate(tgrel, &tuple->t_self, newtup);
993 : 39 : heap_freetuple(newtup);
994 : : }
995 : :
996 : 7460 : heap_freetuple(tuple); /* free either original or new tuple */
1910 andres@anarazel.de 997 : 7460 : table_close(tgrel, RowExclusiveLock);
998 : :
9716 bruce@momjian.us 999 : 7460 : pfree(DatumGetPointer(values[Anum_pg_trigger_tgname - 1]));
1000 : 7460 : pfree(DatumGetPointer(values[Anum_pg_trigger_tgargs - 1]));
5296 tgl@sss.pgh.pa.us 1001 : 7460 : pfree(DatumGetPointer(values[Anum_pg_trigger_tgattr - 1]));
2718 kgrittn@postgresql.o 1002 [ + + ]: 7460 : if (oldtablename)
1003 : 116 : pfree(DatumGetPointer(values[Anum_pg_trigger_tgoldtable - 1]));
1004 [ + + ]: 7460 : if (newtablename)
1005 : 132 : pfree(DatumGetPointer(values[Anum_pg_trigger_tgnewtable - 1]));
1006 : :
1007 : : /*
1008 : : * Update relation's pg_class entry; if necessary; and if not, send an SI
1009 : : * message to make other backends (and this one) rebuild relcache entries.
1010 : : */
1910 andres@anarazel.de 1011 : 7460 : pgrel = table_open(RelationRelationId, RowExclusiveLock);
5173 rhaas@postgresql.org 1012 : 7460 : tuple = SearchSysCacheCopy1(RELOID,
1013 : : ObjectIdGetDatum(RelationGetRelid(rel)));
9370 bruce@momjian.us 1014 [ - + ]: 7460 : if (!HeapTupleIsValid(tuple))
7574 tgl@sss.pgh.pa.us 1015 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u",
1016 : : RelationGetRelid(rel));
2214 alvherre@alvh.no-ip. 1017 [ + + ]:CBC 7460 : if (!((Form_pg_class) GETSTRUCT(tuple))->relhastriggers)
1018 : : {
1019 : 2997 : ((Form_pg_class) GETSTRUCT(tuple))->relhastriggers = true;
1020 : :
1021 : 2997 : CatalogTupleUpdate(pgrel, &tuple->t_self, tuple);
1022 : :
1023 : 2997 : CommandCounterIncrement();
1024 : : }
1025 : : else
1026 : 4463 : CacheInvalidateRelcacheByTuple(tuple);
1027 : :
8886 JanWieck@Yahoo.com 1028 : 7460 : heap_freetuple(tuple);
1910 andres@anarazel.de 1029 : 7460 : table_close(pgrel, RowExclusiveLock);
1030 : :
1031 : : /*
1032 : : * If we're replacing a trigger, flush all the old dependencies before
1033 : : * recording new ones.
1034 : : */
1247 tgl@sss.pgh.pa.us 1035 [ + + ]: 7460 : if (trigger_exists)
1036 : 39 : deleteDependencyRecordsFor(TriggerRelationId, trigoid, true);
1037 : :
1038 : : /*
1039 : : * Record dependencies for trigger. Always place a normal dependency on
1040 : : * the function.
1041 : : */
6269 1042 : 7460 : myself.classId = TriggerRelationId;
1043 : 7460 : myself.objectId = trigoid;
1044 : 7460 : myself.objectSubId = 0;
1045 : :
6940 1046 : 7460 : referenced.classId = ProcedureRelationId;
7947 1047 : 7460 : referenced.objectId = funcoid;
1048 : 7460 : referenced.objectSubId = 0;
1049 : 7460 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1050 : :
5201 1051 [ + + + - ]: 7460 : if (isInternal && OidIsValid(constraintOid))
1052 : : {
1053 : : /*
1054 : : * Internally-generated trigger for a constraint, so make it an
1055 : : * internal dependency of the constraint. We can skip depending on
1056 : : * the relation(s), as there'll be an indirect dependency via the
1057 : : * constraint.
1058 : : */
6269 1059 : 5577 : referenced.classId = ConstraintRelationId;
1060 : 5577 : referenced.objectId = constraintOid;
1061 : 5577 : referenced.objectSubId = 0;
1062 : 5577 : recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
1063 : : }
1064 : : else
1065 : : {
1066 : : /*
1067 : : * User CREATE TRIGGER, so place dependencies. We make trigger be
1068 : : * auto-dropped if its relation is dropped or if the FK relation is
1069 : : * dropped. (Auto drop is compatible with our pre-7.3 behavior.)
1070 : : */
6940 1071 : 1883 : referenced.classId = RelationRelationId;
7947 1072 : 1883 : referenced.objectId = RelationGetRelid(rel);
1073 : 1883 : referenced.objectSubId = 0;
1889 1074 : 1883 : recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
1075 : :
5374 1076 [ + + ]: 1883 : if (OidIsValid(constrrelid))
1077 : : {
6940 1078 : 21 : referenced.classId = RelationRelationId;
7947 1079 : 21 : referenced.objectId = constrrelid;
1080 : 21 : referenced.objectSubId = 0;
1081 : 21 : recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
1082 : : }
1083 : : /* Not possible to have an index dependency in this case */
5374 1084 [ - + ]: 1883 : Assert(!OidIsValid(indexOid));
1085 : :
1086 : : /*
1087 : : * If it's a user-specified constraint trigger, make the constraint
1088 : : * internally dependent on the trigger instead of vice versa.
1089 : : */
5201 1090 [ + + ]: 1883 : if (OidIsValid(constraintOid))
1091 : : {
1092 : 75 : referenced.classId = ConstraintRelationId;
1093 : 75 : referenced.objectId = constraintOid;
1094 : 75 : referenced.objectSubId = 0;
1095 : 75 : recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL);
1096 : : }
1097 : :
1098 : : /*
1099 : : * If it's a partition trigger, create the partition dependencies.
1100 : : */
2214 alvherre@alvh.no-ip. 1101 [ + + ]: 1883 : if (OidIsValid(parentTriggerOid))
1102 : : {
1103 : 432 : ObjectAddressSet(referenced, TriggerRelationId, parentTriggerOid);
1889 tgl@sss.pgh.pa.us 1104 : 432 : recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_PRI);
1105 : 432 : ObjectAddressSet(referenced, RelationRelationId, RelationGetRelid(rel));
1106 : 432 : recordDependencyOn(&myself, &referenced, DEPENDENCY_PARTITION_SEC);
1107 : : }
1108 : : }
1109 : :
1110 : : /* If column-specific trigger, add normal dependencies on columns */
5296 1111 [ + + ]: 7460 : if (columns != NULL)
1112 : : {
1113 : : int i;
1114 : :
1115 : 47 : referenced.classId = RelationRelationId;
1116 : 47 : referenced.objectId = RelationGetRelid(rel);
1117 [ + + ]: 98 : for (i = 0; i < ncolumns; i++)
1118 : : {
1119 : 51 : referenced.objectSubId = columns[i];
1120 : 51 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1121 : : }
1122 : : }
1123 : :
1124 : : /*
1125 : : * If it has a WHEN clause, add dependencies on objects mentioned in the
1126 : : * expression (eg, functions, as well as any columns used).
1127 : : */
2214 alvherre@alvh.no-ip. 1128 [ + + ]: 7460 : if (whenRtable != NIL)
5259 tgl@sss.pgh.pa.us 1129 : 55 : recordDependencyOnExpr(&myself, whenClause, whenRtable,
1130 : : DEPENDENCY_NORMAL);
1131 : :
1132 : : /* Post creation hook for new trigger */
4046 rhaas@postgresql.org 1133 [ + + ]: 7460 : InvokeObjectPostCreateHookArg(TriggerRelationId, trigoid, 0,
1134 : : isInternal);
1135 : :
1136 : : /*
1137 : : * Lastly, create the trigger on child relations, if needed.
1138 : : */
2214 alvherre@alvh.no-ip. 1139 [ + + ]: 7460 : if (partition_recurse)
1140 : : {
1088 1141 : 199 : PartitionDesc partdesc = RelationGetPartitionDesc(rel, true);
1142 : : int i;
1143 : : MemoryContext oldcxt,
1144 : : perChildCxt;
1145 : :
2214 1146 : 199 : perChildCxt = AllocSetContextCreate(CurrentMemoryContext,
1147 : : "part trig clone",
1148 : : ALLOCSET_SMALL_SIZES);
1149 : :
1150 : : /*
1151 : : * We don't currently expect to be called with a valid indexOid. If
1152 : : * that ever changes then we'll need to write code here to find the
1153 : : * corresponding child index.
1154 : : */
586 drowley@postgresql.o 1155 [ - + ]: 199 : Assert(!OidIsValid(indexOid));
1156 : :
2214 alvherre@alvh.no-ip. 1157 : 199 : oldcxt = MemoryContextSwitchTo(perChildCxt);
1158 : :
1159 : : /* Iterate to create the trigger on each existing partition */
1160 [ + + ]: 544 : for (i = 0; i < partdesc->nparts; i++)
1161 : : {
1162 : : CreateTrigStmt *childStmt;
1163 : : Relation childTbl;
1164 : : Node *qual;
1165 : :
1910 andres@anarazel.de 1166 : 348 : childTbl = table_open(partdesc->oids[i], ShareRowExclusiveLock);
1167 : :
1168 : : /*
1169 : : * Initialize our fabricated parse node by copying the original
1170 : : * one, then resetting fields that we pass separately.
1171 : : */
2214 alvherre@alvh.no-ip. 1172 : 348 : childStmt = (CreateTrigStmt *) copyObject(stmt);
1173 : 348 : childStmt->funcname = NIL;
1174 : 348 : childStmt->whenClause = NULL;
1175 : :
1176 : : /* If there is a WHEN clause, create a modified copy of it */
1177 : 348 : qual = copyObject(whenClause);
1178 : : qual = (Node *)
1179 : 348 : map_partition_varattnos((List *) qual, PRS2_OLD_VARNO,
1180 : : childTbl, rel);
1181 : : qual = (Node *)
1182 : 348 : map_partition_varattnos((List *) qual, PRS2_NEW_VARNO,
1183 : : childTbl, rel);
1184 : :
1003 1185 : 348 : CreateTriggerFiringOn(childStmt, queryString,
1186 : 348 : partdesc->oids[i], refRelOid,
1187 : : InvalidOid, InvalidOid,
1188 : : funcoid, trigoid, qual,
1189 : : isInternal, true, trigger_fires_when);
1190 : :
1910 andres@anarazel.de 1191 : 345 : table_close(childTbl, NoLock);
1192 : :
2214 alvherre@alvh.no-ip. 1193 : 345 : MemoryContextReset(perChildCxt);
1194 : : }
1195 : :
1196 : 196 : MemoryContextSwitchTo(oldcxt);
1197 : 196 : MemoryContextDelete(perChildCxt);
1198 : : }
1199 : :
1200 : : /* Keep lock on target rel until end of xact */
1910 andres@anarazel.de 1201 : 7457 : table_close(rel, NoLock);
1202 : :
3330 alvherre@alvh.no-ip. 1203 : 7457 : return myself;
1204 : : }
1205 : :
1206 : : /*
1207 : : * TriggerSetParentTrigger
1208 : : * Set a partition's trigger as child of its parent trigger,
1209 : : * or remove the linkage if parentTrigId is InvalidOid.
1210 : : *
1211 : : * This updates the constraint's pg_trigger row to show it as inherited, and
1212 : : * adds PARTITION dependencies to prevent the trigger from being deleted
1213 : : * on its own. Alternatively, reverse that.
1214 : : */
1215 : : void
830 1216 : 144 : TriggerSetParentTrigger(Relation trigRel,
1217 : : Oid childTrigId,
1218 : : Oid parentTrigId,
1219 : : Oid childTableId)
1220 : : {
1221 : : SysScanDesc tgscan;
1222 : : ScanKeyData skey[1];
1223 : : Form_pg_trigger trigForm;
1224 : : HeapTuple tuple,
1225 : : newtup;
1226 : : ObjectAddress depender;
1227 : : ObjectAddress referenced;
1228 : :
1229 : : /*
1230 : : * Find the trigger to delete.
1231 : : */
1232 : 144 : ScanKeyInit(&skey[0],
1233 : : Anum_pg_trigger_oid,
1234 : : BTEqualStrategyNumber, F_OIDEQ,
1235 : : ObjectIdGetDatum(childTrigId));
1236 : :
1237 : 144 : tgscan = systable_beginscan(trigRel, TriggerOidIndexId, true,
1238 : : NULL, 1, skey);
1239 : :
1240 : 144 : tuple = systable_getnext(tgscan);
1241 [ - + ]: 144 : if (!HeapTupleIsValid(tuple))
830 alvherre@alvh.no-ip. 1242 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for trigger %u", childTrigId);
830 alvherre@alvh.no-ip. 1243 :CBC 144 : newtup = heap_copytuple(tuple);
1244 : 144 : trigForm = (Form_pg_trigger) GETSTRUCT(newtup);
1245 [ + + ]: 144 : if (OidIsValid(parentTrigId))
1246 : : {
1247 : : /* don't allow setting parent for a constraint that already has one */
1248 [ - + ]: 78 : if (OidIsValid(trigForm->tgparentid))
830 alvherre@alvh.no-ip. 1249 [ # # ]:UBC 0 : elog(ERROR, "trigger %u already has a parent trigger",
1250 : : childTrigId);
1251 : :
830 alvherre@alvh.no-ip. 1252 :CBC 78 : trigForm->tgparentid = parentTrigId;
1253 : :
1254 : 78 : CatalogTupleUpdate(trigRel, &tuple->t_self, newtup);
1255 : :
1256 : 78 : ObjectAddressSet(depender, TriggerRelationId, childTrigId);
1257 : :
1258 : 78 : ObjectAddressSet(referenced, TriggerRelationId, parentTrigId);
1259 : 78 : recordDependencyOn(&depender, &referenced, DEPENDENCY_PARTITION_PRI);
1260 : :
1261 : 78 : ObjectAddressSet(referenced, RelationRelationId, childTableId);
1262 : 78 : recordDependencyOn(&depender, &referenced, DEPENDENCY_PARTITION_SEC);
1263 : : }
1264 : : else
1265 : : {
1266 : 66 : trigForm->tgparentid = InvalidOid;
1267 : :
1268 : 66 : CatalogTupleUpdate(trigRel, &tuple->t_self, newtup);
1269 : :
1270 : 66 : deleteDependencyRecordsForClass(TriggerRelationId, childTrigId,
1271 : : TriggerRelationId,
1272 : : DEPENDENCY_PARTITION_PRI);
1273 : 66 : deleteDependencyRecordsForClass(TriggerRelationId, childTrigId,
1274 : : RelationRelationId,
1275 : : DEPENDENCY_PARTITION_SEC);
1276 : : }
1277 : :
1278 : 144 : heap_freetuple(newtup);
1279 : 144 : systable_endscan(tgscan);
1280 : 144 : }
1281 : :
1282 : :
1283 : : /*
1284 : : * Guts of trigger deletion.
1285 : : */
1286 : : void
7947 tgl@sss.pgh.pa.us 1287 : 6333 : RemoveTriggerById(Oid trigOid)
1288 : : {
1289 : : Relation tgrel;
1290 : : SysScanDesc tgscan;
1291 : : ScanKeyData skey[1];
1292 : : HeapTuple tup;
1293 : : Oid relid;
1294 : : Relation rel;
1295 : :
1910 andres@anarazel.de 1296 : 6333 : tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1297 : :
1298 : : /*
1299 : : * Find the trigger to delete.
1300 : : */
7459 tgl@sss.pgh.pa.us 1301 : 6333 : ScanKeyInit(&skey[0],
1302 : : Anum_pg_trigger_oid,
1303 : : BTEqualStrategyNumber, F_OIDEQ,
1304 : : ObjectIdGetDatum(trigOid));
1305 : :
6940 1306 : 6333 : tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
1307 : : NULL, 1, skey);
1308 : :
7947 1309 : 6333 : tup = systable_getnext(tgscan);
1310 [ - + ]: 6333 : if (!HeapTupleIsValid(tup))
7574 tgl@sss.pgh.pa.us 1311 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for trigger %u", trigOid);
1312 : :
1313 : : /*
1314 : : * Open and exclusive-lock the relation the trigger belongs to.
1315 : : */
7947 tgl@sss.pgh.pa.us 1316 :CBC 6333 : relid = ((Form_pg_trigger) GETSTRUCT(tup))->tgrelid;
1317 : :
1910 andres@anarazel.de 1318 : 6333 : rel = table_open(relid, AccessExclusiveLock);
1319 : :
4935 tgl@sss.pgh.pa.us 1320 [ + + ]: 6333 : if (rel->rd_rel->relkind != RELKIND_RELATION &&
3675 noah@leadboat.com 1321 [ + + ]: 1048 : rel->rd_rel->relkind != RELKIND_VIEW &&
2685 rhaas@postgresql.org 1322 [ + + ]: 983 : rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
1323 [ - + ]: 937 : rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
7574 tgl@sss.pgh.pa.us 1324 [ # # ]:UBC 0 : ereport(ERROR,
1325 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1326 : : errmsg("relation \"%s\" cannot have triggers",
1327 : : RelationGetRelationName(rel)),
1328 : : errdetail_relkind_not_supported(rel->rd_rel->relkind)));
1329 : :
8038 tgl@sss.pgh.pa.us 1330 [ + + - + ]:CBC 6333 : if (!allowSystemTableMods && IsSystemRelation(rel))
7574 tgl@sss.pgh.pa.us 1331 [ # # ]:UBC 0 : ereport(ERROR,
1332 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1333 : : errmsg("permission denied: \"%s\" is a system catalog",
1334 : : RelationGetRelationName(rel))));
1335 : :
1336 : : /*
1337 : : * Delete the pg_trigger tuple.
1338 : : */
2629 tgl@sss.pgh.pa.us 1339 :CBC 6333 : CatalogTupleDelete(tgrel, &tup->t_self);
1340 : :
8090 1341 : 6333 : systable_endscan(tgscan);
1910 andres@anarazel.de 1342 : 6333 : table_close(tgrel, RowExclusiveLock);
1343 : :
1344 : : /*
1345 : : * We do not bother to try to determine whether any other triggers remain,
1346 : : * which would be needed in order to decide whether it's safe to clear the
1347 : : * relation's relhastriggers. (In any case, there might be a concurrent
1348 : : * process adding new triggers.) Instead, just force a relcache inval to
1349 : : * make other backends (and this one too!) rebuild their relcache entries.
1350 : : * There's no great harm in leaving relhastriggers true even if there are
1351 : : * no triggers left.
1352 : : */
5635 tgl@sss.pgh.pa.us 1353 : 6333 : CacheInvalidateRelcache(rel);
1354 : :
1355 : : /* Keep lock on trigger's rel until end of xact */
1910 andres@anarazel.de 1356 : 6333 : table_close(rel, NoLock);
9723 vadim4o@yahoo.com 1357 : 6333 : }
1358 : :
1359 : : /*
1360 : : * get_trigger_oid - Look up a trigger by name to find its OID.
1361 : : *
1362 : : * If missing_ok is false, throw an error if trigger not found. If
1363 : : * true, just return InvalidOid.
1364 : : */
1365 : : Oid
5001 rhaas@postgresql.org 1366 : 375 : get_trigger_oid(Oid relid, const char *trigname, bool missing_ok)
1367 : : {
1368 : : Relation tgrel;
1369 : : ScanKeyData skey[2];
1370 : : SysScanDesc tgscan;
1371 : : HeapTuple tup;
1372 : : Oid oid;
1373 : :
1374 : : /*
1375 : : * Find the trigger, verify permissions, set up object address
1376 : : */
1910 andres@anarazel.de 1377 : 375 : tgrel = table_open(TriggerRelationId, AccessShareLock);
1378 : :
5001 rhaas@postgresql.org 1379 : 375 : ScanKeyInit(&skey[0],
1380 : : Anum_pg_trigger_tgrelid,
1381 : : BTEqualStrategyNumber, F_OIDEQ,
1382 : : ObjectIdGetDatum(relid));
1383 : 375 : ScanKeyInit(&skey[1],
1384 : : Anum_pg_trigger_tgname,
1385 : : BTEqualStrategyNumber, F_NAMEEQ,
1386 : : CStringGetDatum(trigname));
1387 : :
1388 : 375 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1389 : : NULL, 2, skey);
1390 : :
1391 : 375 : tup = systable_getnext(tgscan);
1392 : :
1393 [ + + ]: 375 : if (!HeapTupleIsValid(tup))
1394 : : {
1395 [ + + ]: 15 : if (!missing_ok)
1396 [ + - ]: 12 : ereport(ERROR,
1397 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1398 : : errmsg("trigger \"%s\" for table \"%s\" does not exist",
1399 : : trigname, get_rel_name(relid))));
1400 : 3 : oid = InvalidOid;
1401 : : }
1402 : : else
1403 : : {
1972 andres@anarazel.de 1404 : 360 : oid = ((Form_pg_trigger) GETSTRUCT(tup))->oid;
1405 : : }
1406 : :
5001 rhaas@postgresql.org 1407 : 363 : systable_endscan(tgscan);
1910 andres@anarazel.de 1408 : 363 : table_close(tgrel, AccessShareLock);
5001 rhaas@postgresql.org 1409 : 363 : return oid;
1410 : : }
1411 : :
1412 : : /*
1413 : : * Perform permissions and integrity checks before acquiring a relation lock.
1414 : : */
1415 : : static void
4504 1416 : 20 : RangeVarCallbackForRenameTrigger(const RangeVar *rv, Oid relid, Oid oldrelid,
1417 : : void *arg)
1418 : : {
1419 : : HeapTuple tuple;
1420 : : Form_pg_class form;
1421 : :
1422 : 20 : tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1423 [ - + ]: 20 : if (!HeapTupleIsValid(tuple))
4326 bruce@momjian.us 1424 :UBC 0 : return; /* concurrently dropped */
4504 rhaas@postgresql.org 1425 :CBC 20 : form = (Form_pg_class) GETSTRUCT(tuple);
1426 : :
1427 : : /* only tables and views can have triggers */
3675 noah@leadboat.com 1428 [ + + + - ]: 20 : if (form->relkind != RELKIND_RELATION && form->relkind != RELKIND_VIEW &&
2685 rhaas@postgresql.org 1429 [ + - ]: 12 : form->relkind != RELKIND_FOREIGN_TABLE &&
1430 [ - + ]: 12 : form->relkind != RELKIND_PARTITIONED_TABLE)
4326 bruce@momjian.us 1431 [ # # ]:UBC 0 : ereport(ERROR,
1432 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1433 : : errmsg("relation \"%s\" cannot have triggers",
1434 : : rv->relname),
1435 : : errdetail_relkind_not_supported(form->relkind)));
1436 : :
1437 : : /* you must own the table to rename one of its triggers */
518 peter@eisentraut.org 1438 [ - + ]:CBC 20 : if (!object_ownercheck(RelationRelationId, relid, GetUserId()))
2325 peter_e@gmx.net 1439 :UBC 0 : aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname);
3790 rhaas@postgresql.org 1440 [ + + + + ]:CBC 20 : if (!allowSystemTableMods && IsSystemClass(relid, form))
4326 bruce@momjian.us 1441 [ + - ]: 1 : ereport(ERROR,
1442 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1443 : : errmsg("permission denied: \"%s\" is a system catalog",
1444 : : rv->relname)));
1445 : :
4504 rhaas@postgresql.org 1446 : 19 : ReleaseSysCache(tuple);
1447 : : }
1448 : :
1449 : : /*
1450 : : * renametrig - changes the name of a trigger on a relation
1451 : : *
1452 : : * trigger name is changed in trigger catalog.
1453 : : * No record of the previous name is kept.
1454 : : *
1455 : : * get proper relrelation from relation catalog (if not arg)
1456 : : * scan trigger catalog
1457 : : * for name conflict (within rel)
1458 : : * for original trigger (if not arg)
1459 : : * modify tgname in trigger tuple
1460 : : * update row in catalog
1461 : : */
1462 : : ObjectAddress
1463 : 20 : renametrig(RenameStmt *stmt)
1464 : : {
1465 : : Oid tgoid;
1466 : : Relation targetrel;
1467 : : Relation tgrel;
1468 : : HeapTuple tuple;
1469 : : SysScanDesc tgscan;
1470 : : ScanKeyData key[2];
1471 : : Oid relid;
1472 : : ObjectAddress address;
1473 : :
1474 : : /*
1475 : : * Look up name, check permissions, and acquire lock (which we will NOT
1476 : : * release until end of transaction).
1477 : : */
1478 : 20 : relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
1479 : : 0,
1480 : : RangeVarCallbackForRenameTrigger,
1481 : : NULL);
1482 : :
1483 : : /* Have lock already, so just need to build relcache entry. */
1484 : 19 : targetrel = relation_open(relid, NoLock);
1485 : :
1486 : : /*
1487 : : * On partitioned tables, this operation recurses to partitions. Lock all
1488 : : * tables upfront.
1489 : : */
997 alvherre@alvh.no-ip. 1490 [ + + ]: 19 : if (targetrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1491 : 12 : (void) find_all_inheritors(relid, AccessExclusiveLock, NULL);
1492 : :
1493 : 19 : tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1494 : :
1495 : : /*
1496 : : * Search for the trigger to modify.
1497 : : */
7459 tgl@sss.pgh.pa.us 1498 : 19 : ScanKeyInit(&key[0],
1499 : : Anum_pg_trigger_tgrelid,
1500 : : BTEqualStrategyNumber, F_OIDEQ,
1501 : : ObjectIdGetDatum(relid));
1502 : 19 : ScanKeyInit(&key[1],
1503 : : Anum_pg_trigger_tgname,
1504 : : BTEqualStrategyNumber, F_NAMEEQ,
4504 rhaas@postgresql.org 1505 : 19 : PointerGetDatum(stmt->subname));
6940 tgl@sss.pgh.pa.us 1506 : 19 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1507 : : NULL, 2, key);
8020 1508 [ + - ]: 19 : if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1509 : : {
1510 : : Form_pg_trigger trigform;
1511 : :
1910 andres@anarazel.de 1512 : 19 : trigform = (Form_pg_trigger) GETSTRUCT(tuple);
1513 : 19 : tgoid = trigform->oid;
1514 : :
1515 : : /*
1516 : : * If the trigger descends from a trigger on a parent partitioned
1517 : : * table, reject the rename. We don't allow a trigger in a partition
1518 : : * to differ in name from that of its parent: that would lead to an
1519 : : * inconsistency that pg_dump would not reproduce.
1520 : : */
997 alvherre@alvh.no-ip. 1521 [ + + ]: 19 : if (OidIsValid(trigform->tgparentid))
1522 [ + - ]: 3 : ereport(ERROR,
1523 : : errmsg("cannot rename trigger \"%s\" on table \"%s\"",
1524 : : stmt->subname, RelationGetRelationName(targetrel)),
1525 : : errhint("Rename the trigger on the partitioned table \"%s\" instead.",
1526 : : get_rel_name(get_partition_parent(relid, false))));
1527 : :
1528 : :
1529 : : /* Rename the trigger on this relation ... */
1530 : 16 : renametrig_internal(tgrel, targetrel, tuple, stmt->newname,
1531 : 16 : stmt->subname);
1532 : :
1533 : : /* ... and if it is partitioned, recurse to its partitions */
1534 [ + + ]: 16 : if (targetrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1535 : : {
1536 : 9 : PartitionDesc partdesc = RelationGetPartitionDesc(targetrel, true);
1537 : :
1538 [ + + ]: 15 : for (int i = 0; i < partdesc->nparts; i++)
1539 : : {
1540 : 9 : Oid partitionId = partdesc->oids[i];
1541 : :
1542 : 9 : renametrig_partition(tgrel, partitionId, trigform->oid,
1543 : 9 : stmt->newname, stmt->subname);
1544 : : }
1545 : : }
1546 : : }
1547 : : else
1548 : : {
7574 tgl@sss.pgh.pa.us 1549 [ # # ]:UBC 0 : ereport(ERROR,
1550 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1551 : : errmsg("trigger \"%s\" for table \"%s\" does not exist",
1552 : : stmt->subname, RelationGetRelationName(targetrel))));
1553 : : }
1554 : :
3330 alvherre@alvh.no-ip. 1555 :CBC 13 : ObjectAddressSet(address, TriggerRelationId, tgoid);
1556 : :
8020 tgl@sss.pgh.pa.us 1557 : 13 : systable_endscan(tgscan);
1558 : :
1910 andres@anarazel.de 1559 : 13 : table_close(tgrel, RowExclusiveLock);
1560 : :
1561 : : /*
1562 : : * Close rel, but keep exclusive lock!
1563 : : */
4504 rhaas@postgresql.org 1564 : 13 : relation_close(targetrel, NoLock);
1565 : :
3330 alvherre@alvh.no-ip. 1566 : 13 : return address;
1567 : : }
1568 : :
1569 : : /*
1570 : : * Subroutine for renametrig -- perform the actual work of renaming one
1571 : : * trigger on one table.
1572 : : *
1573 : : * If the trigger has a name different from the expected one, raise a
1574 : : * NOTICE about it.
1575 : : */
1576 : : static void
997 1577 : 28 : renametrig_internal(Relation tgrel, Relation targetrel, HeapTuple trigtup,
1578 : : const char *newname, const char *expected_name)
1579 : : {
1580 : : HeapTuple tuple;
1581 : : Form_pg_trigger tgform;
1582 : : ScanKeyData key[2];
1583 : : SysScanDesc tgscan;
1584 : :
1585 : : /* If the trigger already has the new name, nothing to do. */
1586 : 28 : tgform = (Form_pg_trigger) GETSTRUCT(trigtup);
1587 [ - + ]: 28 : if (strcmp(NameStr(tgform->tgname), newname) == 0)
997 alvherre@alvh.no-ip. 1588 :UBC 0 : return;
1589 : :
1590 : : /*
1591 : : * Before actually trying the rename, search for triggers with the same
1592 : : * name. The update would fail with an ugly message in that case, and it
1593 : : * is better to throw a nicer error.
1594 : : */
997 alvherre@alvh.no-ip. 1595 :CBC 28 : ScanKeyInit(&key[0],
1596 : : Anum_pg_trigger_tgrelid,
1597 : : BTEqualStrategyNumber, F_OIDEQ,
1598 : : ObjectIdGetDatum(RelationGetRelid(targetrel)));
1599 : 28 : ScanKeyInit(&key[1],
1600 : : Anum_pg_trigger_tgname,
1601 : : BTEqualStrategyNumber, F_NAMEEQ,
1602 : : PointerGetDatum(newname));
1603 : 28 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1604 : : NULL, 2, key);
1605 [ + + ]: 28 : if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1606 [ + - ]: 3 : ereport(ERROR,
1607 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
1608 : : errmsg("trigger \"%s\" for relation \"%s\" already exists",
1609 : : newname, RelationGetRelationName(targetrel))));
1610 : 25 : systable_endscan(tgscan);
1611 : :
1612 : : /*
1613 : : * The target name is free; update the existing pg_trigger tuple with it.
1614 : : */
1615 : 25 : tuple = heap_copytuple(trigtup); /* need a modifiable copy */
1616 : 25 : tgform = (Form_pg_trigger) GETSTRUCT(tuple);
1617 : :
1618 : : /*
1619 : : * If the trigger has a name different from what we expected, let the user
1620 : : * know. (We can proceed anyway, since we must have reached here following
1621 : : * a tgparentid link.)
1622 : : */
1623 [ - + ]: 25 : if (strcmp(NameStr(tgform->tgname), expected_name) != 0)
997 alvherre@alvh.no-ip. 1624 [ # # ]:UBC 0 : ereport(NOTICE,
1625 : : errmsg("renamed trigger \"%s\" on relation \"%s\"",
1626 : : NameStr(tgform->tgname),
1627 : : RelationGetRelationName(targetrel)));
1628 : :
997 alvherre@alvh.no-ip. 1629 :CBC 25 : namestrcpy(&tgform->tgname, newname);
1630 : :
1631 : 25 : CatalogTupleUpdate(tgrel, &tuple->t_self, tuple);
1632 : :
1633 [ - + ]: 25 : InvokeObjectPostAlterHook(TriggerRelationId, tgform->oid, 0);
1634 : :
1635 : : /*
1636 : : * Invalidate relation's relcache entry so that other backends (and this
1637 : : * one too!) are sent SI message to make them rebuild relcache entries.
1638 : : * (Ideally this should happen automatically...)
1639 : : */
1640 : 25 : CacheInvalidateRelcache(targetrel);
1641 : : }
1642 : :
1643 : : /*
1644 : : * Subroutine for renametrig -- Helper for recursing to partitions when
1645 : : * renaming triggers on a partitioned table.
1646 : : */
1647 : : static void
1648 : 15 : renametrig_partition(Relation tgrel, Oid partitionId, Oid parentTriggerOid,
1649 : : const char *newname, const char *expected_name)
1650 : : {
1651 : : SysScanDesc tgscan;
1652 : : ScanKeyData key;
1653 : : HeapTuple tuple;
1654 : :
1655 : : /*
1656 : : * Given a relation and the OID of a trigger on parent relation, find the
1657 : : * corresponding trigger in the child and rename that trigger to the given
1658 : : * name.
1659 : : */
1660 : 15 : ScanKeyInit(&key,
1661 : : Anum_pg_trigger_tgrelid,
1662 : : BTEqualStrategyNumber, F_OIDEQ,
1663 : : ObjectIdGetDatum(partitionId));
1664 : 15 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1665 : : NULL, 1, &key);
1666 [ + + ]: 24 : while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1667 : : {
1668 : 21 : Form_pg_trigger tgform = (Form_pg_trigger) GETSTRUCT(tuple);
1669 : : Relation partitionRel;
1670 : :
1671 [ + + ]: 21 : if (tgform->tgparentid != parentTriggerOid)
1672 : 9 : continue; /* not our trigger */
1673 : :
1674 : 12 : partitionRel = table_open(partitionId, NoLock);
1675 : :
1676 : : /* Rename the trigger on this partition */
1677 : 12 : renametrig_internal(tgrel, partitionRel, tuple, newname, expected_name);
1678 : :
1679 : : /* And if this relation is partitioned, recurse to its partitions */
1680 [ + + ]: 9 : if (partitionRel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1681 : : {
1682 : 3 : PartitionDesc partdesc = RelationGetPartitionDesc(partitionRel,
1683 : : true);
1684 : :
1685 [ + + ]: 9 : for (int i = 0; i < partdesc->nparts; i++)
1686 : : {
557 drowley@postgresql.o 1687 : 6 : Oid partoid = partdesc->oids[i];
1688 : :
1689 : 6 : renametrig_partition(tgrel, partoid, tgform->oid, newname,
997 alvherre@alvh.no-ip. 1690 : 6 : NameStr(tgform->tgname));
1691 : : }
1692 : : }
1693 : 9 : table_close(partitionRel, NoLock);
1694 : :
1695 : : /* There should be at most one matching tuple */
993 1696 : 9 : break;
1697 : : }
997 1698 : 12 : systable_endscan(tgscan);
1699 : 12 : }
1700 : :
1701 : : /*
1702 : : * EnableDisableTrigger()
1703 : : *
1704 : : * Called by ALTER TABLE ENABLE/DISABLE [ REPLICA | ALWAYS ] TRIGGER
1705 : : * to change 'tgenabled' field for the specified trigger(s)
1706 : : *
1707 : : * rel: relation to process (caller must hold suitable lock on it)
1708 : : * tgname: name of trigger to process, or NULL to scan all triggers
1709 : : * tgparent: if not zero, process only triggers with this tgparentid
1710 : : * fires_when: new value for tgenabled field. In addition to generic
1711 : : * enablement/disablement, this also defines when the trigger
1712 : : * should be fired in session replication roles.
1713 : : * skip_system: if true, skip "system" triggers (constraint triggers)
1714 : : * recurse: if true, recurse to partitions
1715 : : *
1716 : : * Caller should have checked permissions for the table; here we also
1717 : : * enforce that superuser privilege is required to alter the state of
1718 : : * system triggers
1719 : : */
1720 : : void
407 tgl@sss.pgh.pa.us 1721 : 225 : EnableDisableTrigger(Relation rel, const char *tgname, Oid tgparent,
1722 : : char fires_when, bool skip_system, bool recurse,
1723 : : LOCKMODE lockmode)
1724 : : {
1725 : : Relation tgrel;
1726 : : int nkeys;
1727 : : ScanKeyData keys[2];
1728 : : SysScanDesc tgscan;
1729 : : HeapTuple tuple;
1730 : : bool found;
1731 : : bool changed;
1732 : :
1733 : : /* Scan the relevant entries in pg_triggers */
1910 andres@anarazel.de 1734 : 225 : tgrel = table_open(TriggerRelationId, RowExclusiveLock);
1735 : :
6809 tgl@sss.pgh.pa.us 1736 : 225 : ScanKeyInit(&keys[0],
1737 : : Anum_pg_trigger_tgrelid,
1738 : : BTEqualStrategyNumber, F_OIDEQ,
1739 : : ObjectIdGetDatum(RelationGetRelid(rel)));
1740 [ + + ]: 225 : if (tgname)
1741 : : {
1742 : 158 : ScanKeyInit(&keys[1],
1743 : : Anum_pg_trigger_tgname,
1744 : : BTEqualStrategyNumber, F_NAMEEQ,
1745 : : CStringGetDatum(tgname));
1746 : 158 : nkeys = 2;
1747 : : }
1748 : : else
1749 : 67 : nkeys = 1;
1750 : :
1751 : 225 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1752 : : NULL, nkeys, keys);
1753 : :
1754 : 225 : found = changed = false;
1755 : :
1756 [ + + ]: 564 : while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
1757 : : {
1758 : 339 : Form_pg_trigger oldtrig = (Form_pg_trigger) GETSTRUCT(tuple);
1759 : :
407 1760 [ + + + + ]: 339 : if (OidIsValid(tgparent) && tgparent != oldtrig->tgparentid)
1761 : 78 : continue;
1762 : :
5201 1763 [ + + ]: 261 : if (oldtrig->tgisinternal)
1764 : : {
1765 : : /* system trigger ... ok to process? */
6809 1766 [ + + ]: 30 : if (skip_system)
1767 : 6 : continue;
1768 [ - + ]: 24 : if (!superuser())
6809 tgl@sss.pgh.pa.us 1769 [ # # ]:UBC 0 : ereport(ERROR,
1770 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1771 : : errmsg("permission denied: \"%s\" is a system trigger",
1772 : : NameStr(oldtrig->tgname))));
1773 : : }
1774 : :
6809 tgl@sss.pgh.pa.us 1775 :CBC 255 : found = true;
1776 : :
6236 JanWieck@Yahoo.com 1777 [ + + ]: 255 : if (oldtrig->tgenabled != fires_when)
1778 : : {
1779 : : /* need to change this one ... make a copy to scribble on */
6756 bruce@momjian.us 1780 : 240 : HeapTuple newtup = heap_copytuple(tuple);
6809 tgl@sss.pgh.pa.us 1781 : 240 : Form_pg_trigger newtrig = (Form_pg_trigger) GETSTRUCT(newtup);
1782 : :
6236 JanWieck@Yahoo.com 1783 : 240 : newtrig->tgenabled = fires_when;
1784 : :
2630 alvherre@alvh.no-ip. 1785 : 240 : CatalogTupleUpdate(tgrel, &newtup->t_self, newtup);
1786 : :
6809 tgl@sss.pgh.pa.us 1787 : 240 : heap_freetuple(newtup);
1788 : :
1789 : 240 : changed = true;
1790 : : }
1791 : :
1792 : : /*
1793 : : * When altering FOR EACH ROW triggers on a partitioned table, do the
1794 : : * same on the partitions as well, unless ONLY is specified.
1795 : : *
1796 : : * Note that we recurse even if we didn't change the trigger above,
1797 : : * because the partitions' copy of the trigger may have a different
1798 : : * value of tgenabled than the parent's trigger and thus might need to
1799 : : * be changed.
1800 : : */
619 alvherre@alvh.no-ip. 1801 [ + + ]: 255 : if (recurse &&
1802 [ + + ]: 241 : rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
1803 [ + + ]: 43 : (TRIGGER_FOR_ROW(oldtrig->tgtype)))
1804 : : {
1805 : 37 : PartitionDesc partdesc = RelationGetPartitionDesc(rel, true);
1806 : : int i;
1807 : :
1808 [ + + ]: 92 : for (i = 0; i < partdesc->nparts; i++)
1809 : : {
1810 : : Relation part;
1811 : :
1812 : 55 : part = relation_open(partdesc->oids[i], lockmode);
1813 : : /* Match on child triggers' tgparentid, not their name */
407 tgl@sss.pgh.pa.us 1814 : 55 : EnableDisableTrigger(part, NULL, oldtrig->oid,
1815 : : fires_when, skip_system, recurse,
1816 : : lockmode);
619 alvherre@alvh.no-ip. 1817 : 55 : table_close(part, NoLock); /* keep lock till commit */
1818 : : }
1819 : : }
1820 : :
4046 rhaas@postgresql.org 1821 [ + + ]: 255 : InvokeObjectPostAlterHook(TriggerRelationId,
1822 : : oldtrig->oid, 0);
1823 : : }
1824 : :
6809 tgl@sss.pgh.pa.us 1825 : 225 : systable_endscan(tgscan);
1826 : :
1910 andres@anarazel.de 1827 : 225 : table_close(tgrel, RowExclusiveLock);
1828 : :
6809 tgl@sss.pgh.pa.us 1829 [ + + - + ]: 225 : if (tgname && !found)
6809 tgl@sss.pgh.pa.us 1830 [ # # ]:UBC 0 : ereport(ERROR,
1831 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1832 : : errmsg("trigger \"%s\" for table \"%s\" does not exist",
1833 : : tgname, RelationGetRelationName(rel))));
1834 : :
1835 : : /*
1836 : : * If we changed anything, broadcast a SI inval message to force each
1837 : : * backend (including our own!) to rebuild relation's relcache entry.
1838 : : * Otherwise they will fail to apply the change promptly.
1839 : : */
6809 tgl@sss.pgh.pa.us 1840 [ + + ]:CBC 225 : if (changed)
1841 : 216 : CacheInvalidateRelcache(rel);
1842 : 225 : }
1843 : :
1844 : :
1845 : : /*
1846 : : * Build trigger data to attach to the given relcache entry.
1847 : : *
1848 : : * Note that trigger data attached to a relcache entry must be stored in
1849 : : * CacheMemoryContext to ensure it survives as long as the relcache entry.
1850 : : * But we should be running in a less long-lived working context. To avoid
1851 : : * leaking cache memory if this routine fails partway through, we build a
1852 : : * temporary TriggerDesc in working memory and then copy the completed
1853 : : * structure into cache memory.
1854 : : */
1855 : : void
9716 bruce@momjian.us 1856 : 28237 : RelationBuildTriggers(Relation relation)
1857 : : {
1858 : : TriggerDesc *trigdesc;
1859 : : int numtrigs;
1860 : : int maxtrigs;
1861 : : Trigger *triggers;
1862 : : Relation tgrel;
1863 : : ScanKeyData skey;
1864 : : SysScanDesc tgscan;
1865 : : HeapTuple htup;
1866 : : MemoryContext oldContext;
1867 : : int i;
1868 : :
1869 : : /*
1870 : : * Allocate a working array to hold the triggers (the array is extended if
1871 : : * necessary)
1872 : : */
5635 tgl@sss.pgh.pa.us 1873 : 28237 : maxtrigs = 16;
1874 : 28237 : triggers = (Trigger *) palloc(maxtrigs * sizeof(Trigger));
1875 : 28237 : numtrigs = 0;
1876 : :
1877 : : /*
1878 : : * Note: since we scan the triggers using TriggerRelidNameIndexId, we will
1879 : : * be reading the triggers in name order, except possibly during
1880 : : * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
1881 : : * ensures that triggers will be fired in name order.
1882 : : */
7459 1883 : 28237 : ScanKeyInit(&skey,
1884 : : Anum_pg_trigger_tgrelid,
1885 : : BTEqualStrategyNumber, F_OIDEQ,
1886 : : ObjectIdGetDatum(RelationGetRelid(relation)));
1887 : :
1910 andres@anarazel.de 1888 : 28237 : tgrel = table_open(TriggerRelationId, AccessShareLock);
6940 tgl@sss.pgh.pa.us 1889 : 28237 : tgscan = systable_beginscan(tgrel, TriggerRelidNameIndexId, true,
1890 : : NULL, 1, &skey);
1891 : :
8090 1892 [ + + ]: 75810 : while (HeapTupleIsValid(htup = systable_getnext(tgscan)))
1893 : : {
1894 : 47573 : Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(htup);
1895 : : Trigger *build;
1896 : : Datum datum;
1897 : : bool isnull;
1898 : :
5635 1899 [ + + ]: 47573 : if (numtrigs >= maxtrigs)
1900 : : {
1901 : 24 : maxtrigs *= 2;
1902 : 24 : triggers = (Trigger *) repalloc(triggers, maxtrigs * sizeof(Trigger));
1903 : : }
1904 : 47573 : build = &(triggers[numtrigs]);
1905 : :
1972 andres@anarazel.de 1906 : 47573 : build->tgoid = pg_trigger->oid;
7853 tgl@sss.pgh.pa.us 1907 : 47573 : build->tgname = DatumGetCString(DirectFunctionCall1(nameout,
1908 : : NameGetDatum(&pg_trigger->tgname)));
9716 bruce@momjian.us 1909 : 47573 : build->tgfoid = pg_trigger->tgfoid;
1910 : 47573 : build->tgtype = pg_trigger->tgtype;
8964 JanWieck@Yahoo.com 1911 : 47573 : build->tgenabled = pg_trigger->tgenabled;
5201 tgl@sss.pgh.pa.us 1912 : 47573 : build->tgisinternal = pg_trigger->tgisinternal;
1488 alvherre@alvh.no-ip. 1913 : 47573 : build->tgisclone = OidIsValid(pg_trigger->tgparentid);
8049 tgl@sss.pgh.pa.us 1914 : 47573 : build->tgconstrrelid = pg_trigger->tgconstrrelid;
5374 1915 : 47573 : build->tgconstrindid = pg_trigger->tgconstrindid;
6269 1916 : 47573 : build->tgconstraint = pg_trigger->tgconstraint;
8964 JanWieck@Yahoo.com 1917 : 47573 : build->tgdeferrable = pg_trigger->tgdeferrable;
1918 : 47573 : build->tginitdeferred = pg_trigger->tginitdeferred;
9716 bruce@momjian.us 1919 : 47573 : build->tgnargs = pg_trigger->tgnargs;
1920 : : /* tgattr is first var-width field, so OK to access directly */
6956 tgl@sss.pgh.pa.us 1921 : 47573 : build->tgnattr = pg_trigger->tgattr.dim1;
1922 [ + + ]: 47573 : if (build->tgnattr > 0)
1923 : : {
4311 peter_e@gmx.net 1924 : 254 : build->tgattr = (int16 *) palloc(build->tgnattr * sizeof(int16));
6956 tgl@sss.pgh.pa.us 1925 : 254 : memcpy(build->tgattr, &(pg_trigger->tgattr.values),
4311 peter_e@gmx.net 1926 : 254 : build->tgnattr * sizeof(int16));
1927 : : }
1928 : : else
6956 tgl@sss.pgh.pa.us 1929 : 47319 : build->tgattr = NULL;
9716 bruce@momjian.us 1930 [ + + ]: 47573 : if (build->tgnargs > 0)
1931 : : {
1932 : : bytea *val;
1933 : : char *p;
1934 : :
2590 noah@leadboat.com 1935 : 1626 : val = DatumGetByteaPP(fastgetattr(htup,
1936 : : Anum_pg_trigger_tgargs,
1937 : : tgrel->rd_att, &isnull));
9716 bruce@momjian.us 1938 [ - + ]: 1626 : if (isnull)
7574 tgl@sss.pgh.pa.us 1939 [ # # ]:UBC 0 : elog(ERROR, "tgargs is null in trigger for relation \"%s\"",
1940 : : RelationGetRelationName(relation));
2590 noah@leadboat.com 1941 [ + - ]:CBC 1626 : p = (char *) VARDATA_ANY(val);
7853 tgl@sss.pgh.pa.us 1942 : 1626 : build->tgargs = (char **) palloc(build->tgnargs * sizeof(char *));
9716 bruce@momjian.us 1943 [ + + ]: 3611 : for (i = 0; i < build->tgnargs; i++)
1944 : : {
7853 tgl@sss.pgh.pa.us 1945 : 1985 : build->tgargs[i] = pstrdup(p);
9716 bruce@momjian.us 1946 : 1985 : p += strlen(p) + 1;
1947 : : }
1948 : : }
1949 : : else
1950 : 45947 : build->tgargs = NULL;
1951 : :
2718 kgrittn@postgresql.o 1952 : 47573 : datum = fastgetattr(htup, Anum_pg_trigger_tgoldtable,
1953 : : tgrel->rd_att, &isnull);
1954 [ + + ]: 47573 : if (!isnull)
1955 : 367 : build->tgoldtable =
1956 : 367 : DatumGetCString(DirectFunctionCall1(nameout, datum));
1957 : : else
1958 : 47206 : build->tgoldtable = NULL;
1959 : :
1960 : 47573 : datum = fastgetattr(htup, Anum_pg_trigger_tgnewtable,
1961 : : tgrel->rd_att, &isnull);
1962 [ + + ]: 47573 : if (!isnull)
1963 : 518 : build->tgnewtable =
1964 : 518 : DatumGetCString(DirectFunctionCall1(nameout, datum));
1965 : : else
1966 : 47055 : build->tgnewtable = NULL;
1967 : :
5259 tgl@sss.pgh.pa.us 1968 : 47573 : datum = fastgetattr(htup, Anum_pg_trigger_tgqual,
1969 : : tgrel->rd_att, &isnull);
1970 [ + + ]: 47573 : if (!isnull)
1971 : 378 : build->tgqual = TextDatumGetCString(datum);
1972 : : else
1973 : 47195 : build->tgqual = NULL;
1974 : :
5635 1975 : 47573 : numtrigs++;
1976 : : }
1977 : :
8090 1978 : 28237 : systable_endscan(tgscan);
1910 andres@anarazel.de 1979 : 28237 : table_close(tgrel, AccessShareLock);
1980 : :
1981 : : /* There might not be any triggers */
5635 tgl@sss.pgh.pa.us 1982 [ + + ]: 28237 : if (numtrigs == 0)
1983 : : {
1984 : 6711 : pfree(triggers);
1985 : 6711 : return;
1986 : : }
1987 : :
1988 : : /* Build trigdesc */
7823 bruce@momjian.us 1989 : 21526 : trigdesc = (TriggerDesc *) palloc0(sizeof(TriggerDesc));
9716 1990 : 21526 : trigdesc->triggers = triggers;
5635 tgl@sss.pgh.pa.us 1991 : 21526 : trigdesc->numtriggers = numtrigs;
1992 [ + + ]: 69099 : for (i = 0; i < numtrigs; i++)
4935 1993 : 47573 : SetTriggerFlags(trigdesc, &(triggers[i]));
1994 : :
1995 : : /* Copy completed trigdesc into cache storage */
7853 1996 : 21526 : oldContext = MemoryContextSwitchTo(CacheMemoryContext);
1997 : 21526 : relation->trigdesc = CopyTriggerDesc(trigdesc);
1998 : 21526 : MemoryContextSwitchTo(oldContext);
1999 : :
2000 : : /* Release working memory */
2001 : 21526 : FreeTriggerDesc(trigdesc);
2002 : : }
2003 : :
2004 : : /*
2005 : : * Update the TriggerDesc's hint flags to include the specified trigger
2006 : : */
2007 : : static void
4935 2008 : 47573 : SetTriggerFlags(TriggerDesc *trigdesc, Trigger *trigger)
2009 : : {
2010 : 47573 : int16 tgtype = trigger->tgtype;
2011 : :
2012 : 47573 : trigdesc->trig_insert_before_row |=
2013 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2014 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_INSERT);
2015 : 47573 : trigdesc->trig_insert_after_row |=
2016 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2017 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_INSERT);
2018 : 47573 : trigdesc->trig_insert_instead_row |=
2019 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2020 : : TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_INSERT);
2021 : 47573 : trigdesc->trig_insert_before_statement |=
2022 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2023 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_INSERT);
2024 : 47573 : trigdesc->trig_insert_after_statement |=
2025 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2026 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_INSERT);
2027 : 47573 : trigdesc->trig_update_before_row |=
2028 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2029 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_UPDATE);
2030 : 47573 : trigdesc->trig_update_after_row |=
2031 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2032 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_UPDATE);
2033 : 47573 : trigdesc->trig_update_instead_row |=
2034 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2035 : : TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_UPDATE);
2036 : 47573 : trigdesc->trig_update_before_statement |=
2037 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2038 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_UPDATE);
2039 : 47573 : trigdesc->trig_update_after_statement |=
2040 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2041 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_UPDATE);
2042 : 47573 : trigdesc->trig_delete_before_row |=
2043 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2044 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_DELETE);
2045 : 47573 : trigdesc->trig_delete_after_row |=
2046 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2047 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_DELETE);
2048 : 47573 : trigdesc->trig_delete_instead_row |=
2049 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_ROW,
2050 : : TRIGGER_TYPE_INSTEAD, TRIGGER_TYPE_DELETE);
2051 : 47573 : trigdesc->trig_delete_before_statement |=
2052 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2053 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_DELETE);
2054 : 47573 : trigdesc->trig_delete_after_statement |=
2055 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2056 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_DELETE);
2057 : : /* there are no row-level truncate triggers */
2058 : 47573 : trigdesc->trig_truncate_before_statement |=
2059 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2060 : : TRIGGER_TYPE_BEFORE, TRIGGER_TYPE_TRUNCATE);
2061 : 47573 : trigdesc->trig_truncate_after_statement |=
2062 : 47573 : TRIGGER_TYPE_MATCHES(tgtype, TRIGGER_TYPE_STATEMENT,
2063 : : TRIGGER_TYPE_AFTER, TRIGGER_TYPE_TRUNCATE);
2064 : :
2718 kgrittn@postgresql.o 2065 : 95146 : trigdesc->trig_insert_new_table |=
2066 [ + + ]: 64163 : (TRIGGER_FOR_INSERT(tgtype) &&
2067 [ + + ]: 16590 : TRIGGER_USES_TRANSITION_TABLE(trigger->tgnewtable));
2068 : 95146 : trigdesc->trig_update_old_table |=
2069 [ + + ]: 69002 : (TRIGGER_FOR_UPDATE(tgtype) &&
2070 [ + + ]: 21429 : TRIGGER_USES_TRANSITION_TABLE(trigger->tgoldtable));
2071 : 95146 : trigdesc->trig_update_new_table |=
2072 [ + + ]: 69002 : (TRIGGER_FOR_UPDATE(tgtype) &&
2073 [ + + ]: 21429 : TRIGGER_USES_TRANSITION_TABLE(trigger->tgnewtable));
2074 : 95146 : trigdesc->trig_delete_old_table |=
2075 [ + + ]: 60301 : (TRIGGER_FOR_DELETE(tgtype) &&
2076 [ + + ]: 12728 : TRIGGER_USES_TRANSITION_TABLE(trigger->tgoldtable));
9722 vadim4o@yahoo.com 2077 : 47573 : }
2078 : :
2079 : : /*
2080 : : * Copy a TriggerDesc data structure.
2081 : : *
2082 : : * The copy is allocated in the current memory context.
2083 : : */
2084 : : TriggerDesc *
7853 tgl@sss.pgh.pa.us 2085 : 234762 : CopyTriggerDesc(TriggerDesc *trigdesc)
2086 : : {
2087 : : TriggerDesc *newdesc;
2088 : : Trigger *trigger;
2089 : : int i;
2090 : :
2091 [ + + - + ]: 234762 : if (trigdesc == NULL || trigdesc->numtriggers <= 0)
2092 : 204624 : return NULL;
2093 : :
2094 : 30138 : newdesc = (TriggerDesc *) palloc(sizeof(TriggerDesc));
2095 : 30138 : memcpy(newdesc, trigdesc, sizeof(TriggerDesc));
2096 : :
2097 : 30138 : trigger = (Trigger *) palloc(trigdesc->numtriggers * sizeof(Trigger));
2098 : 30138 : memcpy(trigger, trigdesc->triggers,
2099 : 30138 : trigdesc->numtriggers * sizeof(Trigger));
2100 : 30138 : newdesc->triggers = trigger;
2101 : :
2102 [ + + ]: 101597 : for (i = 0; i < trigdesc->numtriggers; i++)
2103 : : {
2104 : 71459 : trigger->tgname = pstrdup(trigger->tgname);
6956 2105 [ + + ]: 71459 : if (trigger->tgnattr > 0)
2106 : : {
2107 : : int16 *newattr;
2108 : :
4311 peter_e@gmx.net 2109 : 494 : newattr = (int16 *) palloc(trigger->tgnattr * sizeof(int16));
6956 tgl@sss.pgh.pa.us 2110 : 494 : memcpy(newattr, trigger->tgattr,
4311 peter_e@gmx.net 2111 : 494 : trigger->tgnattr * sizeof(int16));
6956 tgl@sss.pgh.pa.us 2112 : 494 : trigger->tgattr = newattr;
2113 : : }
7853 2114 [ + + ]: 71459 : if (trigger->tgnargs > 0)
2115 : : {
2116 : : char **newargs;
2117 : : int16 j;
2118 : :
2119 : 4911 : newargs = (char **) palloc(trigger->tgnargs * sizeof(char *));
2120 [ + + ]: 10956 : for (j = 0; j < trigger->tgnargs; j++)
2121 : 6045 : newargs[j] = pstrdup(trigger->tgargs[j]);
2122 : 4911 : trigger->tgargs = newargs;
2123 : : }
5259 2124 [ + + ]: 71459 : if (trigger->tgqual)
2125 : 615 : trigger->tgqual = pstrdup(trigger->tgqual);
2718 kgrittn@postgresql.o 2126 [ + + ]: 71459 : if (trigger->tgoldtable)
2127 : 931 : trigger->tgoldtable = pstrdup(trigger->tgoldtable);
2128 [ + + ]: 71459 : if (trigger->tgnewtable)
2129 : 1106 : trigger->tgnewtable = pstrdup(trigger->tgnewtable);
7853 tgl@sss.pgh.pa.us 2130 : 71459 : trigger++;
2131 : : }
2132 : :
2133 : 30138 : return newdesc;
2134 : : }
2135 : :
2136 : : /*
2137 : : * Free a TriggerDesc data structure.
2138 : : */
2139 : : void
8840 2140 : 526737 : FreeTriggerDesc(TriggerDesc *trigdesc)
2141 : : {
2142 : : Trigger *trigger;
2143 : : int i;
2144 : :
2145 [ + + ]: 526737 : if (trigdesc == NULL)
2146 : 485653 : return;
2147 : :
2148 : 41084 : trigger = trigdesc->triggers;
2149 [ + + ]: 130370 : for (i = 0; i < trigdesc->numtriggers; i++)
2150 : : {
2151 : 89286 : pfree(trigger->tgname);
6956 2152 [ + + ]: 89286 : if (trigger->tgnattr > 0)
2153 : 475 : pfree(trigger->tgattr);
8840 2154 [ + + ]: 89286 : if (trigger->tgnargs > 0)
2155 : : {
2156 [ + + ]: 6845 : while (--(trigger->tgnargs) >= 0)
2157 : 3768 : pfree(trigger->tgargs[trigger->tgnargs]);
2158 : 3077 : pfree(trigger->tgargs);
2159 : : }
5259 2160 [ + + ]: 89286 : if (trigger->tgqual)
2161 : 698 : pfree(trigger->tgqual);
2718 kgrittn@postgresql.o 2162 [ + + ]: 89286 : if (trigger->tgoldtable)
2163 : 694 : pfree(trigger->tgoldtable);
2164 [ + + ]: 89286 : if (trigger->tgnewtable)
2165 : 988 : pfree(trigger->tgnewtable);
8840 tgl@sss.pgh.pa.us 2166 : 89286 : trigger++;
2167 : : }
2168 : 41084 : pfree(trigdesc->triggers);
2169 : 41084 : pfree(trigdesc);
2170 : : }
2171 : :
2172 : : /*
2173 : : * Compare two TriggerDesc structures for logical equality.
2174 : : */
2175 : : #ifdef NOT_USED
2176 : : bool
2177 : : equalTriggerDescs(TriggerDesc *trigdesc1, TriggerDesc *trigdesc2)
2178 : : {
2179 : : int i,
2180 : : j;
2181 : :
2182 : : /*
2183 : : * We need not examine the hint flags, just the trigger array itself; if
2184 : : * we have the same triggers with the same types, the flags should match.
2185 : : *
2186 : : * As of 7.3 we assume trigger set ordering is significant in the
2187 : : * comparison; so we just compare corresponding slots of the two sets.
2188 : : *
2189 : : * Note: comparing the stringToNode forms of the WHEN clauses means that
2190 : : * parse column locations will affect the result. This is okay as long as
2191 : : * this function is only used for detecting exact equality, as for example
2192 : : * in checking for staleness of a cache entry.
2193 : : */
2194 : : if (trigdesc1 != NULL)
2195 : : {
2196 : : if (trigdesc2 == NULL)
2197 : : return false;
2198 : : if (trigdesc1->numtriggers != trigdesc2->numtriggers)
2199 : : return false;
2200 : : for (i = 0; i < trigdesc1->numtriggers; i++)
2201 : : {
2202 : : Trigger *trig1 = trigdesc1->triggers + i;
2203 : : Trigger *trig2 = trigdesc2->triggers + i;
2204 : :
2205 : : if (trig1->tgoid != trig2->tgoid)
2206 : : return false;
2207 : : if (strcmp(trig1->tgname, trig2->tgname) != 0)
2208 : : return false;
2209 : : if (trig1->tgfoid != trig2->tgfoid)
2210 : : return false;
2211 : : if (trig1->tgtype != trig2->tgtype)
2212 : : return false;
2213 : : if (trig1->tgenabled != trig2->tgenabled)
2214 : : return false;
2215 : : if (trig1->tgisinternal != trig2->tgisinternal)
2216 : : return false;
2217 : : if (trig1->tgisclone != trig2->tgisclone)
2218 : : return false;
2219 : : if (trig1->tgconstrrelid != trig2->tgconstrrelid)
2220 : : return false;
2221 : : if (trig1->tgconstrindid != trig2->tgconstrindid)
2222 : : return false;
2223 : : if (trig1->tgconstraint != trig2->tgconstraint)
2224 : : return false;
2225 : : if (trig1->tgdeferrable != trig2->tgdeferrable)
2226 : : return false;
2227 : : if (trig1->tginitdeferred != trig2->tginitdeferred)
2228 : : return false;
2229 : : if (trig1->tgnargs != trig2->tgnargs)
2230 : : return false;
2231 : : if (trig1->tgnattr != trig2->tgnattr)
2232 : : return false;
2233 : : if (trig1->tgnattr > 0 &&
2234 : : memcmp(trig1->tgattr, trig2->tgattr,
2235 : : trig1->tgnattr * sizeof(int16)) != 0)
2236 : : return false;
2237 : : for (j = 0; j < trig1->tgnargs; j++)
2238 : : if (strcmp(trig1->tgargs[j], trig2->tgargs[j]) != 0)
2239 : : return false;
2240 : : if (trig1->tgqual == NULL && trig2->tgqual == NULL)
2241 : : /* ok */ ;
2242 : : else if (trig1->tgqual == NULL || trig2->tgqual == NULL)
2243 : : return false;
2244 : : else if (strcmp(trig1->tgqual, trig2->tgqual) != 0)
2245 : : return false;
2246 : : if (trig1->tgoldtable == NULL && trig2->tgoldtable == NULL)
2247 : : /* ok */ ;
2248 : : else if (trig1->tgoldtable == NULL || trig2->tgoldtable == NULL)
2249 : : return false;
2250 : : else if (strcmp(trig1->tgoldtable, trig2->tgoldtable) != 0)
2251 : : return false;
2252 : : if (trig1->tgnewtable == NULL && trig2->tgnewtable == NULL)
2253 : : /* ok */ ;
2254 : : else if (trig1->tgnewtable == NULL || trig2->tgnewtable == NULL)
2255 : : return false;
2256 : : else if (strcmp(trig1->tgnewtable, trig2->tgnewtable) != 0)
2257 : : return false;
2258 : : }
2259 : : }
2260 : : else if (trigdesc2 != NULL)
2261 : : return false;
2262 : : return true;
2263 : : }
2264 : : #endif /* NOT_USED */
2265 : :
2266 : : /*
2267 : : * Check if there is a row-level trigger with transition tables that prevents
2268 : : * a table from becoming an inheritance child or partition. Return the name
2269 : : * of the first such incompatible trigger, or NULL if there is none.
2270 : : */
2271 : : const char *
2482 rhodiumtoad@postgres 2272 : 1176 : FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
2273 : : {
2274 [ + + ]: 1176 : if (trigdesc != NULL)
2275 : : {
2276 : : int i;
2277 : :
2278 [ + + ]: 168 : for (i = 0; i < trigdesc->numtriggers; ++i)
2279 : : {
2435 tgl@sss.pgh.pa.us 2280 : 123 : Trigger *trigger = &trigdesc->triggers[i];
2281 : :
2482 rhodiumtoad@postgres 2282 [ + - + + ]: 123 : if (trigger->tgoldtable != NULL || trigger->tgnewtable != NULL)
2283 : 6 : return trigger->tgname;
2284 : : }
2285 : : }
2286 : :
2287 : 1170 : return NULL;
2288 : : }
2289 : :
2290 : : /*
2291 : : * Call a trigger function.
2292 : : *
2293 : : * trigdata: trigger descriptor.
2294 : : * tgindx: trigger's index in finfo and instr arrays.
2295 : : * finfo: array of cached trigger function call information.
2296 : : * instr: optional array of EXPLAIN ANALYZE instrumentation state.
2297 : : * per_tuple_context: memory context to execute the function in.
2298 : : *
2299 : : * Returns the tuple (or NULL) as returned by the function.
2300 : : */
2301 : : static HeapTuple
8353 tgl@sss.pgh.pa.us 2302 : 10920 : ExecCallTriggerFunc(TriggerData *trigdata,
2303 : : int tgindx,
2304 : : FmgrInfo *finfo,
2305 : : Instrumentation *instr,
2306 : : MemoryContext per_tuple_context)
2307 : : {
1905 andres@anarazel.de 2308 : 10920 : LOCAL_FCINFO(fcinfo, 0);
2309 : : PgStat_FunctionCallUsage fcusage;
2310 : : Datum result;
2311 : : MemoryContext oldContext;
2312 : :
2313 : : /*
2314 : : * Protect against code paths that may fail to initialize transition table
2315 : : * info.
2316 : : */
2718 kgrittn@postgresql.o 2317 [ + + + + : 10920 : Assert(((TRIGGER_FIRED_BY_INSERT(trigdata->tg_event) ||
+ + + + +
- - + + -
- + ]
2318 : : TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event) ||
2319 : : TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)) &&
2320 : : TRIGGER_FIRED_AFTER(trigdata->tg_event) &&
2321 : : !(trigdata->tg_event & AFTER_TRIGGER_DEFERRABLE) &&
2322 : : !(trigdata->tg_event & AFTER_TRIGGER_INITDEFERRED)) ||
2323 : : (trigdata->tg_oldtable == NULL && trigdata->tg_newtable == NULL));
2324 : :
6960 tgl@sss.pgh.pa.us 2325 : 10920 : finfo += tgindx;
2326 : :
2327 : : /*
2328 : : * We cache fmgr lookup info, to avoid making the lookup again on each
2329 : : * call.
2330 : : */
8353 2331 [ + + ]: 10920 : if (finfo->fn_oid == InvalidOid)
2332 : 9341 : fmgr_info(trigdata->tg_trigger->tgfoid, finfo);
2333 : :
2334 [ - + ]: 10920 : Assert(finfo->fn_oid == trigdata->tg_trigger->tgfoid);
2335 : :
2336 : : /*
2337 : : * If doing EXPLAIN ANALYZE, start charging time to this trigger.
2338 : : */
6960 2339 [ - + ]: 10920 : if (instr)
6960 tgl@sss.pgh.pa.us 2340 :UBC 0 : InstrStartNode(instr + tgindx);
2341 : :
2342 : : /*
2343 : : * Do the function evaluation in the per-tuple memory context, so that
2344 : : * leaked memory will be reclaimed once per tuple. Note in particular that
2345 : : * any new tuple created by the trigger function will live till the end of
2346 : : * the tuple cycle.
2347 : : */
8483 tgl@sss.pgh.pa.us 2348 :CBC 10920 : oldContext = MemoryContextSwitchTo(per_tuple_context);
2349 : :
2350 : : /*
2351 : : * Call the function, passing no arguments but setting a context.
2352 : : */
1905 andres@anarazel.de 2353 : 10920 : InitFunctionCallInfoData(*fcinfo, finfo, 0,
2354 : : InvalidOid, (Node *) trigdata, NULL);
2355 : :
2356 : 10920 : pgstat_init_function_usage(fcinfo, &fcusage);
2357 : :
4463 alvherre@alvh.no-ip. 2358 : 10920 : MyTriggerDepth++;
2359 [ + + ]: 10920 : PG_TRY();
2360 : : {
1905 andres@anarazel.de 2361 : 10920 : result = FunctionCallInvoke(fcinfo);
2362 : : }
1626 peter@eisentraut.org 2363 : 683 : PG_FINALLY();
2364 : : {
4463 alvherre@alvh.no-ip. 2365 : 10920 : MyTriggerDepth--;
2366 : : }
2367 [ + + ]: 10920 : PG_END_TRY();
2368 : :
5813 tgl@sss.pgh.pa.us 2369 : 10237 : pgstat_end_function_usage(&fcusage, true);
2370 : :
8483 2371 : 10237 : MemoryContextSwitchTo(oldContext);
2372 : :
2373 : : /*
2374 : : * Trigger protocol allows function to return a null pointer, but NOT to
2375 : : * set the isnull result flag.
2376 : : */
1905 andres@anarazel.de 2377 [ - + ]: 10237 : if (fcinfo->isnull)
7574 tgl@sss.pgh.pa.us 2378 [ # # ]:UBC 0 : ereport(ERROR,
2379 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2380 : : errmsg("trigger function %u returned null value",
2381 : : fcinfo->flinfo->fn_oid)));
2382 : :
2383 : : /*
2384 : : * If doing EXPLAIN ANALYZE, stop charging time to this trigger, and count
2385 : : * one "tuple returned" (really the number of firings).
2386 : : */
6960 tgl@sss.pgh.pa.us 2387 [ - + ]:CBC 10237 : if (instr)
6529 bruce@momjian.us 2388 :UBC 0 : InstrStopNode(instr + tgindx, 1);
2389 : :
8721 tgl@sss.pgh.pa.us 2390 :CBC 10237 : return (HeapTuple) DatumGetPointer(result);
2391 : : }
2392 : :
2393 : : void
7813 bruce@momjian.us 2394 : 43090 : ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
2395 : : {
2396 : : TriggerDesc *trigdesc;
2397 : : int i;
1511 peter@eisentraut.org 2398 : 43090 : TriggerData LocTriggerData = {0};
2399 : :
7813 bruce@momjian.us 2400 : 43090 : trigdesc = relinfo->ri_TrigDesc;
2401 : :
2402 [ + + ]: 43090 : if (trigdesc == NULL)
2403 : 42984 : return;
4935 tgl@sss.pgh.pa.us 2404 [ + + ]: 3617 : if (!trigdesc->trig_insert_before_statement)
7813 bruce@momjian.us 2405 : 3511 : return;
2406 : :
2407 : : /* no-op if we already fired BS triggers in this context */
2401 tgl@sss.pgh.pa.us 2408 [ - + ]: 106 : if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2409 : : CMD_INSERT))
2401 tgl@sss.pgh.pa.us 2410 :UBC 0 : return;
2411 : :
7813 bruce@momjian.us 2412 :CBC 106 : LocTriggerData.type = T_TriggerData;
2413 : 106 : LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2414 : : TRIGGER_EVENT_BEFORE;
7559 2415 : 106 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
4935 tgl@sss.pgh.pa.us 2416 [ + + ]: 916 : for (i = 0; i < trigdesc->numtriggers; i++)
2417 : : {
2418 : 816 : Trigger *trigger = &trigdesc->triggers[i];
2419 : : HeapTuple newtuple;
2420 : :
2421 [ + + ]: 816 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2422 : : TRIGGER_TYPE_STATEMENT,
2423 : : TRIGGER_TYPE_BEFORE,
2424 : : TRIGGER_TYPE_INSERT))
2425 : 704 : continue;
5259 2426 [ + + ]: 112 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2427 : : NULL, NULL, NULL))
5296 2428 : 15 : continue;
2429 : :
7813 bruce@momjian.us 2430 : 97 : LocTriggerData.tg_trigger = trigger;
2431 : 97 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2432 : : i,
2433 : : relinfo->ri_TrigFunctions,
2434 : : relinfo->ri_TrigInstrument,
2435 [ + + ]: 97 : GetPerTupleMemoryContext(estate));
2436 : :
2437 [ - + ]: 91 : if (newtuple)
7574 tgl@sss.pgh.pa.us 2438 [ # # ]:UBC 0 : ereport(ERROR,
2439 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2440 : : errmsg("BEFORE STATEMENT trigger cannot return a value")));
2441 : : }
2442 : : }
2443 : :
2444 : : void
2482 rhodiumtoad@postgres 2445 :CBC 41884 : ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2446 : : TransitionCaptureState *transition_capture)
2447 : : {
7813 bruce@momjian.us 2448 : 41884 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2449 : :
4935 tgl@sss.pgh.pa.us 2450 [ + + + + ]: 41884 : if (trigdesc && trigdesc->trig_insert_after_statement)
756 alvherre@alvh.no-ip. 2451 : 224 : AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
2452 : : TRIGGER_EVENT_INSERT,
2453 : : false, NULL, NULL, NIL, NULL, transition_capture,
2454 : : false);
7813 bruce@momjian.us 2455 : 41884 : }
2456 : :
2457 : : bool
8353 tgl@sss.pgh.pa.us 2458 : 1198 : ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2459 : : TupleTableSlot *slot)
2460 : : {
2461 : 1198 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
1614 2462 : 1198 : HeapTuple newtuple = NULL;
2463 : : bool should_free;
1511 peter@eisentraut.org 2464 : 1198 : TriggerData LocTriggerData = {0};
2465 : : int i;
2466 : :
8721 tgl@sss.pgh.pa.us 2467 : 1198 : LocTriggerData.type = T_TriggerData;
7813 bruce@momjian.us 2468 : 1198 : LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2469 : : TRIGGER_EVENT_ROW |
2470 : : TRIGGER_EVENT_BEFORE;
8353 tgl@sss.pgh.pa.us 2471 : 1198 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
4935 2472 [ + + ]: 5499 : for (i = 0; i < trigdesc->numtriggers; i++)
2473 : : {
2474 : 4468 : Trigger *trigger = &trigdesc->triggers[i];
2475 : : HeapTuple oldtuple;
2476 : :
2477 [ + + ]: 4468 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2478 : : TRIGGER_TYPE_ROW,
2479 : : TRIGGER_TYPE_BEFORE,
2480 : : TRIGGER_TYPE_INSERT))
2481 : 2122 : continue;
5259 2482 [ + + ]: 2346 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2483 : : NULL, NULL, slot))
5296 2484 : 25 : continue;
2485 : :
1874 andres@anarazel.de 2486 [ + + ]: 2321 : if (!newtuple)
2487 : 1181 : newtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2488 : :
2489 : 2321 : LocTriggerData.tg_trigslot = slot;
8721 tgl@sss.pgh.pa.us 2490 : 2321 : LocTriggerData.tg_trigtuple = oldtuple = newtuple;
8353 2491 : 2321 : LocTriggerData.tg_trigger = trigger;
2492 : 2321 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2493 : : i,
2494 : : relinfo->ri_TrigFunctions,
2495 : : relinfo->ri_TrigInstrument,
8483 2496 [ + + ]: 2321 : GetPerTupleMemoryContext(estate));
9716 bruce@momjian.us 2497 [ + + ]: 2275 : if (newtuple == NULL)
2498 : : {
1977 andres@anarazel.de 2499 [ + + ]: 109 : if (should_free)
1874 2500 : 10 : heap_freetuple(oldtuple);
2501 : 109 : return false; /* "do nothing" */
2502 : : }
2503 [ + + ]: 2166 : else if (newtuple != oldtuple)
2504 : : {
1822 2505 : 373 : ExecForceStoreHeapTuple(newtuple, slot, false);
2506 : :
2507 : : /*
2508 : : * After a tuple in a partition goes through a trigger, the user
2509 : : * could have changed the partition key enough that the tuple no
2510 : : * longer fits the partition. Verify that.
2511 : : */
1488 alvherre@alvh.no-ip. 2512 [ + + ]: 373 : if (trigger->tgisclone &&
2513 [ + + ]: 33 : !ExecPartitionCheck(relinfo, slot, estate, false))
2514 [ + - ]: 12 : ereport(ERROR,
2515 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2516 : : errmsg("moving row to another partition during a BEFORE FOR EACH ROW trigger is not supported"),
2517 : : errdetail("Before executing trigger \"%s\", the row was to be in partition \"%s.%s\".",
2518 : : trigger->tgname,
2519 : : get_namespace_name(RelationGetNamespace(relinfo->ri_RelationDesc)),
2520 : : RelationGetRelationName(relinfo->ri_RelationDesc))));
2521 : :
1874 andres@anarazel.de 2522 [ + + ]: 361 : if (should_free)
2523 : 20 : heap_freetuple(oldtuple);
2524 : :
2525 : : /* signal tuple should be re-fetched if used */
2526 : 361 : newtuple = NULL;
2527 : : }
2528 : : }
2529 : :
2530 : 1031 : return true;
2531 : : }
2532 : :
2533 : : void
8353 tgl@sss.pgh.pa.us 2534 : 5991088 : ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2535 : : TupleTableSlot *slot, List *recheckIndexes,
2536 : : TransitionCaptureState *transition_capture)
2537 : : {
2538 : 5991088 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2539 : :
2482 rhodiumtoad@postgres 2540 [ + + + + : 5991088 : if ((trigdesc && trigdesc->trig_insert_after_row) ||
+ + ]
2541 [ + - ]: 30150 : (transition_capture && transition_capture->tcs_insert_new_table))
756 alvherre@alvh.no-ip. 2542 : 32770 : AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
2543 : : TRIGGER_EVENT_INSERT,
2544 : : true, NULL, slot,
2545 : : recheckIndexes, NULL,
2546 : : transition_capture,
2547 : : false);
7813 bruce@momjian.us 2548 : 5991088 : }
2549 : :
2550 : : bool
4935 tgl@sss.pgh.pa.us 2551 : 90 : ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo,
2552 : : TupleTableSlot *slot)
2553 : : {
2554 : 90 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
1874 andres@anarazel.de 2555 : 90 : HeapTuple newtuple = NULL;
2556 : : bool should_free;
1511 peter@eisentraut.org 2557 : 90 : TriggerData LocTriggerData = {0};
2558 : : int i;
2559 : :
4935 tgl@sss.pgh.pa.us 2560 : 90 : LocTriggerData.type = T_TriggerData;
2561 : 90 : LocTriggerData.tg_event = TRIGGER_EVENT_INSERT |
2562 : : TRIGGER_EVENT_ROW |
2563 : : TRIGGER_EVENT_INSTEAD;
2564 : 90 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2565 [ + + ]: 273 : for (i = 0; i < trigdesc->numtriggers; i++)
2566 : : {
2567 : 192 : Trigger *trigger = &trigdesc->triggers[i];
2568 : : HeapTuple oldtuple;
2569 : :
2570 [ + + ]: 192 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2571 : : TRIGGER_TYPE_ROW,
2572 : : TRIGGER_TYPE_INSTEAD,
2573 : : TRIGGER_TYPE_INSERT))
2574 : 102 : continue;
2575 [ - + ]: 90 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2576 : : NULL, NULL, slot))
4935 tgl@sss.pgh.pa.us 2577 :UBC 0 : continue;
2578 : :
1874 andres@anarazel.de 2579 [ + - ]:CBC 90 : if (!newtuple)
2580 : 90 : newtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2581 : :
2582 : 90 : LocTriggerData.tg_trigslot = slot;
4935 tgl@sss.pgh.pa.us 2583 : 90 : LocTriggerData.tg_trigtuple = oldtuple = newtuple;
2584 : 90 : LocTriggerData.tg_trigger = trigger;
2585 : 90 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2586 : : i,
2587 : : relinfo->ri_TrigFunctions,
2588 : : relinfo->ri_TrigInstrument,
2589 [ + + ]: 90 : GetPerTupleMemoryContext(estate));
2590 [ + + ]: 90 : if (newtuple == NULL)
2591 : : {
1977 andres@anarazel.de 2592 [ + - ]: 9 : if (should_free)
1874 2593 : 9 : heap_freetuple(oldtuple);
2594 : 9 : return false; /* "do nothing" */
2595 : : }
2596 [ + + ]: 81 : else if (newtuple != oldtuple)
2597 : : {
1822 2598 : 18 : ExecForceStoreHeapTuple(newtuple, slot, false);
2599 : :
1874 2600 [ + - ]: 18 : if (should_free)
2601 : 18 : heap_freetuple(oldtuple);
2602 : :
2603 : : /* signal tuple should be re-fetched if used */
2604 : 18 : newtuple = NULL;
2605 : : }
2606 : : }
2607 : :
2608 : 81 : return true;
2609 : : }
2610 : :
2611 : : void
7813 bruce@momjian.us 2612 : 6196 : ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
2613 : : {
2614 : : TriggerDesc *trigdesc;
2615 : : int i;
1511 peter@eisentraut.org 2616 : 6196 : TriggerData LocTriggerData = {0};
2617 : :
7813 bruce@momjian.us 2618 : 6196 : trigdesc = relinfo->ri_TrigDesc;
2619 : :
2620 [ + + ]: 6196 : if (trigdesc == NULL)
2621 : 6157 : return;
4935 tgl@sss.pgh.pa.us 2622 [ + + ]: 813 : if (!trigdesc->trig_delete_before_statement)
7813 bruce@momjian.us 2623 : 753 : return;
2624 : :
2625 : : /* no-op if we already fired BS triggers in this context */
2401 tgl@sss.pgh.pa.us 2626 [ + + ]: 60 : if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2627 : : CMD_DELETE))
2628 : 21 : return;
2629 : :
7813 bruce@momjian.us 2630 : 39 : LocTriggerData.type = T_TriggerData;
2631 : 39 : LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2632 : : TRIGGER_EVENT_BEFORE;
7559 2633 : 39 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
4935 tgl@sss.pgh.pa.us 2634 [ + + ]: 354 : for (i = 0; i < trigdesc->numtriggers; i++)
2635 : : {
2636 : 315 : Trigger *trigger = &trigdesc->triggers[i];
2637 : : HeapTuple newtuple;
2638 : :
2639 [ + + ]: 315 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2640 : : TRIGGER_TYPE_STATEMENT,
2641 : : TRIGGER_TYPE_BEFORE,
2642 : : TRIGGER_TYPE_DELETE))
2643 : 276 : continue;
5259 2644 [ + + ]: 39 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2645 : : NULL, NULL, NULL))
5296 2646 : 6 : continue;
2647 : :
7813 bruce@momjian.us 2648 : 33 : LocTriggerData.tg_trigger = trigger;
2649 : 33 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2650 : : i,
2651 : : relinfo->ri_TrigFunctions,
2652 : : relinfo->ri_TrigInstrument,
2653 [ + + ]: 33 : GetPerTupleMemoryContext(estate));
2654 : :
2655 [ - + ]: 33 : if (newtuple)
7574 tgl@sss.pgh.pa.us 2656 [ # # ]:UBC 0 : ereport(ERROR,
2657 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2658 : : errmsg("BEFORE STATEMENT trigger cannot return a value")));
2659 : : }
2660 : : }
2661 : :
2662 : : void
2482 rhodiumtoad@postgres 2663 :CBC 6116 : ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
2664 : : TransitionCaptureState *transition_capture)
2665 : : {
7813 bruce@momjian.us 2666 : 6116 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2667 : :
4935 tgl@sss.pgh.pa.us 2668 [ + + + + ]: 6116 : if (trigdesc && trigdesc->trig_delete_after_statement)
756 alvherre@alvh.no-ip. 2669 : 115 : AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
2670 : : TRIGGER_EVENT_DELETE,
2671 : : false, NULL, NULL, NIL, NULL, transition_capture,
2672 : : false);
9722 vadim4o@yahoo.com 2673 : 6116 : }
2674 : :
2675 : : /*
2676 : : * Execute BEFORE ROW DELETE triggers.
2677 : : *
2678 : : * True indicates caller can proceed with the delete. False indicates caller
2679 : : * need to suppress the delete and additionally if requested, we need to pass
2680 : : * back the concurrently updated tuple if any.
2681 : : */
2682 : : bool
5284 tgl@sss.pgh.pa.us 2683 : 194 : ExecBRDeleteTriggers(EState *estate, EPQState *epqstate,
2684 : : ResultRelInfo *relinfo,
2685 : : ItemPointer tupleid,
2686 : : HeapTuple fdw_trigtuple,
2687 : : TupleTableSlot **epqslot,
2688 : : TM_Result *tmresult,
2689 : : TM_FailureData *tmfd)
2690 : : {
1874 andres@anarazel.de 2691 : 194 : TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
8353 tgl@sss.pgh.pa.us 2692 : 194 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
6808 2693 : 194 : bool result = true;
1511 peter@eisentraut.org 2694 : 194 : TriggerData LocTriggerData = {0};
2695 : : HeapTuple trigtuple;
1874 andres@anarazel.de 2696 : 194 : bool should_free = false;
2697 : : int i;
2698 : :
3675 noah@leadboat.com 2699 [ - + ]: 194 : Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
2700 [ + + ]: 194 : if (fdw_trigtuple == NULL)
2701 : : {
1654 andres@anarazel.de 2702 : 186 : TupleTableSlot *epqslot_candidate = NULL;
2703 : :
1874 2704 [ + + ]: 186 : if (!GetTupleForTrigger(estate, epqstate, relinfo, tupleid,
2705 : : LockTupleExclusive, slot, &epqslot_candidate,
2706 : : tmresult, tmfd))
3675 noah@leadboat.com 2707 : 6 : return false;
2708 : :
2709 : : /*
2710 : : * If the tuple was concurrently updated and the caller of this
2711 : : * function requested for the updated tuple, skip the trigger
2712 : : * execution.
2713 : : */
1654 andres@anarazel.de 2714 [ + + + - ]: 178 : if (epqslot_candidate != NULL && epqslot != NULL)
2715 : : {
2716 : 1 : *epqslot = epqslot_candidate;
2103 akapila@postgresql.o 2717 : 1 : return false;
2718 : : }
2719 : :
1874 andres@anarazel.de 2720 : 177 : trigtuple = ExecFetchSlotHeapTuple(slot, true, &should_free);
2721 : : }
2722 : : else
2723 : : {
3675 noah@leadboat.com 2724 : 8 : trigtuple = fdw_trigtuple;
1822 andres@anarazel.de 2725 : 8 : ExecForceStoreHeapTuple(trigtuple, slot, false);
2726 : : }
2727 : :
8721 tgl@sss.pgh.pa.us 2728 : 185 : LocTriggerData.type = T_TriggerData;
7813 bruce@momjian.us 2729 : 185 : LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2730 : : TRIGGER_EVENT_ROW |
2731 : : TRIGGER_EVENT_BEFORE;
8353 tgl@sss.pgh.pa.us 2732 : 185 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
4935 2733 [ + + ]: 650 : for (i = 0; i < trigdesc->numtriggers; i++)
2734 : : {
2735 : : HeapTuple newtuple;
2736 : 505 : Trigger *trigger = &trigdesc->triggers[i];
2737 : :
2738 [ + + ]: 505 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2739 : : TRIGGER_TYPE_ROW,
2740 : : TRIGGER_TYPE_BEFORE,
2741 : : TRIGGER_TYPE_DELETE))
2742 : 317 : continue;
5259 2743 [ + + ]: 188 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2744 : : NULL, slot, NULL))
5296 2745 : 7 : continue;
2746 : :
1874 andres@anarazel.de 2747 : 181 : LocTriggerData.tg_trigslot = slot;
8721 tgl@sss.pgh.pa.us 2748 : 181 : LocTriggerData.tg_trigtuple = trigtuple;
8353 2749 : 181 : LocTriggerData.tg_trigger = trigger;
2750 : 181 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2751 : : i,
2752 : : relinfo->ri_TrigFunctions,
2753 : : relinfo->ri_TrigInstrument,
8483 2754 [ + + ]: 181 : GetPerTupleMemoryContext(estate));
9712 vadim4o@yahoo.com 2755 [ + + ]: 167 : if (newtuple == NULL)
2756 : : {
6808 tgl@sss.pgh.pa.us 2757 : 26 : result = false; /* tell caller to suppress delete */
9712 vadim4o@yahoo.com 2758 : 26 : break;
2759 : : }
9204 JanWieck@Yahoo.com 2760 [ + + ]: 141 : if (newtuple != trigtuple)
8886 2761 : 28 : heap_freetuple(newtuple);
2762 : : }
1874 andres@anarazel.de 2763 [ - + ]: 171 : if (should_free)
3675 noah@leadboat.com 2764 :UBC 0 : heap_freetuple(trigtuple);
2765 : :
6808 tgl@sss.pgh.pa.us 2766 :CBC 171 : return result;
2767 : : }
2768 : :
2769 : : /*
2770 : : * Note: is_crosspart_update must be true if the DELETE is being performed
2771 : : * as part of a cross-partition update.
2772 : : */
2773 : : void
756 alvherre@alvh.no-ip. 2774 : 860764 : ExecARDeleteTriggers(EState *estate,
2775 : : ResultRelInfo *relinfo,
2776 : : ItemPointer tupleid,
2777 : : HeapTuple fdw_trigtuple,
2778 : : TransitionCaptureState *transition_capture,
2779 : : bool is_crosspart_update)
2780 : : {
8353 tgl@sss.pgh.pa.us 2781 : 860764 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2782 : :
2482 rhodiumtoad@postgres 2783 [ + + + + : 860764 : if ((trigdesc && trigdesc->trig_delete_after_row) ||
+ + ]
2784 [ + - ]: 2499 : (transition_capture && transition_capture->tcs_delete_old_table))
2785 : : {
3 akorotkov@postgresql 2786 : 3094 : TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
2787 : :
2788 [ - + ]: 3094 : Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
2789 [ + + ]: 3094 : if (fdw_trigtuple == NULL)
2790 : 3086 : GetTupleForTrigger(estate,
2791 : : NULL,
2792 : : relinfo,
2793 : : tupleid,
2794 : : LockTupleExclusive,
2795 : : slot,
2796 : : NULL,
2797 : : NULL,
2798 : : NULL);
2799 : : else
1822 andres@anarazel.de 2800 : 8 : ExecForceStoreHeapTuple(fdw_trigtuple, slot, false);
2801 : :
756 alvherre@alvh.no-ip. 2802 : 3094 : AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
2803 : : TRIGGER_EVENT_DELETE,
2804 : : true, slot, NULL, NIL, NULL,
2805 : : transition_capture,
2806 : : is_crosspart_update);
2807 : : }
9722 vadim4o@yahoo.com 2808 : 860764 : }
2809 : :
2810 : : bool
4935 tgl@sss.pgh.pa.us 2811 : 30 : ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo,
2812 : : HeapTuple trigtuple)
2813 : : {
2814 : 30 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
1874 andres@anarazel.de 2815 : 30 : TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
1511 peter@eisentraut.org 2816 : 30 : TriggerData LocTriggerData = {0};
2817 : : int i;
2818 : :
4935 tgl@sss.pgh.pa.us 2819 : 30 : LocTriggerData.type = T_TriggerData;
2820 : 30 : LocTriggerData.tg_event = TRIGGER_EVENT_DELETE |
2821 : : TRIGGER_EVENT_ROW |
2822 : : TRIGGER_EVENT_INSTEAD;
2823 : 30 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
2824 : :
1822 andres@anarazel.de 2825 : 30 : ExecForceStoreHeapTuple(trigtuple, slot, false);
2826 : :
4935 tgl@sss.pgh.pa.us 2827 [ + + ]: 177 : for (i = 0; i < trigdesc->numtriggers; i++)
2828 : : {
2829 : : HeapTuple rettuple;
2830 : 150 : Trigger *trigger = &trigdesc->triggers[i];
2831 : :
2832 [ + + ]: 150 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2833 : : TRIGGER_TYPE_ROW,
2834 : : TRIGGER_TYPE_INSTEAD,
2835 : : TRIGGER_TYPE_DELETE))
2836 : 120 : continue;
2837 [ - + ]: 30 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2838 : : NULL, slot, NULL))
4935 tgl@sss.pgh.pa.us 2839 :UBC 0 : continue;
2840 : :
1874 andres@anarazel.de 2841 :CBC 30 : LocTriggerData.tg_trigslot = slot;
4935 tgl@sss.pgh.pa.us 2842 : 30 : LocTriggerData.tg_trigtuple = trigtuple;
2843 : 30 : LocTriggerData.tg_trigger = trigger;
2844 : 30 : rettuple = ExecCallTriggerFunc(&LocTriggerData,
2845 : : i,
2846 : : relinfo->ri_TrigFunctions,
2847 : : relinfo->ri_TrigInstrument,
2848 [ + + ]: 30 : GetPerTupleMemoryContext(estate));
2849 [ + + ]: 30 : if (rettuple == NULL)
2850 : 3 : return false; /* Delete was suppressed */
2851 [ - + ]: 27 : if (rettuple != trigtuple)
4935 tgl@sss.pgh.pa.us 2852 :UBC 0 : heap_freetuple(rettuple);
2853 : : }
4935 tgl@sss.pgh.pa.us 2854 :CBC 27 : return true;
2855 : : }
2856 : :
2857 : : void
7813 bruce@momjian.us 2858 : 7366 : ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
2859 : : {
2860 : : TriggerDesc *trigdesc;
2861 : : int i;
1511 peter@eisentraut.org 2862 : 7366 : TriggerData LocTriggerData = {0};
2863 : : Bitmapset *updatedCols;
2864 : :
7813 bruce@momjian.us 2865 : 7366 : trigdesc = relinfo->ri_TrigDesc;
2866 : :
2867 [ + + ]: 7366 : if (trigdesc == NULL)
2868 : 7277 : return;
4935 tgl@sss.pgh.pa.us 2869 [ + + ]: 2025 : if (!trigdesc->trig_update_before_statement)
7813 bruce@momjian.us 2870 : 1936 : return;
2871 : :
2872 : : /* no-op if we already fired BS triggers in this context */
2401 tgl@sss.pgh.pa.us 2873 [ - + ]: 89 : if (before_stmt_triggers_fired(RelationGetRelid(relinfo->ri_RelationDesc),
2874 : : CMD_UPDATE))
2401 tgl@sss.pgh.pa.us 2875 :UBC 0 : return;
2876 : :
2877 : : /* statement-level triggers operate on the parent table */
1161 heikki.linnakangas@i 2878 [ - + ]:CBC 89 : Assert(relinfo->ri_RootResultRelInfo == NULL);
2879 : :
2880 : 89 : updatedCols = ExecGetAllUpdatedCols(relinfo, estate);
2881 : :
7813 bruce@momjian.us 2882 : 89 : LocTriggerData.type = T_TriggerData;
2883 : 89 : LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
2884 : : TRIGGER_EVENT_BEFORE;
7559 2885 : 89 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
1497 peter@eisentraut.org 2886 : 89 : LocTriggerData.tg_updatedcols = updatedCols;
4935 tgl@sss.pgh.pa.us 2887 [ + + ]: 800 : for (i = 0; i < trigdesc->numtriggers; i++)
2888 : : {
2889 : 711 : Trigger *trigger = &trigdesc->triggers[i];
2890 : : HeapTuple newtuple;
2891 : :
2892 [ + + ]: 711 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
2893 : : TRIGGER_TYPE_STATEMENT,
2894 : : TRIGGER_TYPE_BEFORE,
2895 : : TRIGGER_TYPE_UPDATE))
2896 : 622 : continue;
5259 2897 [ + + ]: 89 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
2898 : : updatedCols, NULL, NULL))
5296 2899 : 3 : continue;
2900 : :
7813 bruce@momjian.us 2901 : 86 : LocTriggerData.tg_trigger = trigger;
2902 : 86 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
2903 : : i,
2904 : : relinfo->ri_TrigFunctions,
2905 : : relinfo->ri_TrigInstrument,
2906 [ + - ]: 86 : GetPerTupleMemoryContext(estate));
2907 : :
2908 [ - + ]: 86 : if (newtuple)
7574 tgl@sss.pgh.pa.us 2909 [ # # ]:UBC 0 : ereport(ERROR,
2910 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2911 : : errmsg("BEFORE STATEMENT trigger cannot return a value")));
2912 : : }
2913 : : }
2914 : :
2915 : : void
2482 rhodiumtoad@postgres 2916 :CBC 6971 : ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
2917 : : TransitionCaptureState *transition_capture)
2918 : : {
7813 bruce@momjian.us 2919 : 6971 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
2920 : :
2921 : : /* statement-level triggers operate on the parent table */
1161 heikki.linnakangas@i 2922 [ - + ]: 6971 : Assert(relinfo->ri_RootResultRelInfo == NULL);
2923 : :
4935 tgl@sss.pgh.pa.us 2924 [ + + + + ]: 6971 : if (trigdesc && trigdesc->trig_update_after_statement)
756 alvherre@alvh.no-ip. 2925 : 198 : AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
2926 : : TRIGGER_EVENT_UPDATE,
2927 : : false, NULL, NULL, NIL,
2928 : : ExecGetAllUpdatedCols(relinfo, estate),
2929 : : transition_capture,
2930 : : false);
7813 bruce@momjian.us 2931 : 6971 : }
2932 : :
2933 : : bool
5284 tgl@sss.pgh.pa.us 2934 : 1289 : ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
2935 : : ResultRelInfo *relinfo,
2936 : : ItemPointer tupleid,
2937 : : HeapTuple fdw_trigtuple,
2938 : : TupleTableSlot *newslot,
2939 : : TM_Result *tmresult,
2940 : : TM_FailureData *tmfd)
2941 : : {
8353 2942 : 1289 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
1874 andres@anarazel.de 2943 : 1289 : TupleTableSlot *oldslot = ExecGetTriggerOldSlot(estate, relinfo);
2944 : 1289 : HeapTuple newtuple = NULL;
2945 : : HeapTuple trigtuple;
2946 : 1289 : bool should_free_trig = false;
2947 : 1289 : bool should_free_new = false;
1511 peter@eisentraut.org 2948 : 1289 : TriggerData LocTriggerData = {0};
2949 : : int i;
2950 : : Bitmapset *updatedCols;
2951 : : LockTupleMode lockmode;
2952 : :
2953 : : /* Determine lock mode to use */
3264 andres@anarazel.de 2954 : 1289 : lockmode = ExecUpdateLockMode(estate, relinfo);
2955 : :
3675 noah@leadboat.com 2956 [ - + ]: 1289 : Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
2957 [ + + ]: 1289 : if (fdw_trigtuple == NULL)
2958 : : {
1654 andres@anarazel.de 2959 : 1270 : TupleTableSlot *epqslot_candidate = NULL;
2960 : :
2961 : : /* get a copy of the on-disk tuple we are planning to update */
1874 2962 [ + + ]: 1270 : if (!GetTupleForTrigger(estate, epqstate, relinfo, tupleid,
2963 : : lockmode, oldslot, &epqslot_candidate,
2964 : : tmresult, tmfd))
2965 : 11 : return false; /* cancel the update action */
2966 : :
2967 : : /*
2968 : : * In READ COMMITTED isolation level it's possible that target tuple
2969 : : * was changed due to concurrent update. In that case we have a raw
2970 : : * subplan output tuple in epqslot_candidate, and need to form a new
2971 : : * insertable tuple using ExecGetUpdateNewTuple to replace the one we
2972 : : * received in newslot. Neither we nor our callers have any further
2973 : : * interest in the passed-in tuple, so it's okay to overwrite newslot
2974 : : * with the newer data.
2975 : : */
1654 2976 [ + + ]: 1255 : if (epqslot_candidate != NULL)
2977 : : {
2978 : : TupleTableSlot *epqslot_clean;
2979 : :
1110 tgl@sss.pgh.pa.us 2980 : 3 : epqslot_clean = ExecGetUpdateNewTuple(relinfo, epqslot_candidate,
2981 : : oldslot);
2982 : :
2983 : : /*
2984 : : * Typically, the caller's newslot was also generated by
2985 : : * ExecGetUpdateNewTuple, so that epqslot_clean will be the same
2986 : : * slot and copying is not needed. But do the right thing if it
2987 : : * isn't.
2988 : : */
91 2989 [ - + ]: 3 : if (unlikely(newslot != epqslot_clean))
1654 andres@anarazel.de 2990 :UBC 0 : ExecCopySlot(newslot, epqslot_clean);
2991 : :
2992 : : /*
2993 : : * At this point newslot contains a virtual tuple that may
2994 : : * reference some fields of oldslot's tuple in some disk buffer.
2995 : : * If that tuple is in a different page than the original target
2996 : : * tuple, then our only pin on that buffer is oldslot's, and we're
2997 : : * about to release it. Hence we'd better materialize newslot to
2998 : : * ensure it doesn't contain references into an unpinned buffer.
2999 : : * (We'd materialize it below anyway, but too late for safety.)
3000 : : */
91 tgl@sss.pgh.pa.us 3001 :CBC 3 : ExecMaterializeSlot(newslot);
3002 : : }
3003 : :
3004 : : /*
3005 : : * Here we convert oldslot to a materialized slot holding trigtuple.
3006 : : * Neither slot passed to the triggers will hold any buffer pin.
3007 : : */
1266 3008 : 1255 : trigtuple = ExecFetchSlotHeapTuple(oldslot, true, &should_free_trig);
3009 : : }
3010 : : else
3011 : : {
3012 : : /* Put the FDW-supplied tuple into oldslot to unify the cases */
1822 andres@anarazel.de 3013 : 19 : ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false);
3675 noah@leadboat.com 3014 : 19 : trigtuple = fdw_trigtuple;
3015 : : }
3016 : :
8721 tgl@sss.pgh.pa.us 3017 : 1274 : LocTriggerData.type = T_TriggerData;
7767 bruce@momjian.us 3018 : 1274 : LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
3019 : : TRIGGER_EVENT_ROW |
3020 : : TRIGGER_EVENT_BEFORE;
8353 tgl@sss.pgh.pa.us 3021 : 1274 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
1161 heikki.linnakangas@i 3022 : 1274 : updatedCols = ExecGetAllUpdatedCols(relinfo, estate);
1497 peter@eisentraut.org 3023 : 1274 : LocTriggerData.tg_updatedcols = updatedCols;
4935 tgl@sss.pgh.pa.us 3024 [ + + ]: 5705 : for (i = 0; i < trigdesc->numtriggers; i++)
3025 : : {
3026 : 4514 : Trigger *trigger = &trigdesc->triggers[i];
3027 : : HeapTuple oldtuple;
3028 : :
3029 [ + + ]: 4514 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3030 : : TRIGGER_TYPE_ROW,
3031 : : TRIGGER_TYPE_BEFORE,
3032 : : TRIGGER_TYPE_UPDATE))
3033 : 2198 : continue;
5259 3034 [ + + ]: 2316 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3035 : : updatedCols, oldslot, newslot))
5296 3036 : 43 : continue;
3037 : :
1874 andres@anarazel.de 3038 [ + + ]: 2273 : if (!newtuple)
3039 : 1277 : newtuple = ExecFetchSlotHeapTuple(newslot, true, &should_free_new);
3040 : :
3041 : 2273 : LocTriggerData.tg_trigslot = oldslot;
8721 tgl@sss.pgh.pa.us 3042 : 2273 : LocTriggerData.tg_trigtuple = trigtuple;
3043 : 2273 : LocTriggerData.tg_newtuple = oldtuple = newtuple;
1874 andres@anarazel.de 3044 : 2273 : LocTriggerData.tg_newslot = newslot;
8353 tgl@sss.pgh.pa.us 3045 : 2273 : LocTriggerData.tg_trigger = trigger;
3046 : 2273 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
3047 : : i,
3048 : : relinfo->ri_TrigFunctions,
3049 : : relinfo->ri_TrigInstrument,
8483 3050 [ + - ]: 2273 : GetPerTupleMemoryContext(estate));
3051 : :
9712 vadim4o@yahoo.com 3052 [ + + ]: 2259 : if (newtuple == NULL)
3053 : : {
1874 andres@anarazel.de 3054 [ - + ]: 69 : if (should_free_trig)
3675 noah@leadboat.com 3055 :UBC 0 : heap_freetuple(trigtuple);
1874 andres@anarazel.de 3056 [ + + ]:CBC 69 : if (should_free_new)
3057 : 2 : heap_freetuple(oldtuple);
3058 : 69 : return false; /* "do nothing" */
3059 : : }
3060 [ + + ]: 2190 : else if (newtuple != oldtuple)
3061 : : {
1822 3062 : 649 : ExecForceStoreHeapTuple(newtuple, newslot, false);
3063 : :
3064 : : /*
3065 : : * If the tuple returned by the trigger / being stored, is the old
3066 : : * row version, and the heap tuple passed to the trigger was
3067 : : * allocated locally, materialize the slot. Otherwise we might
3068 : : * free it while still referenced by the slot.
3069 : : */
1823 3070 [ - + - - ]: 649 : if (should_free_trig && newtuple == trigtuple)
1823 andres@anarazel.de 3071 :UBC 0 : ExecMaterializeSlot(newslot);
3072 : :
1874 andres@anarazel.de 3073 [ + + ]:CBC 649 : if (should_free_new)
3074 : 1 : heap_freetuple(oldtuple);
3075 : :
3076 : : /* signal tuple should be re-fetched if used */
3077 : 649 : newtuple = NULL;
3078 : : }
3079 : : }
3080 [ - + ]: 1191 : if (should_free_trig)
1874 andres@anarazel.de 3081 :UBC 0 : heap_freetuple(trigtuple);
3082 : :
1874 andres@anarazel.de 3083 :CBC 1191 : return true;
3084 : : }
3085 : :
3086 : : /*
3087 : : * Note: 'src_partinfo' and 'dst_partinfo', when non-NULL, refer to the source
3088 : : * and destination partitions, respectively, of a cross-partition update of
3089 : : * the root partitioned table mentioned in the query, given by 'relinfo'.
3090 : : * 'tupleid' in that case refers to the ctid of the "old" tuple in the source
3091 : : * partition, and 'newslot' contains the "new" tuple in the destination
3092 : : * partition. This interface allows to support the requirements of
3093 : : * ExecCrossPartitionUpdateForeignKey(); is_crosspart_update must be true in
3094 : : * that case.
3095 : : */
3096 : : void
8353 tgl@sss.pgh.pa.us 3097 : 188299 : ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
3098 : : ResultRelInfo *src_partinfo,
3099 : : ResultRelInfo *dst_partinfo,
3100 : : ItemPointer tupleid,
3101 : : HeapTuple fdw_trigtuple,
3102 : : TupleTableSlot *newslot,
3103 : : List *recheckIndexes,
3104 : : TransitionCaptureState *transition_capture,
3105 : : bool is_crosspart_update)
3106 : : {
3107 : 188299 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3108 : :
2482 rhodiumtoad@postgres 3109 [ + + + + : 188299 : if ((trigdesc && trigdesc->trig_update_after_row) ||
+ + ]
3110 : 180 : (transition_capture &&
3111 [ + + ]: 180 : (transition_capture->tcs_update_old_table ||
3112 [ + - ]: 6 : transition_capture->tcs_update_new_table)))
3113 : : {
3114 : : /*
3115 : : * Note: if the UPDATE is converted into a DELETE+INSERT as part of
3116 : : * update-partition-key operation, then this function is also called
3117 : : * separately for DELETE and INSERT to capture transition table rows.
3118 : : * In such case, either old tuple or new tuple can be NULL.
3119 : : */
3120 : : TupleTableSlot *oldslot;
3121 : : ResultRelInfo *tupsrc;
3122 : :
756 alvherre@alvh.no-ip. 3123 [ + + - + : 1793 : Assert((src_partinfo != NULL && dst_partinfo != NULL) ||
- + ]
3124 : : !is_crosspart_update);
3125 : :
3 akorotkov@postgresql 3126 [ + + ]: 1793 : tupsrc = src_partinfo ? src_partinfo : relinfo;
3127 : 1793 : oldslot = ExecGetTriggerOldSlot(estate, tupsrc);
3128 : :
3129 [ + + + + ]: 1793 : if (fdw_trigtuple == NULL && ItemPointerIsValid(tupleid))
3130 : 1762 : GetTupleForTrigger(estate,
3131 : : NULL,
3132 : : tupsrc,
3133 : : tupleid,
3134 : : LockTupleExclusive,
3135 : : oldslot,
3136 : : NULL,
3137 : : NULL,
3138 : : NULL);
3139 [ + + ]: 31 : else if (fdw_trigtuple != NULL)
1822 andres@anarazel.de 3140 : 10 : ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false);
3141 : : else
3 akorotkov@postgresql 3142 : 21 : ExecClearTuple(oldslot);
3143 : :
756 alvherre@alvh.no-ip. 3144 : 1793 : AfterTriggerSaveEvent(estate, relinfo,
3145 : : src_partinfo, dst_partinfo,
3146 : : TRIGGER_EVENT_UPDATE,
3147 : : true,
3148 : : oldslot, newslot, recheckIndexes,
3149 : : ExecGetAllUpdatedCols(relinfo, estate),
3150 : : transition_capture,
3151 : : is_crosspart_update);
3152 : : }
9722 vadim4o@yahoo.com 3153 : 188299 : }
3154 : :
3155 : : bool
4935 tgl@sss.pgh.pa.us 3156 : 96 : ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
3157 : : HeapTuple trigtuple, TupleTableSlot *newslot)
3158 : : {
3159 : 96 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
1874 andres@anarazel.de 3160 : 96 : TupleTableSlot *oldslot = ExecGetTriggerOldSlot(estate, relinfo);
1614 tgl@sss.pgh.pa.us 3161 : 96 : HeapTuple newtuple = NULL;
3162 : : bool should_free;
1511 peter@eisentraut.org 3163 : 96 : TriggerData LocTriggerData = {0};
3164 : : int i;
3165 : :
4935 tgl@sss.pgh.pa.us 3166 : 96 : LocTriggerData.type = T_TriggerData;
3167 : 96 : LocTriggerData.tg_event = TRIGGER_EVENT_UPDATE |
3168 : : TRIGGER_EVENT_ROW |
3169 : : TRIGGER_EVENT_INSTEAD;
3170 : 96 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
3171 : :
1822 andres@anarazel.de 3172 : 96 : ExecForceStoreHeapTuple(trigtuple, oldslot, false);
3173 : :
4935 tgl@sss.pgh.pa.us 3174 [ + + ]: 369 : for (i = 0; i < trigdesc->numtriggers; i++)
3175 : : {
3176 : 285 : Trigger *trigger = &trigdesc->triggers[i];
3177 : : HeapTuple oldtuple;
3178 : :
3179 [ + + ]: 285 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3180 : : TRIGGER_TYPE_ROW,
3181 : : TRIGGER_TYPE_INSTEAD,
3182 : : TRIGGER_TYPE_UPDATE))
3183 : 189 : continue;
3184 [ - + ]: 96 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3185 : : NULL, oldslot, newslot))
4935 tgl@sss.pgh.pa.us 3186 :UBC 0 : continue;
3187 : :
1874 andres@anarazel.de 3188 [ + - ]:CBC 96 : if (!newtuple)
3189 : 96 : newtuple = ExecFetchSlotHeapTuple(newslot, true, &should_free);
3190 : :
3191 : 96 : LocTriggerData.tg_trigslot = oldslot;
4801 tgl@sss.pgh.pa.us 3192 : 96 : LocTriggerData.tg_trigtuple = trigtuple;
1874 andres@anarazel.de 3193 : 96 : LocTriggerData.tg_newslot = newslot;
4801 tgl@sss.pgh.pa.us 3194 : 96 : LocTriggerData.tg_newtuple = oldtuple = newtuple;
3195 : :
4935 3196 : 96 : LocTriggerData.tg_trigger = trigger;
4801 3197 : 96 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
3198 : : i,
3199 : : relinfo->ri_TrigFunctions,
3200 : : relinfo->ri_TrigInstrument,
4935 3201 [ + + ]: 96 : GetPerTupleMemoryContext(estate));
3202 [ + + ]: 93 : if (newtuple == NULL)
3203 : : {
1874 andres@anarazel.de 3204 : 9 : return false; /* "do nothing" */
3205 : : }
3206 [ + + ]: 84 : else if (newtuple != oldtuple)
3207 : : {
1822 3208 : 27 : ExecForceStoreHeapTuple(newtuple, newslot, false);
3209 : :
1874 3210 [ + - ]: 27 : if (should_free)
3211 : 27 : heap_freetuple(oldtuple);
3212 : :
3213 : : /* signal tuple should be re-fetched if used */
3214 : 27 : newtuple = NULL;
3215 : : }
3216 : : }
3217 : :
3218 : 84 : return true;
3219 : : }
3220 : :
3221 : : void
5861 tgl@sss.pgh.pa.us 3222 : 1679 : ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
3223 : : {
3224 : : TriggerDesc *trigdesc;
3225 : : int i;
1511 peter@eisentraut.org 3226 : 1679 : TriggerData LocTriggerData = {0};
3227 : :
5861 tgl@sss.pgh.pa.us 3228 : 1679 : trigdesc = relinfo->ri_TrigDesc;
3229 : :
3230 [ + + ]: 1679 : if (trigdesc == NULL)
3231 : 1673 : return;
4935 3232 [ + + ]: 379 : if (!trigdesc->trig_truncate_before_statement)
5861 3233 : 373 : return;
3234 : :
3235 : 6 : LocTriggerData.type = T_TriggerData;
3236 : 6 : LocTriggerData.tg_event = TRIGGER_EVENT_TRUNCATE |
3237 : : TRIGGER_EVENT_BEFORE;
3238 : 6 : LocTriggerData.tg_relation = relinfo->ri_RelationDesc;
3239 : :
4935 3240 [ + + ]: 18 : for (i = 0; i < trigdesc->numtriggers; i++)
3241 : : {
3242 : 12 : Trigger *trigger = &trigdesc->triggers[i];
3243 : : HeapTuple newtuple;
3244 : :
3245 [ + + ]: 12 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
3246 : : TRIGGER_TYPE_STATEMENT,
3247 : : TRIGGER_TYPE_BEFORE,
3248 : : TRIGGER_TYPE_TRUNCATE))
3249 : 6 : continue;
5259 3250 [ - + ]: 6 : if (!TriggerEnabled(estate, relinfo, trigger, LocTriggerData.tg_event,
3251 : : NULL, NULL, NULL))
5296 tgl@sss.pgh.pa.us 3252 :UBC 0 : continue;
3253 : :
5861 tgl@sss.pgh.pa.us 3254 :CBC 6 : LocTriggerData.tg_trigger = trigger;
3255 : 6 : newtuple = ExecCallTriggerFunc(&LocTriggerData,
3256 : : i,
3257 : : relinfo->ri_TrigFunctions,
3258 : : relinfo->ri_TrigInstrument,
3259 [ - + ]: 6 : GetPerTupleMemoryContext(estate));
3260 : :
3261 [ - + ]: 6 : if (newtuple)
5861 tgl@sss.pgh.pa.us 3262 [ # # ]:UBC 0 : ereport(ERROR,
3263 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
3264 : : errmsg("BEFORE STATEMENT trigger cannot return a value")));
3265 : : }
3266 : : }
3267 : :
3268 : : void
5861 tgl@sss.pgh.pa.us 3269 :CBC 1675 : ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
3270 : : {
3271 : 1675 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
3272 : :
4935 3273 [ + + + + ]: 1675 : if (trigdesc && trigdesc->trig_truncate_after_statement)
756 alvherre@alvh.no-ip. 3274 : 4 : AfterTriggerSaveEvent(estate, relinfo,
3275 : : NULL, NULL,
3276 : : TRIGGER_EVENT_TRUNCATE,
3277 : : false, NULL, NULL, NIL, NULL, NULL,
3278 : : false);
5861 tgl@sss.pgh.pa.us 3279 : 1675 : }
3280 : :
3281 : :
3282 : : /*
3283 : : * Fetch tuple into "oldslot", dealing with locking and EPQ if necessary
3284 : : */
3285 : : static bool
5300 3286 : 6304 : GetTupleForTrigger(EState *estate,
3287 : : EPQState *epqstate,
3288 : : ResultRelInfo *relinfo,
3289 : : ItemPointer tid,
3290 : : LockTupleMode lockmode,
3291 : : TupleTableSlot *oldslot,
3292 : : TupleTableSlot **epqslot,
3293 : : TM_Result *tmresultp,
3294 : : TM_FailureData *tmfdp)
3295 : : {
8353 3296 : 6304 : Relation relation = relinfo->ri_RelationDesc;
3297 : :
1654 andres@anarazel.de 3298 [ + + ]: 6304 : if (epqslot != NULL)
3299 : : {
3300 : : TM_Result test;
3301 : : TM_FailureData tmfd;
1849 3302 : 1456 : int lockflags = 0;
3303 : :
1654 3304 : 1456 : *epqslot = NULL;
3305 : :
3306 : : /* caller must pass an epqstate if EvalPlanQual is possible */
5284 tgl@sss.pgh.pa.us 3307 [ - + ]: 1456 : Assert(epqstate != NULL);
3308 : :
3309 : : /*
3310 : : * lock tuple for update
3311 : : */
1849 andres@anarazel.de 3312 [ + + ]: 1456 : if (!IsolationUsesXactSnapshot())
3313 : 1024 : lockflags |= TUPLE_LOCK_FLAG_FIND_LAST_VERSION;
1788 3314 : 1456 : test = table_tuple_lock(relation, tid, estate->es_snapshot, oldslot,
3315 : : estate->es_output_cid,
3316 : : lockmode, LockWaitBlock,
3317 : : lockflags,
3318 : : &tmfd);
3319 : :
3320 : : /* Let the caller know about the status of this operation */
398 dean.a.rasheed@gmail 3321 [ + + ]: 1454 : if (tmresultp)
3322 : 108 : *tmresultp = test;
748 alvherre@alvh.no-ip. 3323 [ + + ]: 1454 : if (tmfdp)
3324 : 1451 : *tmfdp = tmfd;
3325 : :
9252 vadim4o@yahoo.com 3326 [ + + + + : 1454 : switch (test)
- - ]
3327 : : {
1849 andres@anarazel.de 3328 : 3 : case TM_SelfModified:
3329 : :
3330 : : /*
3331 : : * The target tuple was already updated or deleted by the
3332 : : * current command, or by a later command in the current
3333 : : * transaction. We ignore the tuple in the former case, and
3334 : : * throw error in the latter case, for the same reasons
3335 : : * enumerated in ExecUpdate and ExecDelete in
3336 : : * nodeModifyTable.c.
3337 : : */
3338 [ + - ]: 3 : if (tmfd.cmax != estate->es_output_cid)
4188 kgrittn@postgresql.o 3339 [ + - ]: 3 : ereport(ERROR,
3340 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3341 : : errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
3342 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3343 : :
3344 : : /* treat it as deleted; do not process */
1874 andres@anarazel.de 3345 :UBC 0 : return false;
3346 : :
1849 andres@anarazel.de 3347 :CBC 1442 : case TM_Ok:
3348 [ + + ]: 1442 : if (tmfd.traversed)
3349 : : {
3350 : : /*
3351 : : * Recheck the tuple using EPQ. For MERGE, we leave this
3352 : : * to the caller (it must do additional rechecking, and
3353 : : * might end up executing a different action entirely).
3354 : : */
398 dean.a.rasheed@gmail 3355 [ + + ]: 13 : if (estate->es_plannedstmt->commandType == CMD_MERGE)
3356 : : {
3357 [ + - ]: 7 : if (tmresultp)
3358 : 7 : *tmresultp = TM_Updated;
3359 : 7 : return false;
3360 : : }
3361 : :
1654 andres@anarazel.de 3362 : 6 : *epqslot = EvalPlanQual(epqstate,
3363 : : relation,
3364 : : relinfo->ri_RangeTableIndex,
3365 : : oldslot);
3366 : :
3367 : : /*
3368 : : * If PlanQual failed for updated tuple - we must not
3369 : : * process this tuple!
3370 : : */
3371 [ + - + + ]: 6 : if (TupIsNull(*epqslot))
3372 : : {
3373 : 2 : *epqslot = NULL;
1849 3374 : 2 : return false;
3375 : : }
3376 : : }
3377 : 1433 : break;
3378 : :
3379 : 1 : case TM_Updated:
3380 [ + - ]: 1 : if (IsolationUsesXactSnapshot())
3381 [ + - ]: 1 : ereport(ERROR,
3382 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3383 : : errmsg("could not serialize access due to concurrent update")));
1788 andres@anarazel.de 3384 [ # # ]:UBC 0 : elog(ERROR, "unexpected table_tuple_lock status: %u", test);
3385 : : break;
3386 : :
1849 andres@anarazel.de 3387 :CBC 8 : case TM_Deleted:
3388 [ + + ]: 8 : if (IsolationUsesXactSnapshot())
3389 [ + - ]: 1 : ereport(ERROR,
3390 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3391 : : errmsg("could not serialize access due to concurrent delete")));
3392 : : /* tuple was deleted */
1874 3393 : 7 : return false;
3394 : :
1849 andres@anarazel.de 3395 :UBC 0 : case TM_Invisible:
3264 3396 [ # # ]: 0 : elog(ERROR, "attempted to lock invisible tuple");
3397 : : break;
3398 : :
9252 vadim4o@yahoo.com 3399 : 0 : default:
1788 andres@anarazel.de 3400 [ # # ]: 0 : elog(ERROR, "unrecognized table_tuple_lock status: %u", test);
3401 : : return false; /* keep compiler quiet */
3402 : : }
3403 : : }
3404 : : else
3405 : : {
3406 : : /*
3407 : : * We expect the tuple to be present, thus very simple error handling
3408 : : * suffices.
3409 : : */
1788 andres@anarazel.de 3410 [ - + ]:CBC 4848 : if (!table_tuple_fetch_row_version(relation, tid, SnapshotAny,
3411 : : oldslot))
1847 andres@anarazel.de 3412 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple for trigger");
3413 : : }
3414 : :
1874 andres@anarazel.de 3415 :CBC 6281 : return true;
3416 : : }
3417 : :
3418 : : /*
3419 : : * Is trigger enabled to fire?
3420 : : */
3421 : : static bool
5259 tgl@sss.pgh.pa.us 3422 : 12163 : TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
3423 : : Trigger *trigger, TriggerEvent event,
3424 : : Bitmapset *modifiedCols,
3425 : : TupleTableSlot *oldslot, TupleTableSlot *newslot)
3426 : : {
3427 : : /* Check replication-role-dependent enable state */
5296 3428 [ + + ]: 12163 : if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
3429 : : {
3430 [ + + ]: 63 : if (trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN ||
3431 [ + + ]: 39 : trigger->tgenabled == TRIGGER_DISABLED)
3432 : 42 : return false;
3433 : : }
3434 : : else /* ORIGIN or LOCAL role */
3435 : : {
3436 [ + + ]: 12100 : if (trigger->tgenabled == TRIGGER_FIRES_ON_REPLICA ||
3437 [ + + ]: 12099 : trigger->tgenabled == TRIGGER_DISABLED)
3438 : 79 : return false;
3439 : : }
3440 : :
3441 : : /*
3442 : : * Check for column-specific trigger (only possible for UPDATE, and in
3443 : : * fact we *must* ignore tgattr for other event types)
3444 : : */
3445 [ + + + + ]: 12042 : if (trigger->tgnattr > 0 && TRIGGER_FIRED_BY_UPDATE(event))
3446 : : {
3447 : : int i;
3448 : : bool modified;
3449 : :
3450 : 212 : modified = false;
3451 [ + + ]: 278 : for (i = 0; i < trigger->tgnattr; i++)
3452 : : {
3453 [ + + ]: 236 : if (bms_is_member(trigger->tgattr[i] - FirstLowInvalidHeapAttributeNumber,
3454 : : modifiedCols))
3455 : : {
3456 : 170 : modified = true;
3457 : 170 : break;
3458 : : }
3459 : : }
3460 [ + + ]: 212 : if (!modified)
3461 : 42 : return false;
3462 : : }
3463 : :
3464 : : /* Check for WHEN clause */
5259 3465 [ + + ]: 12000 : if (trigger->tgqual)
3466 : : {
3467 : : ExprState **predicate;
3468 : : ExprContext *econtext;
3469 : : MemoryContext oldContext;
3470 : : int i;
3471 : :
3472 [ - + ]: 225 : Assert(estate != NULL);
3473 : :
3474 : : /*
3475 : : * trigger is an element of relinfo->ri_TrigDesc->triggers[]; find the
3476 : : * matching element of relinfo->ri_TrigWhenExprs[]
3477 : : */
3478 : 225 : i = trigger - relinfo->ri_TrigDesc->triggers;
3479 : 225 : predicate = &relinfo->ri_TrigWhenExprs[i];
3480 : :
3481 : : /*
3482 : : * If first time through for this WHEN expression, build expression
3483 : : * nodetrees for it. Keep them in the per-query memory context so
3484 : : * they'll survive throughout the query.
3485 : : */
2588 andres@anarazel.de 3486 [ + + ]: 225 : if (*predicate == NULL)
3487 : : {
3488 : : Node *tgqual;
3489 : :
5259 tgl@sss.pgh.pa.us 3490 : 121 : oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
3491 : 121 : tgqual = stringToNode(trigger->tgqual);
3492 : : /* Change references to OLD and NEW to INNER_VAR and OUTER_VAR */
4569 3493 : 121 : ChangeVarNodes(tgqual, PRS2_OLD_VARNO, INNER_VAR, 0);
3494 : 121 : ChangeVarNodes(tgqual, PRS2_NEW_VARNO, OUTER_VAR, 0);
3495 : : /* ExecPrepareQual wants implicit-AND form */
5259 3496 : 121 : tgqual = (Node *) make_ands_implicit((Expr *) tgqual);
2588 andres@anarazel.de 3497 : 121 : *predicate = ExecPrepareQual((List *) tgqual, estate);
5259 tgl@sss.pgh.pa.us 3498 : 121 : MemoryContextSwitchTo(oldContext);
3499 : : }
3500 : :
3501 : : /*
3502 : : * We will use the EState's per-tuple context for evaluating WHEN
3503 : : * expressions (creating it if it's not already there).
3504 : : */
3505 [ + + ]: 225 : econtext = GetPerTupleExprContext(estate);
3506 : :
3507 : : /*
3508 : : * Finally evaluate the expression, making the old and/or new tuples
3509 : : * available as INNER_VAR/OUTER_VAR respectively.
3510 : : */
3511 : 225 : econtext->ecxt_innertuple = oldslot;
3512 : 225 : econtext->ecxt_outertuple = newslot;
2588 andres@anarazel.de 3513 [ + + ]: 225 : if (!ExecQual(*predicate, econtext))
5259 tgl@sss.pgh.pa.us 3514 : 120 : return false;
3515 : : }
3516 : :
5296 3517 : 11880 : return true;
3518 : : }
3519 : :
3520 : :
3521 : : /* ----------
3522 : : * After-trigger stuff
3523 : : *
3524 : : * The AfterTriggersData struct holds data about pending AFTER trigger events
3525 : : * during the current transaction tree. (BEFORE triggers are fired
3526 : : * immediately so we don't need any persistent state about them.) The struct
3527 : : * and most of its subsidiary data are kept in TopTransactionContext; however
3528 : : * some data that can be discarded sooner appears in the CurTransactionContext
3529 : : * of the relevant subtransaction. Also, the individual event records are
3530 : : * kept in a separate sub-context of TopTransactionContext. This is done
3531 : : * mainly so that it's easy to tell from a memory context dump how much space
3532 : : * is being eaten by trigger events.
3533 : : *
3534 : : * Because the list of pending events can grow large, we go to some
3535 : : * considerable effort to minimize per-event memory consumption. The event
3536 : : * records are grouped into chunks and common data for similar events in the
3537 : : * same chunk is only stored once.
3538 : : *
3539 : : * XXX We need to be able to save the per-event data in a file if it grows too
3540 : : * large.
3541 : : * ----------
3542 : : */
3543 : :
3544 : : /* Per-trigger SET CONSTRAINT status */
3545 : : typedef struct SetConstraintTriggerData
3546 : : {
3547 : : Oid sct_tgoid;
3548 : : bool sct_tgisdeferred;
3549 : : } SetConstraintTriggerData;
3550 : :
3551 : : typedef struct SetConstraintTriggerData *SetConstraintTrigger;
3552 : :
3553 : : /*
3554 : : * SET CONSTRAINT intra-transaction status.
3555 : : *
3556 : : * We make this a single palloc'd object so it can be copied and freed easily.
3557 : : *
3558 : : * all_isset and all_isdeferred are used to keep track
3559 : : * of SET CONSTRAINTS ALL {DEFERRED, IMMEDIATE}.
3560 : : *
3561 : : * trigstates[] stores per-trigger tgisdeferred settings.
3562 : : */
3563 : : typedef struct SetConstraintStateData
3564 : : {
3565 : : bool all_isset;
3566 : : bool all_isdeferred;
3567 : : int numstates; /* number of trigstates[] entries in use */
3568 : : int numalloc; /* allocated size of trigstates[] */
3569 : : SetConstraintTriggerData trigstates[FLEXIBLE_ARRAY_MEMBER];
3570 : : } SetConstraintStateData;
3571 : :
3572 : : typedef SetConstraintStateData *SetConstraintState;
3573 : :
3574 : :
3575 : : /*
3576 : : * Per-trigger-event data
3577 : : *
3578 : : * The actual per-event data, AfterTriggerEventData, includes DONE/IN_PROGRESS
3579 : : * status bits, up to two tuple CTIDs, and optionally two OIDs of partitions.
3580 : : * Each event record also has an associated AfterTriggerSharedData that is
3581 : : * shared across all instances of similar events within a "chunk".
3582 : : *
3583 : : * For row-level triggers, we arrange not to waste storage on unneeded ctid
3584 : : * fields. Updates of regular tables use two; inserts and deletes of regular
3585 : : * tables use one; foreign tables always use zero and save the tuple(s) to a
3586 : : * tuplestore. AFTER_TRIGGER_FDW_FETCH directs AfterTriggerExecute() to
3587 : : * retrieve a fresh tuple or pair of tuples from that tuplestore, while
3588 : : * AFTER_TRIGGER_FDW_REUSE directs it to use the most-recently-retrieved
3589 : : * tuple(s). This permits storing tuples once regardless of the number of
3590 : : * row-level triggers on a foreign table.
3591 : : *
3592 : : * When updates on partitioned tables cause rows to move between partitions,
3593 : : * the OIDs of both partitions are stored too, so that the tuples can be
3594 : : * fetched; such entries are marked AFTER_TRIGGER_CP_UPDATE (for "cross-
3595 : : * partition update").
3596 : : *
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.
3603 : : *
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.
3607 : : *
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.
3616 : : */
3617 : : typedef uint32 TriggerFlags;
3618 : :
3619 : : #define AFTER_TRIGGER_OFFSET 0x07FFFFFF /* must be low-order bits */
3620 : : #define AFTER_TRIGGER_DONE 0x80000000
3621 : : #define AFTER_TRIGGER_IN_PROGRESS 0x40000000
3622 : : /* bits describing the size and tuple sources of this event */
3623 : : #define AFTER_TRIGGER_FDW_REUSE 0x00000000
3624 : : #define AFTER_TRIGGER_FDW_FETCH 0x20000000
3625 : : #define AFTER_TRIGGER_1CTID 0x10000000
3626 : : #define AFTER_TRIGGER_2CTID 0x30000000
3627 : : #define AFTER_TRIGGER_CP_UPDATE 0x08000000
3628 : : #define AFTER_TRIGGER_TUP_BITS 0x38000000
3629 : : typedef struct AfterTriggerSharedData *AfterTriggerShared;
3630 : :
3631 : : typedef struct AfterTriggerSharedData
3632 : : {
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 : : Bitmapset *ats_modifiedcols; /* modified columns */
3639 : : } AfterTriggerSharedData;
3640 : :
3641 : : typedef struct AfterTriggerEventData *AfterTriggerEvent;
3642 : :
3643 : : typedef struct AfterTriggerEventData
3644 : : {
3645 : : TriggerFlags ate_flags; /* status bits and offset to shared data */
3646 : : ItemPointerData ate_ctid1; /* inserted, deleted, or old updated tuple */
3647 : : ItemPointerData ate_ctid2; /* new updated tuple */
3648 : :
3649 : : /*
3650 : : * During a cross-partition update of a partitioned table, we also store
3651 : : * the OIDs of source and destination partitions that are needed to fetch
3652 : : * the old (ctid1) and the new tuple (ctid2) from, respectively.
3653 : : */
3654 : : Oid ate_src_part;
3655 : : Oid ate_dst_part;
3656 : : } AfterTriggerEventData;
3657 : :
3658 : : /* AfterTriggerEventData, minus ate_src_part, ate_dst_part */
3659 : : typedef struct AfterTriggerEventDataNoOids
3660 : : {
3661 : : TriggerFlags ate_flags;
3662 : : ItemPointerData ate_ctid1;
3663 : : ItemPointerData ate_ctid2;
3664 : : } AfterTriggerEventDataNoOids;
3665 : :
3666 : : /* AfterTriggerEventData, minus ate_*_part and ate_ctid2 */
3667 : : typedef struct AfterTriggerEventDataOneCtid
3668 : : {
3669 : : TriggerFlags ate_flags; /* status bits and offset to shared data */
3670 : : ItemPointerData ate_ctid1; /* inserted, deleted, or old updated tuple */
3671 : : } AfterTriggerEventDataOneCtid;
3672 : :
3673 : : /* AfterTriggerEventData, minus ate_*_part, ate_ctid1 and ate_ctid2 */
3674 : : typedef struct AfterTriggerEventDataZeroCtids
3675 : : {
3676 : : TriggerFlags ate_flags; /* status bits and offset to shared data */
3677 : : } AfterTriggerEventDataZeroCtids;
3678 : :
3679 : : #define SizeofTriggerEvent(evt) \
3680 : : (((evt)->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_CP_UPDATE ? \
3681 : : sizeof(AfterTriggerEventData) : \
3682 : : (((evt)->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_2CTID ? \
3683 : : sizeof(AfterTriggerEventDataNoOids) : \
3684 : : (((evt)->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_1CTID ? \
3685 : : sizeof(AfterTriggerEventDataOneCtid) : \
3686 : : sizeof(AfterTriggerEventDataZeroCtids))))
3687 : :
3688 : : #define GetTriggerSharedData(evt) \
3689 : : ((AfterTriggerShared) ((char *) (evt) + ((evt)->ate_flags & AFTER_TRIGGER_OFFSET)))
3690 : :
3691 : : /*
3692 : : * To avoid palloc overhead, we keep trigger events in arrays in successively-
3693 : : * larger chunks (a slightly more sophisticated version of an expansible
3694 : : * array). The space between CHUNK_DATA_START and freeptr is occupied by
3695 : : * AfterTriggerEventData records; the space between endfree and endptr is
3696 : : * occupied by AfterTriggerSharedData records.
3697 : : */
3698 : : typedef struct AfterTriggerEventChunk
3699 : : {
3700 : : struct AfterTriggerEventChunk *next; /* list link */
3701 : : char *freeptr; /* start of free space in chunk */
3702 : : char *endfree; /* end of free space in chunk */
3703 : : char *endptr; /* end of chunk */
3704 : : /* event data follows here */
3705 : : } AfterTriggerEventChunk;
3706 : :
3707 : : #define CHUNK_DATA_START(cptr) ((char *) (cptr) + MAXALIGN(sizeof(AfterTriggerEventChunk)))
3708 : :
3709 : : /* A list of events */
3710 : : typedef struct AfterTriggerEventList
3711 : : {
3712 : : AfterTriggerEventChunk *head;
3713 : : AfterTriggerEventChunk *tail;
3714 : : char *tailfree; /* freeptr of tail chunk */
3715 : : } AfterTriggerEventList;
3716 : :
3717 : : /* Macros to help in iterating over a list of events */
3718 : : #define for_each_chunk(cptr, evtlist) \
3719 : : for (cptr = (evtlist).head; cptr != NULL; cptr = cptr->next)
3720 : : #define for_each_event(eptr, cptr) \
3721 : : for (eptr = (AfterTriggerEvent) CHUNK_DATA_START(cptr); \
3722 : : (char *) eptr < (cptr)->freeptr; \
3723 : : eptr = (AfterTriggerEvent) (((char *) eptr) + SizeofTriggerEvent(eptr)))
3724 : : /* Use this if no special per-chunk processing is needed */
3725 : : #define for_each_event_chunk(eptr, cptr, evtlist) \
3726 : : for_each_chunk(cptr, evtlist) for_each_event(eptr, cptr)
3727 : :
3728 : : /* Macros for iterating from a start point that might not be list start */
3729 : : #define for_each_chunk_from(cptr) \
3730 : : for (; cptr != NULL; cptr = cptr->next)
3731 : : #define for_each_event_from(eptr, cptr) \
3732 : : for (; \
3733 : : (char *) eptr < (cptr)->freeptr; \
3734 : : eptr = (AfterTriggerEvent) (((char *) eptr) + SizeofTriggerEvent(eptr)))
3735 : :
3736 : :
3737 : : /*
3738 : : * All per-transaction data for the AFTER TRIGGERS module.
3739 : : *
3740 : : * AfterTriggersData has the following fields:
3741 : : *
3742 : : * firing_counter is incremented for each call of afterTriggerInvokeEvents.
3743 : : * We mark firable events with the current firing cycle's ID so that we can
3744 : : * tell which ones to work on. This ensures sane behavior if a trigger
3745 : : * function chooses to do SET CONSTRAINTS: the inner SET CONSTRAINTS will
3746 : : * only fire those events that weren't already scheduled for firing.
3747 : : *
3748 : : * state keeps track of the transaction-local effects of SET CONSTRAINTS.
3749 : : * This is saved and restored across failed subtransactions.
3750 : : *
3751 : : * events is the current list of deferred events. This is global across
3752 : : * all subtransactions of the current transaction. In a subtransaction
3753 : : * abort, we know that the events added by the subtransaction are at the
3754 : : * end of the list, so it is relatively easy to discard them. The event
3755 : : * list chunks themselves are stored in event_cxt.
3756 : : *
3757 : : * query_depth is the current depth of nested AfterTriggerBeginQuery calls
3758 : : * (-1 when the stack is empty).
3759 : : *
3760 : : * query_stack[query_depth] is the per-query-level data, including these fields:
3761 : : *
3762 : : * events is a list of AFTER trigger events queued by the current query.
3763 : : * None of these are valid until the matching AfterTriggerEndQuery call
3764 : : * occurs. At that point we fire immediate-mode triggers, and append any
3765 : : * deferred events to the main events list.
3766 : : *
3767 : : * fdw_tuplestore is a tuplestore containing the foreign-table tuples
3768 : : * needed by events queued by the current query. (Note: we use just one
3769 : : * tuplestore even though more than one foreign table might be involved.
3770 : : * This is okay because tuplestores don't really care what's in the tuples
3771 : : * they store; but it's possible that someday it'd break.)
3772 : : *
3773 : : * tables is a List of AfterTriggersTableData structs for target tables
3774 : : * of the current query (see below).
3775 : : *
3776 : : * maxquerydepth is just the allocated length of query_stack.
3777 : : *
3778 : : * trans_stack holds per-subtransaction data, including these fields:
3779 : : *
3780 : : * state is NULL or a pointer to a saved copy of the SET CONSTRAINTS
3781 : : * state data. Each subtransaction level that modifies that state first
3782 : : * saves a copy, which we use to restore the state if we abort.
3783 : : *
3784 : : * events is a copy of the events head/tail pointers,
3785 : : * which we use to restore those values during subtransaction abort.
3786 : : *
3787 : : * query_depth is the subtransaction-start-time value of query_depth,
3788 : : * which we similarly use to clean up at subtransaction abort.
3789 : : *
3790 : : * firing_counter is the subtransaction-start-time value of firing_counter.
3791 : : * We use this to recognize which deferred triggers were fired (or marked
3792 : : * for firing) within an aborted subtransaction.
3793 : : *
3794 : : * We use GetCurrentTransactionNestLevel() to determine the correct array
3795 : : * index in trans_stack. maxtransdepth is the number of allocated entries in
3796 : : * trans_stack. (By not keeping our own stack pointer, we can avoid trouble
3797 : : * in cases where errors during subxact abort cause multiple invocations
3798 : : * of AfterTriggerEndSubXact() at the same nesting depth.)
3799 : : *
3800 : : * We create an AfterTriggersTableData struct for each target table of the
3801 : : * current query, and each operation mode (INSERT/UPDATE/DELETE), that has
3802 : : * either transition tables or statement-level triggers. This is used to
3803 : : * hold the relevant transition tables, as well as info tracking whether
3804 : : * we already queued the statement triggers. (We use that info to prevent
3805 : : * firing the same statement triggers more than once per statement, or really
3806 : : * once per transition table set.) These structs, along with the transition
3807 : : * table tuplestores, live in the (sub)transaction's CurTransactionContext.
3808 : : * That's sufficient lifespan because we don't allow transition tables to be
3809 : : * used by deferrable triggers, so they only need to survive until
3810 : : * AfterTriggerEndQuery.
3811 : : */
3812 : : typedef struct AfterTriggersQueryData AfterTriggersQueryData;
3813 : : typedef struct AfterTriggersTransData AfterTriggersTransData;
3814 : : typedef struct AfterTriggersTableData AfterTriggersTableData;
3815 : :
3816 : : typedef struct AfterTriggersData
3817 : : {
3818 : : CommandId firing_counter; /* next firing ID to assign */
3819 : : SetConstraintState state; /* the active S C state */
3820 : : AfterTriggerEventList events; /* deferred-event list */
3821 : : MemoryContext event_cxt; /* memory context for events, if any */
3822 : :
3823 : : /* per-query-level data: */
3824 : : AfterTriggersQueryData *query_stack; /* array of structs shown below */
3825 : : int query_depth; /* current index in above array */
3826 : : int maxquerydepth; /* allocated len of above array */
3827 : :
3828 : : /* per-subtransaction-level data: */
3829 : : AfterTriggersTransData *trans_stack; /* array of structs shown below */
3830 : : int maxtransdepth; /* allocated len of above array */
3831 : : } AfterTriggersData;
3832 : :
3833 : : struct AfterTriggersQueryData
3834 : : {
3835 : : AfterTriggerEventList events; /* events pending from this query */
3836 : : Tuplestorestate *fdw_tuplestore; /* foreign tuples for said events */
3837 : : List *tables; /* list of AfterTriggersTableData, see below */
3838 : : };
3839 : :
3840 : : struct AfterTriggersTransData
3841 : : {
3842 : : /* these fields are just for resetting at subtrans abort: */
3843 : : SetConstraintState state; /* saved S C state, or NULL if not yet saved */
3844 : : AfterTriggerEventList events; /* saved list pointer */
3845 : : int query_depth; /* saved query_depth */
3846 : : CommandId firing_counter; /* saved firing_counter */
3847 : : };
3848 : :
3849 : : struct AfterTriggersTableData
3850 : : {
3851 : : /* relid + cmdType form the lookup key for these structs: */
3852 : : Oid relid; /* target table's OID */
3853 : : CmdType cmdType; /* event type, CMD_INSERT/UPDATE/DELETE */
3854 : : bool closed; /* true when no longer OK to add tuples */
3855 : : bool before_trig_done; /* did we already queue BS triggers? */
3856 : : bool after_trig_done; /* did we already queue AS triggers? */
3857 : : AfterTriggerEventList after_trig_events; /* if so, saved list pointer */
3858 : :
3859 : : /*
3860 : : * We maintain separate transition tables for UPDATE/INSERT/DELETE since
3861 : : * MERGE can run all three actions in a single statement. Note that UPDATE
3862 : : * needs both old and new transition tables whereas INSERT needs only new,
3863 : : * and DELETE needs only old.
3864 : : */
3865 : :
3866 : : /* "old" transition table for UPDATE, if any */
3867 : : Tuplestorestate *old_upd_tuplestore;
3868 : : /* "new" transition table for UPDATE, if any */
3869 : : Tuplestorestate *new_upd_tuplestore;
3870 : : /* "old" transition table for DELETE, if any */
3871 : : Tuplestorestate *old_del_tuplestore;
3872 : : /* "new" transition table for INSERT, if any */
3873 : : Tuplestorestate *new_ins_tuplestore;
3874 : :
3875 : : TupleTableSlot *storeslot; /* for converting to tuplestore's format */
3876 : : };
3877 : :
3878 : : static AfterTriggersData afterTriggers;
3879 : :
3880 : : static void AfterTriggerExecute(EState *estate,
3881 : : AfterTriggerEvent event,
3882 : : ResultRelInfo *relInfo,
3883 : : ResultRelInfo *src_relInfo,
3884 : : ResultRelInfo *dst_relInfo,
3885 : : TriggerDesc *trigdesc,
3886 : : FmgrInfo *finfo,
3887 : : Instrumentation *instr,
3888 : : MemoryContext per_tuple_context,
3889 : : TupleTableSlot *trig_tuple_slot1,
3890 : : TupleTableSlot *trig_tuple_slot2);
3891 : : static AfterTriggersTableData *GetAfterTriggersTableData(Oid relid,
3892 : : CmdType cmdType);
3893 : : static TupleTableSlot *GetAfterTriggersStoreSlot(AfterTriggersTableData *table,
3894 : : TupleDesc tupdesc);
3895 : : static Tuplestorestate *GetAfterTriggersTransitionTable(int event,
3896 : : TupleTableSlot *oldslot,
3897 : : TupleTableSlot *newslot,
3898 : : TransitionCaptureState *transition_capture);
3899 : : static void TransitionTableAddTuple(EState *estate,
3900 : : TransitionCaptureState *transition_capture,
3901 : : ResultRelInfo *relinfo,
3902 : : TupleTableSlot *slot,
3903 : : TupleTableSlot *original_insert_tuple,
3904 : : Tuplestorestate *tuplestore);
3905 : : static void AfterTriggerFreeQuery(AfterTriggersQueryData *qs);
3906 : : static SetConstraintState SetConstraintStateCreate(int numalloc);
3907 : : static SetConstraintState SetConstraintStateCopy(SetConstraintState origstate);
3908 : : static SetConstraintState SetConstraintStateAddItem(SetConstraintState state,
3909 : : Oid tgoid, bool tgisdeferred);
3910 : : static void cancel_prior_stmt_triggers(Oid relid, CmdType cmdType, int tgevent);
3911 : :
3912 : :
3913 : : /*
3914 : : * Get the FDW tuplestore for the current trigger query level, creating it
3915 : : * if necessary.
3916 : : */
3917 : : static Tuplestorestate *
2402 3918 : 50 : GetCurrentFDWTuplestore(void)
3919 : : {
3920 : : Tuplestorestate *ret;
3921 : :
3922 : 50 : ret = afterTriggers.query_stack[afterTriggers.query_depth].fdw_tuplestore;
3675 noah@leadboat.com 3923 [ + + ]: 50 : if (ret == NULL)
3924 : : {
3925 : : MemoryContext oldcxt;
3926 : : ResourceOwner saveResourceOwner;
3927 : :
3928 : : /*
3929 : : * Make the tuplestore valid until end of subtransaction. We really
3930 : : * only need it until AfterTriggerEndQuery().
3931 : : */
2402 tgl@sss.pgh.pa.us 3932 : 18 : oldcxt = MemoryContextSwitchTo(CurTransactionContext);
3675 noah@leadboat.com 3933 : 18 : saveResourceOwner = CurrentResourceOwner;
2377 tgl@sss.pgh.pa.us 3934 : 18 : CurrentResourceOwner = CurTransactionResourceOwner;
3935 : :
3936 : 18 : ret = tuplestore_begin_heap(false, false, work_mem);
3937 : :
3675 noah@leadboat.com 3938 : 18 : CurrentResourceOwner = saveResourceOwner;
3939 : 18 : MemoryContextSwitchTo(oldcxt);
3940 : :
2402 tgl@sss.pgh.pa.us 3941 : 18 : afterTriggers.query_stack[afterTriggers.query_depth].fdw_tuplestore = ret;
3942 : : }
3943 : :
3675 noah@leadboat.com 3944 : 50 : return ret;
3945 : : }
3946 : :
3947 : : /* ----------
3948 : : * afterTriggerCheckState()
3949 : : *
3950 : : * Returns true if the trigger event is actually in state DEFERRED.
3951 : : * ----------
3952 : : */
3953 : : static bool
5651 tgl@sss.pgh.pa.us 3954 : 5846 : afterTriggerCheckState(AfterTriggerShared evtshared)
3955 : : {
3956 : 5846 : Oid tgoid = evtshared->ats_tgoid;
3461 rhaas@postgresql.org 3957 : 5846 : SetConstraintState state = afterTriggers.state;
3958 : : int i;
3959 : :
3960 : : /*
3961 : : * For not-deferrable triggers (i.e. normal AFTER ROW triggers and
3962 : : * constraints declared NOT DEFERRABLE), the state is always false.
3963 : : */
5651 tgl@sss.pgh.pa.us 3964 [ + + ]: 5846 : if ((evtshared->ats_event & AFTER_TRIGGER_DEFERRABLE) == 0)
8964 JanWieck@Yahoo.com 3965 : 5497 : return false;
3966 : :
3967 : : /*
3968 : : * If constraint state exists, SET CONSTRAINTS might have been executed
3969 : : * either for this trigger or for all triggers.
3970 : : */
3461 rhaas@postgresql.org 3971 [ + + ]: 349 : if (state != NULL)
3972 : : {
3973 : : /* Check for SET CONSTRAINTS for this specific trigger. */
3974 [ + + ]: 156 : for (i = 0; i < state->numstates; i++)
3975 : : {
3976 [ + + ]: 123 : if (state->trigstates[i].sct_tgoid == tgoid)
3977 : 30 : return state->trigstates[i].sct_tgisdeferred;
3978 : : }
3979 : :
3980 : : /* Check for SET CONSTRAINTS ALL. */
3981 [ + + ]: 33 : if (state->all_isset)
3982 : 27 : return state->all_isdeferred;
3983 : : }
3984 : :
3985 : : /*
3986 : : * Otherwise return the default state for the trigger.
3987 : : */
5651 tgl@sss.pgh.pa.us 3988 : 292 : return ((evtshared->ats_event & AFTER_TRIGGER_INITDEFERRED) != 0);
3989 : : }
3990 : :
3991 : : /* ----------
3992 : : * afterTriggerCopyBitmap()
3993 : : *
3994 : : * Copy bitmap into AfterTriggerEvents memory context, which is where the after
3995 : : * trigger events are kept.
3996 : : * ----------
3997 : : */
3998 : : static Bitmapset *
287 tomas.vondra@postgre 3999 : 5871 : afterTriggerCopyBitmap(Bitmapset *src)
4000 : : {
4001 : : Bitmapset *dst;
4002 : : MemoryContext oldcxt;
4003 : :
4004 [ + + ]: 5871 : if (src == NULL)
4005 : 4060 : return NULL;
4006 : :
4007 : : /* Create event context if we didn't already */
4008 [ + + ]: 1811 : if (afterTriggers.event_cxt == NULL)
4009 : 642 : afterTriggers.event_cxt =
4010 : 642 : AllocSetContextCreate(TopTransactionContext,
4011 : : "AfterTriggerEvents",
4012 : : ALLOCSET_DEFAULT_SIZES);
4013 : :
4014 : 1811 : oldcxt = MemoryContextSwitchTo(afterTriggers.event_cxt);
4015 : :
4016 : 1811 : dst = bms_copy(src);
4017 : :
4018 : 1811 : MemoryContextSwitchTo(oldcxt);
4019 : :
4020 : 1811 : return dst;
4021 : : }
4022 : :
4023 : : /* ----------
4024 : : * afterTriggerAddEvent()
4025 : : *
4026 : : * Add a new trigger event to the specified queue.
4027 : : * The passed-in event data is copied.
4028 : : * ----------
4029 : : */
4030 : : static void
5651 tgl@sss.pgh.pa.us 4031 : 6160 : afterTriggerAddEvent(AfterTriggerEventList *events,
4032 : : AfterTriggerEvent event, AfterTriggerShared evtshared)
4033 : : {
4034 [ + + + + : 6160 : Size eventsize = SizeofTriggerEvent(event);
+ + ]
4035 : 6160 : Size needed = eventsize + sizeof(AfterTriggerSharedData);
4036 : : AfterTriggerEventChunk *chunk;
4037 : : AfterTriggerShared newshared;
4038 : : AfterTriggerEvent newevent;
4039 : :
4040 : : /*
4041 : : * If empty list or not enough room in the tail chunk, make a new chunk.
4042 : : * We assume here that a new shared record will always be needed.
4043 : : */
4044 : 6160 : chunk = events->tail;
4045 [ + + ]: 6160 : if (chunk == NULL ||
4046 [ - + ]: 2303 : chunk->endfree - chunk->freeptr < needed)
4047 : : {
4048 : : Size chunksize;
4049 : :
4050 : : /* Create event context if we didn't already */
3461 rhaas@postgresql.org 4051 [ + + ]: 3857 : if (afterTriggers.event_cxt == NULL)
4052 : 2623 : afterTriggers.event_cxt =
5651 tgl@sss.pgh.pa.us 4053 : 2623 : AllocSetContextCreate(TopTransactionContext,
4054 : : "AfterTriggerEvents",
4055 : : ALLOCSET_DEFAULT_SIZES);
4056 : :
4057 : : /*
4058 : : * Chunk size starts at 1KB and is allowed to increase up to 1MB.
4059 : : * These numbers are fairly arbitrary, though there is a hard limit at
4060 : : * AFTER_TRIGGER_OFFSET; else we couldn't link event records to their
4061 : : * shared records using the available space in ate_flags. Another
4062 : : * constraint is that if the chunk size gets too huge, the search loop
4063 : : * below would get slow given a (not too common) usage pattern with
4064 : : * many distinct event types in a chunk. Therefore, we double the
4065 : : * preceding chunk size only if there weren't too many shared records
4066 : : * in the preceding chunk; otherwise we halve it. This gives us some
4067 : : * ability to adapt to the actual usage pattern of the current query
4068 : : * while still having large chunk sizes in typical usage. All chunk
4069 : : * sizes used should be MAXALIGN multiples, to ensure that the shared
4070 : : * records will be aligned safely.
4071 : : */
4072 : : #define MIN_CHUNK_SIZE 1024
4073 : : #define MAX_CHUNK_SIZE (1024*1024)
4074 : :
4075 : : #if MAX_CHUNK_SIZE > (AFTER_TRIGGER_OFFSET+1)
4076 : : #error MAX_CHUNK_SIZE must not exceed AFTER_TRIGGER_OFFSET
4077 : : #endif
4078 : :
4079 [ + - ]: 3857 : if (chunk == NULL)
4080 : 3857 : chunksize = MIN_CHUNK_SIZE;
4081 : : else
4082 : : {
4083 : : /* preceding chunk size... */
5651 tgl@sss.pgh.pa.us 4084 :UBC 0 : chunksize = chunk->endptr - (char *) chunk;
4085 : : /* check number of shared records in preceding chunk */
4086 [ # # ]: 0 : if ((chunk->endptr - chunk->endfree) <=
4087 : : (100 * sizeof(AfterTriggerSharedData)))
5421 bruce@momjian.us 4088 : 0 : chunksize *= 2; /* okay, double it */
4089 : : else
4090 : 0 : chunksize /= 2; /* too many shared records */
5651 tgl@sss.pgh.pa.us 4091 : 0 : chunksize = Min(chunksize, MAX_CHUNK_SIZE);
4092 : : }
3461 rhaas@postgresql.org 4093 :CBC 3857 : chunk = MemoryContextAlloc(afterTriggers.event_cxt, chunksize);
5651 tgl@sss.pgh.pa.us 4094 : 3857 : chunk->next = NULL;
4095 : 3857 : chunk->freeptr = CHUNK_DATA_START(chunk);
4096 : 3857 : chunk->endptr = chunk->endfree = (char *) chunk + chunksize;
4097 [ - + ]: 3857 : Assert(chunk->endfree - chunk->freeptr >= needed);
4098 : :
4099 [ + - ]: 3857 : if (events->head == NULL)
4100 : 3857 : events->head = chunk;
4101 : : else
5651 tgl@sss.pgh.pa.us 4102 :UBC 0 : events->tail->next = chunk;
5651 tgl@sss.pgh.pa.us 4103 :CBC 3857 : events->tail = chunk;
4104 : : /* events->tailfree is now out of sync, but we'll fix it below */
4105 : : }
4106 : :
4107 : : /*
4108 : : * Try to locate a matching shared-data record already in the chunk. If
4109 : : * none, make a new one.
4110 : : */
4111 : 6160 : for (newshared = ((AfterTriggerShared) chunk->endptr) - 1;
4112 [ + + ]: 8772 : (char *) newshared >= chunk->endfree;
4113 : 2612 : newshared--)
4114 : : {
4115 [ + + ]: 3350 : if (newshared->ats_tgoid == evtshared->ats_tgoid &&
4116 [ + - ]: 828 : newshared->ats_relid == evtshared->ats_relid &&
4117 [ + + ]: 828 : newshared->ats_event == evtshared->ats_event &&
2402 4118 [ + + ]: 825 : newshared->ats_table == evtshared->ats_table &&
5651 4119 [ + + ]: 807 : newshared->ats_firing_id == 0)
4120 : 738 : break;
4121 : : }
4122 [ + + ]: 6160 : if ((char *) newshared < chunk->endfree)
4123 : : {
4124 : 5422 : *newshared = *evtshared;
5421 bruce@momjian.us 4125 : 5422 : newshared->ats_firing_id = 0; /* just to be sure */
5651 tgl@sss.pgh.pa.us 4126 : 5422 : chunk->endfree = (char *) newshared;
4127 : : }
4128 : :
4129 : : /* Insert the data */
4130 : 6160 : newevent = (AfterTriggerEvent) chunk->freeptr;
4131 : 6160 : memcpy(newevent, event, eventsize);
4132 : : /* ... and link the new event to its shared record */
4133 : 6160 : newevent->ate_flags &= ~AFTER_TRIGGER_OFFSET;
4134 : 6160 : newevent->ate_flags |= (char *) newshared - (char *) newevent;
4135 : :
4136 : 6160 : chunk->freeptr += eventsize;
4137 : 6160 : events->tailfree = chunk->freeptr;
4138 : 6160 : }
4139 : :
4140 : : /* ----------
4141 : : * afterTriggerFreeEventList()
4142 : : *
4143 : : * Free all the event storage in the given list.
4144 : : * ----------
4145 : : */
4146 : : static void
4147 : 8404 : afterTriggerFreeEventList(AfterTriggerEventList *events)
4148 : : {
4149 : : AfterTriggerEventChunk *chunk;
4150 : :
2401 4151 [ + + ]: 11553 : while ((chunk = events->head) != NULL)
4152 : : {
4153 : 3149 : events->head = chunk->next;
5651 4154 : 3149 : pfree(chunk);
4155 : : }
4156 : 8404 : events->tail = NULL;
4157 : 8404 : events->tailfree = NULL;
4158 : 8404 : }
4159 : :
4160 : : /* ----------
4161 : : * afterTriggerRestoreEventList()
4162 : : *
4163 : : * Restore an event list to its prior length, removing all the events
4164 : : * added since it had the value old_events.
4165 : : * ----------
4166 : : */
4167 : : static void
4168 : 4566 : afterTriggerRestoreEventList(AfterTriggerEventList *events,
4169 : : const AfterTriggerEventList *old_events)
4170 : : {
4171 : : AfterTriggerEventChunk *chunk;
4172 : : AfterTriggerEventChunk *next_chunk;
4173 : :
4174 [ + + ]: 4566 : if (old_events->tail == NULL)
4175 : : {
4176 : : /* restoring to a completely empty state, so free everything */
4177 : 4555 : afterTriggerFreeEventList(events);
4178 : : }
4179 : : else
4180 : : {
4181 : 11 : *events = *old_events;
4182 : : /* free any chunks after the last one we want to keep */
4183 [ - + ]: 11 : for (chunk = events->tail->next; chunk != NULL; chunk = next_chunk)
4184 : : {
5651 tgl@sss.pgh.pa.us 4185 :UBC 0 : next_chunk = chunk->next;
4186 : 0 : pfree(chunk);
4187 : : }
4188 : : /* and clean up the tail chunk to be the right length */
5651 tgl@sss.pgh.pa.us 4189 :CBC 11 : events->tail->next = NULL;
4190 : 11 : events->tail->freeptr = events->tailfree;
4191 : :
4192 : : /*
4193 : : * We don't make any effort to remove now-unused shared data records.
4194 : : * They might still be useful, anyway.
4195 : : */
4196 : : }
8964 JanWieck@Yahoo.com 4197 : 4566 : }
4198 : :
4199 : : /* ----------
4200 : : * afterTriggerDeleteHeadEventChunk()
4201 : : *
4202 : : * Remove the first chunk of events from the query level's event list.
4203 : : * Keep any event list pointers elsewhere in the query level's data
4204 : : * structures in sync.
4205 : : * ----------
4206 : : */
4207 : : static void
2401 tgl@sss.pgh.pa.us 4208 :UBC 0 : afterTriggerDeleteHeadEventChunk(AfterTriggersQueryData *qs)
4209 : : {
4210 : 0 : AfterTriggerEventChunk *target = qs->events.head;
4211 : : ListCell *lc;
4212 : :
4213 [ # # # # ]: 0 : Assert(target && target->next);
4214 : :
4215 : : /*
4216 : : * First, update any pointers in the per-table data, so that they won't be
4217 : : * dangling. Resetting obsoleted pointers to NULL will make
4218 : : * cancel_prior_stmt_triggers start from the list head, which is fine.
4219 : : */
4220 [ # # # # : 0 : foreach(lc, qs->tables)
# # ]
4221 : : {
4222 : 0 : AfterTriggersTableData *table = (AfterTriggersTableData *) lfirst(lc);
4223 : :
4224 [ # # ]: 0 : if (table->after_trig_done &&
4225 [ # # ]: 0 : table->after_trig_events.tail == target)
4226 : : {
4227 : 0 : table->after_trig_events.head = NULL;
4228 : 0 : table->after_trig_events.tail = NULL;
4229 : 0 : table->after_trig_events.tailfree = NULL;
4230 : : }
4231 : : }
4232 : :
4233 : : /* Now we can flush the head chunk */
4234 : 0 : qs->events.head = target->next;
4235 : 0 : pfree(target);
4236 : 0 : }
4237 : :
4238 : :
4239 : : /* ----------
4240 : : * AfterTriggerExecute()
4241 : : *
4242 : : * Fetch the required tuples back from the heap and fire one
4243 : : * single trigger function.
4244 : : *
4245 : : * Frequently, this will be fired many times in a row for triggers of
4246 : : * a single relation. Therefore, we cache the open relation and provide
4247 : : * fmgr lookup cache space at the caller level. (For triggers fired at
4248 : : * the end of a query, we can even piggyback on the executor's state.)
4249 : : *
4250 : : * When fired for a cross-partition update of a partitioned table, the old
4251 : : * tuple is fetched using 'src_relInfo' (the source leaf partition) and
4252 : : * the new tuple using 'dst_relInfo' (the destination leaf partition), though
4253 : : * both are converted into the root partitioned table's format before passing
4254 : : * to the trigger function.
4255 : : *
4256 : : * event: event currently being fired.
4257 : : * relInfo: result relation for event.
4258 : : * src_relInfo: source partition of a cross-partition update
4259 : : * dst_relInfo: its destination partition
4260 : : * trigdesc: working copy of rel's trigger info.
4261 : : * finfo: array of fmgr lookup cache entries (one per trigger in trigdesc).
4262 : : * instr: array of EXPLAIN ANALYZE instrumentation nodes (one per trigger),
4263 : : * or NULL if no instrumentation is wanted.
4264 : : * per_tuple_context: memory context to call trigger function in.
4265 : : * trig_tuple_slot1: scratch slot for tg_trigtuple (foreign tables only)
4266 : : * trig_tuple_slot2: scratch slot for tg_newtuple (foreign tables only)
4267 : : * ----------
4268 : : */
4269 : : static void
1874 andres@anarazel.de 4270 :CBC 5707 : AfterTriggerExecute(EState *estate,
4271 : : AfterTriggerEvent event,
4272 : : ResultRelInfo *relInfo,
4273 : : ResultRelInfo *src_relInfo,
4274 : : ResultRelInfo *dst_relInfo,
4275 : : TriggerDesc *trigdesc,
4276 : : FmgrInfo *finfo, Instrumentation *instr,
4277 : : MemoryContext per_tuple_context,
4278 : : TupleTableSlot *trig_tuple_slot1,
4279 : : TupleTableSlot *trig_tuple_slot2)
4280 : : {
4281 : 5707 : Relation rel = relInfo->ri_RelationDesc;
756 alvherre@alvh.no-ip. 4282 : 5707 : Relation src_rel = src_relInfo->ri_RelationDesc;
4283 : 5707 : Relation dst_rel = dst_relInfo->ri_RelationDesc;
5651 tgl@sss.pgh.pa.us 4284 : 5707 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
4285 : 5707 : Oid tgoid = evtshared->ats_tgoid;
1511 peter@eisentraut.org 4286 : 5707 : TriggerData LocTriggerData = {0};
4287 : : HeapTuple rettuple;
4288 : : int tgindx;
1874 andres@anarazel.de 4289 : 5707 : bool should_free_trig = false;
4290 : 5707 : bool should_free_new = false;
4291 : :
4292 : : /*
4293 : : * Locate trigger in trigdesc.
4294 : : */
6960 tgl@sss.pgh.pa.us 4295 [ + - ]: 12751 : for (tgindx = 0; tgindx < trigdesc->numtriggers; tgindx++)
4296 : : {
4297 [ + + ]: 12751 : if (trigdesc->triggers[tgindx].tgoid == tgoid)
4298 : : {
4299 : 5707 : LocTriggerData.tg_trigger = &(trigdesc->triggers[tgindx]);
4300 : 5707 : break;
4301 : : }
4302 : : }
4303 [ - + ]: 5707 : if (LocTriggerData.tg_trigger == NULL)
6960 tgl@sss.pgh.pa.us 4304 [ # # ]:UBC 0 : elog(ERROR, "could not find trigger %u", tgoid);
4305 : :
4306 : : /*
4307 : : * If doing EXPLAIN ANALYZE, start charging time to this trigger. We want
4308 : : * to include time spent re-fetching tuples in the trigger cost.
4309 : : */
6960 tgl@sss.pgh.pa.us 4310 [ - + ]:CBC 5707 : if (instr)
6960 tgl@sss.pgh.pa.us 4311 :UBC 0 : InstrStartNode(instr + tgindx);
4312 : :
4313 : : /*
4314 : : * Fetch the required tuple(s).
4315 : : */
3675 noah@leadboat.com 4316 [ + + + ]:CBC 5707 : switch (event->ate_flags & AFTER_TRIGGER_TUP_BITS)
4317 : : {
4318 : 25 : case AFTER_TRIGGER_FDW_FETCH:
4319 : : {
2402 tgl@sss.pgh.pa.us 4320 : 25 : Tuplestorestate *fdw_tuplestore = GetCurrentFDWTuplestore();
4321 : :
3675 noah@leadboat.com 4322 [ - + ]: 25 : if (!tuplestore_gettupleslot(fdw_tuplestore, true, false,
4323 : : trig_tuple_slot1))
3675 noah@leadboat.com 4324 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple1 for AFTER trigger");
4325 : :
3675 noah@leadboat.com 4326 [ + + ]:CBC 25 : if ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) ==
4327 : 9 : TRIGGER_EVENT_UPDATE &&
4328 [ - + ]: 9 : !tuplestore_gettupleslot(fdw_tuplestore, true, false,
4329 : : trig_tuple_slot2))
3675 noah@leadboat.com 4330 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple2 for AFTER trigger");
4331 : : }
4332 : : /* fall through */
4333 : : case AFTER_TRIGGER_FDW_REUSE:
4334 : :
4335 : : /*
4336 : : * Store tuple in the slot so that tg_trigtuple does not reference
4337 : : * tuplestore memory. (It is formally possible for the trigger
4338 : : * function to queue trigger events that add to the same
4339 : : * tuplestore, which can push other tuples out of memory.) The
4340 : : * distinction is academic, because we start with a minimal tuple
4341 : : * that is stored as a heap tuple, constructed in different memory
4342 : : * context, in the slot anyway.
4343 : : */
1874 andres@anarazel.de 4344 :CBC 29 : LocTriggerData.tg_trigslot = trig_tuple_slot1;
4345 : 29 : LocTriggerData.tg_trigtuple =
4346 : 29 : ExecFetchSlotHeapTuple(trig_tuple_slot1, true, &should_free_trig);
4347 : :
1587 efujita@postgresql.o 4348 [ + + ]: 29 : if ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) ==
4349 : : TRIGGER_EVENT_UPDATE)
4350 : : {
4351 : 11 : LocTriggerData.tg_newslot = trig_tuple_slot2;
4352 : 11 : LocTriggerData.tg_newtuple =
4353 : 11 : ExecFetchSlotHeapTuple(trig_tuple_slot2, true, &should_free_new);
4354 : : }
4355 : : else
4356 : : {
4357 : 18 : LocTriggerData.tg_newtuple = NULL;
4358 : : }
3675 noah@leadboat.com 4359 : 29 : break;
4360 : :
4361 : 5678 : default:
4362 [ + + ]: 5678 : if (ItemPointerIsValid(&(event->ate_ctid1)))
4363 : : {
756 alvherre@alvh.no-ip. 4364 : 5164 : TupleTableSlot *src_slot = ExecGetTriggerOldSlot(estate,
4365 : : src_relInfo);
4366 : :
4367 [ - + ]: 5164 : if (!table_tuple_fetch_row_version(src_rel,
4368 : : &(event->ate_ctid1),
4369 : : SnapshotAny,
4370 : : src_slot))
3675 noah@leadboat.com 4371 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple1 for AFTER trigger");
4372 : :
4373 : : /*
4374 : : * Store the tuple fetched from the source partition into the
4375 : : * target (root partitioned) table slot, converting if needed.
4376 : : */
756 alvherre@alvh.no-ip. 4377 [ + + ]:CBC 5164 : if (src_relInfo != relInfo)
4378 : : {
4379 : 72 : TupleConversionMap *map = ExecGetChildToRootMap(src_relInfo);
4380 : :
4381 : 72 : LocTriggerData.tg_trigslot = ExecGetTriggerOldSlot(estate, relInfo);
4382 [ + + ]: 72 : if (map)
4383 : : {
4384 : 18 : execute_attr_map_slot(map->attrMap,
4385 : : src_slot,
4386 : : LocTriggerData.tg_trigslot);
4387 : : }
4388 : : else
4389 : 54 : ExecCopySlot(LocTriggerData.tg_trigslot, src_slot);
4390 : : }
4391 : : else
4392 : 5092 : LocTriggerData.tg_trigslot = src_slot;
1874 andres@anarazel.de 4393 : 5164 : LocTriggerData.tg_trigtuple =
1847 4394 : 5164 : ExecFetchSlotHeapTuple(LocTriggerData.tg_trigslot, false, &should_free_trig);
4395 : : }
4396 : : else
4397 : : {
3675 noah@leadboat.com 4398 : 514 : LocTriggerData.tg_trigtuple = NULL;
4399 : : }
4400 : :
4401 : : /* don't touch ctid2 if not there */
756 alvherre@alvh.no-ip. 4402 [ + + ]: 5678 : if (((event->ate_flags & AFTER_TRIGGER_TUP_BITS) == AFTER_TRIGGER_2CTID ||
4403 [ + + + - ]: 5750 : (event->ate_flags & AFTER_TRIGGER_CP_UPDATE)) &&
3675 noah@leadboat.com 4404 : 1512 : ItemPointerIsValid(&(event->ate_ctid2)))
4405 : 1512 : {
756 alvherre@alvh.no-ip. 4406 : 1512 : TupleTableSlot *dst_slot = ExecGetTriggerNewSlot(estate,
4407 : : dst_relInfo);
4408 : :
4409 [ - + ]: 1512 : if (!table_tuple_fetch_row_version(dst_rel,
4410 : : &(event->ate_ctid2),
4411 : : SnapshotAny,
4412 : : dst_slot))
3675 noah@leadboat.com 4413 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple2 for AFTER trigger");
4414 : :
4415 : : /*
4416 : : * Store the tuple fetched from the destination partition into
4417 : : * the target (root partitioned) table slot, converting if
4418 : : * needed.
4419 : : */
756 alvherre@alvh.no-ip. 4420 [ + + ]:CBC 1512 : if (dst_relInfo != relInfo)
4421 : : {
4422 : 72 : TupleConversionMap *map = ExecGetChildToRootMap(dst_relInfo);
4423 : :
4424 : 72 : LocTriggerData.tg_newslot = ExecGetTriggerNewSlot(estate, relInfo);
4425 [ + + ]: 72 : if (map)
4426 : : {
4427 : 18 : execute_attr_map_slot(map->attrMap,
4428 : : dst_slot,
4429 : : LocTriggerData.tg_newslot);
4430 : : }
4431 : : else
4432 : 54 : ExecCopySlot(LocTriggerData.tg_newslot, dst_slot);
4433 : : }
4434 : : else
4435 : 1440 : LocTriggerData.tg_newslot = dst_slot;
1874 andres@anarazel.de 4436 : 1512 : LocTriggerData.tg_newtuple =
1847 4437 : 1512 : ExecFetchSlotHeapTuple(LocTriggerData.tg_newslot, false, &should_free_new);
4438 : : }
4439 : : else
4440 : : {
3675 noah@leadboat.com 4441 : 4166 : LocTriggerData.tg_newtuple = NULL;
4442 : : }
4443 : : }
4444 : :
4445 : : /*
4446 : : * Set up the tuplestore information to let the trigger have access to
4447 : : * transition tables. When we first make a transition table available to
4448 : : * a trigger, mark it "closed" so that it cannot change anymore. If any
4449 : : * additional events of the same type get queued in the current trigger
4450 : : * query level, they'll go into new transition tables.
4451 : : */
2482 rhodiumtoad@postgres 4452 : 5707 : LocTriggerData.tg_oldtable = LocTriggerData.tg_newtable = NULL;
2402 tgl@sss.pgh.pa.us 4453 [ + + ]: 5707 : if (evtshared->ats_table)
4454 : : {
2482 rhodiumtoad@postgres 4455 [ + + ]: 267 : if (LocTriggerData.tg_trigger->tgoldtable)
4456 : : {
748 alvherre@alvh.no-ip. 4457 [ + + ]: 150 : if (TRIGGER_FIRED_BY_UPDATE(evtshared->ats_event))
4458 : 78 : LocTriggerData.tg_oldtable = evtshared->ats_table->old_upd_tuplestore;
4459 : : else
4460 : 72 : LocTriggerData.tg_oldtable = evtshared->ats_table->old_del_tuplestore;
2402 tgl@sss.pgh.pa.us 4461 : 150 : evtshared->ats_table->closed = true;
4462 : : }
4463 : :
4464 [ + + ]: 267 : if (LocTriggerData.tg_trigger->tgnewtable)
4465 : : {
748 alvherre@alvh.no-ip. 4466 [ + + ]: 192 : if (TRIGGER_FIRED_BY_INSERT(evtshared->ats_event))
4467 : 105 : LocTriggerData.tg_newtable = evtshared->ats_table->new_ins_tuplestore;
4468 : : else
4469 : 87 : LocTriggerData.tg_newtable = evtshared->ats_table->new_upd_tuplestore;
2402 tgl@sss.pgh.pa.us 4470 : 192 : evtshared->ats_table->closed = true;
4471 : : }
4472 : : }
4473 : :
4474 : : /*
4475 : : * Setup the remaining trigger information
4476 : : */
8721 4477 : 5707 : LocTriggerData.type = T_TriggerData;
7156 4478 : 5707 : LocTriggerData.tg_event =
5651 4479 : 5707 : evtshared->ats_event & (TRIGGER_EVENT_OPMASK | TRIGGER_EVENT_ROW);
8721 4480 : 5707 : LocTriggerData.tg_relation = rel;
1497 peter@eisentraut.org 4481 [ + + ]: 5707 : if (TRIGGER_FOR_UPDATE(LocTriggerData.tg_trigger->tgtype))
4482 : 2651 : LocTriggerData.tg_updatedcols = evtshared->ats_modifiedcols;
4483 : :
7156 tgl@sss.pgh.pa.us 4484 : 5707 : MemoryContextReset(per_tuple_context);
4485 : :
4486 : : /*
4487 : : * Call the trigger and throw away any possibly returned updated tuple.
4488 : : * (Don't let ExecCallTriggerFunc measure EXPLAIN time.)
4489 : : */
8353 4490 : 5707 : rettuple = ExecCallTriggerFunc(&LocTriggerData,
4491 : : tgindx,
4492 : : finfo,
4493 : : NULL,
4494 : : per_tuple_context);
3675 noah@leadboat.com 4495 [ + + ]: 5107 : if (rettuple != NULL &&
4496 [ + + ]: 1667 : rettuple != LocTriggerData.tg_trigtuple &&
4497 [ - + ]: 704 : rettuple != LocTriggerData.tg_newtuple)
8886 JanWieck@Yahoo.com 4498 :UBC 0 : heap_freetuple(rettuple);
4499 : :
4500 : : /*
4501 : : * Release resources
4502 : : */
1874 andres@anarazel.de 4503 [ + + ]:CBC 5107 : if (should_free_trig)
4504 : 86 : heap_freetuple(LocTriggerData.tg_trigtuple);
4505 [ + + ]: 5107 : if (should_free_new)
4506 : 68 : heap_freetuple(LocTriggerData.tg_newtuple);
4507 : :
4508 : : /* don't clear slots' contents if foreign table */
1587 efujita@postgresql.o 4509 [ + + ]: 5107 : if (trig_tuple_slot1 == NULL)
4510 : : {
4511 [ + + ]: 5072 : if (LocTriggerData.tg_trigslot)
4512 : 4585 : ExecClearTuple(LocTriggerData.tg_trigslot);
4513 [ + + ]: 5072 : if (LocTriggerData.tg_newslot)
4514 : 1351 : ExecClearTuple(LocTriggerData.tg_newslot);
4515 : : }
4516 : :
4517 : : /*
4518 : : * If doing EXPLAIN ANALYZE, stop charging time to this trigger, and count
4519 : : * one "tuple returned" (really the number of firings).
4520 : : */
6960 tgl@sss.pgh.pa.us 4521 [ - + ]: 5107 : if (instr)
6529 bruce@momjian.us 4522 :UBC 0 : InstrStopNode(instr + tgindx, 1);
8964 JanWieck@Yahoo.com 4523 :CBC 5107 : }
4524 : :
4525 : :
4526 : : /*
4527 : : * afterTriggerMarkEvents()
4528 : : *
4529 : : * Scan the given event list for not yet invoked events. Mark the ones
4530 : : * that can be invoked now with the current firing ID.
4531 : : *
4532 : : * If move_list isn't NULL, events that are not to be invoked now are
4533 : : * transferred to move_list.
4534 : : *
4535 : : * When immediate_only is true, do not invoke currently-deferred triggers.
4536 : : * (This will be false only at main transaction exit.)
4537 : : *
4538 : : * Returns true if any invokable events were found.
4539 : : */
4540 : : static bool
7156 tgl@sss.pgh.pa.us 4541 : 418075 : afterTriggerMarkEvents(AfterTriggerEventList *events,
4542 : : AfterTriggerEventList *move_list,
4543 : : bool immediate_only)
4544 : : {
4545 : 418075 : bool found = false;
1252 noah@leadboat.com 4546 : 418075 : bool deferred_found = false;
4547 : : AfterTriggerEvent event;
4548 : : AfterTriggerEventChunk *chunk;
4549 : :
5651 tgl@sss.pgh.pa.us 4550 [ + + + + : 428558 : for_each_event_chunk(event, chunk, *events)
+ + + + +
+ ]
4551 : : {
4552 : 6507 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
7156 4553 : 6507 : bool defer_it = false;
4554 : :
5651 4555 [ + + ]: 6507 : if (!(event->ate_flags &
4556 : : (AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS)))
4557 : : {
4558 : : /*
4559 : : * This trigger hasn't been called or scheduled yet. Check if we
4560 : : * should call it now.
4561 : : */
4562 [ + + + + ]: 6087 : if (immediate_only && afterTriggerCheckState(evtshared))
4563 : : {
7156 4564 : 289 : defer_it = true;
4565 : : }
4566 : : else
4567 : : {
4568 : : /*
4569 : : * Mark it as to be fired in this firing cycle.
4570 : : */
3461 rhaas@postgresql.org 4571 : 5798 : evtshared->ats_firing_id = afterTriggers.firing_counter;
5651 tgl@sss.pgh.pa.us 4572 : 5798 : event->ate_flags |= AFTER_TRIGGER_IN_PROGRESS;
7156 4573 : 5798 : found = true;
4574 : : }
4575 : : }
4576 : :
4577 : : /*
4578 : : * If it's deferred, move it to move_list, if requested.
4579 : : */
4580 [ + + + - ]: 6507 : if (defer_it && move_list != NULL)
4581 : : {
1252 noah@leadboat.com 4582 : 289 : deferred_found = true;
4583 : : /* add it to move_list */
5651 tgl@sss.pgh.pa.us 4584 : 289 : afterTriggerAddEvent(move_list, event, evtshared);
4585 : : /* mark original copy "done" so we don't do it again */
4586 : 289 : event->ate_flags |= AFTER_TRIGGER_DONE;
4587 : : }
4588 : : }
4589 : :
4590 : : /*
4591 : : * We could allow deferred triggers if, before the end of the
4592 : : * security-restricted operation, we were to verify that a SET CONSTRAINTS
4593 : : * ... IMMEDIATE has fired all such triggers. For now, don't bother.
4594 : : */
1252 noah@leadboat.com 4595 [ + + + + ]: 418075 : if (deferred_found && InSecurityRestrictedOperation())
4596 [ + - ]: 6 : ereport(ERROR,
4597 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4598 : : errmsg("cannot fire deferred trigger within security-restricted operation")));
4599 : :
7156 tgl@sss.pgh.pa.us 4600 : 418069 : return found;
4601 : : }
4602 : :
4603 : : /*
4604 : : * afterTriggerInvokeEvents()
4605 : : *
4606 : : * Scan the given event list for events that are marked as to be fired
4607 : : * in the current firing cycle, and fire them.
4608 : : *
4609 : : * If estate isn't NULL, we use its result relation info to avoid repeated
4610 : : * openings and closing of trigger target relations. If it is NULL, we
4611 : : * make one locally to cache the info in case there are multiple trigger
4612 : : * events per rel.
4613 : : *
4614 : : * When delete_ok is true, it's safe to delete fully-processed events.
4615 : : * (We are not very tense about that: we simply reset a chunk to be empty
4616 : : * if all its events got fired. The objective here is just to avoid useless
4617 : : * rescanning of events when a trigger queues new events during transaction
4618 : : * end, so it's not necessary to worry much about the case where only
4619 : : * some events are fired.)
4620 : : *
4621 : : * Returns true if no unfired events remain in the list (this allows us
4622 : : * to avoid repeating afterTriggerMarkEvents).
4623 : : */
4624 : : static bool
4625 : 3762 : afterTriggerInvokeEvents(AfterTriggerEventList *events,
4626 : : CommandId firing_id,
4627 : : EState *estate,
4628 : : bool delete_ok)
4629 : : {
5651 4630 : 3762 : bool all_fired = true;
4631 : : AfterTriggerEventChunk *chunk;
4632 : : MemoryContext per_tuple_context;
6087 4633 : 3762 : bool local_estate = false;
1873 andres@anarazel.de 4634 : 3762 : ResultRelInfo *rInfo = NULL;
8353 tgl@sss.pgh.pa.us 4635 : 3762 : Relation rel = NULL;
7853 4636 : 3762 : TriggerDesc *trigdesc = NULL;
8353 4637 : 3762 : FmgrInfo *finfo = NULL;
6960 4638 : 3762 : Instrumentation *instr = NULL;
3675 noah@leadboat.com 4639 : 3762 : TupleTableSlot *slot1 = NULL,
4640 : 3762 : *slot2 = NULL;
4641 : :
4642 : : /* Make a local EState if need be */
6087 tgl@sss.pgh.pa.us 4643 [ + + ]: 3762 : if (estate == NULL)
4644 : : {
4645 : 165 : estate = CreateExecutorState();
4646 : 165 : local_estate = true;
4647 : : }
4648 : :
4649 : : /* Make a per-tuple memory context for trigger function calls */
4650 : : per_tuple_context =
8483 4651 : 3762 : AllocSetContextCreate(CurrentMemoryContext,
4652 : : "AfterTriggerTupleContext",
4653 : : ALLOCSET_DEFAULT_SIZES);
4654 : :
5651 4655 [ + + ]: 6924 : for_each_chunk(chunk, *events)
4656 : : {
4657 : : AfterTriggerEvent event;
4658 : 3762 : bool all_fired_in_chunk = true;
4659 : :
4660 [ + + + + : 9610 : for_each_event(event, chunk)
+ + + + ]
4661 : : {
4662 : 6448 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
4663 : :
4664 : : /*
4665 : : * Is it one for me to fire?
4666 : : */
4667 [ + + ]: 6448 : if ((event->ate_flags & AFTER_TRIGGER_IN_PROGRESS) &&
4668 [ + - ]: 5707 : evtshared->ats_firing_id == firing_id)
8353 4669 : 5107 : {
4670 : : ResultRelInfo *src_rInfo,
4671 : : *dst_rInfo;
4672 : :
4673 : : /*
4674 : : * So let's fire it... but first, find the correct relation if
4675 : : * this is not the same relation as before.
4676 : : */
5651 4677 [ + + + + ]: 5707 : if (rel == NULL || RelationGetRelid(rel) != evtshared->ats_relid)
4678 : : {
756 alvherre@alvh.no-ip. 4679 : 3922 : rInfo = ExecGetTriggerResultRel(estate, evtshared->ats_relid,
4680 : : NULL);
5651 tgl@sss.pgh.pa.us 4681 : 3922 : rel = rInfo->ri_RelationDesc;
4682 : : /* Catch calls with insufficient relcache refcounting */
1058 4683 [ - + ]: 3922 : Assert(!RelationHasReferenceCountZero(rel));
5651 4684 : 3922 : trigdesc = rInfo->ri_TrigDesc;
4685 : 3922 : finfo = rInfo->ri_TrigFunctions;
4686 : 3922 : instr = rInfo->ri_TrigInstrument;
1587 efujita@postgresql.o 4687 [ - + ]: 3922 : if (slot1 != NULL)
4688 : : {
1587 efujita@postgresql.o 4689 :UBC 0 : ExecDropSingleTupleTableSlot(slot1);
4690 : 0 : ExecDropSingleTupleTableSlot(slot2);
4691 : 0 : slot1 = slot2 = NULL;
4692 : : }
3675 noah@leadboat.com 4693 [ + + ]:CBC 3922 : if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
4694 : : {
1977 andres@anarazel.de 4695 : 19 : slot1 = MakeSingleTupleTableSlot(rel->rd_att,
4696 : : &TTSOpsMinimalTuple);
4697 : 19 : slot2 = MakeSingleTupleTableSlot(rel->rd_att,
4698 : : &TTSOpsMinimalTuple);
4699 : : }
2489 tgl@sss.pgh.pa.us 4700 [ - + ]: 3922 : if (trigdesc == NULL) /* should not happen */
5651 tgl@sss.pgh.pa.us 4701 [ # # ]:UBC 0 : elog(ERROR, "relation %u has no triggers",
4702 : : evtshared->ats_relid);
4703 : : }
4704 : :
4705 : : /*
4706 : : * Look up source and destination partition result rels of a
4707 : : * cross-partition update event.
4708 : : */
756 alvherre@alvh.no-ip. 4709 [ + + ]:CBC 5707 : if ((event->ate_flags & AFTER_TRIGGER_TUP_BITS) ==
4710 : : AFTER_TRIGGER_CP_UPDATE)
4711 : : {
4712 [ + - - + ]: 72 : Assert(OidIsValid(event->ate_src_part) &&
4713 : : OidIsValid(event->ate_dst_part));
4714 : 72 : src_rInfo = ExecGetTriggerResultRel(estate,
4715 : : event->ate_src_part,
4716 : : rInfo);
4717 : 72 : dst_rInfo = ExecGetTriggerResultRel(estate,
4718 : : event->ate_dst_part,
4719 : : rInfo);
4720 : : }
4721 : : else
4722 : 5635 : src_rInfo = dst_rInfo = rInfo;
4723 : :
4724 : : /*
4725 : : * Fire it. Note that the AFTER_TRIGGER_IN_PROGRESS flag is
4726 : : * still set, so recursive examinations of the event list
4727 : : * won't try to re-fire it.
4728 : : */
4729 : 5707 : AfterTriggerExecute(estate, event, rInfo,
4730 : : src_rInfo, dst_rInfo,
4731 : : trigdesc, finfo, instr,
4732 : : per_tuple_context, slot1, slot2);
4733 : :
4734 : : /*
4735 : : * Mark the event as done.
4736 : : */
5651 tgl@sss.pgh.pa.us 4737 : 5107 : event->ate_flags &= ~AFTER_TRIGGER_IN_PROGRESS;
4738 : 5107 : event->ate_flags |= AFTER_TRIGGER_DONE;
4739 : : }
4740 [ + + ]: 741 : else if (!(event->ate_flags & AFTER_TRIGGER_DONE))
4741 : : {
4742 : : /* something remains to be done */
4743 : 258 : all_fired = all_fired_in_chunk = false;
4744 : : }
4745 : : }
4746 : :
4747 : : /* Clear the chunk if delete_ok and nothing left of interest */
4748 [ + + + - ]: 3162 : if (delete_ok && all_fired_in_chunk)
4749 : : {
4750 : 83 : chunk->freeptr = CHUNK_DATA_START(chunk);
4751 : 83 : chunk->endfree = chunk->endptr;
4752 : :
4753 : : /*
4754 : : * If it's last chunk, must sync event list's tailfree too. Note
4755 : : * that delete_ok must NOT be passed as true if there could be
4756 : : * additional AfterTriggerEventList values pointing at this event
4757 : : * list, since we'd fail to fix their copies of tailfree.
4758 : : */
4987 4759 [ + - ]: 83 : if (chunk == events->tail)
4760 : 83 : events->tailfree = chunk->freeptr;
4761 : : }
4762 : : }
3675 noah@leadboat.com 4763 [ + + ]: 3162 : if (slot1 != NULL)
4764 : : {
4765 : 19 : ExecDropSingleTupleTableSlot(slot1);
4766 : 19 : ExecDropSingleTupleTableSlot(slot2);
4767 : : }
4768 : :
4769 : : /* Release working resources */
6087 tgl@sss.pgh.pa.us 4770 : 3162 : MemoryContextDelete(per_tuple_context);
4771 : :
4772 [ + + ]: 3162 : if (local_estate)
4773 : : {
1279 heikki.linnakangas@i 4774 : 83 : ExecCloseResultRelations(estate);
1874 andres@anarazel.de 4775 : 83 : ExecResetTupleTable(estate->es_tupleTable, false);
6087 tgl@sss.pgh.pa.us 4776 : 83 : FreeExecutorState(estate);
4777 : : }
4778 : :
5651 4779 : 3162 : return all_fired;
4780 : : }
4781 : :
4782 : :
4783 : : /*
4784 : : * GetAfterTriggersTableData
4785 : : *
4786 : : * Find or create an AfterTriggersTableData struct for the specified
4787 : : * trigger event (relation + operation type). Ignore existing structs
4788 : : * marked "closed"; we don't want to put any additional tuples into them,
4789 : : * nor change their stmt-triggers-fired state.
4790 : : *
4791 : : * Note: the AfterTriggersTableData list is allocated in the current
4792 : : * (sub)transaction's CurTransactionContext. This is OK because
4793 : : * we don't need it to live past AfterTriggerEndQuery.
4794 : : */
4795 : : static AfterTriggersTableData *
2402 4796 : 1068 : GetAfterTriggersTableData(Oid relid, CmdType cmdType)
4797 : : {
4798 : : AfterTriggersTableData *table;
4799 : : AfterTriggersQueryData *qs;
4800 : : MemoryContext oldcxt;
4801 : : ListCell *lc;
4802 : :
4803 : : /* Caller should have ensured query_depth is OK. */
4804 [ + - - + ]: 1068 : Assert(afterTriggers.query_depth >= 0 &&
4805 : : afterTriggers.query_depth < afterTriggers.maxquerydepth);
4806 : 1068 : qs = &afterTriggers.query_stack[afterTriggers.query_depth];
4807 : :
4808 [ + + + + : 1242 : foreach(lc, qs->tables)
+ + ]
4809 : : {
4810 : 704 : table = (AfterTriggersTableData *) lfirst(lc);
4811 [ + + + + ]: 704 : if (table->relid == relid && table->cmdType == cmdType &&
4812 [ + + ]: 548 : !table->closed)
4813 : 530 : return table;
4814 : : }
4815 : :
4816 : 538 : oldcxt = MemoryContextSwitchTo(CurTransactionContext);
4817 : :
4818 : 538 : table = (AfterTriggersTableData *) palloc0(sizeof(AfterTriggersTableData));
4819 : 538 : table->relid = relid;
4820 : 538 : table->cmdType = cmdType;
4821 : 538 : qs->tables = lappend(qs->tables, table);
4822 : :
4823 : 538 : MemoryContextSwitchTo(oldcxt);
4824 : :
4825 : 538 : return table;
4826 : : }
4827 : :
4828 : : /*
4829 : : * Returns a TupleTableSlot suitable for holding the tuples to be put
4830 : : * into AfterTriggersTableData's transition table tuplestores.
4831 : : */
4832 : : static TupleTableSlot *
1142 alvherre@alvh.no-ip. 4833 : 147 : GetAfterTriggersStoreSlot(AfterTriggersTableData *table,
4834 : : TupleDesc tupdesc)
4835 : : {
4836 : : /* Create it if not already done. */
4837 [ + + ]: 147 : if (!table->storeslot)
4838 : : {
4839 : : MemoryContext oldcxt;
4840 : :
4841 : : /*
4842 : : * We need this slot only until AfterTriggerEndQuery, but making it
4843 : : * last till end-of-subxact is good enough. It'll be freed by
4844 : : * AfterTriggerFreeQuery(). However, the passed-in tupdesc might have
4845 : : * a different lifespan, so we'd better make a copy of that.
4846 : : */
4847 : 42 : oldcxt = MemoryContextSwitchTo(CurTransactionContext);
567 tgl@sss.pgh.pa.us 4848 : 42 : tupdesc = CreateTupleDescCopy(tupdesc);
1142 alvherre@alvh.no-ip. 4849 : 42 : table->storeslot = MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual);
4850 : 42 : MemoryContextSwitchTo(oldcxt);
4851 : : }
4852 : :
4853 : 147 : return table->storeslot;
4854 : : }
4855 : :
4856 : : /*
4857 : : * MakeTransitionCaptureState
4858 : : *
4859 : : * Make a TransitionCaptureState object for the given TriggerDesc, target
4860 : : * relation, and operation type. The TCS object holds all the state needed
4861 : : * to decide whether to capture tuples in transition tables.
4862 : : *
4863 : : * If there are no triggers in 'trigdesc' that request relevant transition
4864 : : * tables, then return NULL.
4865 : : *
4866 : : * The resulting object can be passed to the ExecAR* functions. When
4867 : : * dealing with child tables, the caller can set tcs_original_insert_tuple
4868 : : * to avoid having to reconstruct the original tuple in the root table's
4869 : : * format.
4870 : : *
4871 : : * Note that we copy the flags from a parent table into this struct (rather
4872 : : * than subsequently using the relation's TriggerDesc directly) so that we can
4873 : : * use it to control collection of transition tuples from child tables.
4874 : : *
4875 : : * Per SQL spec, all operations of the same kind (INSERT/UPDATE/DELETE)
4876 : : * on the same table during one query should share one transition table.
4877 : : * Therefore, the Tuplestores are owned by an AfterTriggersTableData struct
4878 : : * looked up using the table OID + CmdType, and are merely referenced by
4879 : : * the TransitionCaptureState objects we hand out to callers.
4880 : : */
4881 : : TransitionCaptureState *
2402 tgl@sss.pgh.pa.us 4882 : 56461 : MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
4883 : : {
4884 : : TransitionCaptureState *state;
4885 : : bool need_old_upd,
4886 : : need_new_upd,
4887 : : need_old_del,
4888 : : need_new_ins;
4889 : : AfterTriggersTableData *table;
4890 : : MemoryContext oldcxt;
4891 : : ResourceOwner saveResourceOwner;
4892 : :
4893 [ + + ]: 56461 : if (trigdesc == NULL)
4894 : 50102 : return NULL;
4895 : :
4896 : : /* Detect which table(s) we need. */
4897 [ + + + + : 6359 : switch (cmdType)
- ]
4898 : : {
4899 : 3526 : case CMD_INSERT:
748 alvherre@alvh.no-ip. 4900 : 3526 : need_old_upd = need_old_del = need_new_upd = false;
4901 : 3526 : need_new_ins = trigdesc->trig_insert_new_table;
2402 tgl@sss.pgh.pa.us 4902 : 3526 : break;
4903 : 1908 : case CMD_UPDATE:
748 alvherre@alvh.no-ip. 4904 : 1908 : need_old_upd = trigdesc->trig_update_old_table;
4905 : 1908 : need_new_upd = trigdesc->trig_update_new_table;
4906 : 1908 : need_old_del = need_new_ins = false;
2402 tgl@sss.pgh.pa.us 4907 : 1908 : break;
4908 : 764 : case CMD_DELETE:
748 alvherre@alvh.no-ip. 4909 : 764 : need_old_del = trigdesc->trig_delete_old_table;
4910 : 764 : need_old_upd = need_new_upd = need_new_ins = false;
4911 : 764 : break;
4912 : 161 : case CMD_MERGE:
4913 : 161 : need_old_upd = trigdesc->trig_update_old_table;
4914 : 161 : need_new_upd = trigdesc->trig_update_new_table;
4915 : 161 : need_old_del = trigdesc->trig_delete_old_table;
4916 : 161 : need_new_ins = trigdesc->trig_insert_new_table;
2402 tgl@sss.pgh.pa.us 4917 : 161 : break;
2402 tgl@sss.pgh.pa.us 4918 :UBC 0 : default:
4919 [ # # ]: 0 : elog(ERROR, "unexpected CmdType: %d", (int) cmdType);
4920 : : /* keep compiler quiet */
4921 : : need_old_upd = need_new_upd = need_old_del = need_new_ins = false;
4922 : : break;
4923 : : }
748 alvherre@alvh.no-ip. 4924 [ + + + + :CBC 6359 : if (!need_old_upd && !need_new_upd && !need_new_ins && !need_old_del)
+ + + + ]
2402 tgl@sss.pgh.pa.us 4925 : 6083 : return NULL;
4926 : :
4927 : : /* Check state, like AfterTriggerSaveEvent. */
4928 [ - + ]: 276 : if (afterTriggers.query_depth < 0)
2402 tgl@sss.pgh.pa.us 4929 [ # # ]:UBC 0 : elog(ERROR, "MakeTransitionCaptureState() called outside of query");
4930 : :
4931 : : /* Be sure we have enough space to record events at this query depth. */
2402 tgl@sss.pgh.pa.us 4932 [ + + ]:CBC 276 : if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
4933 : 204 : AfterTriggerEnlargeQueryState();
4934 : :
4935 : : /*
4936 : : * Find or create an AfterTriggersTableData struct to hold the
4937 : : * tuplestore(s). If there's a matching struct but it's marked closed,
4938 : : * ignore it; we need a newer one.
4939 : : *
4940 : : * Note: the AfterTriggersTableData list, as well as the tuplestores, are
4941 : : * allocated in the current (sub)transaction's CurTransactionContext, and
4942 : : * the tuplestores are managed by the (sub)transaction's resource owner.
4943 : : * This is sufficient lifespan because we do not allow triggers using
4944 : : * transition tables to be deferrable; they will be fired during
4945 : : * AfterTriggerEndQuery, after which it's okay to delete the data.
4946 : : */
4947 : 276 : table = GetAfterTriggersTableData(relid, cmdType);
4948 : :
4949 : : /* Now create required tuplestore(s), if we don't have them already. */
4950 : 276 : oldcxt = MemoryContextSwitchTo(CurTransactionContext);
4951 : 276 : saveResourceOwner = CurrentResourceOwner;
2377 4952 : 276 : CurrentResourceOwner = CurTransactionResourceOwner;
4953 : :
748 alvherre@alvh.no-ip. 4954 [ + + + + ]: 276 : if (need_old_upd && table->old_upd_tuplestore == NULL)
4955 : 81 : table->old_upd_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4956 [ + + + + ]: 276 : if (need_new_upd && table->new_upd_tuplestore == NULL)
4957 : 87 : table->new_upd_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4958 [ + + + + ]: 276 : if (need_old_del && table->old_del_tuplestore == NULL)
4959 : 66 : table->old_del_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4960 [ + + + + ]: 276 : if (need_new_ins && table->new_ins_tuplestore == NULL)
4961 : 105 : table->new_ins_tuplestore = tuplestore_begin_heap(false, false, work_mem);
4962 : :
2402 tgl@sss.pgh.pa.us 4963 : 276 : CurrentResourceOwner = saveResourceOwner;
4964 : 276 : MemoryContextSwitchTo(oldcxt);
4965 : :
4966 : : /* Now build the TransitionCaptureState struct, in caller's context */
4967 : 276 : state = (TransitionCaptureState *) palloc0(sizeof(TransitionCaptureState));
4968 : 276 : state->tcs_delete_old_table = trigdesc->trig_delete_old_table;
4969 : 276 : state->tcs_update_old_table = trigdesc->trig_update_old_table;
4970 : 276 : state->tcs_update_new_table = trigdesc->trig_update_new_table;
4971 : 276 : state->tcs_insert_new_table = trigdesc->trig_insert_new_table;
4972 : 276 : state->tcs_private = table;
4973 : :
4974 : 276 : return state;
4975 : : }
4976 : :
4977 : :
4978 : : /* ----------
4979 : : * AfterTriggerBeginXact()
4980 : : *
4981 : : * Called at transaction start (either BEGIN or implicit for single
4982 : : * statement outside of transaction block).
4983 : : * ----------
4984 : : */
4985 : : void
7156 4986 : 431498 : AfterTriggerBeginXact(void)
4987 : : {
4988 : : /*
4989 : : * Initialize after-trigger state structure to empty
4990 : : */
2489 4991 : 431498 : afterTriggers.firing_counter = (CommandId) 1; /* mustn't be 0 */
3461 rhaas@postgresql.org 4992 : 431498 : afterTriggers.query_depth = -1;
4993 : :
4994 : : /*
4995 : : * Verify that there is no leftover state remaining. If these assertions
4996 : : * trip, it means that AfterTriggerEndXact wasn't called or didn't clean
4997 : : * up properly.
4998 : : */
4999 [ - + ]: 431498 : Assert(afterTriggers.state == NULL);
5000 [ - + ]: 431498 : Assert(afterTriggers.query_stack == NULL);
5001 [ - + ]: 431498 : Assert(afterTriggers.maxquerydepth == 0);
5002 [ - + ]: 431498 : Assert(afterTriggers.event_cxt == NULL);
5003 [ - + ]: 431498 : Assert(afterTriggers.events.head == NULL);
2402 tgl@sss.pgh.pa.us 5004 [ - + ]: 431498 : Assert(afterTriggers.trans_stack == NULL);
3461 rhaas@postgresql.org 5005 [ - + ]: 431498 : Assert(afterTriggers.maxtransdepth == 0);
7156 tgl@sss.pgh.pa.us 5006 : 431498 : }
5007 : :
5008 : :
5009 : : /* ----------
5010 : : * AfterTriggerBeginQuery()
5011 : : *
5012 : : * Called just before we start processing a single query within a
5013 : : * transaction (or subtransaction). Most of the real work gets deferred
5014 : : * until somebody actually tries to queue a trigger event.
5015 : : * ----------
5016 : : */
5017 : : void
5018 : 204781 : AfterTriggerBeginQuery(void)
5019 : : {
5020 : : /* Increase the query stack depth */
3461 rhaas@postgresql.org 5021 : 204781 : afterTriggers.query_depth++;
8964 JanWieck@Yahoo.com 5022 : 204781 : }
5023 : :
5024 : :
5025 : : /* ----------
5026 : : * AfterTriggerEndQuery()
5027 : : *
5028 : : * Called after one query has been completely processed. At this time
5029 : : * we invoke all AFTER IMMEDIATE trigger events queued by the query, and
5030 : : * transfer deferred trigger events to the global deferred-trigger list.
5031 : : *
5032 : : * Note that this must be called BEFORE closing down the executor
5033 : : * with ExecutorEnd, because we make use of the EState's info about
5034 : : * target relations. Normally it is called from ExecutorFinish.
5035 : : * ----------
5036 : : */
5037 : : void
6960 tgl@sss.pgh.pa.us 5038 : 202554 : AfterTriggerEndQuery(EState *estate)
5039 : : {
5040 : : AfterTriggersQueryData *qs;
5041 : :
5042 : : /* Must be inside a query, too */
3461 rhaas@postgresql.org 5043 [ - + ]: 202554 : Assert(afterTriggers.query_depth >= 0);
5044 : :
5045 : : /*
5046 : : * If we never even got as far as initializing the event stack, there
5047 : : * certainly won't be any events, so exit quickly.
5048 : : */
5049 [ + + ]: 202554 : if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
5050 : : {
5051 : 198196 : afterTriggers.query_depth--;
5052 : 198196 : return;
5053 : : }
5054 : :
5055 : : /*
5056 : : * Process all immediate-mode triggers queued by the query, and move the
5057 : : * deferred ones to the main list of deferred events.
5058 : : *
5059 : : * Notice that we decide which ones will be fired, and put the deferred
5060 : : * ones on the main list, before anything is actually fired. This ensures
5061 : : * reasonably sane behavior if a trigger function does SET CONSTRAINTS ...
5062 : : * IMMEDIATE: all events we have decided to defer will be available for it
5063 : : * to fire.
5064 : : *
5065 : : * We loop in case a trigger queues more events at the same query level.
5066 : : * Ordinary trigger functions, including all PL/pgSQL trigger functions,
5067 : : * will instead fire any triggers in a dedicated query level. Foreign key
5068 : : * enforcement triggers do add to the current query level, thanks to their
5069 : : * passing fire_triggers = false to SPI_execute_snapshot(). Other
5070 : : * C-language triggers might do likewise.
5071 : : *
5072 : : * If we find no firable events, we don't have to increment
5073 : : * firing_counter.
5074 : : */
2401 tgl@sss.pgh.pa.us 5075 : 4358 : qs = &afterTriggers.query_stack[afterTriggers.query_depth];
5076 : :
5077 : : for (;;)
5078 : : {
2402 5079 [ + + ]: 4511 : if (afterTriggerMarkEvents(&qs->events, &afterTriggers.events, true))
5080 : : {
3461 rhaas@postgresql.org 5081 : 3597 : CommandId firing_id = afterTriggers.firing_counter++;
2401 tgl@sss.pgh.pa.us 5082 : 3597 : AfterTriggerEventChunk *oldtail = qs->events.tail;
5083 : :
5084 [ + + ]: 3597 : if (afterTriggerInvokeEvents(&qs->events, firing_id, estate, false))
5651 5085 : 2926 : break; /* all fired */
5086 : :
5087 : : /*
5088 : : * Firing a trigger could result in query_stack being repalloc'd,
5089 : : * so we must recalculate qs after each afterTriggerInvokeEvents
5090 : : * call. Furthermore, it's unsafe to pass delete_ok = true here,
5091 : : * because that could cause afterTriggerInvokeEvents to try to
5092 : : * access qs->events after the stack has been repalloc'd.
5093 : : */
2401 5094 : 153 : qs = &afterTriggers.query_stack[afterTriggers.query_depth];
5095 : :
5096 : : /*
5097 : : * We'll need to scan the events list again. To reduce the cost
5098 : : * of doing so, get rid of completely-fired chunks. We know that
5099 : : * all events were marked IN_PROGRESS or DONE at the conclusion of
5100 : : * afterTriggerMarkEvents, so any still-interesting events must
5101 : : * have been added after that, and so must be in the chunk that
5102 : : * was then the tail chunk, or in later chunks. So, zap all
5103 : : * chunks before oldtail. This is approximately the same set of
5104 : : * events we would have gotten rid of by passing delete_ok = true.
5105 : : */
5106 [ - + ]: 153 : Assert(oldtail != NULL);
5107 [ - + ]: 153 : while (qs->events.head != oldtail)
2401 tgl@sss.pgh.pa.us 5108 :UBC 0 : afterTriggerDeleteHeadEventChunk(qs);
5109 : : }
5110 : : else
5651 tgl@sss.pgh.pa.us 5111 :CBC 908 : break;
5112 : : }
5113 : :
5114 : : /* Release query-level-local storage, including tuplestores if any */
2402 5115 : 3834 : AfterTriggerFreeQuery(&afterTriggers.query_stack[afterTriggers.query_depth]);
5116 : :
5117 : 3834 : afterTriggers.query_depth--;
5118 : : }
5119 : :
5120 : :
5121 : : /*
5122 : : * AfterTriggerFreeQuery
5123 : : * Release subsidiary storage for a trigger query level.
5124 : : * This includes closing down tuplestores.
5125 : : * Note: it's important for this to be safe if interrupted by an error
5126 : : * and then called again for the same query level.
5127 : : */
5128 : : static void
5129 : 3849 : AfterTriggerFreeQuery(AfterTriggersQueryData *qs)
5130 : : {
5131 : : Tuplestorestate *ts;
5132 : : List *tables;
5133 : : ListCell *lc;
5134 : :
5135 : : /* Drop the trigger events */
5136 : 3849 : afterTriggerFreeEventList(&qs->events);
5137 : :
5138 : : /* Drop FDW tuplestore if any */
5139 : 3849 : ts = qs->fdw_tuplestore;
5140 : 3849 : qs->fdw_tuplestore = NULL;
5141 [ + + ]: 3849 : if (ts)
5142 : 18 : tuplestore_end(ts);
5143 : :
5144 : : /* Release per-table subsidiary storage */
5145 : 3849 : tables = qs->tables;
5146 [ + + + + : 4361 : foreach(lc, tables)
+ + ]
5147 : : {
5148 : 512 : AfterTriggersTableData *table = (AfterTriggersTableData *) lfirst(lc);
5149 : :
748 alvherre@alvh.no-ip. 5150 : 512 : ts = table->old_upd_tuplestore;
5151 : 512 : table->old_upd_tuplestore = NULL;
5152 [ + + ]: 512 : if (ts)
5153 : 75 : tuplestore_end(ts);
5154 : 512 : ts = table->new_upd_tuplestore;
5155 : 512 : table->new_upd_tuplestore = NULL;
5156 [ + + ]: 512 : if (ts)
5157 : 78 : tuplestore_end(ts);
5158 : 512 : ts = table->old_del_tuplestore;
5159 : 512 : table->old_del_tuplestore = NULL;
2402 tgl@sss.pgh.pa.us 5160 [ + + ]: 512 : if (ts)
5161 : 60 : tuplestore_end(ts);
748 alvherre@alvh.no-ip. 5162 : 512 : ts = table->new_ins_tuplestore;
5163 : 512 : table->new_ins_tuplestore = NULL;
2402 tgl@sss.pgh.pa.us 5164 [ + + ]: 512 : if (ts)
5165 : 102 : tuplestore_end(ts);
1142 alvherre@alvh.no-ip. 5166 [ + + ]: 512 : if (table->storeslot)
5167 : : {
567 tgl@sss.pgh.pa.us 5168 : 42 : TupleTableSlot *slot = table->storeslot;
5169 : :
5170 : 42 : table->storeslot = NULL;
5171 : 42 : ExecDropSingleTupleTableSlot(slot);
5172 : : }
5173 : : }
5174 : :
5175 : : /*
5176 : : * Now free the AfterTriggersTableData structs and list cells. Reset list
5177 : : * pointer first; if list_free_deep somehow gets an error, better to leak
5178 : : * that storage than have an infinite loop.
5179 : : */
2402 5180 : 3849 : qs->tables = NIL;
5181 : 3849 : list_free_deep(tables);
8964 JanWieck@Yahoo.com 5182 : 3849 : }
5183 : :
5184 : :
5185 : : /* ----------
5186 : : * AfterTriggerFireDeferred()
5187 : : *
5188 : : * Called just before the current transaction is committed. At this
5189 : : * time we invoke all pending DEFERRED triggers.
5190 : : *
5191 : : * It is possible for other modules to queue additional deferred triggers
5192 : : * during pre-commit processing; therefore xact.c may have to call this
5193 : : * multiple times.
5194 : : * ----------
5195 : : */
5196 : : void
6943 tgl@sss.pgh.pa.us 5197 : 413547 : AfterTriggerFireDeferred(void)
5198 : : {
5199 : : AfterTriggerEventList *events;
5816 alvherre@alvh.no-ip. 5200 : 413547 : bool snap_pushed = false;
5201 : :
5202 : : /* Must not be inside a query */
3461 rhaas@postgresql.org 5203 [ - + ]: 413547 : Assert(afterTriggers.query_depth == -1);
5204 : :
5205 : : /*
5206 : : * If there are any triggers to fire, make sure we have set a snapshot for
5207 : : * them to use. (Since PortalRunUtility doesn't set a snap for COMMIT, we
5208 : : * can't assume ActiveSnapshot is valid on entry.)
5209 : : */
5210 : 413547 : events = &afterTriggers.events;
6943 tgl@sss.pgh.pa.us 5211 [ + + ]: 413547 : if (events->head != NULL)
5212 : : {
5816 alvherre@alvh.no-ip. 5213 : 157 : PushActiveSnapshot(GetTransactionSnapshot());
5214 : 157 : snap_pushed = true;
5215 : : }
5216 : :
5217 : : /*
5218 : : * Run all the remaining triggers. Loop until they are all gone, in case
5219 : : * some trigger queues more for us to do.
5220 : : */
7156 tgl@sss.pgh.pa.us 5221 [ + + ]: 413547 : while (afterTriggerMarkEvents(events, NULL, false))
5222 : : {
3461 rhaas@postgresql.org 5223 : 157 : CommandId firing_id = afterTriggers.firing_counter++;
5224 : :
5651 tgl@sss.pgh.pa.us 5225 [ + - ]: 157 : if (afterTriggerInvokeEvents(events, firing_id, NULL, true))
5226 : 83 : break; /* all fired */
5227 : : }
5228 : :
5229 : : /*
5230 : : * We don't bother freeing the event list, since it will go away anyway
5231 : : * (and more efficiently than via pfree) in AfterTriggerEndXact.
5232 : : */
5233 : :
5816 alvherre@alvh.no-ip. 5234 [ + + ]: 413473 : if (snap_pushed)
5235 : 83 : PopActiveSnapshot();
8964 JanWieck@Yahoo.com 5236 : 413473 : }
5237 : :
5238 : :
5239 : : /* ----------
5240 : : * AfterTriggerEndXact()
5241 : : *
5242 : : * The current transaction is finishing.
5243 : : *
5244 : : * Any unfired triggers are canceled so we simply throw
5245 : : * away anything we know.
5246 : : *
5247 : : * Note: it is possible for this to be called repeatedly in case of
5248 : : * error during transaction abort; therefore, do not complain if
5249 : : * already closed down.
5250 : : * ----------
5251 : : */
5252 : : void
6943 tgl@sss.pgh.pa.us 5253 : 431693 : AfterTriggerEndXact(bool isCommit)
5254 : : {
5255 : : /*
5256 : : * Forget the pending-events list.
5257 : : *
5258 : : * Since all the info is in TopTransactionContext or children thereof, we
5259 : : * don't really need to do anything to reclaim memory. However, the
5260 : : * pending-events list could be large, and so it's useful to discard it as
5261 : : * soon as possible --- especially if we are aborting because we ran out
5262 : : * of memory for the list!
5263 : : */
3461 rhaas@postgresql.org 5264 [ + + ]: 431693 : if (afterTriggers.event_cxt)
5265 : : {
5266 : 3265 : MemoryContextDelete(afterTriggers.event_cxt);
5267 : 3265 : afterTriggers.event_cxt = NULL;
5268 : 3265 : afterTriggers.events.head = NULL;
5269 : 3265 : afterTriggers.events.tail = NULL;
5270 : 3265 : afterTriggers.events.tailfree = NULL;
5271 : : }
5272 : :
5273 : : /*
5274 : : * Forget any subtransaction state as well. Since this can't be very
5275 : : * large, we let the eventual reset of TopTransactionContext free the
5276 : : * memory instead of doing it here.
5277 : : */
2402 tgl@sss.pgh.pa.us 5278 : 431693 : afterTriggers.trans_stack = NULL;
3461 rhaas@postgresql.org 5279 : 431693 : afterTriggers.maxtransdepth = 0;
5280 : :
5281 : :
5282 : : /*
5283 : : * Forget the query stack and constraint-related state information. As
5284 : : * with the subtransaction state information, we don't bother freeing the
5285 : : * memory here.
5286 : : */
5287 : 431693 : afterTriggers.query_stack = NULL;
5288 : 431693 : afterTriggers.maxquerydepth = 0;
5289 : 431693 : afterTriggers.state = NULL;
5290 : :
5291 : : /* No more afterTriggers manipulation until next transaction starts. */
5292 : 431693 : afterTriggers.query_depth = -1;
8964 JanWieck@Yahoo.com 5293 : 431693 : }
5294 : :
5295 : : /*
5296 : : * AfterTriggerBeginSubXact()
5297 : : *
5298 : : * Start a subtransaction.
5299 : : */
5300 : : void
7156 tgl@sss.pgh.pa.us 5301 : 9927 : AfterTriggerBeginSubXact(void)
5302 : : {
7160 5303 : 9927 : int my_level = GetCurrentTransactionNestLevel();
5304 : :
5305 : : /*
5306 : : * Allocate more space in the trans_stack if needed. (Note: because the
5307 : : * minimum nest level of a subtransaction is 2, we waste the first couple
5308 : : * entries of the array; not worth the notational effort to avoid it.)
5309 : : */
3461 rhaas@postgresql.org 5310 [ + + ]: 11308 : while (my_level >= afterTriggers.maxtransdepth)
5311 : : {
5312 [ + + ]: 1381 : if (afterTriggers.maxtransdepth == 0)
5313 : : {
5314 : : /* Arbitrarily initialize for max of 8 subtransaction levels */
2402 tgl@sss.pgh.pa.us 5315 : 1339 : afterTriggers.trans_stack = (AfterTriggersTransData *)
5316 : 1339 : MemoryContextAlloc(TopTransactionContext,
5317 : : 8 * sizeof(AfterTriggersTransData));
5318 : 1339 : afterTriggers.maxtransdepth = 8;
5319 : : }
5320 : : else
5321 : : {
5322 : : /* repalloc will keep the stack in the same context */
3461 rhaas@postgresql.org 5323 : 42 : int new_alloc = afterTriggers.maxtransdepth * 2;
5324 : :
2402 tgl@sss.pgh.pa.us 5325 : 42 : afterTriggers.trans_stack = (AfterTriggersTransData *)
5326 : 42 : repalloc(afterTriggers.trans_stack,
5327 : : new_alloc * sizeof(AfterTriggersTransData));
3461 rhaas@postgresql.org 5328 : 42 : afterTriggers.maxtransdepth = new_alloc;
5329 : : }
5330 : : }
5331 : :
5332 : : /*
5333 : : * Push the current information into the stack. The SET CONSTRAINTS state
5334 : : * is not saved until/unless changed. Likewise, we don't make a
5335 : : * per-subtransaction event context until needed.
5336 : : */
2402 tgl@sss.pgh.pa.us 5337 : 9927 : afterTriggers.trans_stack[my_level].state = NULL;
5338 : 9927 : afterTriggers.trans_stack[my_level].events = afterTriggers.events;
5339 : 9927 : afterTriggers.trans_stack[my_level].query_depth = afterTriggers.query_depth;
5340 : 9927 : afterTriggers.trans_stack[my_level].firing_counter = afterTriggers.firing_counter;
7227 5341 : 9927 : }
5342 : :
5343 : : /*
5344 : : * AfterTriggerEndSubXact()
5345 : : *
5346 : : * The current subtransaction is ending.
5347 : : */
5348 : : void
7156 5349 : 9927 : AfterTriggerEndSubXact(bool isCommit)
5350 : : {
7160 5351 : 9927 : int my_level = GetCurrentTransactionNestLevel();
5352 : : SetConstraintState state;
5353 : : AfterTriggerEvent event;
5354 : : AfterTriggerEventChunk *chunk;
5355 : : CommandId subxact_firing_id;
5356 : :
5357 : : /*
5358 : : * Pop the prior state if needed.
5359 : : */
7227 5360 [ + + ]: 9927 : if (isCommit)
5361 : : {
3461 rhaas@postgresql.org 5362 [ - + ]: 5361 : Assert(my_level < afterTriggers.maxtransdepth);
5363 : : /* If we saved a prior state, we don't need it anymore */
2402 tgl@sss.pgh.pa.us 5364 : 5361 : state = afterTriggers.trans_stack[my_level].state;
7227 5365 [ + + ]: 5361 : if (state != NULL)
5366 : 3 : pfree(state);
5367 : : /* this avoids double pfree if error later: */
2402 5368 : 5361 : afterTriggers.trans_stack[my_level].state = NULL;
3461 rhaas@postgresql.org 5369 [ - + ]: 5361 : Assert(afterTriggers.query_depth ==
5370 : : afterTriggers.trans_stack[my_level].query_depth);
5371 : : }
5372 : : else
5373 : : {
5374 : : /*
5375 : : * Aborting. It is possible subxact start failed before calling
5376 : : * AfterTriggerBeginSubXact, in which case we mustn't risk touching
5377 : : * trans_stack levels that aren't there.
5378 : : */
5379 [ - + ]: 4566 : if (my_level >= afterTriggers.maxtransdepth)
5194 tgl@sss.pgh.pa.us 5380 :UBC 0 : return;
5381 : :
5382 : : /*
5383 : : * Release query-level storage for queries being aborted, and restore
5384 : : * query_depth to its pre-subxact value. This assumes that a
5385 : : * subtransaction will not add events to query levels started in a
5386 : : * earlier transaction state.
5387 : : */
2402 tgl@sss.pgh.pa.us 5388 [ + + ]:CBC 4613 : while (afterTriggers.query_depth > afterTriggers.trans_stack[my_level].query_depth)
5389 : : {
3461 rhaas@postgresql.org 5390 [ + + ]: 47 : if (afterTriggers.query_depth < afterTriggers.maxquerydepth)
2402 tgl@sss.pgh.pa.us 5391 : 15 : AfterTriggerFreeQuery(&afterTriggers.query_stack[afterTriggers.query_depth]);
3461 rhaas@postgresql.org 5392 : 47 : afterTriggers.query_depth--;
5393 : : }
5394 [ - + ]: 4566 : Assert(afterTriggers.query_depth ==
5395 : : afterTriggers.trans_stack[my_level].query_depth);
5396 : :
5397 : : /*
5398 : : * Restore the global deferred-event list to its former length,
5399 : : * discarding any events queued by the subxact.
5400 : : */
5401 : 4566 : afterTriggerRestoreEventList(&afterTriggers.events,
2402 tgl@sss.pgh.pa.us 5402 : 4566 : &afterTriggers.trans_stack[my_level].events);
5403 : :
5404 : : /*
5405 : : * Restore the trigger state. If the saved state is NULL, then this
5406 : : * subxact didn't save it, so it doesn't need restoring.
5407 : : */
5408 : 4566 : state = afterTriggers.trans_stack[my_level].state;
7227 5409 [ + + ]: 4566 : if (state != NULL)
5410 : : {
3461 rhaas@postgresql.org 5411 : 2 : pfree(afterTriggers.state);
5412 : 2 : afterTriggers.state = state;
5413 : : }
5414 : : /* this avoids double pfree if error later: */
2402 tgl@sss.pgh.pa.us 5415 : 4566 : afterTriggers.trans_stack[my_level].state = NULL;
5416 : :
5417 : : /*
5418 : : * Scan for any remaining deferred events that were marked DONE or IN
5419 : : * PROGRESS by this subxact or a child, and un-mark them. We can
5420 : : * recognize such events because they have a firing ID greater than or
5421 : : * equal to the firing_counter value we saved at subtransaction start.
5422 : : * (This essentially assumes that the current subxact includes all
5423 : : * subxacts started after it.)
5424 : : */
5425 : 4566 : subxact_firing_id = afterTriggers.trans_stack[my_level].firing_counter;
3461 rhaas@postgresql.org 5426 [ + - + - : 4588 : for_each_event_chunk(event, chunk, afterTriggers.events)
+ - + + +
+ ]
5427 : : {
5651 tgl@sss.pgh.pa.us 5428 : 11 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
5429 : :
5430 [ + + ]: 11 : if (event->ate_flags &
5431 : : (AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS))
5432 : : {
5433 [ + - ]: 2 : if (evtshared->ats_firing_id >= subxact_firing_id)
5434 : 2 : event->ate_flags &=
5435 : : ~(AFTER_TRIGGER_DONE | AFTER_TRIGGER_IN_PROGRESS);
5436 : : }
5437 : : }
5438 : : }
5439 : : }
5440 : :
5441 : : /*
5442 : : * Get the transition table for the given event and depending on whether we are
5443 : : * processing the old or the new tuple.
5444 : : */
5445 : : static Tuplestorestate *
765 alvherre@alvh.no-ip. 5446 : 33042 : GetAfterTriggersTransitionTable(int event,
5447 : : TupleTableSlot *oldslot,
5448 : : TupleTableSlot *newslot,
5449 : : TransitionCaptureState *transition_capture)
5450 : : {
5451 : 33042 : Tuplestorestate *tuplestore = NULL;
5452 : 33042 : bool delete_old_table = transition_capture->tcs_delete_old_table;
5453 : 33042 : bool update_old_table = transition_capture->tcs_update_old_table;
5454 : 33042 : bool update_new_table = transition_capture->tcs_update_new_table;
5455 : 33042 : bool insert_new_table = transition_capture->tcs_insert_new_table;
5456 : :
5457 : : /*
5458 : : * For INSERT events NEW should be non-NULL, for DELETE events OLD should
5459 : : * be non-NULL, whereas for UPDATE events normally both OLD and NEW are
5460 : : * non-NULL. But for UPDATE events fired for capturing transition tuples
5461 : : * during UPDATE partition-key row movement, OLD is NULL when the event is
5462 : : * for a row being inserted, whereas NEW is NULL when the event is for a
5463 : : * row being deleted.
5464 : : */
5465 [ + + + - : 33042 : Assert(!(event == TRIGGER_EVENT_DELETE && delete_old_table &&
+ - - + ]
5466 : : TupIsNull(oldslot)));
5467 [ + + + - : 33042 : Assert(!(event == TRIGGER_EVENT_INSERT && insert_new_table &&
+ - - + ]
5468 : : TupIsNull(newslot)));
5469 : :
5470 [ + + + - ]: 33042 : if (!TupIsNull(oldslot))
5471 : : {
5472 [ - + - - ]: 2697 : Assert(TupIsNull(newslot));
5473 [ + + + - ]: 2697 : if (event == TRIGGER_EVENT_DELETE && delete_old_table)
748 5474 : 2520 : tuplestore = transition_capture->tcs_private->old_del_tuplestore;
765 5475 [ + - + + ]: 177 : else if (event == TRIGGER_EVENT_UPDATE && update_old_table)
748 5476 : 165 : tuplestore = transition_capture->tcs_private->old_upd_tuplestore;
5477 : : }
765 5478 [ + - + - ]: 30345 : else if (!TupIsNull(newslot))
5479 : : {
5480 [ - + - - ]: 30345 : Assert(TupIsNull(oldslot));
5481 [ + + + - ]: 30345 : if (event == TRIGGER_EVENT_INSERT && insert_new_table)
748 5482 : 30168 : tuplestore = transition_capture->tcs_private->new_ins_tuplestore;
765 5483 [ + - + + ]: 177 : else if (event == TRIGGER_EVENT_UPDATE && update_new_table)
748 5484 : 174 : tuplestore = transition_capture->tcs_private->new_upd_tuplestore;
5485 : : }
5486 : :
765 5487 : 33042 : return tuplestore;
5488 : : }
5489 : :
5490 : : /*
5491 : : * Add the given heap tuple to the given tuplestore, applying the conversion
5492 : : * map if necessary.
5493 : : *
5494 : : * If original_insert_tuple is given, we can add that tuple without conversion.
5495 : : */
5496 : : static void
5497 : 33042 : TransitionTableAddTuple(EState *estate,
5498 : : TransitionCaptureState *transition_capture,
5499 : : ResultRelInfo *relinfo,
5500 : : TupleTableSlot *slot,
5501 : : TupleTableSlot *original_insert_tuple,
5502 : : Tuplestorestate *tuplestore)
5503 : : {
5504 : : TupleConversionMap *map;
5505 : :
5506 : : /*
5507 : : * Nothing needs to be done if we don't have a tuplestore.
5508 : : */
5509 [ + + ]: 33042 : if (tuplestore == NULL)
5510 : 15 : return;
5511 : :
5512 [ + + ]: 33027 : if (original_insert_tuple)
5513 : 63 : tuplestore_puttupleslot(tuplestore, original_insert_tuple);
5514 [ + + ]: 32964 : else if ((map = ExecGetChildToRootMap(relinfo)) != NULL)
5515 : : {
5516 : 147 : AfterTriggersTableData *table = transition_capture->tcs_private;
5517 : : TupleTableSlot *storeslot;
5518 : :
5519 : 147 : storeslot = GetAfterTriggersStoreSlot(table, map->outdesc);
5520 : 147 : execute_attr_map_slot(map->attrMap, slot, storeslot);
5521 : 147 : tuplestore_puttupleslot(tuplestore, storeslot);
5522 : : }
5523 : : else
5524 : 32817 : tuplestore_puttupleslot(tuplestore, slot);
5525 : : }
5526 : :
5527 : : /* ----------
5528 : : * AfterTriggerEnlargeQueryState()
5529 : : *
5530 : : * Prepare the necessary state so that we can record AFTER trigger events
5531 : : * queued by a query. It is allowed to have nested queries within a
5532 : : * (sub)transaction, so we need to have separate state for each query
5533 : : * nesting level.
5534 : : * ----------
5535 : : */
5536 : : static void
3461 rhaas@postgresql.org 5537 : 3430 : AfterTriggerEnlargeQueryState(void)
5538 : : {
3249 bruce@momjian.us 5539 : 3430 : int init_depth = afterTriggers.maxquerydepth;
5540 : :
3461 rhaas@postgresql.org 5541 [ - + ]: 3430 : Assert(afterTriggers.query_depth >= afterTriggers.maxquerydepth);
5542 : :
5543 [ + - ]: 3430 : if (afterTriggers.maxquerydepth == 0)
5544 : : {
3460 5545 : 3430 : int new_alloc = Max(afterTriggers.query_depth + 1, 8);
5546 : :
2402 tgl@sss.pgh.pa.us 5547 : 3430 : afterTriggers.query_stack = (AfterTriggersQueryData *)
3461 rhaas@postgresql.org 5548 : 3430 : MemoryContextAlloc(TopTransactionContext,
5549 : : new_alloc * sizeof(AfterTriggersQueryData));
5550 : 3430 : afterTriggers.maxquerydepth = new_alloc;
5551 : : }
5552 : : else
5553 : : {
5554 : : /* repalloc will keep the stack in the same context */
3461 rhaas@postgresql.org 5555 :UBC 0 : int old_alloc = afterTriggers.maxquerydepth;
3460 5556 : 0 : int new_alloc = Max(afterTriggers.query_depth + 1,
5557 : : old_alloc * 2);
5558 : :
2402 tgl@sss.pgh.pa.us 5559 : 0 : afterTriggers.query_stack = (AfterTriggersQueryData *)
3461 rhaas@postgresql.org 5560 : 0 : repalloc(afterTriggers.query_stack,
5561 : : new_alloc * sizeof(AfterTriggersQueryData));
5562 : 0 : afterTriggers.maxquerydepth = new_alloc;
5563 : : }
5564 : :
5565 : : /* Initialize new array entries to empty */
3461 rhaas@postgresql.org 5566 [ + + ]:CBC 30870 : while (init_depth < afterTriggers.maxquerydepth)
5567 : : {
2402 tgl@sss.pgh.pa.us 5568 : 27440 : AfterTriggersQueryData *qs = &afterTriggers.query_stack[init_depth];
5569 : :
5570 : 27440 : qs->events.head = NULL;
5571 : 27440 : qs->events.tail = NULL;
5572 : 27440 : qs->events.tailfree = NULL;
5573 : 27440 : qs->fdw_tuplestore = NULL;
5574 : 27440 : qs->tables = NIL;
5575 : :
3461 rhaas@postgresql.org 5576 : 27440 : ++init_depth;
5577 : : }
5578 : 3430 : }
5579 : :
5580 : : /*
5581 : : * Create an empty SetConstraintState with room for numalloc trigstates
5582 : : */
5583 : : static SetConstraintState
7156 tgl@sss.pgh.pa.us 5584 : 48 : SetConstraintStateCreate(int numalloc)
5585 : : {
5586 : : SetConstraintState state;
5587 : :
5588 : : /* Behave sanely with numalloc == 0 */
7227 5589 [ + + ]: 48 : if (numalloc <= 0)
5590 : 5 : numalloc = 1;
5591 : :
5592 : : /*
5593 : : * We assume that zeroing will correctly initialize the state values.
5594 : : */
5595 : : state = (SetConstraintState)
5596 : 48 : MemoryContextAllocZero(TopTransactionContext,
5597 : : offsetof(SetConstraintStateData, trigstates) +
3249 bruce@momjian.us 5598 : 48 : numalloc * sizeof(SetConstraintTriggerData));
5599 : :
7227 tgl@sss.pgh.pa.us 5600 : 48 : state->numalloc = numalloc;
5601 : :
5602 : 48 : return state;
5603 : : }
5604 : :
5605 : : /*
5606 : : * Copy a SetConstraintState
5607 : : */
5608 : : static SetConstraintState
7156 5609 : 5 : SetConstraintStateCopy(SetConstraintState origstate)
5610 : : {
5611 : : SetConstraintState state;
5612 : :
5613 : 5 : state = SetConstraintStateCreate(origstate->numstates);
5614 : :
7227 5615 : 5 : state->all_isset = origstate->all_isset;
5616 : 5 : state->all_isdeferred = origstate->all_isdeferred;
5617 : 5 : state->numstates = origstate->numstates;
5618 : 5 : memcpy(state->trigstates, origstate->trigstates,
7156 5619 : 5 : origstate->numstates * sizeof(SetConstraintTriggerData));
5620 : :
7227 5621 : 5 : return state;
5622 : : }
5623 : :
5624 : : /*
5625 : : * Add a per-trigger item to a SetConstraintState. Returns possibly-changed
5626 : : * pointer to the state object (it will change if we have to repalloc).
5627 : : */
5628 : : static SetConstraintState
7156 5629 : 171 : SetConstraintStateAddItem(SetConstraintState state,
5630 : : Oid tgoid, bool tgisdeferred)
5631 : : {
7227 5632 [ + + ]: 171 : if (state->numstates >= state->numalloc)
5633 : : {
7168 bruce@momjian.us 5634 : 15 : int newalloc = state->numalloc * 2;
5635 : :
5636 : 15 : newalloc = Max(newalloc, 8); /* in case original has size 0 */
5637 : : state = (SetConstraintState)
7227 tgl@sss.pgh.pa.us 5638 : 15 : repalloc(state,
5639 : : offsetof(SetConstraintStateData, trigstates) +
3341 5640 : 15 : newalloc * sizeof(SetConstraintTriggerData));
7227 5641 : 15 : state->numalloc = newalloc;
5642 [ - + ]: 15 : Assert(state->numstates < state->numalloc);
5643 : : }
5644 : :
7156 5645 : 171 : state->trigstates[state->numstates].sct_tgoid = tgoid;
5646 : 171 : state->trigstates[state->numstates].sct_tgisdeferred = tgisdeferred;
7227 5647 : 171 : state->numstates++;
5648 : :
5649 : 171 : return state;
5650 : : }
5651 : :
5652 : : /* ----------
5653 : : * AfterTriggerSetState()
5654 : : *
5655 : : * Execute the SET CONSTRAINTS ... utility command.
5656 : : * ----------
5657 : : */
5658 : : void
7156 5659 : 51 : AfterTriggerSetState(ConstraintsSetStmt *stmt)
5660 : : {
7160 5661 : 51 : int my_level = GetCurrentTransactionNestLevel();
5662 : :
5663 : : /* If we haven't already done so, initialize our state. */
3461 rhaas@postgresql.org 5664 [ + + ]: 51 : if (afterTriggers.state == NULL)
5665 : 43 : afterTriggers.state = SetConstraintStateCreate(8);
5666 : :
5667 : : /*
5668 : : * If in a subtransaction, and we didn't save the current state already,
5669 : : * save it so it can be restored if the subtransaction aborts.
5670 : : */
7160 tgl@sss.pgh.pa.us 5671 [ + + ]: 51 : if (my_level > 1 &&
2402 5672 [ + - ]: 5 : afterTriggers.trans_stack[my_level].state == NULL)
5673 : : {
5674 : 5 : afterTriggers.trans_stack[my_level].state =
3461 rhaas@postgresql.org 5675 : 5 : SetConstraintStateCopy(afterTriggers.state);
5676 : : }
5677 : :
5678 : : /*
5679 : : * Handle SET CONSTRAINTS ALL ...
5680 : : */
8768 bruce@momjian.us 5681 [ + + ]: 51 : if (stmt->constraints == NIL)
5682 : : {
5683 : : /*
5684 : : * Forget any previous SET CONSTRAINTS commands in this transaction.
5685 : : */
3461 rhaas@postgresql.org 5686 : 27 : afterTriggers.state->numstates = 0;
5687 : :
5688 : : /*
5689 : : * Set the per-transaction ALL state to known.
5690 : : */
5691 : 27 : afterTriggers.state->all_isset = true;
5692 : 27 : afterTriggers.state->all_isdeferred = stmt->deferred;
5693 : : }
5694 : : else
5695 : : {
5696 : : Relation conrel;
5697 : : Relation tgrel;
5201 tgl@sss.pgh.pa.us 5698 : 24 : List *conoidlist = NIL;
5699 : 24 : List *tgoidlist = NIL;
5700 : : ListCell *lc;
5701 : :
5702 : : /*
5703 : : * Handle SET CONSTRAINTS constraint-name [, ...]
5704 : : *
5705 : : * First, identify all the named constraints and make a list of their
5706 : : * OIDs. Since, unlike the SQL spec, we allow multiple constraints of
5707 : : * the same name within a schema, the specifications are not
5708 : : * necessarily unique. Our strategy is to target all matching
5709 : : * constraints within the first search-path schema that has any
5710 : : * matches, but disregard matches in schemas beyond the first match.
5711 : : * (This is a bit odd but it's the historical behavior.)
5712 : : *
5713 : : * A constraint in a partitioned table may have corresponding
5714 : : * constraints in the partitions. Grab those too.
5715 : : */
1910 andres@anarazel.de 5716 : 24 : conrel = table_open(ConstraintRelationId, AccessShareLock);
5717 : :
5201 tgl@sss.pgh.pa.us 5718 [ + - + + : 48 : foreach(lc, stmt->constraints)
+ + ]
5719 : : {
5720 : 24 : RangeVar *constraint = lfirst(lc);
5721 : : bool found;
5722 : : List *namespacelist;
5723 : : ListCell *nslc;
5724 : :
6562 bruce@momjian.us 5725 [ - + ]: 24 : if (constraint->catalogname)
5726 : : {
6562 bruce@momjian.us 5727 [ # # ]:UBC 0 : if (strcmp(constraint->catalogname, get_database_name(MyDatabaseId)) != 0)
5728 [ # # ]: 0 : ereport(ERROR,
5729 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5730 : : errmsg("cross-database references are not implemented: \"%s.%s.%s\"",
5731 : : constraint->catalogname, constraint->schemaname,
5732 : : constraint->relname)));
5733 : : }
5734 : :
5735 : : /*
5736 : : * If we're given the schema name with the constraint, look only
5737 : : * in that schema. If given a bare constraint name, use the
5738 : : * search path to find the first matching constraint.
5739 : : */
6402 bruce@momjian.us 5740 [ + + ]:CBC 24 : if (constraint->schemaname)
5741 : : {
4096 5742 : 6 : Oid namespaceId = LookupExplicitNamespace(constraint->schemaname,
5743 : : false);
5744 : :
5201 tgl@sss.pgh.pa.us 5745 : 6 : namespacelist = list_make1_oid(namespaceId);
5746 : : }
5747 : : else
5748 : : {
5749 : 18 : namespacelist = fetch_search_path(true);
5750 : : }
5751 : :
8964 JanWieck@Yahoo.com 5752 : 24 : found = false;
5201 tgl@sss.pgh.pa.us 5753 [ + - + - : 60 : foreach(nslc, namespacelist)
+ - ]
5754 : : {
5755 : 60 : Oid namespaceId = lfirst_oid(nslc);
5756 : : SysScanDesc conscan;
5757 : : ScanKeyData skey[2];
5758 : : HeapTuple tup;
5759 : :
5760 : 60 : ScanKeyInit(&skey[0],
5761 : : Anum_pg_constraint_conname,
5762 : : BTEqualStrategyNumber, F_NAMEEQ,
5763 : 60 : CStringGetDatum(constraint->relname));
5764 : 60 : ScanKeyInit(&skey[1],
5765 : : Anum_pg_constraint_connamespace,
5766 : : BTEqualStrategyNumber, F_OIDEQ,
5767 : : ObjectIdGetDatum(namespaceId));
5768 : :
5769 : 60 : conscan = systable_beginscan(conrel, ConstraintNameNspIndexId,
5770 : : true, NULL, 2, skey);
5771 : :
5772 [ + + ]: 108 : while (HeapTupleIsValid(tup = systable_getnext(conscan)))
5773 : : {
5774 : 48 : Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tup);
5775 : :
5776 [ + - ]: 48 : if (con->condeferrable)
1972 andres@anarazel.de 5777 : 48 : conoidlist = lappend_oid(conoidlist, con->oid);
5201 tgl@sss.pgh.pa.us 5778 [ # # ]:UBC 0 : else if (stmt->deferred)
5779 [ # # ]: 0 : ereport(ERROR,
5780 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
5781 : : errmsg("constraint \"%s\" is not deferrable",
5782 : : constraint->relname)));
6562 bruce@momjian.us 5783 :CBC 48 : found = true;
5784 : : }
5785 : :
5201 tgl@sss.pgh.pa.us 5786 : 60 : systable_endscan(conscan);
5787 : :
5788 : : /*
5789 : : * Once we've found a matching constraint we do not search
5790 : : * later parts of the search path.
5791 : : */
6562 bruce@momjian.us 5792 [ + + ]: 60 : if (found)
5793 : 24 : break;
5794 : : }
5795 : :
5201 tgl@sss.pgh.pa.us 5796 : 24 : list_free(namespacelist);
5797 : :
5798 : : /*
5799 : : * Not found ?
5800 : : */
7911 bruce@momjian.us 5801 [ - + ]: 24 : if (!found)
7574 tgl@sss.pgh.pa.us 5802 [ # # ]:UBC 0 : ereport(ERROR,
5803 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5804 : : errmsg("constraint \"%s\" does not exist",
5805 : : constraint->relname)));
5806 : : }
5807 : :
5808 : : /*
5809 : : * Scan for any possible descendants of the constraints. We append
5810 : : * whatever we find to the same list that we're scanning; this has the
5811 : : * effect that we create new scans for those, too, so if there are
5812 : : * further descendents, we'll also catch them.
5813 : : */
2214 alvherre@alvh.no-ip. 5814 [ + - + + :CBC 129 : foreach(lc, conoidlist)
+ + ]
5815 : : {
5816 : 105 : Oid parent = lfirst_oid(lc);
5817 : : ScanKeyData key;
5818 : : SysScanDesc scan;
5819 : : HeapTuple tuple;
5820 : :
5821 : 105 : ScanKeyInit(&key,
5822 : : Anum_pg_constraint_conparentid,
5823 : : BTEqualStrategyNumber, F_OIDEQ,
5824 : : ObjectIdGetDatum(parent));
5825 : :
5826 : 105 : scan = systable_beginscan(conrel, ConstraintParentIndexId, true, NULL, 1, &key);
5827 : :
5828 [ + + ]: 162 : while (HeapTupleIsValid(tuple = systable_getnext(scan)))
5829 : : {
1972 andres@anarazel.de 5830 : 57 : Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple);
5831 : :
5832 : 57 : conoidlist = lappend_oid(conoidlist, con->oid);
5833 : : }
5834 : :
2214 alvherre@alvh.no-ip. 5835 : 105 : systable_endscan(scan);
5836 : : }
5837 : :
1910 andres@anarazel.de 5838 : 24 : table_close(conrel, AccessShareLock);
5839 : :
5840 : : /*
5841 : : * Now, locate the trigger(s) implementing each of these constraints,
5842 : : * and make a list of their OIDs.
5843 : : */
5844 : 24 : tgrel = table_open(TriggerRelationId, AccessShareLock);
5845 : :
5201 tgl@sss.pgh.pa.us 5846 [ + - + + : 129 : foreach(lc, conoidlist)
+ + ]
5847 : : {
5848 : 105 : Oid conoid = lfirst_oid(lc);
5849 : : ScanKeyData skey;
5850 : : SysScanDesc tgscan;
5851 : : HeapTuple htup;
5852 : :
5853 : 105 : ScanKeyInit(&skey,
5854 : : Anum_pg_trigger_tgconstraint,
5855 : : BTEqualStrategyNumber, F_OIDEQ,
5856 : : ObjectIdGetDatum(conoid));
5857 : :
5858 : 105 : tgscan = systable_beginscan(tgrel, TriggerConstraintIndexId, true,
5859 : : NULL, 1, &skey);
5860 : :
5861 [ + + ]: 429 : while (HeapTupleIsValid(htup = systable_getnext(tgscan)))
5862 : : {
5863 : 219 : Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(htup);
5864 : :
5865 : : /*
5866 : : * Silently skip triggers that are marked as non-deferrable in
5867 : : * pg_trigger. This is not an error condition, since a
5868 : : * deferrable RI constraint may have some non-deferrable
5869 : : * actions.
5870 : : */
5871 [ + - ]: 219 : if (pg_trigger->tgdeferrable)
1972 andres@anarazel.de 5872 : 219 : tgoidlist = lappend_oid(tgoidlist, pg_trigger->oid);
5873 : : }
5874 : :
5201 tgl@sss.pgh.pa.us 5875 : 105 : systable_endscan(tgscan);
5876 : : }
5877 : :
1910 andres@anarazel.de 5878 : 24 : table_close(tgrel, AccessShareLock);
5879 : :
5880 : : /*
5881 : : * Now we can set the trigger states of individual triggers for this
5882 : : * xact.
5883 : : */
5201 tgl@sss.pgh.pa.us 5884 [ + - + + : 243 : foreach(lc, tgoidlist)
+ + ]
5885 : : {
5886 : 219 : Oid tgoid = lfirst_oid(lc);
3461 rhaas@postgresql.org 5887 : 219 : SetConstraintState state = afterTriggers.state;
7227 tgl@sss.pgh.pa.us 5888 : 219 : bool found = false;
5889 : : int i;
5890 : :
7158 5891 [ + + ]: 1224 : for (i = 0; i < state->numstates; i++)
5892 : : {
7156 5893 [ + + ]: 1053 : if (state->trigstates[i].sct_tgoid == tgoid)
5894 : : {
5895 : 48 : state->trigstates[i].sct_tgisdeferred = stmt->deferred;
8964 JanWieck@Yahoo.com 5896 : 48 : found = true;
5897 : 48 : break;
5898 : : }
5899 : : }
5900 [ + + ]: 219 : if (!found)
5901 : : {
3461 rhaas@postgresql.org 5902 : 171 : afterTriggers.state =
7156 tgl@sss.pgh.pa.us 5903 : 171 : SetConstraintStateAddItem(state, tgoid, stmt->deferred);
5904 : : }
5905 : : }
5906 : : }
5907 : :
5908 : : /*
5909 : : * SQL99 requires that when a constraint is set to IMMEDIATE, any deferred
5910 : : * checks against that constraint must be made when the SET CONSTRAINTS
5911 : : * command is executed -- i.e. the effects of the SET CONSTRAINTS command
5912 : : * apply retroactively. We've updated the constraints state, so scan the
5913 : : * list of previously deferred events to fire any that have now become
5914 : : * immediate.
5915 : : *
5916 : : * Obviously, if this was SET ... DEFERRED then it can't have converted
5917 : : * any unfired events to immediate, so we need do nothing in that case.
5918 : : */
5919 [ + + ]: 51 : if (!stmt->deferred)
5920 : : {
3461 rhaas@postgresql.org 5921 : 17 : AfterTriggerEventList *events = &afterTriggers.events;
5601 tgl@sss.pgh.pa.us 5922 : 17 : bool snapshot_set = false;
5923 : :
6087 5924 [ + + ]: 17 : while (afterTriggerMarkEvents(events, NULL, true))
5925 : : {
3461 rhaas@postgresql.org 5926 : 8 : CommandId firing_id = afterTriggers.firing_counter++;
5927 : :
5928 : : /*
5929 : : * Make sure a snapshot has been established in case trigger
5930 : : * functions need one. Note that we avoid setting a snapshot if
5931 : : * we don't find at least one trigger that has to be fired now.
5932 : : * This is so that BEGIN; SET CONSTRAINTS ...; SET TRANSACTION
5933 : : * ISOLATION LEVEL SERIALIZABLE; ... works properly. (If we are
5934 : : * at the start of a transaction it's not possible for any trigger
5935 : : * events to be queued yet.)
5936 : : */
5601 tgl@sss.pgh.pa.us 5937 [ + - ]: 8 : if (!snapshot_set)
5938 : : {
5939 : 8 : PushActiveSnapshot(GetTransactionSnapshot());
5940 : 8 : snapshot_set = true;
5941 : : }
5942 : :
5943 : : /*
5944 : : * We can delete fired events if we are at top transaction level,
5945 : : * but we'd better not if inside a subtransaction, since the
5946 : : * subtransaction could later get rolled back.
5947 : : */
5651 tgl@sss.pgh.pa.us 5948 [ # # ]:UBC 0 : if (afterTriggerInvokeEvents(events, firing_id, NULL,
5651 tgl@sss.pgh.pa.us 5949 :CBC 8 : !IsSubTransaction()))
5651 tgl@sss.pgh.pa.us 5950 :UBC 0 : break; /* all fired */
5951 : : }
5952 : :
5601 tgl@sss.pgh.pa.us 5953 [ - + ]:CBC 9 : if (snapshot_set)
5601 tgl@sss.pgh.pa.us 5954 :UBC 0 : PopActiveSnapshot();
5955 : : }
8964 JanWieck@Yahoo.com 5956 :CBC 43 : }
5957 : :
5958 : : /* ----------
5959 : : * AfterTriggerPendingOnRel()
5960 : : * Test to see if there are any pending after-trigger events for rel.
5961 : : *
5962 : : * This is used by TRUNCATE, CLUSTER, ALTER TABLE, etc to detect whether
5963 : : * it is unsafe to perform major surgery on a relation. Note that only
5964 : : * local pending events are examined. We assume that having exclusive lock
5965 : : * on a rel guarantees there are no unserviced events in other backends ---
5966 : : * but having a lock does not prevent there being such events in our own.
5967 : : *
5968 : : * In some scenarios it'd be reasonable to remove pending events (more
5969 : : * specifically, mark them DONE by the current subxact) but without a lot
5970 : : * of knowledge of the trigger semantics we can't do this in general.
5971 : : * ----------
5972 : : */
5973 : : bool
5947 tgl@sss.pgh.pa.us 5974 : 52803 : AfterTriggerPendingOnRel(Oid relid)
5975 : : {
5976 : : AfterTriggerEvent event;
5977 : : AfterTriggerEventChunk *chunk;
5978 : : int depth;
5979 : :
5980 : : /* Scan queued events */
3461 rhaas@postgresql.org 5981 [ + - + - : 52815 : for_each_event_chunk(event, chunk, afterTriggers.events)
+ - + + +
+ ]
5982 : : {
5651 tgl@sss.pgh.pa.us 5983 : 15 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
5984 : :
5985 : : /*
5986 : : * We can ignore completed events. (Even if a DONE flag is rolled
5987 : : * back by subxact abort, it's OK because the effects of the TRUNCATE
5988 : : * or whatever must get rolled back too.)
5989 : : */
5990 [ - + ]: 15 : if (event->ate_flags & AFTER_TRIGGER_DONE)
6432 tgl@sss.pgh.pa.us 5991 :UBC 0 : continue;
5992 : :
5651 tgl@sss.pgh.pa.us 5993 [ + + ]:CBC 15 : if (evtshared->ats_relid == relid)
5947 5994 : 9 : return true;
5995 : : }
5996 : :
5997 : : /*
5998 : : * Also scan events queued by incomplete queries. This could only matter
5999 : : * if TRUNCATE/etc is executed by a function or trigger within an updating
6000 : : * query on the same relation, which is pretty perverse, but let's check.
6001 : : */
3443 rhaas@postgresql.org 6002 [ - + - - ]: 52794 : for (depth = 0; depth <= afterTriggers.query_depth && depth < afterTriggers.maxquerydepth; depth++)
6003 : : {
2402 tgl@sss.pgh.pa.us 6004 [ # # # # :UBC 0 : for_each_event_chunk(event, chunk, afterTriggers.query_stack[depth].events)
# # # # #
# ]
6005 : : {
5651 6006 : 0 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
6007 : :
6008 [ # # ]: 0 : if (event->ate_flags & AFTER_TRIGGER_DONE)
6432 6009 : 0 : continue;
6010 : :
5651 6011 [ # # ]: 0 : if (evtshared->ats_relid == relid)
5947 6012 : 0 : return true;
6013 : : }
6014 : : }
6015 : :
5947 tgl@sss.pgh.pa.us 6016 :CBC 52794 : return false;
6017 : : }
6018 : :
6019 : : /* ----------
6020 : : * AfterTriggerSaveEvent()
6021 : : *
6022 : : * Called by ExecA[RS]...Triggers() to queue up the triggers that should
6023 : : * be fired for an event.
6024 : : *
6025 : : * NOTE: this is called whenever there are any triggers associated with
6026 : : * the event (even if they are disabled). This function decides which
6027 : : * triggers actually need to be queued. It is also called after each row,
6028 : : * even if there are no triggers for that event, if there are any AFTER
6029 : : * STATEMENT triggers for the statement which use transition tables, so that
6030 : : * the transition tuplestores can be built. Furthermore, if the transition
6031 : : * capture is happening for UPDATEd rows being moved to another partition due
6032 : : * to the partition-key being changed, then this function is called once when
6033 : : * the row is deleted (to capture OLD row), and once when the row is inserted
6034 : : * into another partition (to capture NEW row). This is done separately because
6035 : : * DELETE and INSERT happen on different tables.
6036 : : *
6037 : : * Transition tuplestores are built now, rather than when events are pulled
6038 : : * off of the queue because AFTER ROW triggers are allowed to select from the
6039 : : * transition tables for the statement.
6040 : : *
6041 : : * This contains special support to queue the update events for the case where
6042 : : * a partitioned table undergoing a cross-partition update may have foreign
6043 : : * keys pointing into it. Normally, a partitioned table's row triggers are
6044 : : * not fired because the leaf partition(s) which are modified as a result of
6045 : : * the operation on the partitioned table contain the same triggers which are
6046 : : * fired instead. But that general scheme can cause problematic behavior with
6047 : : * foreign key triggers during cross-partition updates, which are implemented
6048 : : * as DELETE on the source partition followed by INSERT into the destination
6049 : : * partition. Specifically, firing DELETE triggers would lead to the wrong
6050 : : * foreign key action to be enforced considering that the original command is
6051 : : * UPDATE; in this case, this function is called with relinfo as the
6052 : : * partitioned table, and src_partinfo and dst_partinfo referring to the
6053 : : * source and target leaf partitions, respectively.
6054 : : *
6055 : : * is_crosspart_update is true either when a DELETE event is fired on the
6056 : : * source partition (which is to be ignored) or an UPDATE event is fired on
6057 : : * the root partitioned table.
6058 : : * ----------
6059 : : */
6060 : : static void
5259 6061 : 38198 : AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
6062 : : ResultRelInfo *src_partinfo,
6063 : : ResultRelInfo *dst_partinfo,
6064 : : int event, bool row_trigger,
6065 : : TupleTableSlot *oldslot, TupleTableSlot *newslot,
6066 : : List *recheckIndexes, Bitmapset *modifiedCols,
6067 : : TransitionCaptureState *transition_capture,
6068 : : bool is_crosspart_update)
6069 : : {
8353 6070 : 38198 : Relation rel = relinfo->ri_RelationDesc;
6071 : 38198 : TriggerDesc *trigdesc = relinfo->ri_TrigDesc;
6072 : : AfterTriggerEventData new_event;
6073 : : AfterTriggerSharedData new_shared;
2402 6074 : 38198 : char relkind = rel->rd_rel->relkind;
6075 : : int tgtype_event;
6076 : : int tgtype_level;
6077 : : int i;
3675 noah@leadboat.com 6078 : 38198 : Tuplestorestate *fdw_tuplestore = NULL;
6079 : :
6080 : : /*
6081 : : * Check state. We use a normal test not Assert because it is possible to
6082 : : * reach here in the wrong state given misconfigured RI triggers, in
6083 : : * particular deferring a cascade action trigger.
6084 : : */
3461 rhaas@postgresql.org 6085 [ - + ]: 38198 : if (afterTriggers.query_depth < 0)
5283 tgl@sss.pgh.pa.us 6086 [ # # ]:UBC 0 : elog(ERROR, "AfterTriggerSaveEvent() called outside of query");
6087 : :
6088 : : /* Be sure we have enough space to record events at this query depth. */
3461 rhaas@postgresql.org 6089 [ + + ]:CBC 38198 : if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
6090 : 3058 : AfterTriggerEnlargeQueryState();
6091 : :
6092 : : /*
6093 : : * If the directly named relation has any triggers with transition tables,
6094 : : * then we need to capture transition tuples.
6095 : : */
2482 rhodiumtoad@postgres 6096 [ + + + + ]: 38198 : if (row_trigger && transition_capture != NULL)
6097 : : {
748 alvherre@alvh.no-ip. 6098 : 32886 : TupleTableSlot *original_insert_tuple = transition_capture->tcs_original_insert_tuple;
6099 : :
6100 : : /*
6101 : : * Capture the old tuple in the appropriate transition table based on
6102 : : * the event.
6103 : : */
765 6104 [ + + + + ]: 32886 : if (!TupIsNull(oldslot))
6105 : : {
6106 : : Tuplestorestate *old_tuplestore;
6107 : :
6108 : 2697 : old_tuplestore = GetAfterTriggersTransitionTable(event,
6109 : : oldslot,
6110 : : NULL,
6111 : : transition_capture);
6112 : 2697 : TransitionTableAddTuple(estate, transition_capture, relinfo,
6113 : : oldslot, NULL, old_tuplestore);
6114 : : }
6115 : :
6116 : : /*
6117 : : * Capture the new tuple in the appropriate transition table based on
6118 : : * the event.
6119 : : */
6120 [ + + + - ]: 32886 : if (!TupIsNull(newslot))
6121 : : {
6122 : : Tuplestorestate *new_tuplestore;
6123 : :
6124 : 30345 : new_tuplestore = GetAfterTriggersTransitionTable(event,
6125 : : NULL,
6126 : : newslot,
6127 : : transition_capture);
6128 : 30345 : TransitionTableAddTuple(estate, transition_capture, relinfo,
6129 : : newslot, original_insert_tuple, new_tuplestore);
6130 : : }
6131 : :
6132 : : /*
6133 : : * If transition tables are the only reason we're here, return. As
6134 : : * mentioned above, we can also be here during update tuple routing in
6135 : : * presence of transition tables, in which case this function is
6136 : : * called separately for OLD and NEW, so we expect exactly one of them
6137 : : * to be NULL.
6138 : : */
2482 rhodiumtoad@postgres 6139 [ + + + + ]: 32886 : if (trigdesc == NULL ||
6140 [ + + + + ]: 32784 : (event == TRIGGER_EVENT_DELETE && !trigdesc->trig_delete_after_row) ||
2489 tgl@sss.pgh.pa.us 6141 [ + + + + ]: 30294 : (event == TRIGGER_EVENT_INSERT && !trigdesc->trig_insert_after_row) ||
2277 rhaas@postgresql.org 6142 [ + + + + ]: 177 : (event == TRIGGER_EVENT_UPDATE && !trigdesc->trig_update_after_row) ||
1874 andres@anarazel.de 6143 [ + - - + : 18 : (event == TRIGGER_EVENT_UPDATE && (TupIsNull(oldslot) ^ TupIsNull(newslot))))
+ - - + -
+ ]
2718 kgrittn@postgresql.o 6144 : 32829 : return;
6145 : : }
6146 : :
6147 : : /*
6148 : : * We normally don't see partitioned tables here for row level triggers
6149 : : * except in the special case of a cross-partition update. In that case,
6150 : : * nodeModifyTable.c:ExecCrossPartitionUpdateForeignKey() calls here to
6151 : : * queue an update event on the root target partitioned table, also
6152 : : * passing the source and destination partitions and their tuples.
6153 : : */
756 alvherre@alvh.no-ip. 6154 [ + + + + : 5369 : Assert(!row_trigger ||
+ - + - +
- - + ]
6155 : : rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE ||
6156 : : (is_crosspart_update &&
6157 : : TRIGGER_FIRED_BY_UPDATE(event) &&
6158 : : src_partinfo != NULL && dst_partinfo != NULL));
6159 : :
6160 : : /*
6161 : : * Validate the event code and collect the associated tuple CTIDs.
6162 : : *
6163 : : * The event code will be used both as a bitmask and an array offset, so
6164 : : * validation is important to make sure we don't walk off the edge of our
6165 : : * arrays.
6166 : : *
6167 : : * Also, if we're considering statement-level triggers, check whether we
6168 : : * already queued a set of them for this event, and cancel the prior set
6169 : : * if so. This preserves the behavior that statement-level triggers fire
6170 : : * just once per statement and fire after row-level triggers.
6171 : : */
5651 tgl@sss.pgh.pa.us 6172 [ + + + + : 5369 : switch (event)
- ]
6173 : : {
6174 : 2844 : case TRIGGER_EVENT_INSERT:
4935 6175 : 2844 : tgtype_event = TRIGGER_TYPE_INSERT;
5651 6176 [ + + ]: 2844 : if (row_trigger)
6177 : : {
1874 andres@anarazel.de 6178 [ - + ]: 2620 : Assert(oldslot == NULL);
6179 [ - + ]: 2620 : Assert(newslot != NULL);
6180 : 2620 : ItemPointerCopy(&(newslot->tts_tid), &(new_event.ate_ctid1));
5651 tgl@sss.pgh.pa.us 6181 : 2620 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
6182 : : }
6183 : : else
6184 : : {
1874 andres@anarazel.de 6185 [ - + ]: 224 : Assert(oldslot == NULL);
6186 [ - + ]: 224 : Assert(newslot == NULL);
5651 tgl@sss.pgh.pa.us 6187 : 224 : ItemPointerSetInvalid(&(new_event.ate_ctid1));
6188 : 224 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
2402 6189 : 224 : cancel_prior_stmt_triggers(RelationGetRelid(rel),
6190 : : CMD_INSERT, event);
6191 : : }
5651 6192 : 2844 : break;
6193 : 710 : case TRIGGER_EVENT_DELETE:
4935 6194 : 710 : tgtype_event = TRIGGER_TYPE_DELETE;
5651 6195 [ + + ]: 710 : if (row_trigger)
6196 : : {
1874 andres@anarazel.de 6197 [ - + ]: 595 : Assert(oldslot != NULL);
6198 [ - + ]: 595 : Assert(newslot == NULL);
6199 : 595 : ItemPointerCopy(&(oldslot->tts_tid), &(new_event.ate_ctid1));
5651 tgl@sss.pgh.pa.us 6200 : 595 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
6201 : : }
6202 : : else
6203 : : {
1874 andres@anarazel.de 6204 [ - + ]: 115 : Assert(oldslot == NULL);
6205 [ - + ]: 115 : Assert(newslot == NULL);
5651 tgl@sss.pgh.pa.us 6206 : 115 : ItemPointerSetInvalid(&(new_event.ate_ctid1));
6207 : 115 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
2402 6208 : 115 : cancel_prior_stmt_triggers(RelationGetRelid(rel),
6209 : : CMD_DELETE, event);
6210 : : }
5651 6211 : 710 : break;
6212 : 1811 : case TRIGGER_EVENT_UPDATE:
4935 6213 : 1811 : tgtype_event = TRIGGER_TYPE_UPDATE;
5651 6214 [ + + ]: 1811 : if (row_trigger)
6215 : : {
1874 andres@anarazel.de 6216 [ - + ]: 1613 : Assert(oldslot != NULL);
6217 [ - + ]: 1613 : Assert(newslot != NULL);
6218 : 1613 : ItemPointerCopy(&(oldslot->tts_tid), &(new_event.ate_ctid1));
6219 : 1613 : ItemPointerCopy(&(newslot->tts_tid), &(new_event.ate_ctid2));
6220 : :
6221 : : /*
6222 : : * Also remember the OIDs of partitions to fetch these tuples
6223 : : * out of later in AfterTriggerExecute().
6224 : : */
756 alvherre@alvh.no-ip. 6225 [ + + ]: 1613 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
6226 : : {
6227 [ + - - + ]: 129 : Assert(src_partinfo != NULL && dst_partinfo != NULL);
6228 : 129 : new_event.ate_src_part =
6229 : 129 : RelationGetRelid(src_partinfo->ri_RelationDesc);
6230 : 129 : new_event.ate_dst_part =
6231 : 129 : RelationGetRelid(dst_partinfo->ri_RelationDesc);
6232 : : }
6233 : : }
6234 : : else
6235 : : {
1874 andres@anarazel.de 6236 [ - + ]: 198 : Assert(oldslot == NULL);
6237 [ - + ]: 198 : Assert(newslot == NULL);
5651 tgl@sss.pgh.pa.us 6238 : 198 : ItemPointerSetInvalid(&(new_event.ate_ctid1));
6239 : 198 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
2402 6240 : 198 : cancel_prior_stmt_triggers(RelationGetRelid(rel),
6241 : : CMD_UPDATE, event);
6242 : : }
5651 6243 : 1811 : break;
6244 : 4 : case TRIGGER_EVENT_TRUNCATE:
4935 6245 : 4 : tgtype_event = TRIGGER_TYPE_TRUNCATE;
1874 andres@anarazel.de 6246 [ - + ]: 4 : Assert(oldslot == NULL);
6247 [ - + ]: 4 : Assert(newslot == NULL);
5651 tgl@sss.pgh.pa.us 6248 : 4 : ItemPointerSetInvalid(&(new_event.ate_ctid1));
6249 : 4 : ItemPointerSetInvalid(&(new_event.ate_ctid2));
6250 : 4 : break;
5651 tgl@sss.pgh.pa.us 6251 :UBC 0 : default:
6252 [ # # ]: 0 : elog(ERROR, "invalid after-trigger event code: %d", event);
6253 : : tgtype_event = 0; /* keep compiler quiet */
6254 : : break;
6255 : : }
6256 : :
6257 : : /* Determine flags */
3675 noah@leadboat.com 6258 [ + + + + ]:CBC 5369 : if (!(relkind == RELKIND_FOREIGN_TABLE && row_trigger))
6259 : : {
756 alvherre@alvh.no-ip. 6260 [ + + + + ]: 5341 : if (row_trigger && event == TRIGGER_EVENT_UPDATE)
6261 : : {
6262 [ + + ]: 1603 : if (relkind == RELKIND_PARTITIONED_TABLE)
6263 : 129 : new_event.ate_flags = AFTER_TRIGGER_CP_UPDATE;
6264 : : else
6265 : 1474 : new_event.ate_flags = AFTER_TRIGGER_2CTID;
6266 : : }
6267 : : else
6268 : 3738 : new_event.ate_flags = AFTER_TRIGGER_1CTID;
6269 : : }
6270 : :
6271 : : /* else, we'll initialize ate_flags for each trigger */
6272 : :
4935 tgl@sss.pgh.pa.us 6273 : 5369 : tgtype_level = (row_trigger ? TRIGGER_TYPE_ROW : TRIGGER_TYPE_STATEMENT);
6274 : :
6275 : : /*
6276 : : * Must convert/copy the source and destination partition tuples into the
6277 : : * root partitioned table's format/slot, because the processing in the
6278 : : * loop below expects both oldslot and newslot tuples to be in that form.
6279 : : */
756 alvherre@alvh.no-ip. 6280 [ + + + + ]: 5369 : if (row_trigger && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
6281 : : {
6282 : : TupleTableSlot *rootslot;
6283 : : TupleConversionMap *map;
6284 : :
6285 : 129 : rootslot = ExecGetTriggerOldSlot(estate, relinfo);
6286 : 129 : map = ExecGetChildToRootMap(src_partinfo);
6287 [ + + ]: 129 : if (map)
6288 : 18 : oldslot = execute_attr_map_slot(map->attrMap,
6289 : : oldslot,
6290 : : rootslot);
6291 : : else
6292 : 111 : oldslot = ExecCopySlot(rootslot, oldslot);
6293 : :
6294 : 129 : rootslot = ExecGetTriggerNewSlot(estate, relinfo);
6295 : 129 : map = ExecGetChildToRootMap(dst_partinfo);
6296 [ + + ]: 129 : if (map)
6297 : 18 : newslot = execute_attr_map_slot(map->attrMap,
6298 : : newslot,
6299 : : rootslot);
6300 : : else
6301 : 111 : newslot = ExecCopySlot(rootslot, newslot);
6302 : : }
6303 : :
4935 tgl@sss.pgh.pa.us 6304 [ + + ]: 24636 : for (i = 0; i < trigdesc->numtriggers; i++)
6305 : : {
6306 : 19267 : Trigger *trigger = &trigdesc->triggers[i];
6307 : :
6308 [ + + ]: 19267 : if (!TRIGGER_TYPE_MATCHES(trigger->tgtype,
6309 : : tgtype_level,
6310 : : TRIGGER_TYPE_AFTER,
6311 : : tgtype_event))
6312 : 12416 : continue;
5259 6313 [ + + ]: 6851 : if (!TriggerEnabled(estate, relinfo, trigger, event,
6314 : : modifiedCols, oldslot, newslot))
5296 6315 : 184 : continue;
6316 : :
3675 noah@leadboat.com 6317 [ + + + + ]: 6667 : if (relkind == RELKIND_FOREIGN_TABLE && row_trigger)
6318 : : {
6319 [ + + ]: 29 : if (fdw_tuplestore == NULL)
6320 : : {
2402 tgl@sss.pgh.pa.us 6321 : 25 : fdw_tuplestore = GetCurrentFDWTuplestore();
3675 noah@leadboat.com 6322 : 25 : new_event.ate_flags = AFTER_TRIGGER_FDW_FETCH;
6323 : : }
6324 : : else
6325 : : /* subsequent event for the same tuple */
6326 : 4 : new_event.ate_flags = AFTER_TRIGGER_FDW_REUSE;
6327 : : }
6328 : :
6329 : : /*
6330 : : * If the trigger is a foreign key enforcement trigger, there are
6331 : : * certain cases where we can skip queueing the event because we can
6332 : : * tell by inspection that the FK constraint will still pass. There
6333 : : * are also some cases during cross-partition updates of a partitioned
6334 : : * table where queuing the event can be skipped.
6335 : : */
2096 peter_e@gmx.net 6336 [ + + + + ]: 6667 : if (TRIGGER_FIRED_BY_UPDATE(event) || TRIGGER_FIRED_BY_DELETE(event))
6337 : : {
6894 neilc@samurai.com 6338 [ + + + - ]: 3230 : switch (RI_FKey_trigger_type(trigger->tgfoid))
6339 : : {
6340 : 1277 : case RI_TRIGGER_PK:
6341 : :
6342 : : /*
6343 : : * For cross-partitioned updates of partitioned PK table,
6344 : : * skip the event fired by the component delete on the
6345 : : * source leaf partition unless the constraint originates
6346 : : * in the partition itself (!tgisclone), because the
6347 : : * update event that will be fired on the root
6348 : : * (partitioned) target table will be used to perform the
6349 : : * necessary foreign key enforcement action.
6350 : : */
756 alvherre@alvh.no-ip. 6351 [ + + ]: 1277 : if (is_crosspart_update &&
6352 [ + + ]: 249 : TRIGGER_FIRED_BY_DELETE(event) &&
6353 [ + + ]: 132 : trigger->tgisclone)
6354 : 123 : continue;
6355 : :
6356 : : /* Update or delete on trigger's PK table */
4317 tgl@sss.pgh.pa.us 6357 [ + + ]: 1154 : if (!RI_FKey_pk_upd_check_required(trigger, rel,
6358 : : oldslot, newslot))
6359 : : {
6360 : : /* skip queuing this event */
6894 neilc@samurai.com 6361 : 271 : continue;
6362 : : }
7156 tgl@sss.pgh.pa.us 6363 : 883 : break;
6364 : :
6894 neilc@samurai.com 6365 : 579 : case RI_TRIGGER_FK:
6366 : :
6367 : : /*
6368 : : * Update on trigger's FK table. We can skip the update
6369 : : * event fired on a partitioned table during a
6370 : : * cross-partition of that table, because the insert event
6371 : : * that is fired on the destination leaf partition would
6372 : : * suffice to perform the necessary foreign key check.
6373 : : * Moreover, RI_FKey_fk_upd_check_required() expects to be
6374 : : * passed a tuple that contains system attributes, most of
6375 : : * which are not present in the virtual slot belonging to
6376 : : * a partitioned table.
6377 : : */
756 alvherre@alvh.no-ip. 6378 [ + + ]: 579 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
6379 [ + + ]: 534 : !RI_FKey_fk_upd_check_required(trigger, rel,
6380 : : oldslot, newslot))
6381 : : {
6382 : : /* skip queuing this event */
6894 neilc@samurai.com 6383 : 343 : continue;
6384 : : }
7156 tgl@sss.pgh.pa.us 6385 : 236 : break;
6386 : :
6894 neilc@samurai.com 6387 : 1374 : case RI_TRIGGER_NONE:
6388 : :
6389 : : /*
6390 : : * Not an FK trigger. No need to queue the update event
6391 : : * fired during a cross-partitioned update of a
6392 : : * partitioned table, because the same row trigger must be
6393 : : * present in the leaf partition(s) that are affected as
6394 : : * part of this update and the events fired on them are
6395 : : * queued instead.
6396 : : */
756 alvherre@alvh.no-ip. 6397 [ + + ]: 1374 : if (row_trigger &&
6398 [ + + ]: 1043 : rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
6399 : 15 : continue;
6894 neilc@samurai.com 6400 : 1359 : break;
6401 : : }
6402 : : }
6403 : :
6404 : : /*
6405 : : * If the trigger is a deferred unique constraint check trigger, only
6406 : : * queue it if the unique constraint was potentially violated, which
6407 : : * we know from index insertion time.
6408 : : */
5373 tgl@sss.pgh.pa.us 6409 [ + + ]: 5915 : if (trigger->tgfoid == F_UNIQUE_KEY_RECHECK)
6410 : : {
6411 [ + + ]: 105 : if (!list_member_oid(recheckIndexes, trigger->tgconstrindid))
6412 : 44 : continue; /* Uniqueness definitely not violated */
6413 : : }
6414 : :
6415 : : /*
6416 : : * Fill in event structure and add it to the current query's queue.
6417 : : * Note we set ats_table to NULL whenever this trigger doesn't use
6418 : : * transition tables, to improve sharability of the shared event data.
6419 : : */
5651 6420 : 5871 : new_shared.ats_event =
7156 6421 : 11742 : (event & TRIGGER_EVENT_OPMASK) |
6422 [ + + ]: 5871 : (row_trigger ? TRIGGER_EVENT_ROW : 0) |
6423 [ + + ]: 5871 : (trigger->tgdeferrable ? AFTER_TRIGGER_DEFERRABLE : 0) |
6424 [ + + ]: 5871 : (trigger->tginitdeferred ? AFTER_TRIGGER_INITDEFERRED : 0);
5651 6425 : 5871 : new_shared.ats_tgoid = trigger->tgoid;
6426 : 5871 : new_shared.ats_relid = RelationGetRelid(rel);
6427 : 5871 : new_shared.ats_firing_id = 0;
2402 6428 [ + + + + : 5871 : if ((trigger->tgoldtable || trigger->tgnewtable) &&
+ - ]
6429 : : transition_capture != NULL)
6430 : 303 : new_shared.ats_table = transition_capture->tcs_private;
6431 : : else
6432 : 5568 : new_shared.ats_table = NULL;
287 tomas.vondra@postgre 6433 : 5871 : new_shared.ats_modifiedcols = afterTriggerCopyBitmap(modifiedCols);
6434 : :
2402 tgl@sss.pgh.pa.us 6435 : 5871 : afterTriggerAddEvent(&afterTriggers.query_stack[afterTriggers.query_depth].events,
6436 : : &new_event, &new_shared);
6437 : : }
6438 : :
6439 : : /*
6440 : : * Finally, spool any foreign tuple(s). The tuplestore squashes them to
6441 : : * minimal tuples, so this loses any system columns. The executor lost
6442 : : * those columns before us, for an unrelated reason, so this is fine.
6443 : : */
3675 noah@leadboat.com 6444 [ + + ]: 5369 : if (fdw_tuplestore)
6445 : : {
1874 andres@anarazel.de 6446 [ + + ]: 25 : if (oldslot != NULL)
6447 : 16 : tuplestore_puttupleslot(fdw_tuplestore, oldslot);
6448 [ + + ]: 25 : if (newslot != NULL)
6449 : 18 : tuplestore_puttupleslot(fdw_tuplestore, newslot);
6450 : : }
6451 : : }
6452 : :
6453 : : /*
6454 : : * Detect whether we already queued BEFORE STATEMENT triggers for the given
6455 : : * relation + operation, and set the flag so the next call will report "true".
6456 : : */
6457 : : static bool
2401 tgl@sss.pgh.pa.us 6458 : 255 : before_stmt_triggers_fired(Oid relid, CmdType cmdType)
6459 : : {
6460 : : bool result;
6461 : : AfterTriggersTableData *table;
6462 : :
6463 : : /* Check state, like AfterTriggerSaveEvent. */
6464 [ - + ]: 255 : if (afterTriggers.query_depth < 0)
2401 tgl@sss.pgh.pa.us 6465 [ # # ]:UBC 0 : elog(ERROR, "before_stmt_triggers_fired() called outside of query");
6466 : :
6467 : : /* Be sure we have enough space to record events at this query depth. */
2401 tgl@sss.pgh.pa.us 6468 [ + + ]:CBC 255 : if (afterTriggers.query_depth >= afterTriggers.maxquerydepth)
6469 : 168 : AfterTriggerEnlargeQueryState();
6470 : :
6471 : : /*
6472 : : * We keep this state in the AfterTriggersTableData that also holds
6473 : : * transition tables for the relation + operation. In this way, if we are
6474 : : * forced to make a new set of transition tables because more tuples get
6475 : : * entered after we've already fired triggers, we will allow a new set of
6476 : : * statement triggers to get queued.
6477 : : */
6478 : 255 : table = GetAfterTriggersTableData(relid, cmdType);
6479 : 255 : result = table->before_trig_done;
6480 : 255 : table->before_trig_done = true;
6481 : 255 : return result;
6482 : : }
6483 : :
6484 : : /*
6485 : : * If we previously queued a set of AFTER STATEMENT triggers for the given
6486 : : * relation + operation, and they've not been fired yet, cancel them. The
6487 : : * caller will queue a fresh set that's after any row-level triggers that may
6488 : : * have been queued by the current sub-statement, preserving (as much as
6489 : : * possible) the property that AFTER ROW triggers fire before AFTER STATEMENT
6490 : : * triggers, and that the latter only fire once. This deals with the
6491 : : * situation where several FK enforcement triggers sequentially queue triggers
6492 : : * for the same table into the same trigger query level. We can't fully
6493 : : * prevent odd behavior though: if there are AFTER ROW triggers taking
6494 : : * transition tables, we don't want to change the transition tables once the
6495 : : * first such trigger has seen them. In such a case, any additional events
6496 : : * will result in creating new transition tables and allowing new firings of
6497 : : * statement triggers.
6498 : : *
6499 : : * This also saves the current event list location so that a later invocation
6500 : : * of this function can cheaply find the triggers we're about to queue and
6501 : : * cancel them.
6502 : : */
6503 : : static void
2402 6504 : 537 : cancel_prior_stmt_triggers(Oid relid, CmdType cmdType, int tgevent)
6505 : : {
6506 : : AfterTriggersTableData *table;
6507 : 537 : AfterTriggersQueryData *qs = &afterTriggers.query_stack[afterTriggers.query_depth];
6508 : :
6509 : : /*
6510 : : * We keep this state in the AfterTriggersTableData that also holds
6511 : : * transition tables for the relation + operation. In this way, if we are
6512 : : * forced to make a new set of transition tables because more tuples get
6513 : : * entered after we've already fired triggers, we will allow a new set of
6514 : : * statement triggers to get queued without canceling the old ones.
6515 : : */
6516 : 537 : table = GetAfterTriggersTableData(relid, cmdType);
6517 : :
2401 6518 [ + + ]: 537 : if (table->after_trig_done)
6519 : : {
6520 : : /*
6521 : : * We want to start scanning from the tail location that existed just
6522 : : * before we inserted any statement triggers. But the events list
6523 : : * might've been entirely empty then, in which case scan from the
6524 : : * current head.
6525 : : */
6526 : : AfterTriggerEvent event;
6527 : : AfterTriggerEventChunk *chunk;
6528 : :
6529 [ + + ]: 33 : if (table->after_trig_events.tail)
6530 : : {
6531 : 30 : chunk = table->after_trig_events.tail;
6532 : 30 : event = (AfterTriggerEvent) table->after_trig_events.tailfree;
6533 : : }
6534 : : else
6535 : : {
2402 6536 : 3 : chunk = qs->events.head;
6537 : 3 : event = NULL;
6538 : : }
6539 : :
6540 [ + + ]: 48 : for_each_chunk_from(chunk)
6541 : : {
6542 [ + + ]: 33 : if (event == NULL)
6543 : 3 : event = (AfterTriggerEvent) CHUNK_DATA_START(chunk);
6544 [ + - + - : 69 : for_each_event_from(event, chunk)
+ + ]
6545 : : {
6546 : 54 : AfterTriggerShared evtshared = GetTriggerSharedData(event);
6547 : :
6548 : : /*
6549 : : * Exit loop when we reach events that aren't AS triggers for
6550 : : * the target relation.
6551 : : */
6552 [ - + ]: 54 : if (evtshared->ats_relid != relid)
2402 tgl@sss.pgh.pa.us 6553 :UBC 0 : goto done;
2402 tgl@sss.pgh.pa.us 6554 [ - + ]:CBC 54 : if ((evtshared->ats_event & TRIGGER_EVENT_OPMASK) != tgevent)
2402 tgl@sss.pgh.pa.us 6555 :UBC 0 : goto done;
2402 tgl@sss.pgh.pa.us 6556 [ + + ]:CBC 54 : if (!TRIGGER_FIRED_FOR_STATEMENT(evtshared->ats_event))
6557 : 18 : goto done;
6558 [ - + ]: 36 : if (!TRIGGER_FIRED_AFTER(evtshared->ats_event))
2402 tgl@sss.pgh.pa.us 6559 :UBC 0 : goto done;
6560 : : /* OK, mark it DONE */
2402 tgl@sss.pgh.pa.us 6561 :CBC 36 : event->ate_flags &= ~AFTER_TRIGGER_IN_PROGRESS;
6562 [ + - ]: 36 : event->ate_flags |= AFTER_TRIGGER_DONE;
6563 : : }
6564 : : /* signal we must reinitialize event ptr for next chunk */
6565 : 15 : event = NULL;
6566 : : }
6567 : : }
6568 : 519 : done:
6569 : :
6570 : : /* In any case, save current insertion point for next time */
2401 6571 : 537 : table->after_trig_done = true;
6572 : 537 : table->after_trig_events = qs->events;
2402 6573 : 537 : }
6574 : :
6575 : : /*
6576 : : * GUC assign_hook for session_replication_role
6577 : : */
6578 : : void
579 6579 : 1397 : assign_session_replication_role(int newval, void *extra)
6580 : : {
6581 : : /*
6582 : : * Must flush the plan cache when changing replication role; but don't
6583 : : * flush unnecessarily.
6584 : : */
6585 [ + + ]: 1397 : if (SessionReplicationRole != newval)
6586 : 469 : ResetPlanCache();
6587 : 1397 : }
6588 : :
6589 : : /*
6590 : : * SQL function pg_trigger_depth()
6591 : : */
6592 : : Datum
4463 alvherre@alvh.no-ip. 6593 : 45 : pg_trigger_depth(PG_FUNCTION_ARGS)
6594 : : {
6595 : 45 : PG_RETURN_INT32(MyTriggerDepth);
6596 : : }
|