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