PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
event_trigger.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * event_trigger.c
4  * PostgreSQL EVENT TRIGGER support code.
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/backend/commands/event_trigger.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/htup_details.h"
17 #include "access/xact.h"
18 #include "catalog/dependency.h"
19 #include "catalog/indexing.h"
20 #include "catalog/objectaccess.h"
22 #include "catalog/pg_namespace.h"
23 #include "catalog/pg_opclass.h"
24 #include "catalog/pg_opfamily.h"
25 #include "catalog/pg_proc.h"
26 #include "catalog/pg_trigger.h"
27 #include "catalog/pg_ts_config.h"
28 #include "catalog/pg_type.h"
29 #include "commands/dbcommands.h"
30 #include "commands/event_trigger.h"
31 #include "commands/extension.h"
32 #include "commands/trigger.h"
33 #include "funcapi.h"
34 #include "parser/parse_func.h"
35 #include "pgstat.h"
36 #include "lib/ilist.h"
37 #include "miscadmin.h"
38 #include "tcop/deparse_utility.h"
39 #include "utils/acl.h"
40 #include "utils/builtins.h"
41 #include "utils/evtcache.h"
42 #include "utils/fmgroids.h"
43 #include "utils/lsyscache.h"
44 #include "utils/memutils.h"
45 #include "utils/rel.h"
46 #include "utils/tqual.h"
47 #include "utils/syscache.h"
48 #include "tcop/utility.h"
49 
50 typedef struct EventTriggerQueryState
51 {
52  /* memory context for this state's objects */
54 
55  /* sql_drop */
58 
59  /* table_rewrite */
60  Oid table_rewrite_oid; /* InvalidOid, or set for
61  * table_rewrite event */
62  int table_rewrite_reason; /* AT_REWRITE reason */
63 
64  /* Support for command collection */
67  List *commandList; /* list of CollectedCommand; see
68  * deparse_utility.h */
71 
73 
74 typedef struct
75 {
76  const char *obtypename;
77  bool supported;
79 
80 typedef enum
81 {
86 
87 /* XXX merge this with ObjectTypeMap? */
89  {"ACCESS METHOD", true},
90  {"AGGREGATE", true},
91  {"CAST", true},
92  {"CONSTRAINT", true},
93  {"COLLATION", true},
94  {"CONVERSION", true},
95  {"DATABASE", false},
96  {"DOMAIN", true},
97  {"EXTENSION", true},
98  {"EVENT TRIGGER", false},
99  {"FOREIGN DATA WRAPPER", true},
100  {"FOREIGN TABLE", true},
101  {"FUNCTION", true},
102  {"INDEX", true},
103  {"LANGUAGE", true},
104  {"MATERIALIZED VIEW", true},
105  {"OPERATOR", true},
106  {"OPERATOR CLASS", true},
107  {"OPERATOR FAMILY", true},
108  {"POLICY", true},
109  {"PUBLICATION", true},
110  {"ROLE", false},
111  {"RULE", true},
112  {"SCHEMA", true},
113  {"SEQUENCE", true},
114  {"SERVER", true},
115  {"SUBSCRIPTION", true},
116  {"TABLE", true},
117  {"TABLESPACE", false},
118  {"TRANSFORM", true},
119  {"TRIGGER", true},
120  {"TEXT SEARCH CONFIGURATION", true},
121  {"TEXT SEARCH DICTIONARY", true},
122  {"TEXT SEARCH PARSER", true},
123  {"TEXT SEARCH TEMPLATE", true},
124  {"TYPE", true},
125  {"USER MAPPING", true},
126  {"VIEW", true},
127  {NULL, false}
128 };
129 
130 /* Support for dropped objects */
131 typedef struct SQLDropObject
132 {
134  const char *schemaname;
135  const char *objname;
136  const char *objidentity;
137  const char *objecttype;
140  bool original;
141  bool normal;
142  bool istemp;
144 } SQLDropObject;
145 
147  HeapTuple tup,
148  Oid newOwnerId);
151  const char *tag);
152 static void error_duplicate_filter_variable(const char *defname);
153 static Datum filter_list_to_array(List *filterlist);
154 static Oid insert_event_trigger_tuple(char *trigname, char *eventname,
155  Oid evtOwner, Oid funcoid, List *tags);
156 static void validate_ddl_tags(const char *filtervar, List *taglist);
157 static void validate_table_rewrite_tags(const char *filtervar, List *taglist);
158 static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata);
159 static const char *stringify_grantobjtype(GrantObjectType objtype);
160 static const char *stringify_adefprivs_objtype(GrantObjectType objtype);
161 
162 /*
163  * Create an event trigger.
164  */
165 Oid
167 {
168  HeapTuple tuple;
169  Oid funcoid;
170  Oid funcrettype;
171  Oid fargtypes[1]; /* dummy */
172  Oid evtowner = GetUserId();
173  ListCell *lc;
174  List *tags = NULL;
175 
176  /*
177  * It would be nice to allow database owners or even regular users to do
178  * this, but there are obvious privilege escalation risks which would have
179  * to somehow be plugged first.
180  */
181  if (!superuser())
182  ereport(ERROR,
183  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
184  errmsg("permission denied to create event trigger \"%s\"",
185  stmt->trigname),
186  errhint("Must be superuser to create an event trigger.")));
187 
188  /* Validate event name. */
189  if (strcmp(stmt->eventname, "ddl_command_start") != 0 &&
190  strcmp(stmt->eventname, "ddl_command_end") != 0 &&
191  strcmp(stmt->eventname, "sql_drop") != 0 &&
192  strcmp(stmt->eventname, "table_rewrite") != 0)
193  ereport(ERROR,
194  (errcode(ERRCODE_SYNTAX_ERROR),
195  errmsg("unrecognized event name \"%s\"",
196  stmt->eventname)));
197 
198  /* Validate filter conditions. */
199  foreach(lc, stmt->whenclause)
200  {
201  DefElem *def = (DefElem *) lfirst(lc);
202 
203  if (strcmp(def->defname, "tag") == 0)
204  {
205  if (tags != NULL)
207  tags = (List *) def->arg;
208  }
209  else
210  ereport(ERROR,
211  (errcode(ERRCODE_SYNTAX_ERROR),
212  errmsg("unrecognized filter variable \"%s\"", def->defname)));
213  }
214 
215  /* Validate tag list, if any. */
216  if ((strcmp(stmt->eventname, "ddl_command_start") == 0 ||
217  strcmp(stmt->eventname, "ddl_command_end") == 0 ||
218  strcmp(stmt->eventname, "sql_drop") == 0)
219  && tags != NULL)
220  validate_ddl_tags("tag", tags);
221  else if (strcmp(stmt->eventname, "table_rewrite") == 0
222  && tags != NULL)
223  validate_table_rewrite_tags("tag", tags);
224 
225  /*
226  * Give user a nice error message if an event trigger of the same name
227  * already exists.
228  */
230  if (HeapTupleIsValid(tuple))
231  ereport(ERROR,
233  errmsg("event trigger \"%s\" already exists",
234  stmt->trigname)));
235 
236  /* Find and validate the trigger function. */
237  funcoid = LookupFuncName(stmt->funcname, 0, fargtypes, false);
238  funcrettype = get_func_rettype(funcoid);
239  if (funcrettype != EVTTRIGGEROID)
240  ereport(ERROR,
241  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
242  errmsg("function %s must return type %s",
243  NameListToString(stmt->funcname), "event_trigger")));
244 
245  /* Insert catalog entries. */
246  return insert_event_trigger_tuple(stmt->trigname, stmt->eventname,
247  evtowner, funcoid, tags);
248 }
249 
250 /*
251  * Validate DDL command tags.
252  */
253 static void
254 validate_ddl_tags(const char *filtervar, List *taglist)
255 {
256  ListCell *lc;
257 
258  foreach(lc, taglist)
259  {
260  const char *tag = strVal(lfirst(lc));
262 
263  result = check_ddl_tag(tag);
265  ereport(ERROR,
266  (errcode(ERRCODE_SYNTAX_ERROR),
267  errmsg("filter value \"%s\" not recognized for filter variable \"%s\"",
268  tag, filtervar)));
270  ereport(ERROR,
271  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
272  /* translator: %s represents an SQL statement name */
273  errmsg("event triggers are not supported for %s",
274  tag)));
275  }
276 }
277 
279 check_ddl_tag(const char *tag)
280 {
281  const char *obtypename;
283 
284  /*
285  * Handle some idiosyncratic special cases.
286  */
287  if (pg_strcasecmp(tag, "CREATE TABLE AS") == 0 ||
288  pg_strcasecmp(tag, "SELECT INTO") == 0 ||
289  pg_strcasecmp(tag, "REFRESH MATERIALIZED VIEW") == 0 ||
290  pg_strcasecmp(tag, "ALTER DEFAULT PRIVILEGES") == 0 ||
291  pg_strcasecmp(tag, "ALTER LARGE OBJECT") == 0 ||
292  pg_strcasecmp(tag, "COMMENT") == 0 ||
293  pg_strcasecmp(tag, "GRANT") == 0 ||
294  pg_strcasecmp(tag, "REVOKE") == 0 ||
295  pg_strcasecmp(tag, "DROP OWNED") == 0 ||
296  pg_strcasecmp(tag, "IMPORT FOREIGN SCHEMA") == 0 ||
297  pg_strcasecmp(tag, "SECURITY LABEL") == 0)
299 
300  /*
301  * Otherwise, command should be CREATE, ALTER, or DROP.
302  */
303  if (pg_strncasecmp(tag, "CREATE ", 7) == 0)
304  obtypename = tag + 7;
305  else if (pg_strncasecmp(tag, "ALTER ", 6) == 0)
306  obtypename = tag + 6;
307  else if (pg_strncasecmp(tag, "DROP ", 5) == 0)
308  obtypename = tag + 5;
309  else
311 
312  /*
313  * ...and the object type should be something recognizable.
314  */
315  for (etsd = event_trigger_support; etsd->obtypename != NULL; etsd++)
316  if (pg_strcasecmp(etsd->obtypename, obtypename) == 0)
317  break;
318  if (etsd->obtypename == NULL)
320  if (!etsd->supported)
323 }
324 
325 /*
326  * Validate DDL command tags for event table_rewrite.
327  */
328 static void
329 validate_table_rewrite_tags(const char *filtervar, List *taglist)
330 {
331  ListCell *lc;
332 
333  foreach(lc, taglist)
334  {
335  const char *tag = strVal(lfirst(lc));
337 
338  result = check_table_rewrite_ddl_tag(tag);
340  ereport(ERROR,
341  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
342  /* translator: %s represents an SQL statement name */
343  errmsg("event triggers are not supported for %s",
344  tag)));
345  }
346 }
347 
350 {
351  if (pg_strcasecmp(tag, "ALTER TABLE") == 0 ||
352  pg_strcasecmp(tag, "ALTER TYPE") == 0)
354 
356 }
357 
358 /*
359  * Complain about a duplicate filter variable.
360  */
361 static void
363 {
364  ereport(ERROR,
365  (errcode(ERRCODE_SYNTAX_ERROR),
366  errmsg("filter variable \"%s\" specified more than once",
367  defname)));
368 }
369 
370 /*
371  * Insert the new pg_event_trigger row and record dependencies.
372  */
373 static Oid
374 insert_event_trigger_tuple(char *trigname, char *eventname, Oid evtOwner,
375  Oid funcoid, List *taglist)
376 {
377  Relation tgrel;
378  Oid trigoid;
379  HeapTuple tuple;
381  bool nulls[Natts_pg_trigger];
382  NameData evtnamedata,
383  evteventdata;
384  ObjectAddress myself,
385  referenced;
386 
387  /* Open pg_event_trigger. */
389 
390  /* Build the new pg_trigger tuple. */
391  memset(nulls, false, sizeof(nulls));
392  namestrcpy(&evtnamedata, trigname);
393  values[Anum_pg_event_trigger_evtname - 1] = NameGetDatum(&evtnamedata);
394  namestrcpy(&evteventdata, eventname);
395  values[Anum_pg_event_trigger_evtevent - 1] = NameGetDatum(&evteventdata);
396  values[Anum_pg_event_trigger_evtowner - 1] = ObjectIdGetDatum(evtOwner);
397  values[Anum_pg_event_trigger_evtfoid - 1] = ObjectIdGetDatum(funcoid);
400  if (taglist == NIL)
401  nulls[Anum_pg_event_trigger_evttags - 1] = true;
402  else
403  values[Anum_pg_event_trigger_evttags - 1] =
404  filter_list_to_array(taglist);
405 
406  /* Insert heap tuple. */
407  tuple = heap_form_tuple(tgrel->rd_att, values, nulls);
408  trigoid = CatalogTupleInsert(tgrel, tuple);
409  heap_freetuple(tuple);
410 
411  /* Depend on owner. */
413 
414  /* Depend on event trigger function. */
416  myself.objectId = trigoid;
417  myself.objectSubId = 0;
418  referenced.classId = ProcedureRelationId;
419  referenced.objectId = funcoid;
420  referenced.objectSubId = 0;
421  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
422 
423  /* Depend on extension, if any. */
424  recordDependencyOnCurrentExtension(&myself, false);
425 
426  /* Post creation hook for new event trigger */
428 
429  /* Close pg_event_trigger. */
431 
432  return trigoid;
433 }
434 
435 /*
436  * In the parser, a clause like WHEN tag IN ('cmd1', 'cmd2') is represented
437  * by a DefElem whose value is a List of String nodes; in the catalog, we
438  * store the list of strings as a text array. This function transforms the
439  * former representation into the latter one.
440  *
441  * For cleanliness, we store command tags in the catalog as text. It's
442  * possible (although not currently anticipated) that we might have
443  * a case-sensitive filter variable in the future, in which case this would
444  * need some further adjustment.
445  */
446 static Datum
448 {
449  ListCell *lc;
450  Datum *data;
451  int i = 0,
452  l = list_length(filterlist);
453 
454  data = (Datum *) palloc(l * sizeof(Datum));
455 
456  foreach(lc, filterlist)
457  {
458  const char *value = strVal(lfirst(lc));
459  char *result,
460  *p;
461 
462  result = pstrdup(value);
463  for (p = result; *p; p++)
464  *p = pg_ascii_toupper((unsigned char) *p);
465  data[i++] = PointerGetDatum(cstring_to_text(result));
466  pfree(result);
467  }
468 
469  return PointerGetDatum(construct_array(data, l, TEXTOID, -1, false, 'i'));
470 }
471 
472 /*
473  * Guts of event trigger deletion.
474  */
475 void
477 {
478  Relation tgrel;
479  HeapTuple tup;
480 
482 
484  if (!HeapTupleIsValid(tup))
485  elog(ERROR, "cache lookup failed for event trigger %u", trigOid);
486 
487  CatalogTupleDelete(tgrel, &tup->t_self);
488 
489  ReleaseSysCache(tup);
490 
492 }
493 
494 /*
495  * ALTER EVENT TRIGGER foo ENABLE|DISABLE|ENABLE ALWAYS|REPLICA
496  */
497 Oid
499 {
500  Relation tgrel;
501  HeapTuple tup;
502  Oid trigoid;
503  Form_pg_event_trigger evtForm;
504  char tgenabled = stmt->tgenabled;
505 
507 
509  CStringGetDatum(stmt->trigname));
510  if (!HeapTupleIsValid(tup))
511  ereport(ERROR,
512  (errcode(ERRCODE_UNDEFINED_OBJECT),
513  errmsg("event trigger \"%s\" does not exist",
514  stmt->trigname)));
515 
516  trigoid = HeapTupleGetOid(tup);
517 
518  if (!pg_event_trigger_ownercheck(trigoid, GetUserId()))
520  stmt->trigname);
521 
522  /* tuple is a copy, so we can modify it below */
523  evtForm = (Form_pg_event_trigger) GETSTRUCT(tup);
524  evtForm->evtenabled = tgenabled;
525 
526  CatalogTupleUpdate(tgrel, &tup->t_self, tup);
527 
529  trigoid, 0);
530 
531  /* clean up */
532  heap_freetuple(tup);
534 
535  return trigoid;
536 }
537 
538 /*
539  * Change event trigger's owner -- by name
540  */
542 AlterEventTriggerOwner(const char *name, Oid newOwnerId)
543 {
544  Oid evtOid;
545  HeapTuple tup;
546  Relation rel;
547  ObjectAddress address;
548 
550 
552 
553  if (!HeapTupleIsValid(tup))
554  ereport(ERROR,
555  (errcode(ERRCODE_UNDEFINED_OBJECT),
556  errmsg("event trigger \"%s\" does not exist", name)));
557 
558  evtOid = HeapTupleGetOid(tup);
559 
560  AlterEventTriggerOwner_internal(rel, tup, newOwnerId);
561 
562  ObjectAddressSet(address, EventTriggerRelationId, evtOid);
563 
564  heap_freetuple(tup);
565 
567 
568  return address;
569 }
570 
571 /*
572  * Change event trigger owner, by OID
573  */
574 void
575 AlterEventTriggerOwner_oid(Oid trigOid, Oid newOwnerId)
576 {
577  HeapTuple tup;
578  Relation rel;
579 
581 
583 
584  if (!HeapTupleIsValid(tup))
585  ereport(ERROR,
586  (errcode(ERRCODE_UNDEFINED_OBJECT),
587  errmsg("event trigger with OID %u does not exist", trigOid)));
588 
589  AlterEventTriggerOwner_internal(rel, tup, newOwnerId);
590 
591  heap_freetuple(tup);
592 
594 }
595 
596 /*
597  * Internal workhorse for changing an event trigger's owner
598  */
599 static void
601 {
603 
604  form = (Form_pg_event_trigger) GETSTRUCT(tup);
605 
606  if (form->evtowner == newOwnerId)
607  return;
608 
611  NameStr(form->evtname));
612 
613  /* New owner must be a superuser */
614  if (!superuser_arg(newOwnerId))
615  ereport(ERROR,
616  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
617  errmsg("permission denied to change owner of event trigger \"%s\"",
618  NameStr(form->evtname)),
619  errhint("The owner of an event trigger must be a superuser.")));
620 
621  form->evtowner = newOwnerId;
622  CatalogTupleUpdate(rel, &tup->t_self, tup);
623 
624  /* Update owner dependency reference */
626  HeapTupleGetOid(tup),
627  newOwnerId);
628 
630  HeapTupleGetOid(tup), 0);
631 }
632 
633 /*
634  * get_event_trigger_oid - Look up an event trigger by name to find its OID.
635  *
636  * If missing_ok is false, throw an error if trigger not found. If
637  * true, just return InvalidOid.
638  */
639 Oid
640 get_event_trigger_oid(const char *trigname, bool missing_ok)
641 {
642  Oid oid;
643 
645  if (!OidIsValid(oid) && !missing_ok)
646  ereport(ERROR,
647  (errcode(ERRCODE_UNDEFINED_OBJECT),
648  errmsg("event trigger \"%s\" does not exist", trigname)));
649  return oid;
650 }
651 
652 /*
653  * Return true when we want to fire given Event Trigger and false otherwise,
654  * filtering on the session replication role and the event trigger registered
655  * tags matching.
656  */
657 static bool
659 {
660  /*
661  * Filter by session replication role, knowing that we never see disabled
662  * items down here.
663  */
665  {
666  if (item->enabled == TRIGGER_FIRES_ON_ORIGIN)
667  return false;
668  }
669  else
670  {
671  if (item->enabled == TRIGGER_FIRES_ON_REPLICA)
672  return false;
673  }
674 
675  /* Filter by tags, if any were specified. */
676  if (item->ntags != 0 && bsearch(tag, item->tag,
677  item->ntags, sizeof(char *),
678  pg_qsort_strcmp) == NULL)
679  return false;
680 
681  /* if we reach that point, we're not filtering out this item */
682  return true;
683 }
684 
685 /*
686  * Setup for running triggers for the given event. Return value is an OID list
687  * of functions to run; if there are any, trigdata is filled with an
688  * appropriate EventTriggerData for them to receive.
689  */
690 static List *
692  EventTriggerEvent event, const char *eventstr,
693  EventTriggerData *trigdata)
694 {
695  const char *tag;
696  List *cachelist;
697  ListCell *lc;
698  List *runlist = NIL;
699 
700  /*
701  * We want the list of command tags for which this procedure is actually
702  * invoked to match up exactly with the list that CREATE EVENT TRIGGER
703  * accepts. This debugging cross-check will throw an error if this
704  * function is invoked for a command tag that CREATE EVENT TRIGGER won't
705  * accept. (Unfortunately, there doesn't seem to be any simple, automated
706  * way to verify that CREATE EVENT TRIGGER doesn't accept extra stuff that
707  * never reaches this control point.)
708  *
709  * If this cross-check fails for you, you probably need to either adjust
710  * standard_ProcessUtility() not to invoke event triggers for the command
711  * type in question, or you need to adjust check_ddl_tag to accept the
712  * relevant command tag.
713  */
714 #ifdef USE_ASSERT_CHECKING
715  {
716  const char *dbgtag;
717 
718  dbgtag = CreateCommandTag(parsetree);
719  if (event == EVT_DDLCommandStart ||
720  event == EVT_DDLCommandEnd ||
721  event == EVT_SQLDrop)
722  {
724  elog(ERROR, "unexpected command tag \"%s\"", dbgtag);
725  }
726  else if (event == EVT_TableRewrite)
727  {
729  elog(ERROR, "unexpected command tag \"%s\"", dbgtag);
730  }
731  }
732 #endif
733 
734  /* Use cache to find triggers for this event; fast exit if none. */
735  cachelist = EventCacheLookup(event);
736  if (cachelist == NIL)
737  return NIL;
738 
739  /* Get the command tag. */
740  tag = CreateCommandTag(parsetree);
741 
742  /*
743  * Filter list of event triggers by command tag, and copy them into our
744  * memory context. Once we start running the command triggers, or indeed
745  * once we do anything at all that touches the catalogs, an invalidation
746  * might leave cachelist pointing at garbage, so we must do this before we
747  * can do much else.
748  */
749  foreach(lc, cachelist)
750  {
751  EventTriggerCacheItem *item = lfirst(lc);
752 
753  if (filter_event_trigger(&tag, item))
754  {
755  /* We must plan to fire this trigger. */
756  runlist = lappend_oid(runlist, item->fnoid);
757  }
758  }
759 
760  /* don't spend any more time on this if no functions to run */
761  if (runlist == NIL)
762  return NIL;
763 
764  trigdata->type = T_EventTriggerData;
765  trigdata->event = eventstr;
766  trigdata->parsetree = parsetree;
767  trigdata->tag = tag;
768 
769  return runlist;
770 }
771 
772 /*
773  * Fire ddl_command_start triggers.
774  */
775 void
777 {
778  List *runlist;
779  EventTriggerData trigdata;
780 
781  /*
782  * Event Triggers are completely disabled in standalone mode. There are
783  * (at least) two reasons for this:
784  *
785  * 1. A sufficiently broken event trigger might not only render the
786  * database unusable, but prevent disabling itself to fix the situation.
787  * In this scenario, restarting in standalone mode provides an escape
788  * hatch.
789  *
790  * 2. BuildEventTriggerCache relies on systable_beginscan_ordered, and
791  * therefore will malfunction if pg_event_trigger's indexes are damaged.
792  * To allow recovery from a damaged index, we need some operating mode
793  * wherein event triggers are disabled. (Or we could implement
794  * heapscan-and-sort logic for that case, but having disaster recovery
795  * scenarios depend on code that's otherwise untested isn't appetizing.)
796  */
797  if (!IsUnderPostmaster)
798  return;
799 
800  runlist = EventTriggerCommonSetup(parsetree,
802  "ddl_command_start",
803  &trigdata);
804  if (runlist == NIL)
805  return;
806 
807  /* Run the triggers. */
808  EventTriggerInvoke(runlist, &trigdata);
809 
810  /* Cleanup. */
811  list_free(runlist);
812 
813  /*
814  * Make sure anything the event triggers did will be visible to the main
815  * command.
816  */
818 }
819 
820 /*
821  * Fire ddl_command_end triggers.
822  */
823 void
825 {
826  List *runlist;
827  EventTriggerData trigdata;
828 
829  /*
830  * See EventTriggerDDLCommandStart for a discussion about why event
831  * triggers are disabled in single user mode.
832  */
833  if (!IsUnderPostmaster)
834  return;
835 
836  runlist = EventTriggerCommonSetup(parsetree,
837  EVT_DDLCommandEnd, "ddl_command_end",
838  &trigdata);
839  if (runlist == NIL)
840  return;
841 
842  /*
843  * Make sure anything the main command did will be visible to the event
844  * triggers.
845  */
847 
848  /* Run the triggers. */
849  EventTriggerInvoke(runlist, &trigdata);
850 
851  /* Cleanup. */
852  list_free(runlist);
853 }
854 
855 /*
856  * Fire sql_drop triggers.
857  */
858 void
860 {
861  List *runlist;
862  EventTriggerData trigdata;
863 
864  /*
865  * See EventTriggerDDLCommandStart for a discussion about why event
866  * triggers are disabled in single user mode.
867  */
868  if (!IsUnderPostmaster)
869  return;
870 
871  /*
872  * Use current state to determine whether this event fires at all. If
873  * there are no triggers for the sql_drop event, then we don't have
874  * anything to do here. Note that dropped object collection is disabled
875  * if this is the case, so even if we were to try to run, the list would
876  * be empty.
877  */
878  if (!currentEventTriggerState ||
879  slist_is_empty(&currentEventTriggerState->SQLDropList))
880  return;
881 
882  runlist = EventTriggerCommonSetup(parsetree,
883  EVT_SQLDrop, "sql_drop",
884  &trigdata);
885 
886  /*
887  * Nothing to do if run list is empty. Note this shouldn't happen,
888  * because if there are no sql_drop events, then objects-to-drop wouldn't
889  * have been collected in the first place and we would have quit above.
890  */
891  if (runlist == NIL)
892  return;
893 
894  /*
895  * Make sure anything the main command did will be visible to the event
896  * triggers.
897  */
899 
900  /*
901  * Make sure pg_event_trigger_dropped_objects only works when running
902  * these triggers. Use PG_TRY to ensure in_sql_drop is reset even when
903  * one trigger fails. (This is perhaps not necessary, as the currentState
904  * variable will be removed shortly by our caller, but it seems better to
905  * play safe.)
906  */
907  currentEventTriggerState->in_sql_drop = true;
908 
909  /* Run the triggers. */
910  PG_TRY();
911  {
912  EventTriggerInvoke(runlist, &trigdata);
913  }
914  PG_CATCH();
915  {
916  currentEventTriggerState->in_sql_drop = false;
917  PG_RE_THROW();
918  }
919  PG_END_TRY();
920  currentEventTriggerState->in_sql_drop = false;
921 
922  /* Cleanup. */
923  list_free(runlist);
924 }
925 
926 
927 /*
928  * Fire table_rewrite triggers.
929  */
930 void
931 EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason)
932 {
933  List *runlist;
934  EventTriggerData trigdata;
935 
936  elog(DEBUG1, "EventTriggerTableRewrite(%u)", tableOid);
937 
938  /*
939  * Event Triggers are completely disabled in standalone mode. There are
940  * (at least) two reasons for this:
941  *
942  * 1. A sufficiently broken event trigger might not only render the
943  * database unusable, but prevent disabling itself to fix the situation.
944  * In this scenario, restarting in standalone mode provides an escape
945  * hatch.
946  *
947  * 2. BuildEventTriggerCache relies on systable_beginscan_ordered, and
948  * therefore will malfunction if pg_event_trigger's indexes are damaged.
949  * To allow recovery from a damaged index, we need some operating mode
950  * wherein event triggers are disabled. (Or we could implement
951  * heapscan-and-sort logic for that case, but having disaster recovery
952  * scenarios depend on code that's otherwise untested isn't appetizing.)
953  */
954  if (!IsUnderPostmaster)
955  return;
956 
957  runlist = EventTriggerCommonSetup(parsetree,
959  "table_rewrite",
960  &trigdata);
961  if (runlist == NIL)
962  return;
963 
964  /*
965  * Make sure pg_event_trigger_table_rewrite_oid only works when running
966  * these triggers. Use PG_TRY to ensure table_rewrite_oid is reset even
967  * when one trigger fails. (This is perhaps not necessary, as the
968  * currentState variable will be removed shortly by our caller, but it
969  * seems better to play safe.)
970  */
971  currentEventTriggerState->table_rewrite_oid = tableOid;
972  currentEventTriggerState->table_rewrite_reason = reason;
973 
974  /* Run the triggers. */
975  PG_TRY();
976  {
977  EventTriggerInvoke(runlist, &trigdata);
978  }
979  PG_CATCH();
980  {
981  currentEventTriggerState->table_rewrite_oid = InvalidOid;
982  currentEventTriggerState->table_rewrite_reason = 0;
983  PG_RE_THROW();
984  }
985  PG_END_TRY();
986 
987  currentEventTriggerState->table_rewrite_oid = InvalidOid;
988  currentEventTriggerState->table_rewrite_reason = 0;
989 
990  /* Cleanup. */
991  list_free(runlist);
992 
993  /*
994  * Make sure anything the event triggers did will be visible to the main
995  * command.
996  */
998 }
999 
1000 /*
1001  * Invoke each event trigger in a list of event triggers.
1002  */
1003 static void
1004 EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
1005 {
1006  MemoryContext context;
1007  MemoryContext oldcontext;
1008  ListCell *lc;
1009  bool first = true;
1010 
1011  /* Guard against stack overflow due to recursive event trigger */
1013 
1014  /*
1015  * Let's evaluate event triggers in their own memory context, so that any
1016  * leaks get cleaned up promptly.
1017  */
1019  "event trigger context",
1021  oldcontext = MemoryContextSwitchTo(context);
1022 
1023  /* Call each event trigger. */
1024  foreach(lc, fn_oid_list)
1025  {
1026  Oid fnoid = lfirst_oid(lc);
1027  FmgrInfo flinfo;
1028  FunctionCallInfoData fcinfo;
1029  PgStat_FunctionCallUsage fcusage;
1030 
1031  elog(DEBUG1, "EventTriggerInvoke %u", fnoid);
1032 
1033  /*
1034  * We want each event trigger to be able to see the results of the
1035  * previous event trigger's action. Caller is responsible for any
1036  * command-counter increment that is needed between the event trigger
1037  * and anything else in the transaction.
1038  */
1039  if (first)
1040  first = false;
1041  else
1043 
1044  /* Look up the function */
1045  fmgr_info(fnoid, &flinfo);
1046 
1047  /* Call the function, passing no arguments but setting a context. */
1048  InitFunctionCallInfoData(fcinfo, &flinfo, 0,
1049  InvalidOid, (Node *) trigdata, NULL);
1050  pgstat_init_function_usage(&fcinfo, &fcusage);
1051  FunctionCallInvoke(&fcinfo);
1052  pgstat_end_function_usage(&fcusage, true);
1053 
1054  /* Reclaim memory. */
1055  MemoryContextReset(context);
1056  }
1057 
1058  /* Restore old memory context and delete the temporary one. */
1059  MemoryContextSwitchTo(oldcontext);
1060  MemoryContextDelete(context);
1061 }
1062 
1063 /*
1064  * Do event triggers support this object type?
1065  */
1066 bool
1068 {
1069  switch (obtype)
1070  {
1071  case OBJECT_DATABASE:
1072  case OBJECT_TABLESPACE:
1073  case OBJECT_ROLE:
1074  /* no support for global objects */
1075  return false;
1076  case OBJECT_EVENT_TRIGGER:
1077  /* no support for event triggers on event triggers */
1078  return false;
1079  case OBJECT_ACCESS_METHOD:
1080  case OBJECT_AGGREGATE:
1081  case OBJECT_AMOP:
1082  case OBJECT_AMPROC:
1083  case OBJECT_ATTRIBUTE:
1084  case OBJECT_CAST:
1085  case OBJECT_COLUMN:
1086  case OBJECT_COLLATION:
1087  case OBJECT_CONVERSION:
1088  case OBJECT_DEFACL:
1089  case OBJECT_DEFAULT:
1090  case OBJECT_DOMAIN:
1091  case OBJECT_DOMCONSTRAINT:
1092  case OBJECT_EXTENSION:
1093  case OBJECT_FDW:
1094  case OBJECT_FOREIGN_SERVER:
1095  case OBJECT_FOREIGN_TABLE:
1096  case OBJECT_FUNCTION:
1097  case OBJECT_INDEX:
1098  case OBJECT_LANGUAGE:
1099  case OBJECT_LARGEOBJECT:
1100  case OBJECT_MATVIEW:
1101  case OBJECT_OPCLASS:
1102  case OBJECT_OPERATOR:
1103  case OBJECT_OPFAMILY:
1104  case OBJECT_POLICY:
1105  case OBJECT_PUBLICATION:
1107  case OBJECT_RULE:
1108  case OBJECT_SCHEMA:
1109  case OBJECT_SEQUENCE:
1110  case OBJECT_SUBSCRIPTION:
1111  case OBJECT_TABCONSTRAINT:
1112  case OBJECT_TABLE:
1113  case OBJECT_TRANSFORM:
1114  case OBJECT_TRIGGER:
1116  case OBJECT_TSDICTIONARY:
1117  case OBJECT_TSPARSER:
1118  case OBJECT_TSTEMPLATE:
1119  case OBJECT_TYPE:
1120  case OBJECT_USER_MAPPING:
1121  case OBJECT_VIEW:
1122  return true;
1123  }
1124  return true;
1125 }
1126 
1127 /*
1128  * Do event triggers support this object class?
1129  */
1130 bool
1132 {
1133  switch (objclass)
1134  {
1135  case OCLASS_DATABASE:
1136  case OCLASS_TBLSPACE:
1137  case OCLASS_ROLE:
1138  /* no support for global objects */
1139  return false;
1140  case OCLASS_EVENT_TRIGGER:
1141  /* no support for event triggers on event triggers */
1142  return false;
1143  case OCLASS_CLASS:
1144  case OCLASS_PROC:
1145  case OCLASS_TYPE:
1146  case OCLASS_CAST:
1147  case OCLASS_COLLATION:
1148  case OCLASS_CONSTRAINT:
1149  case OCLASS_CONVERSION:
1150  case OCLASS_DEFAULT:
1151  case OCLASS_LANGUAGE:
1152  case OCLASS_LARGEOBJECT:
1153  case OCLASS_OPERATOR:
1154  case OCLASS_OPCLASS:
1155  case OCLASS_OPFAMILY:
1156  case OCLASS_AMOP:
1157  case OCLASS_AMPROC:
1158  case OCLASS_REWRITE:
1159  case OCLASS_TRIGGER:
1160  case OCLASS_SCHEMA:
1161  case OCLASS_TRANSFORM:
1162  case OCLASS_TSPARSER:
1163  case OCLASS_TSDICT:
1164  case OCLASS_TSTEMPLATE:
1165  case OCLASS_TSCONFIG:
1166  case OCLASS_FDW:
1167  case OCLASS_FOREIGN_SERVER:
1168  case OCLASS_USER_MAPPING:
1169  case OCLASS_DEFACL:
1170  case OCLASS_EXTENSION:
1171  case OCLASS_POLICY:
1172  case OCLASS_AM:
1173  case OCLASS_PUBLICATION:
1175  case OCLASS_SUBSCRIPTION:
1176  return true;
1177  }
1178 
1179  return true;
1180 }
1181 
1182 bool
1184 {
1185  switch (objtype)
1186  {
1187  case ACL_OBJECT_DATABASE:
1188  case ACL_OBJECT_TABLESPACE:
1189  /* no support for global objects */
1190  return false;
1191 
1192  case ACL_OBJECT_COLUMN:
1193  case ACL_OBJECT_RELATION:
1194  case ACL_OBJECT_SEQUENCE:
1195  case ACL_OBJECT_DOMAIN:
1196  case ACL_OBJECT_FDW:
1198  case ACL_OBJECT_FUNCTION:
1199  case ACL_OBJECT_LANGUAGE:
1201  case ACL_OBJECT_NAMESPACE:
1202  case ACL_OBJECT_TYPE:
1203  return true;
1204  default:
1205  Assert(false);
1206  return true;
1207  }
1208 }
1209 
1210 /*
1211  * Prepare event trigger state for a new complete query to run, if necessary;
1212  * returns whether this was done. If it was, EventTriggerEndCompleteQuery must
1213  * be called when the query is done, regardless of whether it succeeds or fails
1214  * -- so use of a PG_TRY block is mandatory.
1215  */
1216 bool
1218 {
1220  MemoryContext cxt;
1221 
1222  /*
1223  * Currently, sql_drop, table_rewrite, ddl_command_end events are the only
1224  * reason to have event trigger state at all; so if there are none, don't
1225  * install one.
1226  */
1228  return false;
1229 
1231  "event trigger state",
1233  state = MemoryContextAlloc(cxt, sizeof(EventTriggerQueryState));
1234  state->cxt = cxt;
1235  slist_init(&(state->SQLDropList));
1236  state->in_sql_drop = false;
1237  state->table_rewrite_oid = InvalidOid;
1238 
1239  state->commandCollectionInhibited = currentEventTriggerState ?
1240  currentEventTriggerState->commandCollectionInhibited : false;
1241  state->currentCommand = NULL;
1242  state->commandList = NIL;
1244  currentEventTriggerState = state;
1245 
1246  return true;
1247 }
1248 
1249 /*
1250  * Query completed (or errored out) -- clean up local state, return to previous
1251  * one.
1252  *
1253  * Note: it's an error to call this routine if EventTriggerBeginCompleteQuery
1254  * returned false previously.
1255  *
1256  * Note: this might be called in the PG_CATCH block of a failing transaction,
1257  * so be wary of running anything unnecessary. (In particular, it's probably
1258  * unwise to try to allocate memory.)
1259  */
1260 void
1262 {
1263  EventTriggerQueryState *prevstate;
1264 
1265  prevstate = currentEventTriggerState->previous;
1266 
1267  /* this avoids the need for retail pfree of SQLDropList items: */
1268  MemoryContextDelete(currentEventTriggerState->cxt);
1269 
1270  currentEventTriggerState = prevstate;
1271 }
1272 
1273 /*
1274  * Do we need to keep close track of objects being dropped?
1275  *
1276  * This is useful because there is a cost to running with them enabled.
1277  */
1278 bool
1280 {
1281  /*
1282  * true if any sql_drop, table_rewrite, ddl_command_end event trigger
1283  * exists
1284  */
1285  return list_length(EventCacheLookup(EVT_SQLDrop)) > 0 ||
1288 }
1289 
1290 /*
1291  * Support for dropped objects information on event trigger functions.
1292  *
1293  * We keep the list of objects dropped by the current command in current
1294  * state's SQLDropList (comprising SQLDropObject items). Each time a new
1295  * command is to start, a clean EventTriggerQueryState is created; commands
1296  * that drop objects do the dependency.c dance to drop objects, which
1297  * populates the current state's SQLDropList; when the event triggers are
1298  * invoked they can consume the list via pg_event_trigger_dropped_objects().
1299  * When the command finishes, the EventTriggerQueryState is cleared, and
1300  * the one from the previous command is restored (when no command is in
1301  * execution, the current state is NULL).
1302  *
1303  * All this lets us support the case that an event trigger function drops
1304  * objects "reentrantly".
1305  */
1306 
1307 /*
1308  * Register one object as being dropped by the current command.
1309  */
1310 void
1311 EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
1312 {
1313  SQLDropObject *obj;
1314  MemoryContext oldcxt;
1315 
1316  if (!currentEventTriggerState)
1317  return;
1318 
1320 
1321  /* don't report temp schemas except my own */
1322  if (object->classId == NamespaceRelationId &&
1323  (isAnyTempNamespace(object->objectId) &&
1324  !isTempNamespace(object->objectId)))
1325  return;
1326 
1327  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1328 
1329  obj = palloc0(sizeof(SQLDropObject));
1330  obj->address = *object;
1331  obj->original = original;
1332  obj->normal = normal;
1333 
1334  /*
1335  * Obtain schema names from the object's catalog tuple, if one exists;
1336  * this lets us skip objects in temp schemas. We trust that
1337  * ObjectProperty contains all object classes that can be
1338  * schema-qualified.
1339  */
1340  if (is_objectclass_supported(object->classId))
1341  {
1342  Relation catalog;
1343  HeapTuple tuple;
1344 
1345  catalog = heap_open(obj->address.classId, AccessShareLock);
1346  tuple = get_catalog_object_by_oid(catalog, obj->address.objectId);
1347 
1348  if (tuple)
1349  {
1350  AttrNumber attnum;
1351  Datum datum;
1352  bool isnull;
1353 
1355  if (attnum != InvalidAttrNumber)
1356  {
1357  datum = heap_getattr(tuple, attnum,
1358  RelationGetDescr(catalog), &isnull);
1359  if (!isnull)
1360  {
1361  Oid namespaceId;
1362 
1363  namespaceId = DatumGetObjectId(datum);
1364  /* temp objects are only reported if they are my own */
1365  if (isTempNamespace(namespaceId))
1366  {
1367  obj->schemaname = "pg_temp";
1368  obj->istemp = true;
1369  }
1370  else if (isAnyTempNamespace(namespaceId))
1371  {
1372  pfree(obj);
1373  heap_close(catalog, AccessShareLock);
1374  MemoryContextSwitchTo(oldcxt);
1375  return;
1376  }
1377  else
1378  {
1379  obj->schemaname = get_namespace_name(namespaceId);
1380  obj->istemp = false;
1381  }
1382  }
1383  }
1384 
1386  obj->address.objectSubId == 0)
1387  {
1388  attnum = get_object_attnum_name(obj->address.classId);
1389  if (attnum != InvalidAttrNumber)
1390  {
1391  datum = heap_getattr(tuple, attnum,
1392  RelationGetDescr(catalog), &isnull);
1393  if (!isnull)
1394  obj->objname = pstrdup(NameStr(*DatumGetName(datum)));
1395  }
1396  }
1397  }
1398 
1399  heap_close(catalog, AccessShareLock);
1400  }
1401  else
1402  {
1403  if (object->classId == NamespaceRelationId &&
1404  isTempNamespace(object->objectId))
1405  obj->istemp = true;
1406  }
1407 
1408  /* object identity, objname and objargs */
1409  obj->objidentity =
1410  getObjectIdentityParts(&obj->address, &obj->addrnames, &obj->addrargs);
1411 
1412  /* object type */
1414 
1415  slist_push_head(&(currentEventTriggerState->SQLDropList), &obj->next);
1416 
1417  MemoryContextSwitchTo(oldcxt);
1418 }
1419 
1420 /*
1421  * pg_event_trigger_dropped_objects
1422  *
1423  * Make the list of dropped objects available to the user function run by the
1424  * Event Trigger.
1425  */
1426 Datum
1428 {
1429  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1430  TupleDesc tupdesc;
1431  Tuplestorestate *tupstore;
1432  MemoryContext per_query_ctx;
1433  MemoryContext oldcontext;
1434  slist_iter iter;
1435 
1436  /*
1437  * Protect this function from being called out of context
1438  */
1439  if (!currentEventTriggerState ||
1440  !currentEventTriggerState->in_sql_drop)
1441  ereport(ERROR,
1442  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1443  errmsg("%s can only be called in a sql_drop event trigger function",
1444  "pg_event_trigger_dropped_objects()")));
1445 
1446  /* check to see if caller supports us returning a tuplestore */
1447  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1448  ereport(ERROR,
1449  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1450  errmsg("set-valued function called in context that cannot accept a set")));
1451  if (!(rsinfo->allowedModes & SFRM_Materialize))
1452  ereport(ERROR,
1453  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1454  errmsg("materialize mode required, but it is not allowed in this context")));
1455 
1456  /* Build a tuple descriptor for our result type */
1457  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1458  elog(ERROR, "return type must be a row type");
1459 
1460  /* Build tuplestore to hold the result rows */
1461  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1462  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1463 
1464  tupstore = tuplestore_begin_heap(true, false, work_mem);
1465  rsinfo->returnMode = SFRM_Materialize;
1466  rsinfo->setResult = tupstore;
1467  rsinfo->setDesc = tupdesc;
1468 
1469  MemoryContextSwitchTo(oldcontext);
1470 
1471  slist_foreach(iter, &(currentEventTriggerState->SQLDropList))
1472  {
1473  SQLDropObject *obj;
1474  int i = 0;
1475  Datum values[12];
1476  bool nulls[12];
1477 
1478  obj = slist_container(SQLDropObject, next, iter.cur);
1479 
1480  MemSet(values, 0, sizeof(values));
1481  MemSet(nulls, 0, sizeof(nulls));
1482 
1483  /* classid */
1484  values[i++] = ObjectIdGetDatum(obj->address.classId);
1485 
1486  /* objid */
1487  values[i++] = ObjectIdGetDatum(obj->address.objectId);
1488 
1489  /* objsubid */
1490  values[i++] = Int32GetDatum(obj->address.objectSubId);
1491 
1492  /* original */
1493  values[i++] = BoolGetDatum(obj->original);
1494 
1495  /* normal */
1496  values[i++] = BoolGetDatum(obj->normal);
1497 
1498  /* is_temporary */
1499  values[i++] = BoolGetDatum(obj->istemp);
1500 
1501  /* object_type */
1502  values[i++] = CStringGetTextDatum(obj->objecttype);
1503 
1504  /* schema_name */
1505  if (obj->schemaname)
1506  values[i++] = CStringGetTextDatum(obj->schemaname);
1507  else
1508  nulls[i++] = true;
1509 
1510  /* object_name */
1511  if (obj->objname)
1512  values[i++] = CStringGetTextDatum(obj->objname);
1513  else
1514  nulls[i++] = true;
1515 
1516  /* object_identity */
1517  if (obj->objidentity)
1518  values[i++] = CStringGetTextDatum(obj->objidentity);
1519  else
1520  nulls[i++] = true;
1521 
1522  /* address_names and address_args */
1523  if (obj->addrnames)
1524  {
1525  values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrnames));
1526 
1527  if (obj->addrargs)
1528  values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrargs));
1529  else
1531  }
1532  else
1533  {
1534  nulls[i++] = true;
1535  nulls[i++] = true;
1536  }
1537 
1538  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1539  }
1540 
1541  /* clean up and return the tuplestore */
1542  tuplestore_donestoring(tupstore);
1543 
1544  return (Datum) 0;
1545 }
1546 
1547 /*
1548  * pg_event_trigger_table_rewrite_oid
1549  *
1550  * Make the Oid of the table going to be rewritten available to the user
1551  * function run by the Event Trigger.
1552  */
1553 Datum
1555 {
1556  /*
1557  * Protect this function from being called out of context
1558  */
1559  if (!currentEventTriggerState ||
1560  currentEventTriggerState->table_rewrite_oid == InvalidOid)
1561  ereport(ERROR,
1562  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1563  errmsg("%s can only be called in a table_rewrite event trigger function",
1564  "pg_event_trigger_table_rewrite_oid()")));
1565 
1566  PG_RETURN_OID(currentEventTriggerState->table_rewrite_oid);
1567 }
1568 
1569 /*
1570  * pg_event_trigger_table_rewrite_reason
1571  *
1572  * Make the rewrite reason available to the user.
1573  */
1574 Datum
1576 {
1577  /*
1578  * Protect this function from being called out of context
1579  */
1580  if (!currentEventTriggerState ||
1581  currentEventTriggerState->table_rewrite_reason == 0)
1582  ereport(ERROR,
1583  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1584  errmsg("%s can only be called in a table_rewrite event trigger function",
1585  "pg_event_trigger_table_rewrite_reason()")));
1586 
1587  PG_RETURN_INT32(currentEventTriggerState->table_rewrite_reason);
1588 }
1589 
1590 /*-------------------------------------------------------------------------
1591  * Support for DDL command deparsing
1592  *
1593  * The routines below enable an event trigger function to obtain a list of
1594  * DDL commands as they are executed. There are three main pieces to this
1595  * feature:
1596  *
1597  * 1) Within ProcessUtilitySlow, or some sub-routine thereof, each DDL command
1598  * adds a struct CollectedCommand representation of itself to the command list,
1599  * using the routines below.
1600  *
1601  * 2) Some time after that, ddl_command_end fires and the command list is made
1602  * available to the event trigger function via pg_event_trigger_ddl_commands();
1603  * the complete command details are exposed as a column of type pg_ddl_command.
1604  *
1605  * 3) An extension can install a function capable of taking a value of type
1606  * pg_ddl_command and transform it into some external, user-visible and/or
1607  * -modifiable representation.
1608  *-------------------------------------------------------------------------
1609  */
1610 
1611 /*
1612  * Inhibit DDL command collection.
1613  */
1614 void
1616 {
1617  if (!currentEventTriggerState)
1618  return;
1619 
1620  currentEventTriggerState->commandCollectionInhibited = true;
1621 }
1622 
1623 /*
1624  * Re-establish DDL command collection.
1625  */
1626 void
1628 {
1629  if (!currentEventTriggerState)
1630  return;
1631 
1632  currentEventTriggerState->commandCollectionInhibited = false;
1633 }
1634 
1635 /*
1636  * EventTriggerCollectSimpleCommand
1637  * Save data about a simple DDL command that was just executed
1638  *
1639  * address identifies the object being operated on. secondaryObject is an
1640  * object address that was related in some way to the executed command; its
1641  * meaning is command-specific.
1642  *
1643  * For instance, for an ALTER obj SET SCHEMA command, objtype is the type of
1644  * object being moved, objectId is its OID, and secondaryOid is the OID of the
1645  * old schema. (The destination schema OID can be obtained by catalog lookup
1646  * of the object.)
1647  */
1648 void
1650  ObjectAddress secondaryObject,
1651  Node *parsetree)
1652 {
1653  MemoryContext oldcxt;
1654  CollectedCommand *command;
1655 
1656  /* ignore if event trigger context not set, or collection disabled */
1657  if (!currentEventTriggerState ||
1658  currentEventTriggerState->commandCollectionInhibited)
1659  return;
1660 
1661  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1662 
1663  command = palloc(sizeof(CollectedCommand));
1664 
1665  command->type = SCT_Simple;
1666  command->in_extension = creating_extension;
1667 
1668  command->d.simple.address = address;
1669  command->d.simple.secondaryObject = secondaryObject;
1670  command->parsetree = copyObject(parsetree);
1671 
1672  currentEventTriggerState->commandList = lappend(currentEventTriggerState->commandList,
1673  command);
1674 
1675  MemoryContextSwitchTo(oldcxt);
1676 }
1677 
1678 /*
1679  * EventTriggerAlterTableStart
1680  * Prepare to receive data on an ALTER TABLE command about to be executed
1681  *
1682  * Note we don't collect the command immediately; instead we keep it in
1683  * currentCommand, and only when we're done processing the subcommands we will
1684  * add it to the command list.
1685  *
1686  * XXX -- this API isn't considering the possibility of an ALTER TABLE command
1687  * being called reentrantly by an event trigger function. Do we need stackable
1688  * commands at this level? Perhaps at least we should detect the condition and
1689  * raise an error.
1690  */
1691 void
1693 {
1694  MemoryContext oldcxt;
1695  CollectedCommand *command;
1696 
1697  /* ignore if event trigger context not set, or collection disabled */
1698  if (!currentEventTriggerState ||
1699  currentEventTriggerState->commandCollectionInhibited)
1700  return;
1701 
1702  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1703 
1704  command = palloc(sizeof(CollectedCommand));
1705 
1706  command->type = SCT_AlterTable;
1707  command->in_extension = creating_extension;
1708 
1709  command->d.alterTable.classId = RelationRelationId;
1710  command->d.alterTable.objectId = InvalidOid;
1711  command->d.alterTable.subcmds = NIL;
1712  command->parsetree = copyObject(parsetree);
1713 
1714  currentEventTriggerState->currentCommand = command;
1715 
1716  MemoryContextSwitchTo(oldcxt);
1717 }
1718 
1719 /*
1720  * Remember the OID of the object being affected by an ALTER TABLE.
1721  *
1722  * This is needed because in some cases we don't know the OID until later.
1723  */
1724 void
1726 {
1727  if (!currentEventTriggerState ||
1728  currentEventTriggerState->commandCollectionInhibited)
1729  return;
1730 
1731  currentEventTriggerState->currentCommand->d.alterTable.objectId = objectId;
1732 }
1733 
1734 /*
1735  * EventTriggerCollectAlterTableSubcmd
1736  * Save data about a single part of an ALTER TABLE.
1737  *
1738  * Several different commands go through this path, but apart from ALTER TABLE
1739  * itself, they are all concerned with AlterTableCmd nodes that are generated
1740  * internally, so that's all that this code needs to handle at the moment.
1741  */
1742 void
1744 {
1745  MemoryContext oldcxt;
1747 
1748  /* ignore if event trigger context not set, or collection disabled */
1749  if (!currentEventTriggerState ||
1750  currentEventTriggerState->commandCollectionInhibited)
1751  return;
1752 
1753  Assert(IsA(subcmd, AlterTableCmd));
1754  Assert(OidIsValid(currentEventTriggerState->currentCommand->d.alterTable.objectId));
1755 
1756  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1757 
1758  newsub = palloc(sizeof(CollectedATSubcmd));
1759  newsub->address = address;
1760  newsub->parsetree = copyObject(subcmd);
1761 
1762  currentEventTriggerState->currentCommand->d.alterTable.subcmds =
1763  lappend(currentEventTriggerState->currentCommand->d.alterTable.subcmds, newsub);
1764 
1765  MemoryContextSwitchTo(oldcxt);
1766 }
1767 
1768 /*
1769  * EventTriggerAlterTableEnd
1770  * Finish up saving an ALTER TABLE command, and add it to command list.
1771  *
1772  * FIXME this API isn't considering the possibility that an xact/subxact is
1773  * aborted partway through. Probably it's best to add an
1774  * AtEOSubXact_EventTriggers() to fix this.
1775  */
1776 void
1778 {
1779  /* ignore if event trigger context not set, or collection disabled */
1780  if (!currentEventTriggerState ||
1781  currentEventTriggerState->commandCollectionInhibited)
1782  return;
1783 
1784  /* If no subcommands, don't collect */
1785  if (list_length(currentEventTriggerState->currentCommand->d.alterTable.subcmds) != 0)
1786  {
1787  currentEventTriggerState->commandList =
1788  lappend(currentEventTriggerState->commandList,
1789  currentEventTriggerState->currentCommand);
1790  }
1791  else
1792  pfree(currentEventTriggerState->currentCommand);
1793 
1794  currentEventTriggerState->currentCommand = NULL;
1795 }
1796 
1797 /*
1798  * EventTriggerCollectGrant
1799  * Save data about a GRANT/REVOKE command being executed
1800  *
1801  * This function creates a copy of the InternalGrant, as the original might
1802  * not have the right lifetime.
1803  */
1804 void
1806 {
1807  MemoryContext oldcxt;
1808  CollectedCommand *command;
1809  InternalGrant *icopy;
1810  ListCell *cell;
1811 
1812  /* ignore if event trigger context not set, or collection disabled */
1813  if (!currentEventTriggerState ||
1814  currentEventTriggerState->commandCollectionInhibited)
1815  return;
1816 
1817  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1818 
1819  /*
1820  * This is tedious, but necessary.
1821  */
1822  icopy = palloc(sizeof(InternalGrant));
1823  memcpy(icopy, istmt, sizeof(InternalGrant));
1824  icopy->objects = list_copy(istmt->objects);
1825  icopy->grantees = list_copy(istmt->grantees);
1826  icopy->col_privs = NIL;
1827  foreach(cell, istmt->col_privs)
1828  icopy->col_privs = lappend(icopy->col_privs, copyObject(lfirst(cell)));
1829 
1830  /* Now collect it, using the copied InternalGrant */
1831  command = palloc(sizeof(CollectedCommand));
1832  command->type = SCT_Grant;
1833  command->in_extension = creating_extension;
1834  command->d.grant.istmt = icopy;
1835  command->parsetree = NULL;
1836 
1837  currentEventTriggerState->commandList =
1838  lappend(currentEventTriggerState->commandList, command);
1839 
1840  MemoryContextSwitchTo(oldcxt);
1841 }
1842 
1843 /*
1844  * EventTriggerCollectAlterOpFam
1845  * Save data about an ALTER OPERATOR FAMILY ADD/DROP command being
1846  * executed
1847  */
1848 void
1850  List *operators, List *procedures)
1851 {
1852  MemoryContext oldcxt;
1853  CollectedCommand *command;
1854 
1855  /* ignore if event trigger context not set, or collection disabled */
1856  if (!currentEventTriggerState ||
1857  currentEventTriggerState->commandCollectionInhibited)
1858  return;
1859 
1860  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1861 
1862  command = palloc(sizeof(CollectedCommand));
1863  command->type = SCT_AlterOpFamily;
1864  command->in_extension = creating_extension;
1865  ObjectAddressSet(command->d.opfam.address,
1866  OperatorFamilyRelationId, opfamoid);
1867  command->d.opfam.operators = operators;
1868  command->d.opfam.procedures = procedures;
1869  command->parsetree = copyObject(stmt);
1870 
1871  currentEventTriggerState->commandList =
1872  lappend(currentEventTriggerState->commandList, command);
1873 
1874  MemoryContextSwitchTo(oldcxt);
1875 }
1876 
1877 /*
1878  * EventTriggerCollectCreateOpClass
1879  * Save data about a CREATE OPERATOR CLASS command being executed
1880  */
1881 void
1883  List *operators, List *procedures)
1884 {
1885  MemoryContext oldcxt;
1886  CollectedCommand *command;
1887 
1888  /* ignore if event trigger context not set, or collection disabled */
1889  if (!currentEventTriggerState ||
1890  currentEventTriggerState->commandCollectionInhibited)
1891  return;
1892 
1893  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1894 
1895  command = palloc0(sizeof(CollectedCommand));
1896  command->type = SCT_CreateOpClass;
1897  command->in_extension = creating_extension;
1898  ObjectAddressSet(command->d.createopc.address,
1899  OperatorClassRelationId, opcoid);
1900  command->d.createopc.operators = operators;
1901  command->d.createopc.procedures = procedures;
1902  command->parsetree = copyObject(stmt);
1903 
1904  currentEventTriggerState->commandList =
1905  lappend(currentEventTriggerState->commandList, command);
1906 
1907  MemoryContextSwitchTo(oldcxt);
1908 }
1909 
1910 /*
1911  * EventTriggerCollectAlterTSConfig
1912  * Save data about an ALTER TEXT SEARCH CONFIGURATION command being
1913  * executed
1914  */
1915 void
1917  Oid *dictIds, int ndicts)
1918 {
1919  MemoryContext oldcxt;
1920  CollectedCommand *command;
1921 
1922  /* ignore if event trigger context not set, or collection disabled */
1923  if (!currentEventTriggerState ||
1924  currentEventTriggerState->commandCollectionInhibited)
1925  return;
1926 
1927  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1928 
1929  command = palloc0(sizeof(CollectedCommand));
1930  command->type = SCT_AlterTSConfig;
1931  command->in_extension = creating_extension;
1932  ObjectAddressSet(command->d.atscfg.address,
1933  TSConfigRelationId, cfgId);
1934  command->d.atscfg.dictIds = palloc(sizeof(Oid) * ndicts);
1935  memcpy(command->d.atscfg.dictIds, dictIds, sizeof(Oid) * ndicts);
1936  command->d.atscfg.ndicts = ndicts;
1937  command->parsetree = copyObject(stmt);
1938 
1939  currentEventTriggerState->commandList =
1940  lappend(currentEventTriggerState->commandList, command);
1941 
1942  MemoryContextSwitchTo(oldcxt);
1943 }
1944 
1945 /*
1946  * EventTriggerCollectAlterDefPrivs
1947  * Save data about an ALTER DEFAULT PRIVILEGES command being
1948  * executed
1949  */
1950 void
1952 {
1953  MemoryContext oldcxt;
1954  CollectedCommand *command;
1955 
1956  /* ignore if event trigger context not set, or collection disabled */
1957  if (!currentEventTriggerState ||
1958  currentEventTriggerState->commandCollectionInhibited)
1959  return;
1960 
1961  oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
1962 
1963  command = palloc0(sizeof(CollectedCommand));
1964  command->type = SCT_AlterDefaultPrivileges;
1965  command->d.defprivs.objtype = stmt->action->objtype;
1966  command->in_extension = creating_extension;
1967  command->parsetree = copyObject(stmt);
1968 
1969  currentEventTriggerState->commandList =
1970  lappend(currentEventTriggerState->commandList, command);
1971  MemoryContextSwitchTo(oldcxt);
1972 }
1973 
1974 /*
1975  * In a ddl_command_end event trigger, this function reports the DDL commands
1976  * being run.
1977  */
1978 Datum
1980 {
1981  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1982  TupleDesc tupdesc;
1983  Tuplestorestate *tupstore;
1984  MemoryContext per_query_ctx;
1985  MemoryContext oldcontext;
1986  ListCell *lc;
1987 
1988  /*
1989  * Protect this function from being called out of context
1990  */
1991  if (!currentEventTriggerState)
1992  ereport(ERROR,
1993  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1994  errmsg("%s can only be called in an event trigger function",
1995  "pg_event_trigger_ddl_commands()")));
1996 
1997  /* check to see if caller supports us returning a tuplestore */
1998  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1999  ereport(ERROR,
2000  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2001  errmsg("set-valued function called in context that cannot accept a set")));
2002  if (!(rsinfo->allowedModes & SFRM_Materialize))
2003  ereport(ERROR,
2004  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2005  errmsg("materialize mode required, but it is not allowed in this context")));
2006 
2007  /* Build a tuple descriptor for our result type */
2008  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
2009  elog(ERROR, "return type must be a row type");
2010 
2011  /* Build tuplestore to hold the result rows */
2012  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
2013  oldcontext = MemoryContextSwitchTo(per_query_ctx);
2014 
2015  tupstore = tuplestore_begin_heap(true, false, work_mem);
2016  rsinfo->returnMode = SFRM_Materialize;
2017  rsinfo->setResult = tupstore;
2018  rsinfo->setDesc = tupdesc;
2019 
2020  MemoryContextSwitchTo(oldcontext);
2021 
2022  foreach(lc, currentEventTriggerState->commandList)
2023  {
2024  CollectedCommand *cmd = lfirst(lc);
2025  Datum values[9];
2026  bool nulls[9];
2027  ObjectAddress addr;
2028  int i = 0;
2029 
2030  /*
2031  * For IF NOT EXISTS commands that attempt to create an existing
2032  * object, the returned OID is Invalid. Don't return anything.
2033  *
2034  * One might think that a viable alternative would be to look up the
2035  * Oid of the existing object and run the deparse with that. But
2036  * since the parse tree might be different from the one that created
2037  * the object in the first place, we might not end up in a consistent
2038  * state anyway.
2039  */
2040  if (cmd->type == SCT_Simple &&
2041  !OidIsValid(cmd->d.simple.address.objectId))
2042  continue;
2043 
2044  MemSet(nulls, 0, sizeof(nulls));
2045 
2046  switch (cmd->type)
2047  {
2048  case SCT_Simple:
2049  case SCT_AlterTable:
2050  case SCT_AlterOpFamily:
2051  case SCT_CreateOpClass:
2052  case SCT_AlterTSConfig:
2053  {
2054  char *identity;
2055  char *type;
2056  char *schema = NULL;
2057 
2058  if (cmd->type == SCT_Simple)
2059  addr = cmd->d.simple.address;
2060  else if (cmd->type == SCT_AlterTable)
2061  ObjectAddressSet(addr,
2062  cmd->d.alterTable.classId,
2063  cmd->d.alterTable.objectId);
2064  else if (cmd->type == SCT_AlterOpFamily)
2065  addr = cmd->d.opfam.address;
2066  else if (cmd->type == SCT_CreateOpClass)
2067  addr = cmd->d.createopc.address;
2068  else if (cmd->type == SCT_AlterTSConfig)
2069  addr = cmd->d.atscfg.address;
2070 
2071  type = getObjectTypeDescription(&addr);
2072  identity = getObjectIdentity(&addr);
2073 
2074  /*
2075  * Obtain schema name, if any ("pg_temp" if a temp
2076  * object). If the object class is not in the supported
2077  * list here, we assume it's a schema-less object type,
2078  * and thus "schema" remains set to NULL.
2079  */
2081  {
2082  AttrNumber nspAttnum;
2083 
2084  nspAttnum = get_object_attnum_namespace(addr.classId);
2085  if (nspAttnum != InvalidAttrNumber)
2086  {
2087  Relation catalog;
2088  HeapTuple objtup;
2089  Oid schema_oid;
2090  bool isnull;
2091 
2092  catalog = heap_open(addr.classId, AccessShareLock);
2093  objtup = get_catalog_object_by_oid(catalog,
2094  addr.objectId);
2095  if (!HeapTupleIsValid(objtup))
2096  elog(ERROR, "cache lookup failed for object %u/%u",
2097  addr.classId, addr.objectId);
2098  schema_oid =
2099  heap_getattr(objtup, nspAttnum,
2100  RelationGetDescr(catalog), &isnull);
2101  if (isnull)
2102  elog(ERROR,
2103  "invalid null namespace in object %u/%u/%d",
2104  addr.classId, addr.objectId, addr.objectSubId);
2105  /* XXX not quite get_namespace_name_or_temp */
2106  if (isAnyTempNamespace(schema_oid))
2107  schema = pstrdup("pg_temp");
2108  else
2109  schema = get_namespace_name(schema_oid);
2110 
2111  heap_close(catalog, AccessShareLock);
2112  }
2113  }
2114 
2115  /* classid */
2116  values[i++] = ObjectIdGetDatum(addr.classId);
2117  /* objid */
2118  values[i++] = ObjectIdGetDatum(addr.objectId);
2119  /* objsubid */
2120  values[i++] = Int32GetDatum(addr.objectSubId);
2121  /* command tag */
2122  values[i++] = CStringGetTextDatum(CreateCommandTag(cmd->parsetree));
2123  /* object_type */
2124  values[i++] = CStringGetTextDatum(type);
2125  /* schema */
2126  if (schema == NULL)
2127  nulls[i++] = true;
2128  else
2129  values[i++] = CStringGetTextDatum(schema);
2130  /* identity */
2131  values[i++] = CStringGetTextDatum(identity);
2132  /* in_extension */
2133  values[i++] = BoolGetDatum(cmd->in_extension);
2134  /* command */
2135  values[i++] = PointerGetDatum(cmd);
2136  }
2137  break;
2138 
2140  /* classid */
2141  nulls[i++] = true;
2142  /* objid */
2143  nulls[i++] = true;
2144  /* objsubid */
2145  nulls[i++] = true;
2146  /* command tag */
2147  values[i++] = CStringGetTextDatum(CreateCommandTag(cmd->parsetree));
2148  /* object_type */
2150  cmd->d.defprivs.objtype));
2151  /* schema */
2152  nulls[i++] = true;
2153  /* identity */
2154  nulls[i++] = true;
2155  /* in_extension */
2156  values[i++] = BoolGetDatum(cmd->in_extension);
2157  /* command */
2158  values[i++] = PointerGetDatum(cmd);
2159  break;
2160 
2161  case SCT_Grant:
2162  /* classid */
2163  nulls[i++] = true;
2164  /* objid */
2165  nulls[i++] = true;
2166  /* objsubid */
2167  nulls[i++] = true;
2168  /* command tag */
2169  values[i++] = CStringGetTextDatum(cmd->d.grant.istmt->is_grant ?
2170  "GRANT" : "REVOKE");
2171  /* object_type */
2173  cmd->d.grant.istmt->objtype));
2174  /* schema */
2175  nulls[i++] = true;
2176  /* identity */
2177  nulls[i++] = true;
2178  /* in_extension */
2179  values[i++] = BoolGetDatum(cmd->in_extension);
2180  /* command */
2181  values[i++] = PointerGetDatum(cmd);
2182  break;
2183  }
2184 
2185  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2186  }
2187 
2188  /* clean up and return the tuplestore */
2189  tuplestore_donestoring(tupstore);
2190 
2191  PG_RETURN_VOID();
2192 }
2193 
2194 /*
2195  * Return the GrantObjectType as a string, as it would appear in GRANT and
2196  * REVOKE commands.
2197  */
2198 static const char *
2200 {
2201  switch (objtype)
2202  {
2203  case ACL_OBJECT_COLUMN:
2204  return "COLUMN";
2205  case ACL_OBJECT_RELATION:
2206  return "TABLE";
2207  case ACL_OBJECT_SEQUENCE:
2208  return "SEQUENCE";
2209  case ACL_OBJECT_DATABASE:
2210  return "DATABASE";
2211  case ACL_OBJECT_DOMAIN:
2212  return "DOMAIN";
2213  case ACL_OBJECT_FDW:
2214  return "FOREIGN DATA WRAPPER";
2216  return "FOREIGN SERVER";
2217  case ACL_OBJECT_FUNCTION:
2218  return "FUNCTION";
2219  case ACL_OBJECT_LANGUAGE:
2220  return "LANGUAGE";
2222  return "LARGE OBJECT";
2223  case ACL_OBJECT_NAMESPACE:
2224  return "SCHEMA";
2225  case ACL_OBJECT_TABLESPACE:
2226  return "TABLESPACE";
2227  case ACL_OBJECT_TYPE:
2228  return "TYPE";
2229  default:
2230  elog(ERROR, "unrecognized type %d", objtype);
2231  return "???"; /* keep compiler quiet */
2232  }
2233 }
2234 
2235 /*
2236  * Return the GrantObjectType as a string; as above, but use the spelling
2237  * in ALTER DEFAULT PRIVILEGES commands instead.
2238  */
2239 static const char *
2241 {
2242  switch (objtype)
2243  {
2244  case ACL_OBJECT_RELATION:
2245  return "TABLES";
2246  break;
2247  case ACL_OBJECT_FUNCTION:
2248  return "FUNCTIONS";
2249  break;
2250  case ACL_OBJECT_SEQUENCE:
2251  return "SEQUENCES";
2252  break;
2253  case ACL_OBJECT_TYPE:
2254  return "TYPES";
2255  break;
2256  default:
2257  elog(ERROR, "unrecognized type %d", objtype);
2258  return "???"; /* keep compiler quiet */
2259  }
2260 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:735
#define Anum_pg_event_trigger_evtname
bool get_object_namensp_unique(Oid class_id)
#define NIL
Definition: pg_list.h:69
#define Anum_pg_event_trigger_evtevent
slist_node next
Definition: fmgr.h:53
static struct @76 value
static void validate_ddl_tags(const char *filtervar, List *taglist)
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
#define NameGetDatum(X)
Definition: postgres.h:603
GrantObjectType
Definition: parsenodes.h:1733
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define DEBUG1
Definition: elog.h:25
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Datum pg_event_trigger_dropped_objects(PG_FUNCTION_ARGS)
static int32 next
Definition: blutils.c:210
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
ObjectAddress address
char * getObjectTypeDescription(const ObjectAddress *object)
struct CollectedCommand::@58::@61 grant
#define RelationGetDescr(relation)
Definition: rel.h:425
Oid GetUserId(void)
Definition: miscinit.c:283
Datum pg_event_trigger_table_rewrite_reason(PG_FUNCTION_ARGS)
bool trackDroppedObjectsNeeded(void)
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
static Oid insert_event_trigger_tuple(char *trigname, char *eventname, Oid evtOwner, Oid funcoid, List *tags)
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
#define ProcedureRelationId
Definition: pg_proc.h:33
const char * objname
#define DatumGetObjectId(X)
Definition: postgres.h:508
char * pstrdup(const char *in)
Definition: mcxt.c:1165
#define RelationRelationId
Definition: pg_class.h:29
struct CollectedCommand::@58::@59 simple
char * getObjectIdentityParts(const ObjectAddress *object, List **objname, List **objargs)
struct CollectedCommand::@58::@65 defprivs
Datum pg_event_trigger_table_rewrite_oid(PG_FUNCTION_ARGS)
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
#define AccessShareLock
Definition: lockdefs.h:36
void EventTriggerDDLCommandEnd(Node *parsetree)
#define OperatorClassRelationId
Definition: pg_opclass.h:49
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
static const char * stringify_adefprivs_objtype(GrantObjectType objtype)
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
#define OperatorFamilyRelationId
Definition: pg_opfamily.h:29
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:853
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
const char * tag
Definition: event_trigger.h:28
void EventTriggerInhibitCommandCollection(void)
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
AttrNumber get_object_attnum_namespace(Oid class_id)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
CollectedCommandType type
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:158
const char * objidentity
Oid get_event_trigger_oid(const char *trigname, bool missing_ok)
#define Anum_pg_event_trigger_evtfoid
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3424
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:217
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2312
#define OidIsValid(objectId)
Definition: c.h:534
#define TRIGGER_FIRES_ON_ORIGIN
Definition: trigger.h:109
static event_trigger_command_tag_check_result check_ddl_tag(const char *tag)
int SessionReplicationRole
Definition: trigger.c:64
struct CollectedCommand::@58::@64 atscfg
const char * objecttype
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
static void AlterEventTriggerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1427
static const char * stringify_grantobjtype(GrantObjectType objtype)
EventTriggerEvent
Definition: evtcache.h:19
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:176
ObjectAddress AlterEventTriggerOwner(const char *name, Oid newOwnerId)
void EventTriggerAlterTableStart(Node *parsetree)
#define DatumGetName(X)
Definition: postgres.h:593
#define TSConfigRelationId
Definition: pg_ts_config.h:31
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:303
void pfree(void *pointer)
Definition: mcxt.c:992
static void slist_init(slist_head *head)
Definition: ilist.h:554
static bool filter_event_trigger(const char **tag, EventTriggerCacheItem *item)
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
static void validate_table_rewrite_tags(const char *filtervar, List *taglist)
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid)
Definition: aclchk.c:4912
static Datum filter_list_to_array(List *filterlist)
char * getObjectIdentity(const ObjectAddress *object)
static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag(const char *tag)
void EventTriggerDDLCommandStart(Node *parsetree)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
const char * event
Definition: event_trigger.h:26
ItemPointerData t_self
Definition: htup.h:65
void EventTriggerSQLDrop(Node *parsetree)
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
Definition: c.h:489
static void error_duplicate_filter_variable(const char *defname)
Datum pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS)
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:129
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
void check_stack_depth(void)
Definition: postgres.c:3096
CollectedCommand * currentCommand
Definition: event_trigger.c:66
bool IsUnderPostmaster
Definition: globals.c:100
ObjectClass
Definition: dependency.h:129
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3378
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:586
struct CollectedCommand::@58::@63 createopc
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:2952
struct CollectedCommand::@58::@62 opfam
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void EventTriggerCollectAlterTSConfig(AlterTSConfigurationStmt *stmt, Oid cfgId, Oid *dictIds, int ndicts)
void RemoveEventTriggerById(Oid trigOid)
#define ereport(elevel, rest)
Definition: elog.h:122
static event_trigger_support_data event_trigger_support[]
Definition: event_trigger.c:88
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
struct EventTriggerQueryState * previous
Definition: event_trigger.c:69
MemoryContext TopMemoryContext
Definition: mcxt.c:43
const char * CreateCommandTag(Node *parsetree)
Definition: utility.c:2012
void EventTriggerCollectAlterOpFam(AlterOpFamilyStmt *stmt, Oid opfamoid, List *operators, List *procedures)
Oid AlterEventTrigger(AlterEventTrigStmt *stmt)
Node * arg
Definition: parsenodes.h:676
ObjectType
Definition: parsenodes.h:1520
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
List * lappend(List *list, void *datum)
Definition: list.c:128
const char * schemaname
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
void EventTriggerEndCompleteQuery(void)
event_trigger_command_tag_check_result
Definition: event_trigger.c:80
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
struct EventTriggerQueryState EventTriggerQueryState
char * NameListToString(List *names)
Definition: namespace.c:2897
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:316
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
AttrNumber get_object_attnum_name(Oid class_id)
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
void EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address)
void EventTriggerUndoInhibitCommandCollection(void)
uintptr_t Datum
Definition: postgres.h:374
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:101
void CommandCounterIncrement(void)
Definition: xact.c:921
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Oid CreateEventTrigger(CreateEventTrigStmt *stmt)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
int work_mem
Definition: globals.c:112
void EventTriggerCollectAlterDefPrivs(AlterDefaultPrivilegesStmt *stmt)
TupleDesc rd_att
Definition: rel.h:114
#define BoolGetDatum(X)
Definition: postgres.h:410
#define InvalidOid
Definition: postgres_ext.h:36
bool creating_extension
Definition: extension.c:67
void AlterEventTriggerOwner_oid(Oid trigOid, Oid newOwnerId)
bool EventTriggerSupportsObjectType(ObjectType obtype)
int allowedModes
Definition: execnodes.h:199
#define PG_RETURN_VOID()
Definition: fmgr.h:293
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
#define PG_CATCH()
Definition: elog.h:293
text * cstring_to_text(const char *s)
Definition: varlena.c:151
FormData_pg_event_trigger * Form_pg_event_trigger
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define EVTTRIGGEROID
Definition: pg_type.h:682
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Definition: regguts.h:298
#define EventTriggerRelationId
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define Anum_pg_event_trigger_evttags
void EventTriggerCollectCreateOpClass(CreateOpClassStmt *stmt, Oid opcoid, List *operators, List *procedures)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:112
static int list_length(const List *l)
Definition: pg_list.h:89
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
#define PG_RE_THROW()
Definition: elog.h:314
int pg_qsort_strcmp(const void *a, const void *b)
Definition: qsort.c:232
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:133
HeapTuple get_catalog_object_by_oid(Relation catalog, Oid objectId)
const char * name
Definition: encode.c:521
#define Anum_pg_event_trigger_evtenabled
#define InvalidAttrNumber
Definition: attnum.h:23
union CollectedCommand::@58 d
void EventTriggerAlterTableRelid(Oid objectId)
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define CharGetDatum(X)
Definition: postgres.h:424
Tuplestorestate * setResult
Definition: execnodes.h:204
#define TRIGGER_FIRES_ON_REPLICA
Definition: trigger.h:111
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1577
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:1908
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void pgstat_init_function_usage(FunctionCallInfoData *fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1505
bool EventTriggerSupportsGrantObjectType(GrantObjectType objtype)
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
ExprContext * econtext
Definition: execnodes.h:197
GrantObjectType objtype
Definition: parsenodes.h:1755
void EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason)
#define slist_foreach(iter, lhead)
Definition: ilist.h:700
#define Int32GetDatum(X)
Definition: postgres.h:487
struct SQLDropObject SQLDropObject
TupleDesc setDesc
Definition: execnodes.h:205
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
void list_free(List *list)
Definition: list.c:1133
int i
List * EventCacheLookup(EventTriggerEvent event)
Definition: evtcache.c:64
#define NameStr(name)
Definition: c.h:495
#define CStringGetTextDatum(s)
Definition: builtins.h:90
#define Anum_pg_event_trigger_evtowner
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:2990
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
void EventTriggerCollectGrant(InternalGrant *istmt)
char * defname
Definition: parsenodes.h:675
ArrayType * strlist_to_textarray(List *list)
#define elog
Definition: elog.h:219
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
bool is_objectclass_supported(Oid class_id)
#define PG_TRY()
Definition: elog.h:284
static color newsub(struct colormap *cm, color co)
Definition: regc_color.c:389
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:34
Definition: pg_list.h:45
#define PG_RETURN_OID(x)
Definition: fmgr.h:304
struct CollectedCommand::@58::@60 alterTable
int16 AttrNumber
Definition: attnum.h:21
#define PG_END_TRY()
Definition: elog.h:300
#define lfirst_oid(lc)
Definition: pg_list.h:108
ObjectAddress address
bool EventTriggerBeginCompleteQuery(void)
#define Natts_pg_trigger
Definition: pg_trigger.h:78
void EventTriggerAlterTableEnd(void)