PostgreSQL Source Code  git master
event_trigger.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/trigger.h"
#include "funcapi.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "pgstat.h"
#include "tcop/deparse_utility.h"
#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/evtcache.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for event_trigger.c:

Go to the source code of this file.

Data Structures

struct  EventTriggerQueryState
 
struct  event_trigger_support_data
 
struct  SQLDropObject
 

Typedefs

typedef struct EventTriggerQueryState EventTriggerQueryState
 
typedef struct SQLDropObject SQLDropObject
 

Enumerations

enum  event_trigger_command_tag_check_result { EVENT_TRIGGER_COMMAND_TAG_OK, EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED, EVENT_TRIGGER_COMMAND_TAG_NOT_RECOGNIZED }
 

Functions

static void AlterEventTriggerOwner_internal (Relation rel, HeapTuple tup, Oid newOwnerId)
 
static event_trigger_command_tag_check_result check_ddl_tag (const char *tag)
 
static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag (const char *tag)
 
static void error_duplicate_filter_variable (const char *defname)
 
static Datum filter_list_to_array (List *filterlist)
 
static Oid insert_event_trigger_tuple (const char *trigname, const char *eventname, Oid evtOwner, Oid funcoid, List *tags)
 
static void validate_ddl_tags (const char *filtervar, List *taglist)
 
static void validate_table_rewrite_tags (const char *filtervar, List *taglist)
 
static void EventTriggerInvoke (List *fn_oid_list, EventTriggerData *trigdata)
 
static const char * stringify_grant_objtype (ObjectType objtype)
 
static const char * stringify_adefprivs_objtype (ObjectType objtype)
 
Oid CreateEventTrigger (CreateEventTrigStmt *stmt)
 
void RemoveEventTriggerById (Oid trigOid)
 
Oid AlterEventTrigger (AlterEventTrigStmt *stmt)
 
ObjectAddress AlterEventTriggerOwner (const char *name, Oid newOwnerId)
 
void AlterEventTriggerOwner_oid (Oid trigOid, Oid newOwnerId)
 
Oid get_event_trigger_oid (const char *trigname, bool missing_ok)
 
static bool filter_event_trigger (const char **tag, EventTriggerCacheItem *item)
 
static ListEventTriggerCommonSetup (Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
 
void EventTriggerDDLCommandStart (Node *parsetree)
 
void EventTriggerDDLCommandEnd (Node *parsetree)
 
void EventTriggerSQLDrop (Node *parsetree)
 
void EventTriggerTableRewrite (Node *parsetree, Oid tableOid, int reason)
 
bool EventTriggerSupportsObjectType (ObjectType obtype)
 
bool EventTriggerSupportsObjectClass (ObjectClass objclass)
 
bool EventTriggerBeginCompleteQuery (void)
 
void EventTriggerEndCompleteQuery (void)
 
bool trackDroppedObjectsNeeded (void)
 
void EventTriggerSQLDropAddObject (const ObjectAddress *object, bool original, bool normal)
 
Datum pg_event_trigger_dropped_objects (PG_FUNCTION_ARGS)
 
Datum pg_event_trigger_table_rewrite_oid (PG_FUNCTION_ARGS)
 
Datum pg_event_trigger_table_rewrite_reason (PG_FUNCTION_ARGS)
 
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)
 
Datum pg_event_trigger_ddl_commands (PG_FUNCTION_ARGS)
 

Variables

static EventTriggerQueryStatecurrentEventTriggerState = NULL
 
static const event_trigger_support_data event_trigger_support []
 

Typedef Documentation

◆ EventTriggerQueryState

◆ SQLDropObject

typedef struct SQLDropObject SQLDropObject

Enumeration Type Documentation

◆ event_trigger_command_tag_check_result

Enumerator
EVENT_TRIGGER_COMMAND_TAG_OK 
EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED 
EVENT_TRIGGER_COMMAND_TAG_NOT_RECOGNIZED 

Definition at line 81 of file event_trigger.c.

Function Documentation

◆ AlterEventTrigger()

Oid AlterEventTrigger ( AlterEventTrigStmt stmt)

Definition at line 503 of file event_trigger.c.

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

Referenced by standard_ProcessUtility().

504 {
505  Relation tgrel;
506  HeapTuple tup;
507  Oid trigoid;
508  Form_pg_event_trigger evtForm;
509  char tgenabled = stmt->tgenabled;
510 
511  tgrel = table_open(EventTriggerRelationId, RowExclusiveLock);
512 
514  CStringGetDatum(stmt->trigname));
515  if (!HeapTupleIsValid(tup))
516  ereport(ERROR,
517  (errcode(ERRCODE_UNDEFINED_OBJECT),
518  errmsg("event trigger \"%s\" does not exist",
519  stmt->trigname)));
520 
521  evtForm = (Form_pg_event_trigger) GETSTRUCT(tup);
522  trigoid = evtForm->oid;
523 
524  if (!pg_event_trigger_ownercheck(trigoid, GetUserId()))
526  stmt->trigname);
527 
528  /* tuple is a copy, so we can modify it below */
529  evtForm->evtenabled = tgenabled;
530 
531  CatalogTupleUpdate(tgrel, &tup->t_self, tup);
532 
533  InvokeObjectPostAlterHook(EventTriggerRelationId,
534  trigoid, 0);
535 
536  /* clean up */
537  heap_freetuple(tup);
539 
540  return trigoid;
541 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:380
int errcode(int sqlerrcode)
Definition: elog.c:608
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
#define ERROR
Definition: elog.h:43
bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid)
Definition: aclchk.c:5144
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
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:224
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ AlterEventTriggerOwner()

ObjectAddress AlterEventTriggerOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 547 of file event_trigger.c.

References AlterEventTriggerOwner_internal(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, EVENTTRIGGERNAME, GETSTRUCT, heap_freetuple(), HeapTupleIsValid, ObjectAddressSet, RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by ExecAlterOwnerStmt().

548 {
549  Oid evtOid;
550  HeapTuple tup;
551  Form_pg_event_trigger evtForm;
552  Relation rel;
553  ObjectAddress address;
554 
555  rel = table_open(EventTriggerRelationId, RowExclusiveLock);
556 
558 
559  if (!HeapTupleIsValid(tup))
560  ereport(ERROR,
561  (errcode(ERRCODE_UNDEFINED_OBJECT),
562  errmsg("event trigger \"%s\" does not exist", name)));
563 
564  evtForm = (Form_pg_event_trigger) GETSTRUCT(tup);
565  evtOid = evtForm->oid;
566 
567  AlterEventTriggerOwner_internal(rel, tup, newOwnerId);
568 
569  ObjectAddressSet(address, EventTriggerRelationId, evtOid);
570 
571  heap_freetuple(tup);
572 
574 
575  return address;
576 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:608
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
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:578
#define ereport(elevel, rest)
Definition: elog.h:141
FormData_pg_event_trigger * Form_pg_event_trigger
#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:174
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ AlterEventTriggerOwner_internal()

static void AlterEventTriggerOwner_internal ( Relation  rel,
HeapTuple  tup,
Oid  newOwnerId 
)
static

Definition at line 607 of file event_trigger.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, CatalogTupleUpdate(), changeDependencyOnOwner(), ereport, errcode(), errhint(), errmsg(), ERROR, GETSTRUCT, GetUserId(), InvokeObjectPostAlterHook, NameStr, OBJECT_EVENT_TRIGGER, pg_event_trigger_ownercheck(), superuser_arg(), and HeapTupleData::t_self.

Referenced by AlterEventTriggerOwner(), and AlterEventTriggerOwner_oid().

608 {
610 
611  form = (Form_pg_event_trigger) GETSTRUCT(tup);
612 
613  if (form->evtowner == newOwnerId)
614  return;
615 
616  if (!pg_event_trigger_ownercheck(form->oid, GetUserId()))
618  NameStr(form->evtname));
619 
620  /* New owner must be a superuser */
621  if (!superuser_arg(newOwnerId))
622  ereport(ERROR,
623  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
624  errmsg("permission denied to change owner of event trigger \"%s\"",
625  NameStr(form->evtname)),
626  errhint("The owner of an event trigger must be a superuser.")));
627 
628  form->evtowner = newOwnerId;
629  CatalogTupleUpdate(rel, &tup->t_self, tup);
630 
631  /* Update owner dependency reference */
632  changeDependencyOnOwner(EventTriggerRelationId,
633  form->oid,
634  newOwnerId);
635 
636  InvokeObjectPostAlterHook(EventTriggerRelationId,
637  form->oid, 0);
638 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:380
int errcode(int sqlerrcode)
Definition: elog.c:608
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:309
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
#define ERROR
Definition: elog.h:43
bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid)
Definition: aclchk.c:5144
ItemPointerData t_self
Definition: htup.h:65
#define ereport(elevel, rest)
Definition: elog.h:141
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
FormData_pg_event_trigger * Form_pg_event_trigger
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:224
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NameStr(name)
Definition: c.h:616

◆ AlterEventTriggerOwner_oid()

void AlterEventTriggerOwner_oid ( Oid  trigOid,
Oid  newOwnerId 
)

Definition at line 582 of file event_trigger.c.

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

Referenced by shdepReassignOwned().

583 {
584  HeapTuple tup;
585  Relation rel;
586 
587  rel = table_open(EventTriggerRelationId, RowExclusiveLock);
588 
590 
591  if (!HeapTupleIsValid(tup))
592  ereport(ERROR,
593  (errcode(ERRCODE_UNDEFINED_OBJECT),
594  errmsg("event trigger with OID %u does not exist", trigOid)));
595 
596  AlterEventTriggerOwner_internal(rel, tup, newOwnerId);
597 
598  heap_freetuple(tup);
599 
601 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errcode(int sqlerrcode)
Definition: elog.c:608
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
static void AlterEventTriggerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:141
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ check_ddl_tag()

static event_trigger_command_tag_check_result check_ddl_tag ( const char *  tag)
static

Definition at line 281 of file event_trigger.c.

References EVENT_TRIGGER_COMMAND_TAG_NOT_RECOGNIZED, EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED, EVENT_TRIGGER_COMMAND_TAG_OK, event_trigger_support_data::obtypename, pg_strcasecmp(), pg_strncasecmp(), and event_trigger_support_data::supported.

Referenced by EventTriggerCommonSetup(), and validate_ddl_tags().

282 {
283  const char *obtypename;
284  const event_trigger_support_data *etsd;
285 
286  /*
287  * Handle some idiosyncratic special cases.
288  */
289  if (pg_strcasecmp(tag, "CREATE TABLE AS") == 0 ||
290  pg_strcasecmp(tag, "SELECT INTO") == 0 ||
291  pg_strcasecmp(tag, "REFRESH MATERIALIZED VIEW") == 0 ||
292  pg_strcasecmp(tag, "ALTER DEFAULT PRIVILEGES") == 0 ||
293  pg_strcasecmp(tag, "ALTER LARGE OBJECT") == 0 ||
294  pg_strcasecmp(tag, "COMMENT") == 0 ||
295  pg_strcasecmp(tag, "GRANT") == 0 ||
296  pg_strcasecmp(tag, "REVOKE") == 0 ||
297  pg_strcasecmp(tag, "DROP OWNED") == 0 ||
298  pg_strcasecmp(tag, "IMPORT FOREIGN SCHEMA") == 0 ||
299  pg_strcasecmp(tag, "SECURITY LABEL") == 0)
301 
302  /*
303  * Otherwise, command should be CREATE, ALTER, or DROP.
304  */
305  if (pg_strncasecmp(tag, "CREATE ", 7) == 0)
306  obtypename = tag + 7;
307  else if (pg_strncasecmp(tag, "ALTER ", 6) == 0)
308  obtypename = tag + 6;
309  else if (pg_strncasecmp(tag, "DROP ", 5) == 0)
310  obtypename = tag + 5;
311  else
313 
314  /*
315  * ...and the object type should be something recognizable.
316  */
317  for (etsd = event_trigger_support; etsd->obtypename != NULL; etsd++)
318  if (pg_strcasecmp(etsd->obtypename, obtypename) == 0)
319  break;
320  if (etsd->obtypename == NULL)
322  if (!etsd->supported)
325 }
static const event_trigger_support_data event_trigger_support[]
Definition: event_trigger.c:89
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69

◆ check_table_rewrite_ddl_tag()

static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag ( const char *  tag)
static

Definition at line 351 of file event_trigger.c.

References EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED, EVENT_TRIGGER_COMMAND_TAG_OK, and pg_strcasecmp().

Referenced by EventTriggerCommonSetup(), and validate_table_rewrite_tags().

352 {
353  if (pg_strcasecmp(tag, "ALTER TABLE") == 0 ||
354  pg_strcasecmp(tag, "ALTER TYPE") == 0)
356 
358 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

◆ 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 evtowner = GetUserId();
175  ListCell *lc;
176  List *tags = NULL;
177 
178  /*
179  * It would be nice to allow database owners or even regular users to do
180  * this, but there are obvious privilege escalation risks which would have
181  * to somehow be plugged first.
182  */
183  if (!superuser())
184  ereport(ERROR,
185  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
186  errmsg("permission denied to create event trigger \"%s\"",
187  stmt->trigname),
188  errhint("Must be superuser to create an event trigger.")));
189 
190  /* Validate event name. */
191  if (strcmp(stmt->eventname, "ddl_command_start") != 0 &&
192  strcmp(stmt->eventname, "ddl_command_end") != 0 &&
193  strcmp(stmt->eventname, "sql_drop") != 0 &&
194  strcmp(stmt->eventname, "table_rewrite") != 0)
195  ereport(ERROR,
196  (errcode(ERRCODE_SYNTAX_ERROR),
197  errmsg("unrecognized event name \"%s\"",
198  stmt->eventname)));
199 
200  /* Validate filter conditions. */
201  foreach(lc, stmt->whenclause)
202  {
203  DefElem *def = (DefElem *) lfirst(lc);
204 
205  if (strcmp(def->defname, "tag") == 0)
206  {
207  if (tags != NULL)
209  tags = (List *) def->arg;
210  }
211  else
212  ereport(ERROR,
213  (errcode(ERRCODE_SYNTAX_ERROR),
214  errmsg("unrecognized filter variable \"%s\"", def->defname)));
215  }
216 
217  /* Validate tag list, if any. */
218  if ((strcmp(stmt->eventname, "ddl_command_start") == 0 ||
219  strcmp(stmt->eventname, "ddl_command_end") == 0 ||
220  strcmp(stmt->eventname, "sql_drop") == 0)
221  && tags != NULL)
222  validate_ddl_tags("tag", tags);
223  else if (strcmp(stmt->eventname, "table_rewrite") == 0
224  && tags != NULL)
225  validate_table_rewrite_tags("tag", tags);
226 
227  /*
228  * Give user a nice error message if an event trigger of the same name
229  * already exists.
230  */
232  if (HeapTupleIsValid(tuple))
233  ereport(ERROR,
235  errmsg("event trigger \"%s\" already exists",
236  stmt->trigname)));
237 
238  /* Find and validate the trigger function. */
239  funcoid = LookupFuncName(stmt->funcname, 0, NULL, false);
240  funcrettype = get_func_rettype(funcoid);
241  if (funcrettype != EVTTRIGGEROID)
242  ereport(ERROR,
243  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
244  errmsg("function %s must return type %s",
245  NameListToString(stmt->funcname), "event_trigger")));
246 
247  /* Insert catalog entries. */
248  return insert_event_trigger_tuple(stmt->trigname, stmt->eventname,
249  evtowner, funcoid, tags);
250 }
static void validate_ddl_tags(const char *filtervar, List *taglist)
int errhint(const char *fmt,...)
Definition: elog.c:1069
Oid GetUserId(void)
Definition: miscinit.c:380
int errcode(int sqlerrcode)
Definition: elog.c:608
bool superuser(void)
Definition: superuser.c:46
unsigned int Oid
Definition: postgres_ext.h:31
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1457
#define ERROR
Definition: elog.h:43
static void validate_table_rewrite_tags(const char *filtervar, List *taglist)
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
Definition: parse_func.c:2101
static void error_duplicate_filter_variable(const char *defname)
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
Node * arg
Definition: parsenodes.h:731
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
char * NameListToString(List *names)
Definition: namespace.c:3094
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:190
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * defname
Definition: parsenodes.h:730
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
Definition: pg_list.h:50

◆ error_duplicate_filter_variable()

static void error_duplicate_filter_variable ( const char *  defname)
static

Definition at line 364 of file event_trigger.c.

References ereport, errcode(), errmsg(), and ERROR.

Referenced by CreateEventTrigger().

365 {
366  ereport(ERROR,
367  (errcode(ERRCODE_SYNTAX_ERROR),
368  errmsg("filter variable \"%s\" specified more than once",
369  defname)));
370 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ EventTriggerAlterTableEnd()

void EventTriggerAlterTableEnd ( void  )

Definition at line 1789 of file event_trigger.c.

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

Referenced by AlterTableMoveAll(), and ProcessUtilitySlow().

1790 {
1791  CollectedCommand *parent;
1792 
1793  /* ignore if event trigger context not set, or collection disabled */
1794  if (!currentEventTriggerState ||
1796  return;
1797 
1799 
1800  /* If no subcommands, don't collect */
1802  {
1806  }
1807  else
1809 
1811 }
union CollectedCommand::@111 d
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
void pfree(void *pointer)
Definition: mcxt.c:1056
CollectedCommand * currentCommand
Definition: event_trigger.c:67
struct CollectedCommand * parent
List * lappend(List *list, void *datum)
Definition: list.c:322
static int list_length(const List *l)
Definition: pg_list.h:169
struct CollectedCommand::@111::@113 alterTable

◆ EventTriggerAlterTableRelid()

void EventTriggerAlterTableRelid ( Oid  objectId)

Definition at line 1736 of file event_trigger.c.

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

Referenced by AlterTableInternal(), and ProcessUtilitySlow().

1737 {
1738  if (!currentEventTriggerState ||
1740  return;
1741 
1742  currentEventTriggerState->currentCommand->d.alterTable.objectId = objectId;
1743 }
union CollectedCommand::@111 d
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
CollectedCommand * currentCommand
Definition: event_trigger.c:67
struct CollectedCommand::@111::@113 alterTable

◆ EventTriggerAlterTableStart()

void EventTriggerAlterTableStart ( Node parsetree)

Definition at line 1702 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::parent, CollectedCommand::parsetree, SCT_AlterTable, and CollectedCommand::type.

Referenced by AlterTableMoveAll(), and ProcessUtilitySlow().

1703 {
1704  MemoryContext oldcxt;
1705  CollectedCommand *command;
1706 
1707  /* ignore if event trigger context not set, or collection disabled */
1708  if (!currentEventTriggerState ||
1710  return;
1711 
1713 
1714  command = palloc(sizeof(CollectedCommand));
1715 
1716  command->type = SCT_AlterTable;
1717  command->in_extension = creating_extension;
1718 
1719  command->d.alterTable.classId = RelationRelationId;
1720  command->d.alterTable.objectId = InvalidOid;
1721  command->d.alterTable.subcmds = NIL;
1722  command->parsetree = copyObject(parsetree);
1723 
1726 
1727  MemoryContextSwitchTo(oldcxt);
1728 }
#define NIL
Definition: pg_list.h:65
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
CollectedCommand * currentCommand
Definition: event_trigger.c:67
struct CollectedCommand * parent
#define InvalidOid
Definition: postgres_ext.h:36
bool creating_extension
Definition: extension.c:70
struct CollectedCommand::@111::@113 alterTable
void * palloc(Size size)
Definition: mcxt.c:949
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerBeginCompleteQuery()

bool EventTriggerBeginCompleteQuery ( void  )

Definition at line 1230 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().

1231 {
1233  MemoryContext cxt;
1234 
1235  /*
1236  * Currently, sql_drop, table_rewrite, ddl_command_end events are the only
1237  * reason to have event trigger state at all; so if there are none, don't
1238  * install one.
1239  */
1241  return false;
1242 
1244  "event trigger state",
1246  state = MemoryContextAlloc(cxt, sizeof(EventTriggerQueryState));
1247  state->cxt = cxt;
1248  slist_init(&(state->SQLDropList));
1249  state->in_sql_drop = false;
1250  state->table_rewrite_oid = InvalidOid;
1251 
1254  state->currentCommand = NULL;
1255  state->commandList = NIL;
1257  currentEventTriggerState = state;
1258 
1259  return true;
1260 }
#define NIL
Definition: pg_list.h:65
#define AllocSetContextCreate
Definition: memutils.h:170
bool trackDroppedObjectsNeeded(void)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
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:67
struct EventTriggerQueryState * previous
Definition: event_trigger.c:70
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define InvalidOid
Definition: postgres_ext.h:36
Definition: regguts.h:298
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796

◆ EventTriggerCollectAlterDefPrivs()

void EventTriggerCollectAlterDefPrivs ( AlterDefaultPrivilegesStmt stmt)

Definition at line 1967 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().

1968 {
1969  MemoryContext oldcxt;
1970  CollectedCommand *command;
1971 
1972  /* ignore if event trigger context not set, or collection disabled */
1973  if (!currentEventTriggerState ||
1975  return;
1976 
1978 
1979  command = palloc0(sizeof(CollectedCommand));
1980  command->type = SCT_AlterDefaultPrivileges;
1981  command->d.defprivs.objtype = stmt->action->objtype;
1982  command->in_extension = creating_extension;
1983  command->parsetree = (Node *) copyObject(stmt);
1984 
1987  MemoryContextSwitchTo(oldcxt);
1988 }
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
struct CollectedCommand::@111::@118 defprivs
List * lappend(List *list, void *datum)
Definition: list.c:322
void * palloc0(Size size)
Definition: mcxt.c:980
bool creating_extension
Definition: extension.c:70
ObjectType objtype
Definition: parsenodes.h:1906
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerCollectAlterOpFam()

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

Definition at line 1865 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().

1867 {
1868  MemoryContext oldcxt;
1869  CollectedCommand *command;
1870 
1871  /* ignore if event trigger context not set, or collection disabled */
1872  if (!currentEventTriggerState ||
1874  return;
1875 
1877 
1878  command = palloc(sizeof(CollectedCommand));
1879  command->type = SCT_AlterOpFamily;
1880  command->in_extension = creating_extension;
1881  ObjectAddressSet(command->d.opfam.address,
1882  OperatorFamilyRelationId, opfamoid);
1883  command->d.opfam.operators = operators;
1884  command->d.opfam.procedures = procedures;
1885  command->parsetree = (Node *) copyObject(stmt);
1886 
1889 
1890  MemoryContextSwitchTo(oldcxt);
1891 }
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
List * lappend(List *list, void *datum)
Definition: list.c:322
bool creating_extension
Definition: extension.c:70
struct CollectedCommand::@111::@115 opfam
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void * palloc(Size size)
Definition: mcxt.c:949
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerCollectAlterTableSubcmd()

void EventTriggerCollectAlterTableSubcmd ( Node subcmd,
ObjectAddress  address 
)

Definition at line 1754 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().

1755 {
1756  MemoryContext oldcxt;
1758 
1759  /* ignore if event trigger context not set, or collection disabled */
1760  if (!currentEventTriggerState ||
1762  return;
1763 
1764  Assert(IsA(subcmd, AlterTableCmd));
1767 
1769 
1770  newsub = palloc(sizeof(CollectedATSubcmd));
1771  newsub->address = address;
1772  newsub->parsetree = copyObject(subcmd);
1773 
1776 
1777  MemoryContextSwitchTo(oldcxt);
1778 }
union CollectedCommand::@111 d
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define OidIsValid(objectId)
Definition: c.h:645
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
CollectedCommand * currentCommand
Definition: event_trigger.c:67
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:739
struct CollectedCommand::@111::@113 alterTable
void * palloc(Size size)
Definition: mcxt.c:949
#define copyObject(obj)
Definition: nodes.h:641
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 1932 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().

1934 {
1935  MemoryContext oldcxt;
1936  CollectedCommand *command;
1937 
1938  /* ignore if event trigger context not set, or collection disabled */
1939  if (!currentEventTriggerState ||
1941  return;
1942 
1944 
1945  command = palloc0(sizeof(CollectedCommand));
1946  command->type = SCT_AlterTSConfig;
1947  command->in_extension = creating_extension;
1948  ObjectAddressSet(command->d.atscfg.address,
1949  TSConfigRelationId, cfgId);
1950  command->d.atscfg.dictIds = palloc(sizeof(Oid) * ndicts);
1951  memcpy(command->d.atscfg.dictIds, dictIds, sizeof(Oid) * ndicts);
1952  command->d.atscfg.ndicts = ndicts;
1953  command->parsetree = (Node *) copyObject(stmt);
1954 
1957 
1958  MemoryContextSwitchTo(oldcxt);
1959 }
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
CollectedCommandType type
unsigned int Oid
Definition: postgres_ext.h:31
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
struct CollectedCommand::@111::@117 atscfg
List * lappend(List *list, void *datum)
Definition: list.c:322
void * palloc0(Size size)
Definition: mcxt.c:980
bool creating_extension
Definition: extension.c:70
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
void * palloc(Size size)
Definition: mcxt.c:949
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerCollectCreateOpClass()

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

Definition at line 1898 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().

1900 {
1901  MemoryContext oldcxt;
1902  CollectedCommand *command;
1903 
1904  /* ignore if event trigger context not set, or collection disabled */
1905  if (!currentEventTriggerState ||
1907  return;
1908 
1910 
1911  command = palloc0(sizeof(CollectedCommand));
1912  command->type = SCT_CreateOpClass;
1913  command->in_extension = creating_extension;
1914  ObjectAddressSet(command->d.createopc.address,
1915  OperatorClassRelationId, opcoid);
1916  command->d.createopc.operators = operators;
1917  command->d.createopc.procedures = procedures;
1918  command->parsetree = (Node *) copyObject(stmt);
1919 
1922 
1923  MemoryContextSwitchTo(oldcxt);
1924 }
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
struct CollectedCommand::@111::@116 createopc
List * lappend(List *list, void *datum)
Definition: list.c:322
void * palloc0(Size size)
Definition: mcxt.c:980
bool creating_extension
Definition: extension.c:70
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerCollectGrant()

void EventTriggerCollectGrant ( InternalGrant istmt)

Definition at line 1821 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().

1822 {
1823  MemoryContext oldcxt;
1824  CollectedCommand *command;
1825  InternalGrant *icopy;
1826  ListCell *cell;
1827 
1828  /* ignore if event trigger context not set, or collection disabled */
1829  if (!currentEventTriggerState ||
1831  return;
1832 
1834 
1835  /*
1836  * This is tedious, but necessary.
1837  */
1838  icopy = palloc(sizeof(InternalGrant));
1839  memcpy(icopy, istmt, sizeof(InternalGrant));
1840  icopy->objects = list_copy(istmt->objects);
1841  icopy->grantees = list_copy(istmt->grantees);
1842  icopy->col_privs = NIL;
1843  foreach(cell, istmt->col_privs)
1844  icopy->col_privs = lappend(icopy->col_privs, copyObject(lfirst(cell)));
1845 
1846  /* Now collect it, using the copied InternalGrant */
1847  command = palloc(sizeof(CollectedCommand));
1848  command->type = SCT_Grant;
1849  command->in_extension = creating_extension;
1850  command->d.grant.istmt = icopy;
1851  command->parsetree = NULL;
1852 
1855 
1856  MemoryContextSwitchTo(oldcxt);
1857 }
#define NIL
Definition: pg_list.h:65
union CollectedCommand::@111 d
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * list_copy(const List *oldlist)
Definition: list.c:1404
struct CollectedCommand::@111::@114 grant
CollectedCommandType type
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
List * lappend(List *list, void *datum)
Definition: list.c:322
bool creating_extension
Definition: extension.c:70
#define lfirst(lc)
Definition: pg_list.h:190
void * palloc(Size size)
Definition: mcxt.c:949
#define copyObject(obj)
Definition: nodes.h:641

◆ EventTriggerCollectSimpleCommand()

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

Definition at line 1664 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().

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

◆ EventTriggerCommonSetup()

static List* EventTriggerCommonSetup ( Node parsetree,
EventTriggerEvent  event,
const char *  eventstr,
EventTriggerData trigdata 
)
static

Definition at line 699 of file event_trigger.c.

References check_ddl_tag(), check_table_rewrite_ddl_tag(), CreateCommandTag(), elog, ERROR, EventTriggerData::event, EVENT_TRIGGER_COMMAND_TAG_OK, EventCacheLookup(), EVT_DDLCommandEnd, EVT_DDLCommandStart, EVT_SQLDrop, EVT_TableRewrite, filter_event_trigger(), EventTriggerCacheItem::fnoid, lappend_oid(), lfirst, NIL, EventTriggerData::parsetree, T_EventTriggerData, EventTriggerData::tag, and EventTriggerData::type.

Referenced by EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerSQLDrop(), and EventTriggerTableRewrite().

702 {
703  const char *tag;
704  List *cachelist;
705  ListCell *lc;
706  List *runlist = NIL;
707 
708  /*
709  * We want the list of command tags for which this procedure is actually
710  * invoked to match up exactly with the list that CREATE EVENT TRIGGER
711  * accepts. This debugging cross-check will throw an error if this
712  * function is invoked for a command tag that CREATE EVENT TRIGGER won't
713  * accept. (Unfortunately, there doesn't seem to be any simple, automated
714  * way to verify that CREATE EVENT TRIGGER doesn't accept extra stuff that
715  * never reaches this control point.)
716  *
717  * If this cross-check fails for you, you probably need to either adjust
718  * standard_ProcessUtility() not to invoke event triggers for the command
719  * type in question, or you need to adjust check_ddl_tag to accept the
720  * relevant command tag.
721  */
722 #ifdef USE_ASSERT_CHECKING
723  {
724  const char *dbgtag;
725 
726  dbgtag = CreateCommandTag(parsetree);
727  if (event == EVT_DDLCommandStart ||
728  event == EVT_DDLCommandEnd ||
729  event == EVT_SQLDrop)
730  {
732  elog(ERROR, "unexpected command tag \"%s\"", dbgtag);
733  }
734  else if (event == EVT_TableRewrite)
735  {
737  elog(ERROR, "unexpected command tag \"%s\"", dbgtag);
738  }
739  }
740 #endif
741 
742  /* Use cache to find triggers for this event; fast exit if none. */
743  cachelist = EventCacheLookup(event);
744  if (cachelist == NIL)
745  return NIL;
746 
747  /* Get the command tag. */
748  tag = CreateCommandTag(parsetree);
749 
750  /*
751  * Filter list of event triggers by command tag, and copy them into our
752  * memory context. Once we start running the command triggers, or indeed
753  * once we do anything at all that touches the catalogs, an invalidation
754  * might leave cachelist pointing at garbage, so we must do this before we
755  * can do much else.
756  */
757  foreach(lc, cachelist)
758  {
759  EventTriggerCacheItem *item = lfirst(lc);
760 
761  if (filter_event_trigger(&tag, item))
762  {
763  /* We must plan to fire this trigger. */
764  runlist = lappend_oid(runlist, item->fnoid);
765  }
766  }
767 
768  /* don't spend any more time on this if no functions to run */
769  if (runlist == NIL)
770  return NIL;
771 
772  trigdata->type = T_EventTriggerData;
773  trigdata->event = eventstr;
774  trigdata->parsetree = parsetree;
775  trigdata->tag = tag;
776 
777  return runlist;
778 }
#define NIL
Definition: pg_list.h:65
const char * tag
Definition: event_trigger.h:28
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
static event_trigger_command_tag_check_result check_ddl_tag(const char *tag)
static bool filter_event_trigger(const char **tag, EventTriggerCacheItem *item)
#define ERROR
Definition: elog.h:43
static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag(const char *tag)
const char * event
Definition: event_trigger.h:26
const char * CreateCommandTag(Node *parsetree)
Definition: utility.c:2093
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:228
List * EventCacheLookup(EventTriggerEvent event)
Definition: evtcache.c:64
Definition: pg_list.h:50

◆ EventTriggerDDLCommandEnd()

void EventTriggerDDLCommandEnd ( Node parsetree)

Definition at line 832 of file event_trigger.c.

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

Referenced by ProcessUtilitySlow().

833 {
834  List *runlist;
835  EventTriggerData trigdata;
836 
837  /*
838  * See EventTriggerDDLCommandStart for a discussion about why event
839  * triggers are disabled in single user mode.
840  */
841  if (!IsUnderPostmaster)
842  return;
843 
844  /*
845  * Also do nothing if our state isn't set up, which it won't be if there
846  * weren't any relevant event triggers at the start of the current DDL
847  * command. This test might therefore seem optional, but it's important
848  * because EventTriggerCommonSetup might find triggers that didn't exist
849  * at the time the command started. Although this function itself
850  * wouldn't crash, the event trigger functions would presumably call
851  * pg_event_trigger_ddl_commands which would fail. Better to do nothing
852  * until the next command.
853  */
855  return;
856 
857  runlist = EventTriggerCommonSetup(parsetree,
858  EVT_DDLCommandEnd, "ddl_command_end",
859  &trigdata);
860  if (runlist == NIL)
861  return;
862 
863  /*
864  * Make sure anything the main command did will be visible to the event
865  * triggers.
866  */
868 
869  /* Run the triggers. */
870  EventTriggerInvoke(runlist, &trigdata);
871 
872  /* Cleanup. */
873  list_free(runlist);
874 }
#define NIL
Definition: pg_list.h:65
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata)
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
static List * EventTriggerCommonSetup(Node *parsetree, EventTriggerEvent event, const char *eventstr, EventTriggerData *trigdata)
bool IsUnderPostmaster
Definition: globals.c:109
void CommandCounterIncrement(void)
Definition: xact.c:1005
void list_free(List *list)
Definition: list.c:1377
Definition: pg_list.h:50

◆ EventTriggerDDLCommandStart()

void EventTriggerDDLCommandStart ( Node parsetree)

Definition at line 784 of file event_trigger.c.

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

Referenced by ProcessUtilitySlow().

785 {
786  List *runlist;
787  EventTriggerData trigdata;
788 
789  /*
790  * Event Triggers are completely disabled in standalone mode. There are
791  * (at least) two reasons for this:
792  *
793  * 1. A sufficiently broken event trigger might not only render the
794  * database unusable, but prevent disabling itself to fix the situation.
795  * In this scenario, restarting in standalone mode provides an escape
796  * hatch.
797  *
798  * 2. BuildEventTriggerCache relies on systable_beginscan_ordered, and
799  * therefore will malfunction if pg_event_trigger's indexes are damaged.
800  * To allow recovery from a damaged index, we need some operating mode
801  * wherein event triggers are disabled. (Or we could implement
802  * heapscan-and-sort logic for that case, but having disaster recovery
803  * scenarios depend on code that's otherwise untested isn't appetizing.)
804  */
805  if (!IsUnderPostmaster)
806  return;
807 
808  runlist = EventTriggerCommonSetup(parsetree,
810  "ddl_command_start",
811  &trigdata);
812  if (runlist == NIL)
813  return;
814 
815  /* Run the triggers. */
816  EventTriggerInvoke(runlist, &trigdata);
817 
818  /* Cleanup. */
819  list_free(runlist);
820 
821  /*
822  * Make sure anything the event triggers did will be visible to the main
823  * command.
824  */
826 }
#define NIL
Definition: pg_list.h:65
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:109
void CommandCounterIncrement(void)
Definition: xact.c:1005
void list_free(List *list)
Definition: list.c:1377
Definition: pg_list.h:50

◆ EventTriggerEndCompleteQuery()

void EventTriggerEndCompleteQuery ( void  )

Definition at line 1274 of file event_trigger.c.

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

Referenced by ProcessUtilitySlow().

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

◆ EventTriggerInhibitCommandCollection()

void EventTriggerInhibitCommandCollection ( void  )

Definition at line 1630 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited.

Referenced by ProcessUtilitySlow().

1631 {
1633  return;
1634 
1636 }
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73

◆ EventTriggerInvoke()

static void EventTriggerInvoke ( List fn_oid_list,
EventTriggerData trigdata 
)
static

Definition at line 1028 of file event_trigger.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, check_stack_depth(), CommandCounterIncrement(), CurrentMemoryContext, DEBUG1, elog, fmgr_info(), FunctionCallInvoke, InitFunctionCallInfoData, InvalidOid, lfirst_oid, LOCAL_FCINFO, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), pgstat_end_function_usage(), and pgstat_init_function_usage().

Referenced by EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerSQLDrop(), and EventTriggerTableRewrite().

1029 {
1030  MemoryContext context;
1031  MemoryContext oldcontext;
1032  ListCell *lc;
1033  bool first = true;
1034 
1035  /* Guard against stack overflow due to recursive event trigger */
1037 
1038  /*
1039  * Let's evaluate event triggers in their own memory context, so that any
1040  * leaks get cleaned up promptly.
1041  */
1043  "event trigger context",
1045  oldcontext = MemoryContextSwitchTo(context);
1046 
1047  /* Call each event trigger. */
1048  foreach(lc, fn_oid_list)
1049  {
1050  LOCAL_FCINFO(fcinfo, 0);
1051  Oid fnoid = lfirst_oid(lc);
1052  FmgrInfo flinfo;
1053  PgStat_FunctionCallUsage fcusage;
1054 
1055  elog(DEBUG1, "EventTriggerInvoke %u", fnoid);
1056 
1057  /*
1058  * We want each event trigger to be able to see the results of the
1059  * previous event trigger's action. Caller is responsible for any
1060  * command-counter increment that is needed between the event trigger
1061  * and anything else in the transaction.
1062  */
1063  if (first)
1064  first = false;
1065  else
1067 
1068  /* Look up the function */
1069  fmgr_info(fnoid, &flinfo);
1070 
1071  /* Call the function, passing no arguments but setting a context. */
1072  InitFunctionCallInfoData(*fcinfo, &flinfo, 0,
1073  InvalidOid, (Node *) trigdata, NULL);
1074  pgstat_init_function_usage(fcinfo, &fcusage);
1075  FunctionCallInvoke(fcinfo);
1076  pgstat_end_function_usage(&fcusage, true);
1077 
1078  /* Reclaim memory. */
1079  MemoryContextReset(context);
1080  }
1081 
1082  /* Restore old memory context and delete the temporary one. */
1083  MemoryContextSwitchTo(oldcontext);
1084  MemoryContextDelete(context);
1085 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
#define DEBUG1
Definition: elog.h:25
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
unsigned int Oid
Definition: postgres_ext.h:31
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1620
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:167
void check_stack_depth(void)
Definition: postgres.c:3284
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void CommandCounterIncrement(void)
Definition: xact.c:1005
#define InvalidOid
Definition: postgres_ext.h:36
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1692
#define elog(elevel,...)
Definition: elog.h:228
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ EventTriggerSQLDrop()

void EventTriggerSQLDrop ( Node parsetree)

Definition at line 880 of file event_trigger.c.

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

Referenced by ProcessUtilitySlow().

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

◆ EventTriggerSQLDropAddObject()

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

Definition at line 1324 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_attnum_oid(), get_object_namensp_unique(), getObjectClass(), getObjectIdentityParts(), getObjectTypeDescription(), heap_getattr, 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(), EventTriggerQueryState::SQLDropList, table_close(), and table_open().

Referenced by deleteObjectsInList(), and DropSubscription().

1325 {
1326  SQLDropObject *obj;
1327  MemoryContext oldcxt;
1328 
1330  return;
1331 
1333 
1334  /* don't report temp schemas except my own */
1335  if (object->classId == NamespaceRelationId &&
1336  (isAnyTempNamespace(object->objectId) &&
1337  !isTempNamespace(object->objectId)))
1338  return;
1339 
1341 
1342  obj = palloc0(sizeof(SQLDropObject));
1343  obj->address = *object;
1344  obj->original = original;
1345  obj->normal = normal;
1346 
1347  /*
1348  * Obtain schema names from the object's catalog tuple, if one exists;
1349  * this lets us skip objects in temp schemas. We trust that
1350  * ObjectProperty contains all object classes that can be
1351  * schema-qualified.
1352  */
1353  if (is_objectclass_supported(object->classId))
1354  {
1355  Relation catalog;
1356  HeapTuple tuple;
1357 
1358  catalog = table_open(obj->address.classId, AccessShareLock);
1359  tuple = get_catalog_object_by_oid(catalog,
1360  get_object_attnum_oid(object->classId),
1361  obj->address.objectId);
1362 
1363  if (tuple)
1364  {
1366  Datum datum;
1367  bool isnull;
1368 
1370  if (attnum != InvalidAttrNumber)
1371  {
1372  datum = heap_getattr(tuple, attnum,
1373  RelationGetDescr(catalog), &isnull);
1374  if (!isnull)
1375  {
1376  Oid namespaceId;
1377 
1378  namespaceId = DatumGetObjectId(datum);
1379  /* temp objects are only reported if they are my own */
1380  if (isTempNamespace(namespaceId))
1381  {
1382  obj->schemaname = "pg_temp";
1383  obj->istemp = true;
1384  }
1385  else if (isAnyTempNamespace(namespaceId))
1386  {
1387  pfree(obj);
1388  table_close(catalog, AccessShareLock);
1389  MemoryContextSwitchTo(oldcxt);
1390  return;
1391  }
1392  else
1393  {
1394  obj->schemaname = get_namespace_name(namespaceId);
1395  obj->istemp = false;
1396  }
1397  }
1398  }
1399 
1401  obj->address.objectSubId == 0)
1402  {
1403  attnum = get_object_attnum_name(obj->address.classId);
1404  if (attnum != InvalidAttrNumber)
1405  {
1406  datum = heap_getattr(tuple, attnum,
1407  RelationGetDescr(catalog), &isnull);
1408  if (!isnull)
1409  obj->objname = pstrdup(NameStr(*DatumGetName(datum)));
1410  }
1411  }
1412  }
1413 
1414  table_close(catalog, AccessShareLock);
1415  }
1416  else
1417  {
1418  if (object->classId == NamespaceRelationId &&
1419  isTempNamespace(object->objectId))
1420  obj->istemp = true;
1421  }
1422 
1423  /* object identity, objname and objargs */
1424  obj->objidentity =
1425  getObjectIdentityParts(&obj->address, &obj->addrnames, &obj->addrargs);
1426 
1427  /* object type */
1429 
1431 
1432  MemoryContextSwitchTo(oldcxt);
1433 }
bool get_object_namensp_unique(Oid class_id)
slist_node next
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
ObjectAddress address
char * getObjectTypeDescription(const ObjectAddress *object)
AttrNumber get_object_attnum_oid(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:448
const char * objname
#define DatumGetObjectId(X)
Definition: postgres.h:500
char * pstrdup(const char *in)
Definition: mcxt.c:1186
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)
const char * objidentity
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2712
const char * objecttype
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
#define DatumGetName(X)
Definition: postgres.h:585
void pfree(void *pointer)
Definition: mcxt.c:1056
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:3149
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
const char * schemaname
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:980
HeapTuple get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:739
#define InvalidAttrNumber
Definition: attnum.h:23
#define NameStr(name)
Definition: c.h:616
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:3187
bool is_objectclass_supported(Oid class_id)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
int16 AttrNumber
Definition: attnum.h:21

◆ EventTriggerSupportsObjectClass()

bool EventTriggerSupportsObjectClass ( ObjectClass  objclass)

Definition at line 1165 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().

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

◆ EventTriggerSupportsObjectType()

bool EventTriggerSupportsObjectType ( ObjectType  obtype)

Definition at line 1091 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().

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

◆ EventTriggerTableRewrite()

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

Definition at line 951 of file event_trigger.c.

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

Referenced by ATRewriteTables().

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

◆ EventTriggerUndoInhibitCommandCollection()

void EventTriggerUndoInhibitCommandCollection ( void  )

Definition at line 1642 of file event_trigger.c.

References EventTriggerQueryState::commandCollectionInhibited.

Referenced by ProcessUtilitySlow().

1643 {
1645  return;
1646 
1648 }
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73

◆ filter_event_trigger()

static bool filter_event_trigger ( const char **  tag,
EventTriggerCacheItem item 
)
static

Definition at line 666 of file event_trigger.c.

References EventTriggerCacheItem::enabled, EventTriggerCacheItem::ntags, pg_qsort_strcmp(), SESSION_REPLICATION_ROLE_REPLICA, SessionReplicationRole, EventTriggerCacheItem::tag, TRIGGER_FIRES_ON_ORIGIN, and TRIGGER_FIRES_ON_REPLICA.

Referenced by EventTriggerCommonSetup().

667 {
668  /*
669  * Filter by session replication role, knowing that we never see disabled
670  * items down here.
671  */
673  {
674  if (item->enabled == TRIGGER_FIRES_ON_ORIGIN)
675  return false;
676  }
677  else
678  {
679  if (item->enabled == TRIGGER_FIRES_ON_REPLICA)
680  return false;
681  }
682 
683  /* Filter by tags, if any were specified. */
684  if (item->ntags != 0 && bsearch(tag, item->tag,
685  item->ntags, sizeof(char *),
686  pg_qsort_strcmp) == NULL)
687  return false;
688 
689  /* if we reach that point, we're not filtering out this item */
690  return true;
691 }
#define TRIGGER_FIRES_ON_ORIGIN
Definition: trigger.h:155
int SessionReplicationRole
Definition: trigger.c:67
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:147
int pg_qsort_strcmp(const void *a, const void *b)
Definition: qsort.c:232
#define TRIGGER_FIRES_ON_REPLICA
Definition: trigger.h:157

◆ filter_list_to_array()

static Datum filter_list_to_array ( List filterlist)
static

Definition at line 452 of file event_trigger.c.

References construct_array(), cstring_to_text(), i, lfirst, list_length(), palloc(), pfree(), pg_ascii_toupper(), PointerGetDatum, pstrdup(), strVal, and value.

Referenced by insert_event_trigger_tuple().

453 {
454  ListCell *lc;
455  Datum *data;
456  int i = 0,
457  l = list_length(filterlist);
458 
459  data = (Datum *) palloc(l * sizeof(Datum));
460 
461  foreach(lc, filterlist)
462  {
463  const char *value = strVal(lfirst(lc));
464  char *result,
465  *p;
466 
467  result = pstrdup(value);
468  for (p = result; *p; p++)
469  *p = pg_ascii_toupper((unsigned char) *p);
470  data[i++] = PointerGetDatum(cstring_to_text(result));
471  pfree(result);
472  }
473 
474  return PointerGetDatum(construct_array(data, l, TEXTOID, -1, false, 'i'));
475 }
#define PointerGetDatum(X)
Definition: postgres.h:556
char * pstrdup(const char *in)
Definition: mcxt.c:1186
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
static struct @145 value
#define strVal(v)
Definition: value.h:54
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
void pfree(void *pointer)
Definition: mcxt.c:1056
uintptr_t Datum
Definition: postgres.h:367
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ get_event_trigger_oid()

Oid get_event_trigger_oid ( const char *  trigname,
bool  missing_ok 
)

Definition at line 647 of file event_trigger.c.

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

Referenced by get_object_address_unqualified().

648 {
649  Oid oid;
650 
651  oid = GetSysCacheOid1(EVENTTRIGGERNAME, Anum_pg_event_trigger_oid,
652  CStringGetDatum(trigname));
653  if (!OidIsValid(oid) && !missing_ok)
654  ereport(ERROR,
655  (errcode(ERRCODE_UNDEFINED_OBJECT),
656  errmsg("event trigger \"%s\" does not exist", trigname)));
657  return oid;
658 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ insert_event_trigger_tuple()

static Oid insert_event_trigger_tuple ( const char *  trigname,
const char *  eventname,
Oid  evtOwner,
Oid  funcoid,
List tags 
)
static

Definition at line 376 of file event_trigger.c.

References CatalogTupleInsert(), CharGetDatum, ObjectAddress::classId, DEPENDENCY_NORMAL, EventTriggerOidIndexId, filter_list_to_array(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), InvokeObjectPostCreateHook, NameGetDatum, namestrcpy(), NIL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, RelationData::rd_att, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, table_close(), table_open(), TRIGGER_FIRES_ON_ORIGIN, and values.

Referenced by CreateEventTrigger().

378 {
379  Relation tgrel;
380  Oid trigoid;
381  HeapTuple tuple;
382  Datum values[Natts_pg_trigger];
383  bool nulls[Natts_pg_trigger];
384  NameData evtnamedata,
385  evteventdata;
386  ObjectAddress myself,
387  referenced;
388 
389  /* Open pg_event_trigger. */
390  tgrel = table_open(EventTriggerRelationId, RowExclusiveLock);
391 
392  /* Build the new pg_trigger tuple. */
394  Anum_pg_event_trigger_oid);
395  values[Anum_pg_event_trigger_oid - 1] = ObjectIdGetDatum(trigoid);
396  memset(nulls, false, sizeof(nulls));
397  namestrcpy(&evtnamedata, trigname);
398  values[Anum_pg_event_trigger_evtname - 1] = NameGetDatum(&evtnamedata);
399  namestrcpy(&evteventdata, eventname);
400  values[Anum_pg_event_trigger_evtevent - 1] = NameGetDatum(&evteventdata);
401  values[Anum_pg_event_trigger_evtowner - 1] = ObjectIdGetDatum(evtOwner);
402  values[Anum_pg_event_trigger_evtfoid - 1] = ObjectIdGetDatum(funcoid);
403  values[Anum_pg_event_trigger_evtenabled - 1] =
405  if (taglist == NIL)
406  nulls[Anum_pg_event_trigger_evttags - 1] = true;
407  else
408  values[Anum_pg_event_trigger_evttags - 1] =
409  filter_list_to_array(taglist);
410 
411  /* Insert heap tuple. */
412  tuple = heap_form_tuple(tgrel->rd_att, values, nulls);
413  CatalogTupleInsert(tgrel, tuple);
414  heap_freetuple(tuple);
415 
416  /* Depend on owner. */
417  recordDependencyOnOwner(EventTriggerRelationId, trigoid, evtOwner);
418 
419  /* Depend on event trigger function. */
420  myself.classId = EventTriggerRelationId;
421  myself.objectId = trigoid;
422  myself.objectSubId = 0;
423  referenced.classId = ProcedureRelationId;
424  referenced.objectId = funcoid;
425  referenced.objectSubId = 0;
426  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
427 
428  /* Depend on extension, if any. */
429  recordDependencyOnCurrentExtension(&myself, false);
430 
431  /* Post creation hook for new event trigger */
432  InvokeObjectPostCreateHook(EventTriggerRelationId, trigoid, 0);
433 
434  /* Close pg_event_trigger. */
436 
437  return trigoid;
438 }
#define NIL
Definition: pg_list.h:65
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:322
#define NameGetDatum(X)
Definition: postgres.h:595
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:250
#define TRIGGER_FIRES_ON_ORIGIN
Definition: trigger.h:155
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
static Datum filter_list_to_array(List *filterlist)
Definition: c.h:610
#define RowExclusiveLock
Definition: lockdefs.h:38
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:84
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:138
#define EventTriggerOidIndexId
Definition: indexing.h:263
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183

◆ pg_event_trigger_ddl_commands()

Datum pg_event_trigger_ddl_commands ( PG_FUNCTION_ARGS  )

Definition at line 1995 of file event_trigger.c.

References AccessShareLock, ReturnSetInfo::allowedModes, CollectedCommand::alterTable, CollectedCommand::atscfg, BoolGetDatum, ObjectAddress::classId, EventTriggerQueryState::commandList, CreateCommandTag(), CollectedCommand::createopc, CStringGetTextDatum, CollectedCommand::d, CollectedCommand::defprivs, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), get_catalog_object_by_oid(), get_namespace_name(), get_object_attnum_namespace(), get_object_attnum_oid(), getObjectIdentity(), getObjectTypeDescription(), CollectedCommand::grant, heap_getattr, HeapTupleIsValid, i, CollectedCommand::in_extension, Int32GetDatum, InvalidAttrNumber, is_objectclass_supported(), IsA, isAnyTempNamespace(), lfirst, MemoryContextSwitchTo(), MemSet, ObjectAddressSet, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, CollectedCommand::opfam, CollectedCommand::parsetree, PG_RETURN_VOID, PointerGetDatum, pstrdup(), RelationGetDescr, ReturnSetInfo::returnMode, SCT_AlterDefaultPrivileges, SCT_AlterOpFamily, SCT_AlterTable, SCT_AlterTSConfig, SCT_CreateOpClass, SCT_Grant, SCT_Simple, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, CollectedCommand::simple, stringify_adefprivs_objtype(), stringify_grant_objtype(), table_close(), table_open(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), CollectedCommand::type, generate_unaccent_rules::type, TYPEFUNC_COMPOSITE, values, and work_mem.

1996 {
1997  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1998  TupleDesc tupdesc;
1999  Tuplestorestate *tupstore;
2000  MemoryContext per_query_ctx;
2001  MemoryContext oldcontext;
2002  ListCell *lc;
2003 
2004  /*
2005  * Protect this function from being called out of context
2006  */
2008  ereport(ERROR,
2009  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
2010  errmsg("%s can only be called in an event trigger function",
2011  "pg_event_trigger_ddl_commands()")));
2012 
2013  /* check to see if caller supports us returning a tuplestore */
2014  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
2015  ereport(ERROR,
2016  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2017  errmsg("set-valued function called in context that cannot accept a set")));
2018  if (!(rsinfo->allowedModes & SFRM_Materialize))
2019  ereport(ERROR,
2020  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2021  errmsg("materialize mode required, but it is not allowed in this context")));
2022 
2023  /* Build a tuple descriptor for our result type */
2024  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
2025  elog(ERROR, "return type must be a row type");
2026 
2027  /* Build tuplestore to hold the result rows */
2028  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
2029  oldcontext = MemoryContextSwitchTo(per_query_ctx);
2030 
2031  tupstore = tuplestore_begin_heap(true, false, work_mem);
2032  rsinfo->returnMode = SFRM_Materialize;
2033  rsinfo->setResult = tupstore;
2034  rsinfo->setDesc = tupdesc;
2035 
2036  MemoryContextSwitchTo(oldcontext);
2037 
2039  {
2040  CollectedCommand *cmd = lfirst(lc);
2041  Datum values[9];
2042  bool nulls[9];
2043  ObjectAddress addr;
2044  int i = 0;
2045 
2046  /*
2047  * For IF NOT EXISTS commands that attempt to create an existing
2048  * object, the returned OID is Invalid. Don't return anything.
2049  *
2050  * One might think that a viable alternative would be to look up the
2051  * Oid of the existing object and run the deparse with that. But
2052  * since the parse tree might be different from the one that created
2053  * the object in the first place, we might not end up in a consistent
2054  * state anyway.
2055  */
2056  if (cmd->type == SCT_Simple &&
2057  !OidIsValid(cmd->d.simple.address.objectId))
2058  continue;
2059 
2060  MemSet(nulls, 0, sizeof(nulls));
2061 
2062  switch (cmd->type)
2063  {
2064  case SCT_Simple:
2065  case SCT_AlterTable:
2066  case SCT_AlterOpFamily:
2067  case SCT_CreateOpClass:
2068  case SCT_AlterTSConfig:
2069  {
2070  char *identity;
2071  char *type;
2072  char *schema = NULL;
2073 
2074  if (cmd->type == SCT_Simple)
2075  addr = cmd->d.simple.address;
2076  else if (cmd->type == SCT_AlterTable)
2077  ObjectAddressSet(addr,
2078  cmd->d.alterTable.classId,
2079  cmd->d.alterTable.objectId);
2080  else if (cmd->type == SCT_AlterOpFamily)
2081  addr = cmd->d.opfam.address;
2082  else if (cmd->type == SCT_CreateOpClass)
2083  addr = cmd->d.createopc.address;
2084  else if (cmd->type == SCT_AlterTSConfig)
2085  addr = cmd->d.atscfg.address;
2086 
2087  type = getObjectTypeDescription(&addr);
2088  identity = getObjectIdentity(&addr);
2089 
2090  /*
2091  * Obtain schema name, if any ("pg_temp" if a temp
2092  * object). If the object class is not in the supported
2093  * list here, we assume it's a schema-less object type,
2094  * and thus "schema" remains set to NULL.
2095  */
2097  {
2098  AttrNumber nspAttnum;
2099 
2100  nspAttnum = get_object_attnum_namespace(addr.classId);
2101  if (nspAttnum != InvalidAttrNumber)
2102  {
2103  Relation catalog;
2104  HeapTuple objtup;
2105  Oid schema_oid;
2106  bool isnull;
2107 
2108  catalog = table_open(addr.classId, AccessShareLock);
2109  objtup = get_catalog_object_by_oid(catalog,
2111  addr.objectId);
2112  if (!HeapTupleIsValid(objtup))
2113  elog(ERROR, "cache lookup failed for object %u/%u",
2114  addr.classId, addr.objectId);
2115  schema_oid =
2116  heap_getattr(objtup, nspAttnum,
2117  RelationGetDescr(catalog), &isnull);
2118  if (isnull)
2119  elog(ERROR,
2120  "invalid null namespace in object %u/%u/%d",
2121  addr.classId, addr.objectId, addr.objectSubId);
2122  /* XXX not quite get_namespace_name_or_temp */
2123  if (isAnyTempNamespace(schema_oid))
2124  schema = pstrdup("pg_temp");
2125  else
2126  schema = get_namespace_name(schema_oid);
2127 
2128  table_close(catalog, AccessShareLock);
2129  }
2130  }
2131 
2132  /* classid */
2133  values[i++] = ObjectIdGetDatum(addr.classId);
2134  /* objid */
2135  values[i++] = ObjectIdGetDatum(addr.objectId);
2136  /* objsubid */
2137  values[i++] = Int32GetDatum(addr.objectSubId);
2138  /* command tag */
2139  values[i++] = CStringGetTextDatum(CreateCommandTag(cmd->parsetree));
2140  /* object_type */
2141  values[i++] = CStringGetTextDatum(type);
2142  /* schema */
2143  if (schema == NULL)
2144  nulls[i++] = true;
2145  else
2146  values[i++] = CStringGetTextDatum(schema);
2147  /* identity */
2148  values[i++] = CStringGetTextDatum(identity);
2149  /* in_extension */
2150  values[i++] = BoolGetDatum(cmd->in_extension);
2151  /* command */
2152  values[i++] = PointerGetDatum(cmd);
2153  }
2154  break;
2155 
2157  /* classid */
2158  nulls[i++] = true;
2159  /* objid */
2160  nulls[i++] = true;
2161  /* objsubid */
2162  nulls[i++] = true;
2163  /* command tag */
2164  values[i++] = CStringGetTextDatum(CreateCommandTag(cmd->parsetree));
2165  /* object_type */
2167  cmd->d.defprivs.objtype));
2168  /* schema */
2169  nulls[i++] = true;
2170  /* identity */
2171  nulls[i++] = true;
2172  /* in_extension */
2173  values[i++] = BoolGetDatum(cmd->in_extension);
2174  /* command */
2175  values[i++] = PointerGetDatum(cmd);
2176  break;
2177 
2178  case SCT_Grant:
2179  /* classid */
2180  nulls[i++] = true;
2181  /* objid */
2182  nulls[i++] = true;
2183  /* objsubid */
2184  nulls[i++] = true;
2185  /* command tag */
2186  values[i++] = CStringGetTextDatum(cmd->d.grant.istmt->is_grant ?
2187  "GRANT" : "REVOKE");
2188  /* object_type */
2190  cmd->d.grant.istmt->objtype));
2191  /* schema */
2192  nulls[i++] = true;
2193  /* identity */
2194  nulls[i++] = true;
2195  /* in_extension */
2196  values[i++] = BoolGetDatum(cmd->in_extension);
2197  /* command */
2198  values[i++] = PointerGetDatum(cmd);
2199  break;
2200  }
2201 
2202  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2203  }
2204 
2205  /* clean up and return the tuplestore */
2206  tuplestore_donestoring(tupstore);
2207 
2208  PG_RETURN_VOID();
2209 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
union CollectedCommand::@111 d
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
char * getObjectTypeDescription(const ObjectAddress *object)
AttrNumber get_object_attnum_oid(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:448
#define PointerGetDatum(X)
Definition: postgres.h:556
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:962
struct CollectedCommand::@111::@114 grant
AttrNumber get_object_attnum_namespace(Oid class_id)
struct CollectedCommand::@111::@112 simple
CollectedCommandType type
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
struct CollectedCommand::@111::@116 createopc
struct CollectedCommand::@111::@118 defprivs
struct CollectedCommand::@111::@117 atscfg
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * getObjectIdentity(const ObjectAddress *object)
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define ereport(elevel, rest)
Definition: elog.h:141
const char * CreateCommandTag(Node *parsetree)
Definition: utility.c:2093
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
HeapTuple get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
static const char * stringify_adefprivs_objtype(ObjectType objtype)
#define BoolGetDatum(X)
Definition: postgres.h:402
int allowedModes
Definition: execnodes.h:302
#define PG_RETURN_VOID()
Definition: fmgr.h:339
SetFunctionReturnMode returnMode
Definition: execnodes.h:304
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:190
struct CollectedCommand::@111::@115 opfam
struct CollectedCommand::@111::@113 alterTable
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:230
#define InvalidAttrNumber
Definition: attnum.h:23
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
Tuplestorestate * setResult
Definition: execnodes.h:307
static Datum values[MAXATTR]
Definition: bootstrap.c:167
ExprContext * econtext
Definition: execnodes.h:300
#define Int32GetDatum(X)
Definition: postgres.h:479
TupleDesc setDesc
Definition: execnodes.h:308
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:83
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:3187
bool is_objectclass_supported(Oid class_id)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
static const char * stringify_grant_objtype(ObjectType objtype)
int16 AttrNumber
Definition: attnum.h:21

◆ pg_event_trigger_dropped_objects()

Datum pg_event_trigger_dropped_objects ( PG_FUNCTION_ARGS  )

Definition at line 1442 of file event_trigger.c.

References SQLDropObject::addrargs, SQLDropObject::address, SQLDropObject::addrnames, ReturnSetInfo::allowedModes, BoolGetDatum, ObjectAddress::classId, construct_empty_array(), CStringGetTextDatum, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), i, EventTriggerQueryState::in_sql_drop, Int32GetDatum, IsA, SQLDropObject::istemp, MemoryContextSwitchTo(), MemSet, next, SQLDropObject::normal, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, SQLDropObject::objecttype, SQLDropObject::objidentity, SQLDropObject::objname, SQLDropObject::original, PointerGetDatum, ReturnSetInfo::returnMode, SQLDropObject::schemaname, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, slist_container, slist_foreach, EventTriggerQueryState::SQLDropList, strlist_to_textarray(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

1443 {
1444  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1445  TupleDesc tupdesc;
1446  Tuplestorestate *tupstore;
1447  MemoryContext per_query_ctx;
1448  MemoryContext oldcontext;
1449  slist_iter iter;
1450 
1451  /*
1452  * Protect this function from being called out of context
1453  */
1454  if (!currentEventTriggerState ||
1456  ereport(ERROR,
1457  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1458  errmsg("%s can only be called in a sql_drop event trigger function",
1459  "pg_event_trigger_dropped_objects()")));
1460 
1461  /* check to see if caller supports us returning a tuplestore */
1462  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1463  ereport(ERROR,
1464  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1465  errmsg("set-valued function called in context that cannot accept a set")));
1466  if (!(rsinfo->allowedModes & SFRM_Materialize))
1467  ereport(ERROR,
1468  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1469  errmsg("materialize mode required, but it is not allowed in this context")));
1470 
1471  /* Build a tuple descriptor for our result type */
1472  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1473  elog(ERROR, "return type must be a row type");
1474 
1475  /* Build tuplestore to hold the result rows */
1476  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1477  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1478 
1479  tupstore = tuplestore_begin_heap(true, false, work_mem);
1480  rsinfo->returnMode = SFRM_Materialize;
1481  rsinfo->setResult = tupstore;
1482  rsinfo->setDesc = tupdesc;
1483 
1484  MemoryContextSwitchTo(oldcontext);
1485 
1487  {
1488  SQLDropObject *obj;
1489  int i = 0;
1490  Datum values[12];
1491  bool nulls[12];
1492 
1493  obj = slist_container(SQLDropObject, next, iter.cur);
1494 
1495  MemSet(values, 0, sizeof(values));
1496  MemSet(nulls, 0, sizeof(nulls));
1497 
1498  /* classid */
1499  values[i++] = ObjectIdGetDatum(obj->address.classId);
1500 
1501  /* objid */
1502  values[i++] = ObjectIdGetDatum(obj->address.objectId);
1503 
1504  /* objsubid */
1505  values[i++] = Int32GetDatum(obj->address.objectSubId);
1506 
1507  /* original */
1508  values[i++] = BoolGetDatum(obj->original);
1509 
1510  /* normal */
1511  values[i++] = BoolGetDatum(obj->normal);
1512 
1513  /* is_temporary */
1514  values[i++] = BoolGetDatum(obj->istemp);
1515 
1516  /* object_type */
1517  values[i++] = CStringGetTextDatum(obj->objecttype);
1518 
1519  /* schema_name */
1520  if (obj->schemaname)
1521  values[i++] = CStringGetTextDatum(obj->schemaname);
1522  else
1523  nulls[i++] = true;
1524 
1525  /* object_name */
1526  if (obj->objname)
1527  values[i++] = CStringGetTextDatum(obj->objname);
1528  else
1529  nulls[i++] = true;
1530 
1531  /* object_identity */
1532  if (obj->objidentity)
1533  values[i++] = CStringGetTextDatum(obj->objidentity);
1534  else
1535  nulls[i++] = true;
1536 
1537  /* address_names and address_args */
1538  if (obj->addrnames)
1539  {
1540  values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrnames));
1541 
1542  if (obj->addrargs)
1543  values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrargs));
1544  else
1545  values[i++] = PointerGetDatum(construct_empty_array(TEXTOID));
1546  }
1547  else
1548  {
1549  nulls[i++] = true;
1550  nulls[i++] = true;
1551  }
1552 
1553  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1554  }
1555 
1556  /* clean up and return the tuplestore */
1557  tuplestore_donestoring(tupstore);
1558 
1559  return (Datum) 0;
1560 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
static int32 next
Definition: blutils.c:213
ObjectAddress address
#define PointerGetDatum(X)
Definition: postgres.h:556
const char * objname
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:962
const char * objidentity
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3410
const char * objecttype
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
const char * schemaname
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
#define BoolGetDatum(X)
Definition: postgres.h:402
int allowedModes
Definition: execnodes.h:302
SetFunctionReturnMode returnMode
Definition: execnodes.h:304
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:230
Tuplestorestate * setResult
Definition: execnodes.h:307
static Datum values[MAXATTR]
Definition: bootstrap.c:167
ExprContext * econtext
Definition: execnodes.h:300
#define slist_foreach(iter, lhead)
Definition: ilist.h:700
#define Int32GetDatum(X)
Definition: postgres.h:479
TupleDesc setDesc
Definition: execnodes.h:308
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:83
ArrayType * strlist_to_textarray(List *list)

◆ pg_event_trigger_table_rewrite_oid()

Datum pg_event_trigger_table_rewrite_oid ( PG_FUNCTION_ARGS  )

Definition at line 1569 of file event_trigger.c.

References ereport, errcode(), errmsg(), ERROR, InvalidOid, PG_RETURN_OID, and EventTriggerQueryState::table_rewrite_oid.

1570 {
1571  /*
1572  * Protect this function from being called out of context
1573  */
1574  if (!currentEventTriggerState ||
1576  ereport(ERROR,
1577  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1578  errmsg("%s can only be called in a table_rewrite event trigger function",
1579  "pg_event_trigger_table_rewrite_oid()")));
1580 
1582 }
int errcode(int sqlerrcode)
Definition: elog.c:608
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define PG_RETURN_OID(x)
Definition: fmgr.h:350

◆ pg_event_trigger_table_rewrite_reason()

Datum pg_event_trigger_table_rewrite_reason ( PG_FUNCTION_ARGS  )

Definition at line 1590 of file event_trigger.c.

References ereport, errcode(), errmsg(), ERROR, PG_RETURN_INT32, and EventTriggerQueryState::table_rewrite_reason.

1591 {
1592  /*
1593  * Protect this function from being called out of context
1594  */
1595  if (!currentEventTriggerState ||
1597  ereport(ERROR,
1598  (errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED),
1599  errmsg("%s can only be called in a table_rewrite event trigger function",
1600  "pg_event_trigger_table_rewrite_reason()")));
1601 
1603 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:608
static EventTriggerQueryState * currentEventTriggerState
Definition: event_trigger.c:73
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ RemoveEventTriggerById()

void RemoveEventTriggerById ( Oid  trigOid)

Definition at line 481 of file event_trigger.c.

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

Referenced by doDeletion().

482 {
483  Relation tgrel;
484  HeapTuple tup;
485 
486  tgrel = table_open(EventTriggerRelationId, RowExclusiveLock);
487 
489  if (!HeapTupleIsValid(tup))
490  elog(ERROR, "cache lookup failed for event trigger %u", trigOid);
491 
492  CatalogTupleDelete(tgrel, &tup->t_self);
493 
494  ReleaseSysCache(tup);
495 
497 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#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:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:228
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ stringify_adefprivs_objtype()

static const char * stringify_adefprivs_objtype ( ObjectType  objtype)
static

Definition at line 2298 of file event_trigger.c.

References elog, ERROR, 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 pg_event_trigger_ddl_commands().

2299 {
2300  switch (objtype)
2301  {
2302  case OBJECT_COLUMN:
2303  return "COLUMNS";
2304  case OBJECT_TABLE:
2305  return "TABLES";
2306  case OBJECT_SEQUENCE:
2307  return "SEQUENCES";
2308  case OBJECT_DATABASE:
2309  return "DATABASES";
2310  case OBJECT_DOMAIN:
2311  return "DOMAINS";
2312  case OBJECT_FDW:
2313  return "FOREIGN DATA WRAPPERS";
2314  case OBJECT_FOREIGN_SERVER:
2315  return "FOREIGN SERVERS";
2316  case OBJECT_FUNCTION:
2317  return "FUNCTIONS";
2318  case OBJECT_LANGUAGE:
2319  return "LANGUAGES";
2320  case OBJECT_LARGEOBJECT:
2321  return "LARGE OBJECTS";
2322  case OBJECT_SCHEMA:
2323  return "SCHEMAS";
2324  case OBJECT_PROCEDURE:
2325  return "PROCEDURES";
2326  case OBJECT_ROUTINE:
2327  return "ROUTINES";
2328  case OBJECT_TABLESPACE:
2329  return "TABLESPACES";
2330  case OBJECT_TYPE:
2331  return "TYPES";
2332  /* these currently aren't used */
2333  case OBJECT_ACCESS_METHOD:
2334  case OBJECT_AGGREGATE:
2335  case OBJECT_AMOP:
2336  case OBJECT_AMPROC:
2337  case OBJECT_ATTRIBUTE:
2338  case OBJECT_CAST:
2339  case OBJECT_COLLATION:
2340  case OBJECT_CONVERSION:
2341  case OBJECT_DEFAULT:
2342  case OBJECT_DEFACL:
2343  case OBJECT_DOMCONSTRAINT:
2344  case OBJECT_EVENT_TRIGGER:
2345  case OBJECT_EXTENSION:
2346  case OBJECT_FOREIGN_TABLE:
2347  case OBJECT_INDEX:
2348  case OBJECT_MATVIEW:
2349  case OBJECT_OPCLASS:
2350  case OBJECT_OPERATOR:
2351  case OBJECT_OPFAMILY:
2352  case OBJECT_POLICY:
2353  case OBJECT_PUBLICATION:
2355  case OBJECT_ROLE:
2356  case OBJECT_RULE:
2357  case OBJECT_STATISTIC_EXT:
2358  case OBJECT_SUBSCRIPTION:
2359  case OBJECT_TABCONSTRAINT:
2360  case OBJECT_TRANSFORM:
2361  case OBJECT_TRIGGER:
2363  case OBJECT_TSDICTIONARY:
2364  case OBJECT_TSPARSER:
2365  case OBJECT_TSTEMPLATE:
2366  case OBJECT_USER_MAPPING:
2367  case OBJECT_VIEW:
2368  elog(ERROR, "unsupported object type: %d", (int) objtype);
2369  }
2370 
2371  return "???"; /* keep compiler quiet */
2372 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228

◆ stringify_grant_objtype()

static const char * stringify_grant_objtype ( ObjectType  objtype)
static

Definition at line 2216 of file event_trigger.c.

References elog, ERROR, 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 pg_event_trigger_ddl_commands().

2217 {
2218  switch (objtype)
2219  {
2220  case OBJECT_COLUMN:
2221  return "COLUMN";
2222  case OBJECT_TABLE:
2223  return "TABLE";
2224  case OBJECT_SEQUENCE:
2225  return "SEQUENCE";
2226  case OBJECT_DATABASE:
2227  return "DATABASE";
2228  case OBJECT_DOMAIN:
2229  return "DOMAIN";
2230  case OBJECT_FDW:
2231  return "FOREIGN DATA WRAPPER";
2232  case OBJECT_FOREIGN_SERVER:
2233  return "FOREIGN SERVER";
2234  case OBJECT_FUNCTION:
2235  return "FUNCTION";
2236  case OBJECT_LANGUAGE:
2237  return "LANGUAGE";
2238  case OBJECT_LARGEOBJECT:
2239  return "LARGE OBJECT";
2240  case OBJECT_SCHEMA:
2241  return "SCHEMA";
2242  case OBJECT_PROCEDURE:
2243  return "PROCEDURE";
2244  case OBJECT_ROUTINE:
2245  return "ROUTINE";
2246  case OBJECT_TABLESPACE:
2247  return "TABLESPACE";
2248  case OBJECT_TYPE:
2249  return "TYPE";
2250  /* these currently aren't used */
2251  case OBJECT_ACCESS_METHOD:
2252  case OBJECT_AGGREGATE:
2253  case OBJECT_AMOP:
2254  case OBJECT_AMPROC:
2255  case OBJECT_ATTRIBUTE:
2256  case OBJECT_CAST:
2257  case OBJECT_COLLATION:
2258  case OBJECT_CONVERSION:
2259  case OBJECT_DEFAULT:
2260  case OBJECT_DEFACL:
2261  case OBJECT_DOMCONSTRAINT:
2262  case OBJECT_EVENT_TRIGGER:
2263  case OBJECT_EXTENSION:
2264  case OBJECT_FOREIGN_TABLE:
2265  case OBJECT_INDEX:
2266  case OBJECT_MATVIEW:
2267  case OBJECT_OPCLASS:
2268  case OBJECT_OPERATOR:
2269  case OBJECT_OPFAMILY:
2270  case OBJECT_POLICY:
2271  case OBJECT_PUBLICATION:
2273  case OBJECT_ROLE:
2274  case OBJECT_RULE:
2275  case OBJECT_STATISTIC_EXT:
2276  case OBJECT_SUBSCRIPTION:
2277  case OBJECT_TABCONSTRAINT:
2278  case OBJECT_TRANSFORM:
2279  case OBJECT_TRIGGER:
2281  case OBJECT_TSDICTIONARY:
2282  case OBJECT_TSPARSER:
2283  case OBJECT_TSTEMPLATE:
2284  case OBJECT_USER_MAPPING:
2285  case OBJECT_VIEW:
2286  elog(ERROR, "unsupported object type: %d", (int) objtype);
2287  }
2288 
2289  return "???"; /* keep compiler quiet */
2290 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228

◆ trackDroppedObjectsNeeded()

bool trackDroppedObjectsNeeded ( void  )

Definition at line 1292 of file event_trigger.c.

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

Referenced by deleteObjectsInList(), and EventTriggerBeginCompleteQuery().

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

◆ validate_ddl_tags()

static void validate_ddl_tags ( const char *  filtervar,
List taglist 
)
static

Definition at line 256 of file event_trigger.c.

References check_ddl_tag(), ereport, errcode(), errmsg(), ERROR, EVENT_TRIGGER_COMMAND_TAG_NOT_RECOGNIZED, EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED, lfirst, and strVal.

Referenced by CreateEventTrigger().

257 {
258  ListCell *lc;
259 
260  foreach(lc, taglist)
261  {
262  const char *tag = strVal(lfirst(lc));
264 
265  result = check_ddl_tag(tag);
267  ereport(ERROR,
268  (errcode(ERRCODE_SYNTAX_ERROR),
269  errmsg("filter value \"%s\" not recognized for filter variable \"%s\"",
270  tag, filtervar)));
272  ereport(ERROR,
273  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
274  /* translator: %s represents an SQL statement name */
275  errmsg("event triggers are not supported for %s",
276  tag)));
277  }
278 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
static event_trigger_command_tag_check_result check_ddl_tag(const char *tag)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
event_trigger_command_tag_check_result
Definition: event_trigger.c:81
#define lfirst(lc)
Definition: pg_list.h:190
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ validate_table_rewrite_tags()

static void validate_table_rewrite_tags ( const char *  filtervar,
List taglist 
)
static

Definition at line 331 of file event_trigger.c.

References check_table_rewrite_ddl_tag(), ereport, errcode(), errmsg(), ERROR, EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED, lfirst, and strVal.

Referenced by CreateEventTrigger().

332 {
333  ListCell *lc;
334 
335  foreach(lc, taglist)
336  {
337  const char *tag = strVal(lfirst(lc));
339 
340  result = check_table_rewrite_ddl_tag(tag);
342  ereport(ERROR,
343  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
344  /* translator: %s represents an SQL statement name */
345  errmsg("event triggers are not supported for %s",
346  tag)));
347  }
348 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag(const char *tag)
#define ereport(elevel, rest)
Definition: elog.h:141
event_trigger_command_tag_check_result
Definition: event_trigger.c:81
#define lfirst(lc)
Definition: pg_list.h:190
int errmsg(const char *fmt,...)
Definition: elog.c:822

Variable Documentation

◆ currentEventTriggerState

EventTriggerQueryState* currentEventTriggerState = NULL
static

Definition at line 73 of file event_trigger.c.

Referenced by EventTriggerBeginCompleteQuery().

◆ event_trigger_support

const event_trigger_support_data event_trigger_support[]
static

Definition at line 89 of file event_trigger.c.