PostgreSQL Source Code  git master
event_trigger.h File Reference
Include dependency graph for event_trigger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  EventTriggerData
 

Macros

#define AT_REWRITE_ALTER_PERSISTENCE   0x01
 
#define AT_REWRITE_DEFAULT_VAL   0x02
 
#define AT_REWRITE_COLUMN_REWRITE   0x04
 
#define AT_REWRITE_ALTER_OID   0x08
 
#define CALLED_AS_EVENT_TRIGGER(fcinfo)   ((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))
 

Typedefs

typedef struct EventTriggerData EventTriggerData
 

Functions

Oid CreateEventTrigger (CreateEventTrigStmt *stmt)
 
void RemoveEventTriggerById (Oid ctrigOid)
 
Oid get_event_trigger_oid (const char *trigname, bool missing_ok)
 
Oid AlterEventTrigger (AlterEventTrigStmt *stmt)
 
ObjectAddress AlterEventTriggerOwner (const char *name, Oid newOwnerId)
 
void AlterEventTriggerOwner_oid (Oid, Oid newOwnerId)
 
bool EventTriggerSupportsObjectType (ObjectType obtype)
 
bool EventTriggerSupportsObjectClass (ObjectClass objclass)
 
void EventTriggerDDLCommandStart (Node *parsetree)
 
void EventTriggerDDLCommandEnd (Node *parsetree)
 
void EventTriggerSQLDrop (Node *parsetree)
 
void EventTriggerTableRewrite (Node *parsetree, Oid tableOid, int reason)
 
bool EventTriggerBeginCompleteQuery (void)
 
void EventTriggerEndCompleteQuery (void)
 
bool trackDroppedObjectsNeeded (void)
 
void EventTriggerSQLDropAddObject (const ObjectAddress *object, bool original, bool normal)
 
void EventTriggerInhibitCommandCollection (void)
 
void EventTriggerUndoInhibitCommandCollection (void)
 
void EventTriggerCollectSimpleCommand (ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
 
void EventTriggerAlterTableStart (Node *parsetree)
 
void EventTriggerAlterTableRelid (Oid objectId)
 
void EventTriggerCollectAlterTableSubcmd (Node *subcmd, ObjectAddress address)
 
void EventTriggerAlterTableEnd (void)
 
void EventTriggerCollectGrant (InternalGrant *istmt)
 
void EventTriggerCollectAlterOpFam (AlterOpFamilyStmt *stmt, Oid opfamoid, List *operators, List *procedures)
 
void EventTriggerCollectCreateOpClass (CreateOpClassStmt *stmt, Oid opcoid, List *operators, List *procedures)
 
void EventTriggerCollectAlterTSConfig (AlterTSConfigurationStmt *stmt, Oid cfgId, Oid *dictIds, int ndicts)
 
void EventTriggerCollectAlterDefPrivs (AlterDefaultPrivilegesStmt *stmt)
 

Macro Definition Documentation

◆ AT_REWRITE_ALTER_OID

#define AT_REWRITE_ALTER_OID   0x08

Definition at line 34 of file event_trigger.h.

Referenced by ATExecAddColumn(), and ATExecDropColumn().

◆ AT_REWRITE_ALTER_PERSISTENCE

#define AT_REWRITE_ALTER_PERSISTENCE   0x01

Definition at line 31 of file event_trigger.h.

Referenced by ATPrepCmd().

◆ AT_REWRITE_COLUMN_REWRITE

#define AT_REWRITE_COLUMN_REWRITE   0x04

Definition at line 33 of file event_trigger.h.

Referenced by ATPrepAlterColumnType().

◆ AT_REWRITE_DEFAULT_VAL

#define AT_REWRITE_DEFAULT_VAL   0x02

Definition at line 32 of file event_trigger.h.

Referenced by ATExecAddColumn().

◆ CALLED_AS_EVENT_TRIGGER

#define CALLED_AS_EVENT_TRIGGER (   fcinfo)    ((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))

Typedef Documentation

◆ EventTriggerData

Function Documentation

◆ AlterEventTrigger()

Oid AlterEventTrigger ( AlterEventTrigStmt stmt)

Definition at line 501 of file event_trigger.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, CatalogTupleUpdate(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, EVENTTRIGGERNAME, GETSTRUCT, GetUserId(), heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvokeObjectPostAlterHook, OBJECT_EVENT_TRIGGER, pg_event_trigger_ownercheck(), RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, AlterEventTrigStmt::tgenabled, and AlterEventTrigStmt::trigname.

Referenced by standard_ProcessUtility().

502 {
503  Relation tgrel;
504  HeapTuple tup;
505  Oid trigoid;
506  Form_pg_event_trigger evtForm;
507  char tgenabled = stmt->tgenabled;
508 
509  tgrel = heap_open(EventTriggerRelationId, RowExclusiveLock);
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 
531  InvokeObjectPostAlterHook(EventTriggerRelationId,
532  trigoid, 0);
533 
534  /* clean up */
535  heap_freetuple(tup);
537 
538  return trigoid;
539 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
Oid GetUserId(void)
Definition: miscinit.c:379
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ERROR
Definition: elog.h:43
bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid)
Definition: aclchk.c:5142
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
FormData_pg_event_trigger * Form_pg_event_trigger
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:211
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707

◆ AlterEventTriggerOwner()

ObjectAddress AlterEventTriggerOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 545 of file event_trigger.c.

References AlterEventTriggerOwner_internal(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, EVENTTRIGGERNAME, heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, ObjectAddressSet, RowExclusiveLock, and SearchSysCacheCopy1.

Referenced by ExecAlterOwnerStmt().

546 {
547  Oid evtOid;
548  HeapTuple tup;
549  Relation rel;
550  ObjectAddress address;
551 
552  rel = heap_open(EventTriggerRelationId, RowExclusiveLock);
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 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
static void AlterEventTriggerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
const char * name
Definition: encode.c:521
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707

◆ AlterEventTriggerOwner_oid()

void AlterEventTriggerOwner_oid ( Oid  ,
Oid  newOwnerId 
)

Definition at line 578 of file event_trigger.c.

References AlterEventTriggerOwner_internal(), ereport, errcode(), errmsg(), ERROR, EVENTTRIGGEROID, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, ObjectIdGetDatum, RowExclusiveLock, and SearchSysCacheCopy1.

Referenced by shdepReassignOwned().

579 {
580  HeapTuple tup;
581  Relation rel;
582 
583  rel = heap_open(EventTriggerRelationId, RowExclusiveLock);
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 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
static void AlterEventTriggerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ CreateEventTrigger()

Oid CreateEventTrigger ( CreateEventTrigStmt stmt)

Definition at line 169 of file event_trigger.c.

References DefElem::arg, CStringGetDatum, DefElem::defname, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, error_duplicate_filter_variable(), CreateEventTrigStmt::eventname, EVENTTRIGGERNAME, CreateEventTrigStmt::funcname, get_func_rettype(), GetUserId(), HeapTupleIsValid, insert_event_trigger_tuple(), lfirst, LookupFuncName(), NameListToString(), SearchSysCache1(), superuser(), CreateEventTrigStmt::trigname, validate_ddl_tags(), validate_table_rewrite_tags(), and CreateEventTrigStmt::whenclause.

Referenced by standard_ProcessUtility().

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 }
static void validate_ddl_tags(const char *filtervar, List *taglist)
int errhint(const char *fmt,...)
Definition: elog.c:987
Oid GetUserId(void)
Definition: miscinit.c:379
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1444
#define ERROR
Definition: elog.h:43
static void validate_table_rewrite_tags(const char *filtervar, List *taglist)
static void error_duplicate_filter_variable(const char *defname)
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:731
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
char * NameListToString(List *names)
Definition: namespace.c:3082
static Oid insert_event_trigger_tuple(const char *trigname, const char *eventname, Oid evtOwner, Oid funcoid, List *tags)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:106
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:1974
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:730
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:45

◆ EventTriggerAlterTableEnd()

void EventTriggerAlterTableEnd ( void  )

Definition at line 1791 of file event_trigger.c.

References CollectedCommand::alterTable, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, EventTriggerQueryState::currentCommand, CollectedCommand::d, lappend(), list_length(), and pfree().

Referenced by AlterTableMoveAll(), and ProcessUtilitySlow().

1792 {
1793  /* ignore if event trigger context not set, or collection disabled */
1794  if (!currentEventTriggerState ||
1796  return;
1797 
1798  /* If no subcommands, don't collect */
1800  {
1804  }
1805  else
1807 
1809 }
struct CollectedCommand::@112::@114 alterTable
union CollectedCommand::@112 d
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
void pfree(void *pointer)
Definition: mcxt.c:1031
CollectedCommand * currentCommand
Definition: event_trigger.c:66
List * lappend(List *list, void *datum)
Definition: list.c:128
static int list_length(const List *l)
Definition: pg_list.h:89

◆ EventTriggerAlterTableRelid()

void EventTriggerAlterTableRelid ( Oid  objectId)

Definition at line 1739 of file event_trigger.c.

References CollectedCommand::alterTable, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::currentCommand, and CollectedCommand::d.

Referenced by AlterTableInternal(), and ProcessUtilitySlow().

1740 {
1741  if (!currentEventTriggerState ||
1743  return;
1744 
1745  currentEventTriggerState->currentCommand->d.alterTable.objectId = objectId;
1746 }
struct CollectedCommand::@112::@114 alterTable
union CollectedCommand::@112 d
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
CollectedCommand * currentCommand
Definition: event_trigger.c:66

◆ EventTriggerAlterTableStart()

void EventTriggerAlterTableStart ( Node parsetree)

Definition at line 1706 of file event_trigger.c.

References CollectedCommand::alterTable, EventTriggerQueryState::commandCollectionInhibited, copyObject, creating_extension, EventTriggerQueryState::currentCommand, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::in_extension, InvalidOid, MemoryContextSwitchTo(), NIL, palloc(), CollectedCommand::parsetree, SCT_AlterTable, and CollectedCommand::type.

Referenced by AlterTableMoveAll(), and ProcessUtilitySlow().

1707 {
1708  MemoryContext oldcxt;
1709  CollectedCommand *command;
1710 
1711  /* ignore if event trigger context not set, or collection disabled */
1712  if (!currentEventTriggerState ||
1714  return;
1715 
1717 
1718  command = palloc(sizeof(CollectedCommand));
1719 
1720  command->type = SCT_AlterTable;
1721  command->in_extension = creating_extension;
1722 
1723  command->d.alterTable.classId = RelationRelationId;
1724  command->d.alterTable.objectId = InvalidOid;
1725  command->d.alterTable.subcmds = NIL;
1726  command->parsetree = copyObject(parsetree);
1727 
1729 
1730  MemoryContextSwitchTo(oldcxt);
1731 }
#define NIL
Definition: pg_list.h:69
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct CollectedCommand::@112::@114 alterTable
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
CollectedCommand * currentCommand
Definition: event_trigger.c:66
#define InvalidOid
Definition: postgres_ext.h:36
bool creating_extension
Definition: extension.c:67
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerBeginCompleteQuery()

bool EventTriggerBeginCompleteQuery ( void  )

Definition at line 1231 of file event_trigger.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, EventTriggerQueryState::currentCommand, currentEventTriggerState, EventTriggerQueryState::cxt, EventTriggerQueryState::in_sql_drop, InvalidOid, MemoryContextAlloc(), NIL, EventTriggerQueryState::previous, slist_init(), EventTriggerQueryState::SQLDropList, EventTriggerQueryState::table_rewrite_oid, TopMemoryContext, and trackDroppedObjectsNeeded().

Referenced by ProcessUtilitySlow().

1232 {
1234  MemoryContext cxt;
1235 
1236  /*
1237  * Currently, sql_drop, table_rewrite, ddl_command_end events are the only
1238  * reason to have event trigger state at all; so if there are none, don't
1239  * install one.
1240  */
1242  return false;
1243 
1245  "event trigger state",
1247  state = MemoryContextAlloc(cxt, sizeof(EventTriggerQueryState));
1248  state->cxt = cxt;
1249  slist_init(&(state->SQLDropList));
1250  state->in_sql_drop = false;
1251  state->table_rewrite_oid = InvalidOid;
1252 
1255  state->currentCommand = NULL;
1256  state->commandList = NIL;
1258  currentEventTriggerState = state;
1259 
1260  return true;
1261 }
#define NIL
Definition: pg_list.h:69
bool trackDroppedObjectsNeeded(void)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
static void slist_init(slist_head *head)
Definition: ilist.h:554
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
CollectedCommand * currentCommand
Definition: event_trigger.c:66
struct EventTriggerQueryState * previous
Definition: event_trigger.c:69
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
#define InvalidOid
Definition: postgres_ext.h:36
Definition: regguts.h:298
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771

◆ EventTriggerCollectAlterDefPrivs()

void EventTriggerCollectAlterDefPrivs ( AlterDefaultPrivilegesStmt stmt)

Definition at line 1965 of file event_trigger.c.

References AlterDefaultPrivilegesStmt::action, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::defprivs, CollectedCommand::in_extension, lappend(), MemoryContextSwitchTo(), GrantStmt::objtype, palloc0(), CollectedCommand::parsetree, SCT_AlterDefaultPrivileges, and CollectedCommand::type.

Referenced by ProcessUtilitySlow().

1966 {
1967  MemoryContext oldcxt;
1968  CollectedCommand *command;
1969 
1970  /* ignore if event trigger context not set, or collection disabled */
1971  if (!currentEventTriggerState ||
1973  return;
1974 
1976 
1977  command = palloc0(sizeof(CollectedCommand));
1978  command->type = SCT_AlterDefaultPrivileges;
1979  command->d.defprivs.objtype = stmt->action->objtype;
1980  command->in_extension = creating_extension;
1981  command->parsetree = (Node *) copyObject(stmt);
1982 
1985  MemoryContextSwitchTo(oldcxt);
1986 }
struct CollectedCommand::@112::@119 defprivs
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
List * lappend(List *list, void *datum)
Definition: list.c:128
void * palloc0(Size size)
Definition: mcxt.c:955
bool creating_extension
Definition: extension.c:67
ObjectType objtype
Definition: parsenodes.h:1872
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerCollectAlterOpFam()

void EventTriggerCollectAlterOpFam ( AlterOpFamilyStmt stmt,
Oid  opfamoid,
List operators,
List procedures 
)

Definition at line 1863 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::in_extension, lappend(), MemoryContextSwitchTo(), ObjectAddressSet, CollectedCommand::opfam, palloc(), CollectedCommand::parsetree, SCT_AlterOpFamily, and CollectedCommand::type.

Referenced by AlterOpFamilyAdd(), and AlterOpFamilyDrop().

1865 {
1866  MemoryContext oldcxt;
1867  CollectedCommand *command;
1868 
1869  /* ignore if event trigger context not set, or collection disabled */
1870  if (!currentEventTriggerState ||
1872  return;
1873 
1875 
1876  command = palloc(sizeof(CollectedCommand));
1877  command->type = SCT_AlterOpFamily;
1878  command->in_extension = creating_extension;
1879  ObjectAddressSet(command->d.opfam.address,
1880  OperatorFamilyRelationId, opfamoid);
1881  command->d.opfam.operators = operators;
1882  command->d.opfam.procedures = procedures;
1883  command->parsetree = (Node *) copyObject(stmt);
1884 
1887 
1888  MemoryContextSwitchTo(oldcxt);
1889 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
struct CollectedCommand::@112::@116 opfam
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
List * lappend(List *list, void *datum)
Definition: list.c:128
bool creating_extension
Definition: extension.c:67
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerCollectAlterTableSubcmd()

void EventTriggerCollectAlterTableSubcmd ( Node subcmd,
ObjectAddress  address 
)

Definition at line 1757 of file event_trigger.c.

References CollectedATSubcmd::address, CollectedCommand::alterTable, Assert, EventTriggerQueryState::commandCollectionInhibited, copyObject, EventTriggerQueryState::currentCommand, EventTriggerQueryState::cxt, CollectedCommand::d, IsA, lappend(), MemoryContextSwitchTo(), newsub(), OidIsValid, palloc(), and CollectedATSubcmd::parsetree.

Referenced by ATExecCmd().

1758 {
1759  MemoryContext oldcxt;
1761 
1762  /* ignore if event trigger context not set, or collection disabled */
1763  if (!currentEventTriggerState ||
1765  return;
1766 
1767  Assert(IsA(subcmd, AlterTableCmd));
1769 
1771 
1772  newsub = palloc(sizeof(CollectedATSubcmd));
1773  newsub->address = address;
1774  newsub->parsetree = copyObject(subcmd);
1775 
1778 
1779  MemoryContextSwitchTo(oldcxt);
1780 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct CollectedCommand::@112::@114 alterTable
union CollectedCommand::@112 d
#define OidIsValid(objectId)
Definition: c.h:605
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
CollectedCommand * currentCommand
Definition: event_trigger.c:66
List * lappend(List *list, void *datum)
Definition: list.c:128
#define Assert(condition)
Definition: c.h:699
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630
static color newsub(struct colormap *cm, color co)
Definition: regc_color.c:389
ObjectAddress address

◆ EventTriggerCollectAlterTSConfig()

void EventTriggerCollectAlterTSConfig ( AlterTSConfigurationStmt stmt,
Oid  cfgId,
Oid dictIds,
int  ndicts 
)

Definition at line 1930 of file event_trigger.c.

References CollectedCommand::atscfg, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::in_extension, lappend(), MemoryContextSwitchTo(), ObjectAddressSet, palloc(), palloc0(), CollectedCommand::parsetree, SCT_AlterTSConfig, and CollectedCommand::type.

Referenced by DropConfigurationMapping(), and MakeConfigurationMapping().

1932 {
1933  MemoryContext oldcxt;
1934  CollectedCommand *command;
1935 
1936  /* ignore if event trigger context not set, or collection disabled */
1937  if (!currentEventTriggerState ||
1939  return;
1940 
1942 
1943  command = palloc0(sizeof(CollectedCommand));
1944  command->type = SCT_AlterTSConfig;
1945  command->in_extension = creating_extension;
1946  ObjectAddressSet(command->d.atscfg.address,
1947  TSConfigRelationId, cfgId);
1948  command->d.atscfg.dictIds = palloc(sizeof(Oid) * ndicts);
1949  memcpy(command->d.atscfg.dictIds, dictIds, sizeof(Oid) * ndicts);
1950  command->d.atscfg.ndicts = ndicts;
1951  command->parsetree = (Node *) copyObject(stmt);
1952 
1955 
1956  MemoryContextSwitchTo(oldcxt);
1957 }
struct CollectedCommand::@112::@118 atscfg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
union CollectedCommand::@112 d
CollectedCommandType type
unsigned int Oid
Definition: postgres_ext.h:31
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
List * lappend(List *list, void *datum)
Definition: list.c:128
void * palloc0(Size size)
Definition: mcxt.c:955
bool creating_extension
Definition: extension.c:67
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerCollectCreateOpClass()

void EventTriggerCollectCreateOpClass ( CreateOpClassStmt stmt,
Oid  opcoid,
List operators,
List procedures 
)

Definition at line 1896 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, CollectedCommand::createopc, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::in_extension, lappend(), MemoryContextSwitchTo(), ObjectAddressSet, palloc0(), CollectedCommand::parsetree, SCT_CreateOpClass, and CollectedCommand::type.

Referenced by DefineOpClass().

1898 {
1899  MemoryContext oldcxt;
1900  CollectedCommand *command;
1901 
1902  /* ignore if event trigger context not set, or collection disabled */
1903  if (!currentEventTriggerState ||
1905  return;
1906 
1908 
1909  command = palloc0(sizeof(CollectedCommand));
1910  command->type = SCT_CreateOpClass;
1911  command->in_extension = creating_extension;
1912  ObjectAddressSet(command->d.createopc.address,
1913  OperatorClassRelationId, opcoid);
1914  command->d.createopc.operators = operators;
1915  command->d.createopc.procedures = procedures;
1916  command->parsetree = (Node *) copyObject(stmt);
1917 
1920 
1921  MemoryContextSwitchTo(oldcxt);
1922 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
struct CollectedCommand::@112::@117 createopc
List * lappend(List *list, void *datum)
Definition: list.c:128
void * palloc0(Size size)
Definition: mcxt.c:955
bool creating_extension
Definition: extension.c:67
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerCollectGrant()

void EventTriggerCollectGrant ( InternalGrant istmt)

Definition at line 1819 of file event_trigger.c.

References InternalGrant::col_privs, EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::grant, InternalGrant::grantees, CollectedCommand::in_extension, lappend(), lfirst, list_copy(), MemoryContextSwitchTo(), NIL, InternalGrant::objects, palloc(), CollectedCommand::parsetree, SCT_Grant, and CollectedCommand::type.

Referenced by ExecGrantStmt_oids().

1820 {
1821  MemoryContext oldcxt;
1822  CollectedCommand *command;
1823  InternalGrant *icopy;
1824  ListCell *cell;
1825 
1826  /* ignore if event trigger context not set, or collection disabled */
1827  if (!currentEventTriggerState ||
1829  return;
1830 
1832 
1833  /*
1834  * This is tedious, but necessary.
1835  */
1836  icopy = palloc(sizeof(InternalGrant));
1837  memcpy(icopy, istmt, sizeof(InternalGrant));
1838  icopy->objects = list_copy(istmt->objects);
1839  icopy->grantees = list_copy(istmt->grantees);
1840  icopy->col_privs = NIL;
1841  foreach(cell, istmt->col_privs)
1842  icopy->col_privs = lappend(icopy->col_privs, copyObject(lfirst(cell)));
1843 
1844  /* Now collect it, using the copied InternalGrant */
1845  command = palloc(sizeof(CollectedCommand));
1846  command->type = SCT_Grant;
1847  command->in_extension = creating_extension;
1848  command->d.grant.istmt = icopy;
1849  command->parsetree = NULL;
1850 
1853 
1854  MemoryContextSwitchTo(oldcxt);
1855 }
struct CollectedCommand::@112::@115 grant
#define NIL
Definition: pg_list.h:69
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * list_copy(const List *oldlist)
Definition: list.c:1160
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
List * lappend(List *list, void *datum)
Definition: list.c:128
bool creating_extension
Definition: extension.c:67
#define lfirst(lc)
Definition: pg_list.h:106
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerCollectSimpleCommand()

void EventTriggerCollectSimpleCommand ( ObjectAddress  address,
ObjectAddress  secondaryObject,
Node parsetree 
)

Definition at line 1663 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited, EventTriggerQueryState::commandList, copyObject, creating_extension, EventTriggerQueryState::cxt, CollectedCommand::d, CollectedCommand::in_extension, lappend(), MemoryContextSwitchTo(), palloc(), CollectedCommand::parsetree, SCT_Simple, CollectedCommand::simple, and CollectedCommand::type.

Referenced by AlterPublicationOptions(), CreateSchemaCommand(), ProcessUtilitySlow(), and PublicationAddTables().

1666 {
1667  MemoryContext oldcxt;
1668  CollectedCommand *command;
1669 
1670  /* ignore if event trigger context not set, or collection disabled */
1671  if (!currentEventTriggerState ||
1673  return;
1674 
1676 
1677  command = palloc(sizeof(CollectedCommand));
1678 
1679  command->type = SCT_Simple;
1680  command->in_extension = creating_extension;
1681 
1682  command->d.simple.address = address;
1683  command->d.simple.secondaryObject = secondaryObject;
1684  command->parsetree = copyObject(parsetree);
1685 
1687  command);
1688 
1689  MemoryContextSwitchTo(oldcxt);
1690 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
union CollectedCommand::@112 d
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
List * lappend(List *list, void *datum)
Definition: list.c:128
bool creating_extension
Definition: extension.c:67
struct CollectedCommand::@112::@113 simple
void * palloc(Size size)
Definition: mcxt.c:924
#define copyObject(obj)
Definition: nodes.h:630

◆ EventTriggerDDLCommandEnd()

void EventTriggerDDLCommandEnd ( Node parsetree)

Definition at line 827 of file event_trigger.c.

References CommandCounterIncrement(), EventTriggerCommonSetup(), EventTriggerInvoke(), EVT_DDLCommandEnd, IsUnderPostmaster, list_free(), and NIL.

Referenced by ProcessUtilitySlow().

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  /*
840  * Also do nothing if our state isn't set up, which it won't be if there
841  * weren't any relevant event triggers at the start of the current DDL
842  * command. This test might therefore seem optional, but it's important
843  * because EventTriggerCommonSetup might find triggers that didn't exist
844  * at the time the command started. Although this function itself
845  * wouldn't crash, the event trigger functions would presumably call
846  * pg_event_trigger_ddl_commands which would fail. Better to do nothing
847  * until the next command.
848  */
850  return;
851 
852  runlist = EventTriggerCommonSetup(parsetree,
853  EVT_DDLCommandEnd, "ddl_command_end",
854  &trigdata);
855  if (runlist == NIL)
856  return;
857 
858  /*
859  * Make sure anything the main command did will be visible to the event
860  * triggers.
861  */
863 
864  /* Run the triggers. */
865  EventTriggerInvoke(runlist, &trigdata);
866 
867  /* Cleanup. */
868  list_free(runlist);
869 }
#define NIL
Definition: pg_list.h:69
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
bool IsUnderPostmaster
Definition: globals.c:110
void CommandCounterIncrement(void)
Definition: xact.c:914
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45

◆ EventTriggerDDLCommandStart()

void EventTriggerDDLCommandStart ( Node parsetree)

Definition at line 779 of file event_trigger.c.

References CommandCounterIncrement(), EventTriggerCommonSetup(), EventTriggerInvoke(), EVT_DDLCommandStart, IsUnderPostmaster, list_free(), and NIL.

Referenced by ProcessUtilitySlow().

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 }
#define NIL
Definition: pg_list.h:69
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
bool IsUnderPostmaster
Definition: globals.c:110
void CommandCounterIncrement(void)
Definition: xact.c:914
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45

◆ EventTriggerEndCompleteQuery()

void EventTriggerEndCompleteQuery ( void  )

Definition at line 1275 of file event_trigger.c.

References EventTriggerQueryState::cxt, MemoryContextDelete(), and EventTriggerQueryState::previous.

Referenced by ProcessUtilitySlow().

1276 {
1277  EventTriggerQueryState *prevstate;
1278 
1279  prevstate = currentEventTriggerState->previous;
1280 
1281  /* this avoids the need for retail pfree of SQLDropList items: */
1283 
1284  currentEventTriggerState = prevstate;
1285 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
struct EventTriggerQueryState * previous
Definition: event_trigger.c:69

◆ EventTriggerInhibitCommandCollection()

void EventTriggerInhibitCommandCollection ( void  )

Definition at line 1629 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited.

Referenced by ProcessUtilitySlow().

1630 {
1632  return;
1633 
1635 }
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72

◆ EventTriggerSQLDrop()

void EventTriggerSQLDrop ( Node parsetree)

Definition at line 875 of file event_trigger.c.

References CommandCounterIncrement(), EventTriggerCommonSetup(), EventTriggerInvoke(), EVT_SQLDrop, EventTriggerQueryState::in_sql_drop, IsUnderPostmaster, list_free(), NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, slist_is_empty(), and EventTriggerQueryState::SQLDropList.

Referenced by ProcessUtilitySlow().

876 {
877  List *runlist;
878  EventTriggerData trigdata;
879 
880  /*
881  * See EventTriggerDDLCommandStart for a discussion about why event
882  * triggers are disabled in single user mode.
883  */
884  if (!IsUnderPostmaster)
885  return;
886 
887  /*
888  * Use current state to determine whether this event fires at all. If
889  * there are no triggers for the sql_drop event, then we don't have
890  * anything to do here. Note that dropped object collection is disabled
891  * if this is the case, so even if we were to try to run, the list would
892  * be empty.
893  */
896  return;
897 
898  runlist = EventTriggerCommonSetup(parsetree,
899  EVT_SQLDrop, "sql_drop",
900  &trigdata);
901 
902  /*
903  * Nothing to do if run list is empty. Note this typically can't happen,
904  * because if there are no sql_drop events, then objects-to-drop wouldn't
905  * have been collected in the first place and we would have quit above.
906  * But it could occur if event triggers were dropped partway through.
907  */
908  if (runlist == NIL)
909  return;
910 
911  /*
912  * Make sure anything the main command did will be visible to the event
913  * triggers.
914  */
916 
917  /*
918  * Make sure pg_event_trigger_dropped_objects only works when running
919  * these triggers. Use PG_TRY to ensure in_sql_drop is reset even when
920  * one trigger fails. (This is perhaps not necessary, as the currentState
921  * variable will be removed shortly by our caller, but it seems better to
922  * play safe.)
923  */
925 
926  /* Run the triggers. */
927  PG_TRY();
928  {
929  EventTriggerInvoke(runlist, &trigdata);
930  }
931  PG_CATCH();
932  {
934  PG_RE_THROW();
935  }
936  PG_END_TRY();
938 
939  /* Cleanup. */
940  list_free(runlist);
941 }
#define NIL
Definition: pg_list.h:69
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
bool IsUnderPostmaster
Definition: globals.c:110
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
void CommandCounterIncrement(void)
Definition: xact.c:914
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
void list_free(List *list)
Definition: list.c:1133
#define PG_TRY()
Definition: elog.h:284
Definition: pg_list.h:45
#define PG_END_TRY()
Definition: elog.h:300

◆ EventTriggerSQLDropAddObject()

void EventTriggerSQLDropAddObject ( const ObjectAddress object,
bool  original,
bool  normal 
)

Definition at line 1325 of file event_trigger.c.

References AccessShareLock, SQLDropObject::addrargs, SQLDropObject::address, SQLDropObject::addrnames, Assert, attnum, ObjectAddress::classId, EventTriggerQueryState::cxt, DatumGetName, DatumGetObjectId, EventTriggerSupportsObjectClass(), get_catalog_object_by_oid(), get_namespace_name(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_namensp_unique(), getObjectClass(), getObjectIdentityParts(), getObjectTypeDescription(), heap_close, heap_getattr, heap_open(), InvalidAttrNumber, is_objectclass_supported(), isAnyTempNamespace(), SQLDropObject::istemp, isTempNamespace(), MemoryContextSwitchTo(), NameStr, SQLDropObject::next, SQLDropObject::normal, ObjectAddress::objectId, ObjectAddress::objectSubId, SQLDropObject::objecttype, SQLDropObject::objidentity, SQLDropObject::objname, SQLDropObject::original, palloc0(), pfree(), pstrdup(), RelationGetDescr, SQLDropObject::schemaname, slist_push_head(), and EventTriggerQueryState::SQLDropList.

Referenced by deleteObjectsInList(), and DropSubscription().

1326 {
1327  SQLDropObject *obj;
1328  MemoryContext oldcxt;
1329 
1331  return;
1332 
1334 
1335  /* don't report temp schemas except my own */
1336  if (object->classId == NamespaceRelationId &&
1337  (isAnyTempNamespace(object->objectId) &&
1338  !isTempNamespace(object->objectId)))
1339  return;
1340 
1342 
1343  obj = palloc0(sizeof(SQLDropObject));
1344  obj->address = *object;
1345  obj->original = original;
1346  obj->normal = normal;
1347 
1348  /*
1349  * Obtain schema names from the object's catalog tuple, if one exists;
1350  * this lets us skip objects in temp schemas. We trust that
1351  * ObjectProperty contains all object classes that can be
1352  * schema-qualified.
1353  */
1354  if (is_objectclass_supported(object->classId))
1355  {
1356  Relation catalog;
1357  HeapTuple tuple;
1358 
1359  catalog = heap_open(obj->address.classId, AccessShareLock);
1360  tuple = get_catalog_object_by_oid(catalog, obj->address.objectId);
1361 
1362  if (tuple)
1363  {
1365  Datum datum;
1366  bool isnull;
1367 
1369  if (attnum != InvalidAttrNumber)
1370  {
1371  datum = heap_getattr(tuple, attnum,
1372  RelationGetDescr(catalog), &isnull);
1373  if (!isnull)
1374  {
1375  Oid namespaceId;
1376 
1377  namespaceId = DatumGetObjectId(datum);
1378  /* temp objects are only reported if they are my own */
1379  if (isTempNamespace(namespaceId))
1380  {
1381  obj->schemaname = "pg_temp";
1382  obj->istemp = true;
1383  }
1384  else if (isAnyTempNamespace(namespaceId))
1385  {
1386  pfree(obj);
1387  heap_close(catalog, AccessShareLock);
1388  MemoryContextSwitchTo(oldcxt);
1389  return;
1390  }
1391  else
1392  {
1393  obj->schemaname = get_namespace_name(namespaceId);
1394  obj->istemp = false;
1395  }
1396  }
1397  }
1398 
1400  obj->address.objectSubId == 0)
1401  {
1402  attnum = get_object_attnum_name(obj->address.classId);
1403  if (attnum != InvalidAttrNumber)
1404  {
1405  datum = heap_getattr(tuple, attnum,
1406  RelationGetDescr(catalog), &isnull);
1407  if (!isnull)
1408  obj->objname = pstrdup(NameStr(*DatumGetName(datum)));
1409  }
1410  }
1411  }
1412 
1413  heap_close(catalog, AccessShareLock);
1414  }
1415  else
1416  {
1417  if (object->classId == NamespaceRelationId &&
1418  isTempNamespace(object->objectId))
1419  obj->istemp = true;
1420  }
1421 
1422  /* object identity, objname and objargs */
1423  obj->objidentity =
1424  getObjectIdentityParts(&obj->address, &obj->addrnames, &obj->addrargs);
1425 
1426  /* object type */
1428 
1430 
1431  MemoryContextSwitchTo(oldcxt);
1432 }
bool get_object_namensp_unique(Oid class_id)
slist_node next
ObjectAddress address
char * getObjectTypeDescription(const ObjectAddress *object)
#define RelationGetDescr(relation)
Definition: rel.h:433
const char * objname
#define DatumGetObjectId(X)
Definition: postgres.h:485
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * getObjectIdentityParts(const ObjectAddress *object, List **objname, List **objargs)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
AttrNumber get_object_attnum_namespace(Oid class_id)
#define heap_close(r, l)
Definition: heapam.h:97
const char * objidentity
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2416
const char * objecttype
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
#define DatumGetName(X)
Definition: postgres.h:570
void pfree(void *pointer)
Definition: mcxt.c:1031
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:3137
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
const char * schemaname
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:781
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:955
uintptr_t Datum
Definition: postgres.h:367
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:699
HeapTuple get_catalog_object_by_oid(Relation catalog, Oid objectId)
#define InvalidAttrNumber
Definition: attnum.h:23
#define NameStr(name)
Definition: c.h:576
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:3175
bool is_objectclass_supported(Oid class_id)
int16 AttrNumber
Definition: attnum.h:21

◆ EventTriggerSupportsObjectClass()

bool EventTriggerSupportsObjectClass ( ObjectClass  objclass)

Definition at line 1166 of file event_trigger.c.

References OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC, OCLASS_CAST, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DATABASE, OCLASS_DEFACL, OCLASS_DEFAULT, OCLASS_EVENT_TRIGGER, OCLASS_EXTENSION, OCLASS_FDW, OCLASS_FOREIGN_SERVER, OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_POLICY, OCLASS_PROC, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL, OCLASS_REWRITE, OCLASS_ROLE, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT, OCLASS_SUBSCRIPTION, OCLASS_TBLSPACE, OCLASS_TRANSFORM, OCLASS_TRIGGER, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, and OCLASS_USER_MAPPING.

Referenced by deleteObjectsInList(), and EventTriggerSQLDropAddObject().

1167 {
1168  switch (objclass)
1169  {
1170  case OCLASS_DATABASE:
1171  case OCLASS_TBLSPACE:
1172  case OCLASS_ROLE:
1173  /* no support for global objects */
1174  return false;
1175  case OCLASS_EVENT_TRIGGER:
1176  /* no support for event triggers on event triggers */
1177  return false;
1178  case OCLASS_CLASS:
1179  case OCLASS_PROC:
1180  case OCLASS_TYPE:
1181  case OCLASS_CAST:
1182  case OCLASS_COLLATION:
1183  case OCLASS_CONSTRAINT:
1184  case OCLASS_CONVERSION:
1185  case OCLASS_DEFAULT:
1186  case OCLASS_LANGUAGE:
1187  case OCLASS_LARGEOBJECT:
1188  case OCLASS_OPERATOR:
1189  case OCLASS_OPCLASS:
1190  case OCLASS_OPFAMILY:
1191  case OCLASS_AM:
1192  case OCLASS_AMOP:
1193  case OCLASS_AMPROC:
1194  case OCLASS_REWRITE:
1195  case OCLASS_TRIGGER:
1196  case OCLASS_SCHEMA:
1197  case OCLASS_STATISTIC_EXT:
1198  case OCLASS_TSPARSER:
1199  case OCLASS_TSDICT:
1200  case OCLASS_TSTEMPLATE:
1201  case OCLASS_TSCONFIG:
1202  case OCLASS_FDW:
1203  case OCLASS_FOREIGN_SERVER:
1204  case OCLASS_USER_MAPPING:
1205  case OCLASS_DEFACL:
1206  case OCLASS_EXTENSION:
1207  case OCLASS_POLICY:
1208  case OCLASS_PUBLICATION:
1210  case OCLASS_SUBSCRIPTION:
1211  case OCLASS_TRANSFORM:
1212  return true;
1213 
1214  /*
1215  * There's intentionally no default: case here; we want the
1216  * compiler to warn if a new OCLASS hasn't been handled above.
1217  */
1218  }
1219 
1220  /* Shouldn't get here, but if we do, say "no support" */
1221  return false;
1222 }

◆ EventTriggerSupportsObjectType()

bool EventTriggerSupportsObjectType ( ObjectType  obtype)

Definition at line 1092 of file event_trigger.c.

References OBJECT_ACCESS_METHOD, OBJECT_AGGREGATE, OBJECT_AMOP, OBJECT_AMPROC, OBJECT_ATTRIBUTE, OBJECT_CAST, OBJECT_COLLATION, OBJECT_COLUMN, OBJECT_CONVERSION, OBJECT_DATABASE, OBJECT_DEFACL, OBJECT_DEFAULT, OBJECT_DOMAIN, OBJECT_DOMCONSTRAINT, OBJECT_EVENT_TRIGGER, OBJECT_EXTENSION, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FOREIGN_TABLE, OBJECT_FUNCTION, OBJECT_INDEX, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_MATVIEW, OBJECT_OPCLASS, OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_POLICY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, OBJECT_PUBLICATION_REL, OBJECT_ROLE, OBJECT_ROUTINE, OBJECT_RULE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABCONSTRAINT, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TRANSFORM, OBJECT_TRIGGER, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_USER_MAPPING, and OBJECT_VIEW.

Referenced by ExecGrantStmt_oids(), and standard_ProcessUtility().

1093 {
1094  switch (obtype)
1095  {
1096  case OBJECT_DATABASE:
1097  case OBJECT_TABLESPACE:
1098  case OBJECT_ROLE:
1099  /* no support for global objects */
1100  return false;
1101  case OBJECT_EVENT_TRIGGER:
1102  /* no support for event triggers on event triggers */
1103  return false;
1104  case OBJECT_ACCESS_METHOD:
1105  case OBJECT_AGGREGATE:
1106  case OBJECT_AMOP:
1107  case OBJECT_AMPROC:
1108  case OBJECT_ATTRIBUTE:
1109  case OBJECT_CAST:
1110  case OBJECT_COLUMN:
1111  case OBJECT_COLLATION:
1112  case OBJECT_CONVERSION:
1113  case OBJECT_DEFACL:
1114  case OBJECT_DEFAULT:
1115  case OBJECT_DOMAIN:
1116  case OBJECT_DOMCONSTRAINT:
1117  case OBJECT_EXTENSION:
1118  case OBJECT_FDW:
1119  case OBJECT_FOREIGN_SERVER:
1120  case OBJECT_FOREIGN_TABLE:
1121  case OBJECT_FUNCTION:
1122  case OBJECT_INDEX:
1123  case OBJECT_LANGUAGE:
1124  case OBJECT_LARGEOBJECT:
1125  case OBJECT_MATVIEW:
1126  case OBJECT_OPCLASS:
1127  case OBJECT_OPERATOR:
1128  case OBJECT_OPFAMILY:
1129  case OBJECT_POLICY:
1130  case OBJECT_PROCEDURE:
1131  case OBJECT_PUBLICATION:
1133  case OBJECT_ROUTINE:
1134  case OBJECT_RULE:
1135  case OBJECT_SCHEMA:
1136  case OBJECT_SEQUENCE:
1137  case OBJECT_SUBSCRIPTION:
1138  case OBJECT_STATISTIC_EXT:
1139  case OBJECT_TABCONSTRAINT:
1140  case OBJECT_TABLE:
1141  case OBJECT_TRANSFORM:
1142  case OBJECT_TRIGGER:
1144  case OBJECT_TSDICTIONARY:
1145  case OBJECT_TSPARSER:
1146  case OBJECT_TSTEMPLATE:
1147  case OBJECT_TYPE:
1148  case OBJECT_USER_MAPPING:
1149  case OBJECT_VIEW:
1150  return true;
1151 
1152  /*
1153  * There's intentionally no default: case here; we want the
1154  * compiler to warn if a new ObjectType hasn't been handled above.
1155  */
1156  }
1157 
1158  /* Shouldn't get here, but if we do, say "no support" */
1159  return false;
1160 }

◆ EventTriggerTableRewrite()

void EventTriggerTableRewrite ( Node parsetree,
Oid  tableOid,
int  reason 
)

Definition at line 948 of file event_trigger.c.

References CommandCounterIncrement(), EventTriggerCommonSetup(), EventTriggerInvoke(), EVT_TableRewrite, InvalidOid, IsUnderPostmaster, list_free(), NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, EventTriggerQueryState::table_rewrite_oid, and EventTriggerQueryState::table_rewrite_reason.

Referenced by ATRewriteTables().

949 {
950  List *runlist;
951  EventTriggerData trigdata;
952 
953  /*
954  * Event Triggers are completely disabled in standalone mode. There are
955  * (at least) two reasons for this:
956  *
957  * 1. A sufficiently broken event trigger might not only render the
958  * database unusable, but prevent disabling itself to fix the situation.
959  * In this scenario, restarting in standalone mode provides an escape
960  * hatch.
961  *
962  * 2. BuildEventTriggerCache relies on systable_beginscan_ordered, and
963  * therefore will malfunction if pg_event_trigger's indexes are damaged.
964  * To allow recovery from a damaged index, we need some operating mode
965  * wherein event triggers are disabled. (Or we could implement
966  * heapscan-and-sort logic for that case, but having disaster recovery
967  * scenarios depend on code that's otherwise untested isn't appetizing.)
968  */
969  if (!IsUnderPostmaster)
970  return;
971 
972  /*
973  * Also do nothing if our state isn't set up, which it won't be if there
974  * weren't any relevant event triggers at the start of the current DDL
975  * command. This test might therefore seem optional, but it's
976  * *necessary*, because EventTriggerCommonSetup might find triggers that
977  * didn't exist at the time the command started.
978  */
980  return;
981 
982  runlist = EventTriggerCommonSetup(parsetree,
984  "table_rewrite",
985  &trigdata);
986  if (runlist == NIL)
987  return;
988 
989  /*
990  * Make sure pg_event_trigger_table_rewrite_oid only works when running
991  * these triggers. Use PG_TRY to ensure table_rewrite_oid is reset even
992  * when one trigger fails. (This is perhaps not necessary, as the
993  * currentState variable will be removed shortly by our caller, but it
994  * seems better to play safe.)
995  */
998 
999  /* Run the triggers. */
1000  PG_TRY();
1001  {
1002  EventTriggerInvoke(runlist, &trigdata);
1003  }
1004  PG_CATCH();
1005  {
1008  PG_RE_THROW();
1009  }
1010  PG_END_TRY();
1011 
1014 
1015  /* Cleanup. */
1016  list_free(runlist);
1017 
1018  /*
1019  * Make sure anything the event triggers did will be visible to the main
1020  * command.
1021  */
1023 }
#define NIL
Definition: pg_list.h:69
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
bool IsUnderPostmaster
Definition: globals.c:110
void CommandCounterIncrement(void)
Definition: xact.c:914
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
void list_free(List *list)
Definition: list.c:1133
#define PG_TRY()
Definition: elog.h:284
Definition: pg_list.h:45
#define PG_END_TRY()
Definition: elog.h:300

◆ EventTriggerUndoInhibitCommandCollection()

void EventTriggerUndoInhibitCommandCollection ( void  )

Definition at line 1641 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited.

Referenced by ProcessUtilitySlow().

1642 {
1644  return;
1645 
1647 }
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:72

◆ get_event_trigger_oid()

Oid get_event_trigger_oid ( const char *  trigname,
bool  missing_ok 
)

Definition at line 643 of file event_trigger.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, EVENTTRIGGERNAME, GetSysCacheOid1, and OidIsValid.

Referenced by get_object_address_unqualified().

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 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ RemoveEventTriggerById()

void RemoveEventTriggerById ( Oid  ctrigOid)

Definition at line 479 of file event_trigger.c.

References CatalogTupleDelete(), elog, ERROR, EVENTTRIGGEROID, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), and HeapTupleData::t_self.

Referenced by doDeletion().

480 {
481  Relation tgrel;
482  HeapTuple tup;
483 
484  tgrel = heap_open(EventTriggerRelationId, RowExclusiveLock);
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 }
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog
Definition: elog.h:219

◆ trackDroppedObjectsNeeded()

bool trackDroppedObjectsNeeded ( void  )

Definition at line 1293 of file event_trigger.c.

References EventCacheLookup(), EVT_DDLCommandEnd, EVT_SQLDrop, EVT_TableRewrite, and list_length().

Referenced by deleteObjectsInList(), and EventTriggerBeginCompleteQuery().

1294 {
1295  /*
1296  * true if any sql_drop, table_rewrite, ddl_command_end event trigger
1297  * exists
1298  */
1299  return list_length(EventCacheLookup(EVT_SQLDrop)) > 0 ||
1302 }
static int list_length(const List *l)
Definition: pg_list.h:89
List * EventCacheLookup(EventTriggerEvent event)
Definition: evtcache.c:64