| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* event_trigger.c |
| 4 |
* PostgreSQL EVENT TRIGGER support code. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/commands/event_trigger.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
#include "postgres.h" |
| 15 |
|
| 16 |
#include "access/heapam.h" |
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "access/table.h" |
| 19 |
#include "access/xact.h" |
| 20 |
#include "catalog/catalog.h" |
| 21 |
#include "catalog/dependency.h" |
| 22 |
#include "catalog/indexing.h" |
| 23 |
#include "catalog/objectaccess.h" |
| 24 |
#include "catalog/pg_attrdef.h" |
| 25 |
#include "catalog/pg_authid.h" |
| 26 |
#include "catalog/pg_auth_members.h" |
| 27 |
#include "catalog/pg_database.h" |
| 28 |
#include "catalog/pg_event_trigger.h" |
| 29 |
#include "catalog/pg_namespace.h" |
| 30 |
#include "catalog/pg_opclass.h" |
| 31 |
#include "catalog/pg_opfamily.h" |
| 32 |
#include "catalog/pg_parameter_acl.h" |
| 33 |
#include "catalog/pg_policy.h" |
| 34 |
#include "catalog/pg_proc.h" |
| 35 |
#include "catalog/pg_tablespace.h" |
| 36 |
#include "catalog/pg_trigger.h" |
| 37 |
#include "catalog/pg_ts_config.h" |
| 38 |
#include "catalog/pg_type.h" |
| 39 |
#include "commands/event_trigger.h" |
| 40 |
#include "commands/extension.h" |
| 41 |
#include "commands/trigger.h" |
| 42 |
#include "funcapi.h" |
| 43 |
#include "lib/ilist.h" |
| 44 |
#include "miscadmin.h" |
| 45 |
#include "parser/parse_func.h" |
| 46 |
#include "pgstat.h" |
| 47 |
#include "storage/lmgr.h" |
| 48 |
#include "tcop/deparse_utility.h" |
| 49 |
#include "tcop/utility.h" |
| 50 |
#include "utils/acl.h" |
| 51 |
#include "utils/builtins.h" |
| 52 |
#include "utils/evtcache.h" |
| 53 |
#include "utils/fmgroids.h" |
| 54 |
#include "utils/fmgrprotos.h" |
| 55 |
#include "utils/lsyscache.h" |
| 56 |
#include "utils/memutils.h" |
| 57 |
#include "utils/rel.h" |
| 58 |
#include "utils/snapmgr.h" |
| 59 |
#include "utils/syscache.h" |
| 60 |
|
| 61 |
typedef struct EventTriggerQueryState |
| 62 |
{ |
| 63 |
/* memory context for this state's objects */ |
| 64 |
MemoryContext cxt; |
| 65 |
|
| 66 |
/* sql_drop */ |
| 67 |
slist_head SQLDropList; |
| 68 |
bool in_sql_drop; |
| 69 |
|
| 70 |
/* table_rewrite */ |
| 71 |
Oid table_rewrite_oid; /* InvalidOid, or set for table_rewrite |
| 72 |
* event */ |
| 73 |
int table_rewrite_reason; /* AT_REWRITE reason */ |
| 74 |
|
| 75 |
/* Support for command collection */ |
| 76 |
bool commandCollectionInhibited; |
| 77 |
CollectedCommand *currentCommand; |
| 78 |
List *commandList; /* list of CollectedCommand; see |
| 79 |
* deparse_utility.h */ |
| 80 |
struct EventTriggerQueryState *previous; |
| 81 |
} EventTriggerQueryState; |
| 82 |
|
| 83 |
static EventTriggerQueryState *currentEventTriggerState = NULL; |
| 84 |
|
| 85 |
/* GUC parameter */ |
| 86 |
bool event_triggers = true; |
| 87 |
|
| 88 |
/* Support for dropped objects */ |
| 89 |
typedef struct SQLDropObject |
| 90 |
{ |
| 91 |
ObjectAddress address; |
| 92 |
const char *schemaname; |
| 93 |
const char *objname; |
| 94 |
const char *objidentity; |
| 95 |
const char *objecttype; |
| 96 |
List *addrnames; |
| 97 |
List *addrargs; |
| 98 |
bool original; |
| 99 |
bool normal; |
| 100 |
bool istemp; |
| 101 |
slist_node next; |
| 102 |
} SQLDropObject; |
| 103 |
|
| 104 |
static void AlterEventTriggerOwner_internal(Relation rel, |
| 105 |
HeapTuple tup, |
| 106 |
Oid newOwnerId); |
| 107 |
static void error_duplicate_filter_variable(const char *defname); |
| 108 |
static Datum filter_list_to_array(List *filterlist); |
| 109 |
static Oid insert_event_trigger_tuple(const char *trigname, const char *eventname, |
| 110 |
Oid evtOwner, Oid funcoid, List *taglist); |
| 111 |
static void validate_ddl_tags(const char *filtervar, List *taglist); |
| 112 |
static void validate_table_rewrite_tags(const char *filtervar, List *taglist); |
| 113 |
static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata); |
| 114 |
static bool obtain_object_name_namespace(const ObjectAddress *object, |
| 115 |
SQLDropObject *obj); |
| 116 |
static const char *stringify_grant_objtype(ObjectType objtype); |
| 117 |
static const char *stringify_adefprivs_objtype(ObjectType objtype); |
| 118 |
static void SetDatabaseHasLoginEventTriggers(void); |
| 119 |
|
| 120 |
/* |
| 121 |
* Create an event trigger. |
| 122 |
*/ |
| 123 |
Oid |
| 124 |
CreateEventTrigger(CreateEventTrigStmt *stmt) |
| 125 |
{ |
| 126 |
HeapTuple tuple; |
| 127 |
Oid funcoid; |
| 128 |
Oid funcrettype; |
| 129 |
Oid evtowner = GetUserId(); |
| 130 |
ListCell *lc; |
| 131 |
List *tags = NULL; |
| 132 |
|
| 133 |
/* |
| 134 |
* It would be nice to allow database owners or even regular users to do |
| 135 |
* this, but there are obvious privilege escalation risks which would have |
| 136 |
* to somehow be plugged first. |
| 137 |
*/ |
| 138 |
if (!superuser()) |
| 139 |
ereport(ERROR, |
| 140 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 141 |
errmsg("permission denied to create event trigger \"%s\"", |
| 142 |
stmt->trigname), |
| 143 |
errhint("Must be superuser to create an event trigger."))); |
| 144 |
|
| 145 |
/* Validate event name. */ |
| 146 |
if (strcmp(stmt->eventname, "ddl_command_start") != 0 && |
| 147 |
strcmp(stmt->eventname, "ddl_command_end") != 0 && |
| 148 |
strcmp(stmt->eventname, "sql_drop") != 0 && |
| 149 |
strcmp(stmt->eventname, "login") != 0 && |
| 150 |
strcmp(stmt->eventname, "table_rewrite") != 0) |
| 151 |
ereport(ERROR, |
| 152 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 153 |
errmsg("unrecognized event name \"%s\"", |
| 154 |
stmt->eventname))); |
| 155 |
|
| 156 |
/* Validate filter conditions. */ |
| 157 |
foreach(lc, stmt->whenclause) |
| 158 |
{ |
| 159 |
DefElem *def = (DefElem *) lfirst(lc); |
| 160 |
|
| 161 |
if (strcmp(def->defname, "tag") == 0) |
| 162 |
{ |
| 163 |
if (tags != NULL) |
| 164 |
error_duplicate_filter_variable(def->defname); |
| 165 |
tags = (List *) def->arg; |
| 166 |
} |
| 167 |
else |
| 168 |
ereport(ERROR, |
| 169 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 170 |
errmsg("unrecognized filter variable \"%s\"", def->defname))); |
| 171 |
} |
| 172 |
|
| 173 |
/* Validate tag list, if any. */ |
| 174 |
if ((strcmp(stmt->eventname, "ddl_command_start") == 0 || |
| 175 |
strcmp(stmt->eventname, "ddl_command_end") == 0 || |
| 176 |
strcmp(stmt->eventname, "sql_drop") == 0) |
| 177 |
&& tags != NULL) |
| 178 |
validate_ddl_tags("tag", tags); |
| 179 |
else if (strcmp(stmt->eventname, "table_rewrite") == 0 |
| 180 |
&& tags != NULL) |
| 181 |
validate_table_rewrite_tags("tag", tags); |
| 182 |
else if (strcmp(stmt->eventname, "login") == 0 && tags != NULL) |
| 183 |
ereport(ERROR, |
| 184 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 185 |
errmsg("tag filtering is not supported for login event triggers"))); |
| 186 |
|
| 187 |
/* |
| 188 |
* Give user a nice error message if an event trigger of the same name |
| 189 |
* already exists. |
| 190 |
*/ |
| 191 |
tuple = SearchSysCache1(EVENTTRIGGERNAME, CStringGetDatum(stmt->trigname)); |
| 192 |
if (HeapTupleIsValid(tuple)) |
| 193 |
ereport(ERROR, |
| 194 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 195 |
errmsg("event trigger \"%s\" already exists", |
| 196 |
stmt->trigname))); |
| 197 |
|
| 198 |
/* Find and validate the trigger function. */ |
| 199 |
funcoid = LookupFuncName(stmt->funcname, 0, NULL, false); |
| 200 |
funcrettype = get_func_rettype(funcoid); |
| 201 |
if (funcrettype != EVENT_TRIGGEROID) |
| 202 |
ereport(ERROR, |
| 203 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 204 |
errmsg("function %s must return type %s", |
| 205 |
NameListToString(stmt->funcname), "event_trigger"))); |
| 206 |
|
| 207 |
/* Insert catalog entries. */ |
| 208 |
return insert_event_trigger_tuple(stmt->trigname, stmt->eventname, |
| 209 |
evtowner, funcoid, tags); |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* Validate DDL command tags. |
| 214 |
*/ |
| 215 |
static void |
| 216 |
validate_ddl_tags(const char *filtervar, List *taglist) |
| 217 |
{ |
| 218 |
ListCell *lc; |
| 219 |
|
| 220 |
foreach(lc, taglist) |
| 221 |
{ |
| 222 |
const char *tagstr = strVal(lfirst(lc)); |
| 223 |
CommandTag commandTag = GetCommandTagEnum(tagstr); |
| 224 |
|
| 225 |
if (commandTag == CMDTAG_UNKNOWN) |
| 226 |
ereport(ERROR, |
| 227 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 228 |
errmsg("filter value \"%s\" not recognized for filter variable \"%s\"", |
| 229 |
tagstr, filtervar))); |
| 230 |
if (!command_tag_event_trigger_ok(commandTag)) |
| 231 |
ereport(ERROR, |
| 232 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 233 |
/* translator: %s represents an SQL statement name */ |
| 234 |
errmsg("event triggers are not supported for %s", |
| 235 |
tagstr))); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Validate DDL command tags for event table_rewrite. |
| 241 |
*/ |
| 242 |
static void |
| 243 |
validate_table_rewrite_tags(const char *filtervar, List *taglist) |
| 244 |
{ |
| 245 |
ListCell *lc; |
| 246 |
|
| 247 |
foreach(lc, taglist) |
| 248 |
{ |
| 249 |
const char *tagstr = strVal(lfirst(lc)); |
| 250 |
CommandTag commandTag = GetCommandTagEnum(tagstr); |
| 251 |
|
| 252 |
if (!command_tag_table_rewrite_ok(commandTag)) |
| 253 |
ereport(ERROR, |
| 254 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 255 |
/* translator: %s represents an SQL statement name */ |
| 256 |
errmsg("event triggers are not supported for %s", |
| 257 |
tagstr))); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* Complain about a duplicate filter variable. |
| 263 |
*/ |
| 264 |
static void |
| 265 |
error_duplicate_filter_variable(const char *defname) |
| 266 |
{ |
| 267 |
ereport(ERROR, |
| 268 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 269 |
errmsg("filter variable \"%s\" specified more than once", |
| 270 |
defname))); |
| 271 |
} |
| 272 |
|
| 273 |
/* |
| 274 |
* Insert the new pg_event_trigger row and record dependencies. |
| 275 |
*/ |
| 276 |
static Oid |
| 277 |
insert_event_trigger_tuple(const char *trigname, const char *eventname, Oid evtOwner, |
| 278 |
Oid funcoid, List *taglist) |
| 279 |
{ |
| 280 |
Relation tgrel; |
| 281 |
Oid trigoid; |
| 282 |
HeapTuple tuple; |
| 283 |
Datum values[Natts_pg_event_trigger]; |
| 284 |
bool nulls[Natts_pg_event_trigger]; |
| 285 |
NameData evtnamedata, |
| 286 |
evteventdata; |
| 287 |
ObjectAddress myself, |
| 288 |
referenced; |
| 289 |
|
| 290 |
/* Open pg_event_trigger. */ |
| 291 |
tgrel = table_open(EventTriggerRelationId, RowExclusiveLock); |
| 292 |
|
| 293 |
/* Build the new pg_trigger tuple. */ |
| 294 |
trigoid = GetNewOidWithIndex(tgrel, EventTriggerOidIndexId, |
| 295 |
Anum_pg_event_trigger_oid); |
| 296 |
values[Anum_pg_event_trigger_oid - 1] = ObjectIdGetDatum(trigoid); |
| 297 |
memset(nulls, false, sizeof(nulls)); |
| 298 |
namestrcpy(&evtnamedata, trigname); |
| 299 |
values[Anum_pg_event_trigger_evtname - 1] = NameGetDatum(&evtnamedata); |
| 300 |
namestrcpy(&evteventdata, eventname); |
| 301 |
values[Anum_pg_event_trigger_evtevent - 1] = NameGetDatum(&evteventdata); |
| 302 |
values[Anum_pg_event_trigger_evtowner - 1] = ObjectIdGetDatum(evtOwner); |
| 303 |
values[Anum_pg_event_trigger_evtfoid - 1] = ObjectIdGetDatum(funcoid); |
| 304 |
values[Anum_pg_event_trigger_evtenabled - 1] = |
| 305 |
CharGetDatum(TRIGGER_FIRES_ON_ORIGIN); |
| 306 |
if (taglist == NIL) |
| 307 |
nulls[Anum_pg_event_trigger_evttags - 1] = true; |
| 308 |
else |
| 309 |
values[Anum_pg_event_trigger_evttags - 1] = |
| 310 |
filter_list_to_array(taglist); |
| 311 |
|
| 312 |
/* Insert heap tuple. */ |
| 313 |
tuple = heap_form_tuple(tgrel->rd_att, values, nulls); |
| 314 |
CatalogTupleInsert(tgrel, tuple); |
| 315 |
heap_freetuple(tuple); |
| 316 |
|
| 317 |
/* |
| 318 |
* Login event triggers have an additional flag in pg_database to enable |
| 319 |
* faster lookups in hot codepaths. Set the flag unless already True. |
| 320 |
*/ |
| 321 |
if (strcmp(eventname, "login") == 0) |
| 322 |
SetDatabaseHasLoginEventTriggers(); |
| 323 |
|
| 324 |
/* Depend on owner. */ |
| 325 |
recordDependencyOnOwner(EventTriggerRelationId, trigoid, evtOwner); |
| 326 |
|
| 327 |
/* Depend on event trigger function. */ |
| 328 |
myself.classId = EventTriggerRelationId; |
| 329 |
myself.objectId = trigoid; |
| 330 |
myself.objectSubId = 0; |
| 331 |
referenced.classId = ProcedureRelationId; |
| 332 |
referenced.objectId = funcoid; |
| 333 |
referenced.objectSubId = 0; |
| 334 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 335 |
|
| 336 |
/* Depend on extension, if any. */ |
| 337 |
recordDependencyOnCurrentExtension(&myself, false); |
| 338 |
|
| 339 |
/* Post creation hook for new event trigger */ |
| 340 |
InvokeObjectPostCreateHook(EventTriggerRelationId, trigoid, 0); |
| 341 |
|
| 342 |
/* Close pg_event_trigger. */ |
| 343 |
table_close(tgrel, RowExclusiveLock); |
| 344 |
|
| 345 |
return trigoid; |
| 346 |
} |
| 347 |
|
| 348 |
/* |
| 349 |
* In the parser, a clause like WHEN tag IN ('cmd1', 'cmd2') is represented |
| 350 |
* by a DefElem whose value is a List of String nodes; in the catalog, we |
| 351 |
* store the list of strings as a text array. This function transforms the |
| 352 |
* former representation into the latter one. |
| 353 |
* |
| 354 |
* For cleanliness, we store command tags in the catalog as text. It's |
| 355 |
* possible (although not currently anticipated) that we might have |
| 356 |
* a case-sensitive filter variable in the future, in which case this would |
| 357 |
* need some further adjustment. |
| 358 |
*/ |
| 359 |
static Datum |
| 360 |
filter_list_to_array(List *filterlist) |
| 361 |
{ |
| 362 |
ListCell *lc; |
| 363 |
Datum *data; |
| 364 |
int i = 0, |
| 365 |
l = list_length(filterlist); |
| 366 |
|
| 367 |
data = (Datum *) palloc(l * sizeof(Datum)); |
| 368 |
|
| 369 |
foreach(lc, filterlist) |
| 370 |
{ |
| 371 |
const char *value = strVal(lfirst(lc)); |
| 372 |
char *result, |
| 373 |
*p; |
| 374 |
|
| 375 |
result = pstrdup(value); |
| 376 |
for (p = result; *p; p++) |
| 377 |
*p = pg_ascii_toupper((unsigned char) *p); |
| 378 |
data[i++] = PointerGetDatum(cstring_to_text(result)); |
| 379 |
pfree(result); |
| 380 |
} |
| 381 |
|
| 382 |
return PointerGetDatum(construct_array_builtin(data, l, TEXTOID)); |
| 383 |
} |
| 384 |
|
| 385 |
/* |
| 386 |
* Set pg_database.dathasloginevt flag for current database indicating that |
| 387 |
* current database has on login event triggers. |
| 388 |
*/ |
| 389 |
void |
| 390 |
SetDatabaseHasLoginEventTriggers(void) |
| 391 |
{ |
| 392 |
/* Set dathasloginevt flag in pg_database */ |
| 393 |
Form_pg_database db; |
| 394 |
Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); |
| 395 |
ItemPointerData otid; |
| 396 |
HeapTuple tuple; |
| 397 |
|
| 398 |
/* |
| 399 |
* Use shared lock to prevent a conflict with EventTriggerOnLogin() trying |
| 400 |
* to reset pg_database.dathasloginevt flag. Note, this lock doesn't |
| 401 |
* effectively blocks database or other objection. It's just custom lock |
| 402 |
* tag used to prevent multiple backends changing |
| 403 |
* pg_database.dathasloginevt flag. |
| 404 |
*/ |
| 405 |
LockSharedObject(DatabaseRelationId, MyDatabaseId, 0, AccessExclusiveLock); |
| 406 |
|
| 407 |
tuple = SearchSysCacheLockedCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); |
| 408 |
if (!HeapTupleIsValid(tuple)) |
| 409 |
elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); |
| 410 |
otid = tuple->t_self; |
| 411 |
db = (Form_pg_database) GETSTRUCT(tuple); |
| 412 |
if (!db->dathasloginevt) |
| 413 |
{ |
| 414 |
db->dathasloginevt = true; |
| 415 |
CatalogTupleUpdate(pg_db, &otid, tuple); |
| 416 |
CommandCounterIncrement(); |
| 417 |
} |
| 418 |
UnlockTuple(pg_db, &otid, InplaceUpdateTupleLock); |
| 419 |
table_close(pg_db, RowExclusiveLock); |
| 420 |
heap_freetuple(tuple); |
| 421 |
} |
| 422 |
|
| 423 |
/* |
| 424 |
* ALTER EVENT TRIGGER foo ENABLE|DISABLE|ENABLE ALWAYS|REPLICA |
| 425 |
*/ |
| 426 |
Oid |
| 427 |
AlterEventTrigger(AlterEventTrigStmt *stmt) |
| 428 |
{ |
| 429 |
Relation tgrel; |
| 430 |
HeapTuple tup; |
| 431 |
Oid trigoid; |
| 432 |
Form_pg_event_trigger evtForm; |
| 433 |
char tgenabled = stmt->tgenabled; |
| 434 |
|
| 435 |
tgrel = table_open(EventTriggerRelationId, RowExclusiveLock); |
| 436 |
|
| 437 |
tup = SearchSysCacheCopy1(EVENTTRIGGERNAME, |
| 438 |
CStringGetDatum(stmt->trigname)); |
| 439 |
if (!HeapTupleIsValid(tup)) |
| 440 |
ereport(ERROR, |
| 441 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 442 |
errmsg("event trigger \"%s\" does not exist", |
| 443 |
stmt->trigname))); |
| 444 |
|
| 445 |
evtForm = (Form_pg_event_trigger) GETSTRUCT(tup); |
| 446 |
trigoid = evtForm->oid; |
| 447 |
|
| 448 |
if (!object_ownercheck(EventTriggerRelationId, trigoid, GetUserId())) |
| 449 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_EVENT_TRIGGER, |
| 450 |
stmt->trigname); |
| 451 |
|
| 452 |
/* tuple is a copy, so we can modify it below */ |
| 453 |
evtForm->evtenabled = tgenabled; |
| 454 |
|
| 455 |
CatalogTupleUpdate(tgrel, &tup->t_self, tup); |
| 456 |
|
| 457 |
/* |
| 458 |
* Login event triggers have an additional flag in pg_database to enable |
| 459 |
* faster lookups in hot codepaths. Set the flag unless already True. |
| 460 |
*/ |
| 461 |
if (namestrcmp(&evtForm->evtevent, "login") == 0 && |
| 462 |
tgenabled != TRIGGER_DISABLED) |
| 463 |
SetDatabaseHasLoginEventTriggers(); |
| 464 |
|
| 465 |
InvokeObjectPostAlterHook(EventTriggerRelationId, |
| 466 |
trigoid, 0); |
| 467 |
|
| 468 |
/* clean up */ |
| 469 |
heap_freetuple(tup); |
| 470 |
table_close(tgrel, RowExclusiveLock); |
| 471 |
|
| 472 |
return trigoid; |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
* Change event trigger's owner -- by name |
| 477 |
*/ |
| 478 |
ObjectAddress |
| 479 |
AlterEventTriggerOwner(const char *name, Oid newOwnerId) |
| 480 |
{ |
| 481 |
Oid evtOid; |
| 482 |
HeapTuple tup; |
| 483 |
Form_pg_event_trigger evtForm; |
| 484 |
Relation rel; |
| 485 |
ObjectAddress address; |
| 486 |
|
| 487 |
rel = table_open(EventTriggerRelationId, RowExclusiveLock); |
| 488 |
|
| 489 |
tup = SearchSysCacheCopy1(EVENTTRIGGERNAME, CStringGetDatum(name)); |
| 490 |
|
| 491 |
if (!HeapTupleIsValid(tup)) |
| 492 |
ereport(ERROR, |
| 493 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 494 |
errmsg("event trigger \"%s\" does not exist", name))); |
| 495 |
|
| 496 |
evtForm = (Form_pg_event_trigger) GETSTRUCT(tup); |
| 497 |
evtOid = evtForm->oid; |
| 498 |
|
| 499 |
AlterEventTriggerOwner_internal(rel, tup, newOwnerId); |
| 500 |
|
| 501 |
ObjectAddressSet(address, EventTriggerRelationId, evtOid); |
| 502 |
|
| 503 |
heap_freetuple(tup); |
| 504 |
|
| 505 |
table_close(rel, RowExclusiveLock); |
| 506 |
|
| 507 |
return address; |
| 508 |
} |
| 509 |
|
| 510 |
/* |
| 511 |
* Change event trigger owner, by OID |
| 512 |
*/ |
| 513 |
void |
| 514 |
AlterEventTriggerOwner_oid(Oid trigOid, Oid newOwnerId) |
| 515 |
{ |
| 516 |
HeapTuple tup; |
| 517 |
Relation rel; |
| 518 |
|
| 519 |
rel = table_open(EventTriggerRelationId, RowExclusiveLock); |
| 520 |
|
| 521 |
tup = SearchSysCacheCopy1(EVENTTRIGGEROID, ObjectIdGetDatum(trigOid)); |
| 522 |
|
| 523 |
if (!HeapTupleIsValid(tup)) |
| 524 |
ereport(ERROR, |
| 525 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 526 |
errmsg("event trigger with OID %u does not exist", trigOid))); |
| 527 |
|
| 528 |
AlterEventTriggerOwner_internal(rel, tup, newOwnerId); |
| 529 |
|
| 530 |
heap_freetuple(tup); |
| 531 |
|
| 532 |
table_close(rel, RowExclusiveLock); |
| 533 |
} |
| 534 |
|
| 535 |
/* |
| 536 |
* Internal workhorse for changing an event trigger's owner |
| 537 |
*/ |
| 538 |
static void |
| 539 |
AlterEventTriggerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId) |
| 540 |
{ |
| 541 |
Form_pg_event_trigger form; |
| 542 |
|
| 543 |
form = (Form_pg_event_trigger) GETSTRUCT(tup); |
| 544 |
|
| 545 |
if (form->evtowner == newOwnerId) |
| 546 |
return; |
| 547 |
|
| 548 |
if (!object_ownercheck(EventTriggerRelationId, form->oid, GetUserId())) |
| 549 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_EVENT_TRIGGER, |
| 550 |
NameStr(form->evtname)); |
| 551 |
|
| 552 |
/* New owner must be a superuser */ |
| 553 |
if (!superuser_arg(newOwnerId)) |
| 554 |
ereport(ERROR, |
| 555 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 556 |
errmsg("permission denied to change owner of event trigger \"%s\"", |
| 557 |
NameStr(form->evtname)), |
| 558 |
errhint("The owner of an event trigger must be a superuser."))); |
| 559 |
|
| 560 |
form->evtowner = newOwnerId; |
| 561 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 562 |
|
| 563 |
/* Update owner dependency reference */ |
| 564 |
changeDependencyOnOwner(EventTriggerRelationId, |
| 565 |
form->oid, |
| 566 |
newOwnerId); |
| 567 |
|
| 568 |
InvokeObjectPostAlterHook(EventTriggerRelationId, |
| 569 |
form->oid, 0); |
| 570 |
} |
| 571 |
|
| 572 |
/* |
| 573 |
* get_event_trigger_oid - Look up an event trigger by name to find its OID. |
| 574 |
* |
| 575 |
* If missing_ok is false, throw an error if trigger not found. If |
| 576 |
* true, just return InvalidOid. |
| 577 |
*/ |
| 578 |
Oid |
| 579 |
get_event_trigger_oid(const char *trigname, bool missing_ok) |
| 580 |
{ |
| 581 |
Oid oid; |
| 582 |
|
| 583 |
oid = GetSysCacheOid1(EVENTTRIGGERNAME, Anum_pg_event_trigger_oid, |
| 584 |
CStringGetDatum(trigname)); |
| 585 |
if (!OidIsValid(oid) && !missing_ok) |
| 586 |
ereport(ERROR, |
| 587 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 588 |
errmsg("event trigger \"%s\" does not exist", trigname))); |
| 589 |
return oid; |
| 590 |
} |
| 591 |
|
| 592 |
/* |
| 593 |
* Return true when we want to fire given Event Trigger and false otherwise, |
| 594 |
* filtering on the session replication role and the event trigger registered |
| 595 |
* tags matching. |
| 596 |
*/ |
| 597 |
static bool |
| 598 |
filter_event_trigger(CommandTag tag, EventTriggerCacheItem *item) |
| 599 |
{ |
| 600 |
/* |
| 601 |
* Filter by session replication role, knowing that we never see disabled |
| 602 |
* items down here. |
| 603 |
*/ |
| 604 |
if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA) |
| 605 |
{ |
| 606 |
if (item->enabled == TRIGGER_FIRES_ON_ORIGIN) |
| 607 |
return false; |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
if (item->enabled == TRIGGER_FIRES_ON_REPLICA) |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
/* Filter by tags, if any were specified. */ |
| 616 |
if (!bms_is_empty(item->tagset) && !bms_is_member(tag, item->tagset)) |
| 617 |
return false; |
| 618 |
|
| 619 |
/* if we reach that point, we're not filtering out this item */ |
| 620 |
return true; |
| 621 |
} |
| 622 |
|
| 623 |
static CommandTag |
| 624 |
EventTriggerGetTag(Node *parsetree, EventTriggerEvent event) |
| 625 |
{ |
| 626 |
if (event == EVT_Login) |
| 627 |
return CMDTAG_LOGIN; |
| 628 |
else |
| 629 |
return CreateCommandTag(parsetree); |
| 630 |
} |
| 631 |
|
| 632 |
/* |
| 633 |
* Setup for running triggers for the given event. Return value is an OID list |
| 634 |
* of functions to run; if there are any, trigdata is filled with an |
| 635 |
* appropriate EventTriggerData for them to receive. |
| 636 |
*/ |
| 637 |
static List * |
| 638 |
EventTriggerCommonSetup(Node *parsetree, |
| 639 |
EventTriggerEvent event, const char *eventstr, |
| 640 |
EventTriggerData *trigdata, bool unfiltered) |
| 641 |
{ |
| 642 |
CommandTag tag; |
| 643 |
List *cachelist; |
| 644 |
ListCell *lc; |
| 645 |
List *runlist = NIL; |
| 646 |
|
| 647 |
/* |
| 648 |
* We want the list of command tags for which this procedure is actually |
| 649 |
* invoked to match up exactly with the list that CREATE EVENT TRIGGER |
| 650 |
* accepts. This debugging cross-check will throw an error if this |
| 651 |
* function is invoked for a command tag that CREATE EVENT TRIGGER won't |
| 652 |
* accept. (Unfortunately, there doesn't seem to be any simple, automated |
| 653 |
* way to verify that CREATE EVENT TRIGGER doesn't accept extra stuff that |
| 654 |
* never reaches this control point.) |
| 655 |
* |
| 656 |
* If this cross-check fails for you, you probably need to either adjust |
| 657 |
* standard_ProcessUtility() not to invoke event triggers for the command |
| 658 |
* type in question, or you need to adjust event_trigger_ok to accept the |
| 659 |
* relevant command tag. |
| 660 |
*/ |
| 661 |
#ifdef USE_ASSERT_CHECKING |
| 662 |
{ |
| 663 |
CommandTag dbgtag; |
| 664 |
|
| 665 |
dbgtag = EventTriggerGetTag(parsetree, event); |
| 666 |
|
| 667 |
if (event == EVT_DDLCommandStart || |
| 668 |
event == EVT_DDLCommandEnd || |
| 669 |
event == EVT_SQLDrop || |
| 670 |
event == EVT_Login) |
| 671 |
{ |
| 672 |
if (!command_tag_event_trigger_ok(dbgtag)) |
| 673 |
elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); |
| 674 |
} |
| 675 |
else if (event == EVT_TableRewrite) |
| 676 |
{ |
| 677 |
if (!command_tag_table_rewrite_ok(dbgtag)) |
| 678 |
elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); |
| 679 |
} |
| 680 |
} |
| 681 |
#endif |
| 682 |
|
| 683 |
/* Use cache to find triggers for this event; fast exit if none. */ |
| 684 |
cachelist = EventCacheLookup(event); |
| 685 |
if (cachelist == NIL) |
| 686 |
return NIL; |
| 687 |
|
| 688 |
/* Get the command tag. */ |
| 689 |
tag = EventTriggerGetTag(parsetree, event); |
| 690 |
|
| 691 |
/* |
| 692 |
* Filter list of event triggers by command tag, and copy them into our |
| 693 |
* memory context. Once we start running the command triggers, or indeed |
| 694 |
* once we do anything at all that touches the catalogs, an invalidation |
| 695 |
* might leave cachelist pointing at garbage, so we must do this before we |
| 696 |
* can do much else. |
| 697 |
*/ |
| 698 |
foreach(lc, cachelist) |
| 699 |
{ |
| 700 |
EventTriggerCacheItem *item = lfirst(lc); |
| 701 |
|
| 702 |
if (unfiltered || filter_event_trigger(tag, item)) |
| 703 |
{ |
| 704 |
/* We must plan to fire this trigger. */ |
| 705 |
runlist = lappend_oid(runlist, item->fnoid); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
/* Don't spend any more time on this if no functions to run */ |
| 710 |
if (runlist == NIL) |
| 711 |
return NIL; |
| 712 |
|
| 713 |
trigdata->type = T_EventTriggerData; |
| 714 |
trigdata->event = eventstr; |
| 715 |
trigdata->parsetree = parsetree; |
| 716 |
trigdata->tag = tag; |
| 717 |
|
| 718 |
return runlist; |
| 719 |
} |
| 720 |
|
| 721 |
/* |
| 722 |
* Fire ddl_command_start triggers. |
| 723 |
*/ |
| 724 |
void |
| 725 |
EventTriggerDDLCommandStart(Node *parsetree) |
| 726 |
{ |
| 727 |
List *runlist; |
| 728 |
EventTriggerData trigdata; |
| 729 |
|
| 730 |
/* |
| 731 |
* Event Triggers are completely disabled in standalone mode. There are |
| 732 |
* (at least) two reasons for this: |
| 733 |
* |
| 734 |
* 1. A sufficiently broken event trigger might not only render the |
| 735 |
* database unusable, but prevent disabling itself to fix the situation. |
| 736 |
* In this scenario, restarting in standalone mode provides an escape |
| 737 |
* hatch. |
| 738 |
* |
| 739 |
* 2. BuildEventTriggerCache relies on systable_beginscan_ordered, and |
| 740 |
* therefore will malfunction if pg_event_trigger's indexes are damaged. |
| 741 |
* To allow recovery from a damaged index, we need some operating mode |
| 742 |
* wherein event triggers are disabled. (Or we could implement |
| 743 |
* heapscan-and-sort logic for that case, but having disaster recovery |
| 744 |
* scenarios depend on code that's otherwise untested isn't appetizing.) |
| 745 |
* |
| 746 |
* Additionally, event triggers can be disabled with a superuser-only GUC |
| 747 |
* to make fixing database easier as per 1 above. |
| 748 |
*/ |
| 749 |
if (!IsUnderPostmaster || !event_triggers) |
| 750 |
return; |
| 751 |
|
| 752 |
runlist = EventTriggerCommonSetup(parsetree, |
| 753 |
EVT_DDLCommandStart, |
| 754 |
"ddl_command_start", |
| 755 |
&trigdata, false); |
| 756 |
if (runlist == NIL) |
| 757 |
return; |
| 758 |
|
| 759 |
/* Run the triggers. */ |
| 760 |
EventTriggerInvoke(runlist, &trigdata); |
| 761 |
|
| 762 |
/* Cleanup. */ |
| 763 |
list_free(runlist); |
| 764 |
|
| 765 |
/* |
| 766 |
* Make sure anything the event triggers did will be visible to the main |
| 767 |
* command. |
| 768 |
*/ |
| 769 |
CommandCounterIncrement(); |
| 770 |
} |
| 771 |
|
| 772 |
/* |
| 773 |
* Fire ddl_command_end triggers. |
| 774 |
*/ |
| 775 |
void |
| 776 |
EventTriggerDDLCommandEnd(Node *parsetree) |
| 777 |
{ |
| 778 |
List *runlist; |
| 779 |
EventTriggerData trigdata; |
| 780 |
|
| 781 |
/* |
| 782 |
* See EventTriggerDDLCommandStart for a discussion about why event |
| 783 |
* triggers are disabled in single user mode or via GUC. |
| 784 |
*/ |
| 785 |
if (!IsUnderPostmaster || !event_triggers) |
| 786 |
return; |
| 787 |
|
| 788 |
/* |
| 789 |
* Also do nothing if our state isn't set up, which it won't be if there |
| 790 |
* weren't any relevant event triggers at the start of the current DDL |
| 791 |
* command. This test might therefore seem optional, but it's important |
| 792 |
* because EventTriggerCommonSetup might find triggers that didn't exist |
| 793 |
* at the time the command started. Although this function itself |
| 794 |
* wouldn't crash, the event trigger functions would presumably call |
| 795 |
* pg_event_trigger_ddl_commands which would fail. Better to do nothing |
| 796 |
* until the next command. |
| 797 |
*/ |
| 798 |
if (!currentEventTriggerState) |
| 799 |
return; |
| 800 |
|
| 801 |
runlist = EventTriggerCommonSetup(parsetree, |
| 802 |
EVT_DDLCommandEnd, "ddl_command_end", |
| 803 |
&trigdata, false); |
| 804 |
if (runlist == NIL) |
| 805 |
return; |
| 806 |
|
| 807 |
/* |
| 808 |
* Make sure anything the main command did will be visible to the event |
| 809 |
* triggers. |
| 810 |
*/ |
| 811 |
CommandCounterIncrement(); |
| 812 |
|
| 813 |
/* Run the triggers. */ |
| 814 |
EventTriggerInvoke(runlist, &trigdata); |
| 815 |
|
| 816 |
/* Cleanup. */ |
| 817 |
list_free(runlist); |
| 818 |
} |
| 819 |
|
| 820 |
/* |
| 821 |
* Fire sql_drop triggers. |
| 822 |
*/ |
| 823 |
void |
| 824 |
EventTriggerSQLDrop(Node *parsetree) |
| 825 |
{ |
| 826 |
List *runlist; |
| 827 |
EventTriggerData trigdata; |
| 828 |
|
| 829 |
/* |
| 830 |
* See EventTriggerDDLCommandStart for a discussion about why event |
| 831 |
* triggers are disabled in single user mode or via a GUC. |
| 832 |
*/ |
| 833 |
if (!IsUnderPostmaster || !event_triggers) |
| 834 |
return; |
| 835 |
|
| 836 |
/* |
| 837 |
* Use current state to determine whether this event fires at all. If |
| 838 |
* there are no triggers for the sql_drop event, then we don't have |
| 839 |
* anything to do here. Note that dropped object collection is disabled |
| 840 |
* if this is the case, so even if we were to try to run, the list would |
| 841 |
* be empty. |
| 842 |
*/ |
| 843 |
if (!currentEventTriggerState || |
| 844 |
slist_is_empty(¤tEventTriggerState->SQLDropList)) |
| 845 |
return; |
| 846 |
|
| 847 |
runlist = EventTriggerCommonSetup(parsetree, |
| 848 |
EVT_SQLDrop, "sql_drop", |
| 849 |
&trigdata, false); |
| 850 |
|
| 851 |
/* |
| 852 |
* Nothing to do if run list is empty. Note this typically can't happen, |
| 853 |
* because if there are no sql_drop events, then objects-to-drop wouldn't |
| 854 |
* have been collected in the first place and we would have quit above. |
| 855 |
* But it could occur if event triggers were dropped partway through. |
| 856 |
*/ |
| 857 |
if (runlist == NIL) |
| 858 |
return; |
| 859 |
|
| 860 |
/* |
| 861 |
* Make sure anything the main command did will be visible to the event |
| 862 |
* triggers. |
| 863 |
*/ |
| 864 |
CommandCounterIncrement(); |
| 865 |
|
| 866 |
/* |
| 867 |
* Make sure pg_event_trigger_dropped_objects only works when running |
| 868 |
* these triggers. Use PG_TRY to ensure in_sql_drop is reset even when |
| 869 |
* one trigger fails. (This is perhaps not necessary, as the currentState |
| 870 |
* variable will be removed shortly by our caller, but it seems better to |
| 871 |
* play safe.) |
| 872 |
*/ |
| 873 |
currentEventTriggerState->in_sql_drop = true; |
| 874 |
|
| 875 |
/* Run the triggers. */ |
| 876 |
PG_TRY(); |
| 877 |
{ |
| 878 |
EventTriggerInvoke(runlist, &trigdata); |
| 879 |
} |
| 880 |
PG_FINALLY(); |
| 881 |
{ |
| 882 |
currentEventTriggerState->in_sql_drop = false; |
| 883 |
} |
| 884 |
PG_END_TRY(); |
| 885 |
|
| 886 |
/* Cleanup. */ |
| 887 |
list_free(runlist); |
| 888 |
} |
| 889 |
|
| 890 |
/* |
| 891 |
* Fire login event triggers if any are present. The dathasloginevt |
| 892 |
* pg_database flag is left unchanged when an event trigger is dropped to avoid |
| 893 |
* complicating the codepath in the case of multiple event triggers. This |
| 894 |
* function will instead unset the flag if no trigger is defined. |
| 895 |
*/ |
| 896 |
void |
| 897 |
EventTriggerOnLogin(void) |
| 898 |
{ |
| 899 |
List *runlist; |
| 900 |
EventTriggerData trigdata; |
| 901 |
|
| 902 |
/* |
| 903 |
* See EventTriggerDDLCommandStart for a discussion about why event |
| 904 |
* triggers are disabled in single user mode or via a GUC. We also need a |
| 905 |
* database connection (some background workers don't have it). |
| 906 |
*/ |
| 907 |
if (!IsUnderPostmaster || !event_triggers || |
| 908 |
!OidIsValid(MyDatabaseId) || !MyDatabaseHasLoginEventTriggers) |
| 909 |
return; |
| 910 |
|
| 911 |
StartTransactionCommand(); |
| 912 |
runlist = EventTriggerCommonSetup(NULL, |
| 913 |
EVT_Login, "login", |
| 914 |
&trigdata, false); |
| 915 |
|
| 916 |
if (runlist != NIL) |
| 917 |
{ |
| 918 |
/* |
| 919 |
* Event trigger execution may require an active snapshot. |
| 920 |
*/ |
| 921 |
PushActiveSnapshot(GetTransactionSnapshot()); |
| 922 |
|
| 923 |
/* Run the triggers. */ |
| 924 |
EventTriggerInvoke(runlist, &trigdata); |
| 925 |
|
| 926 |
/* Cleanup. */ |
| 927 |
list_free(runlist); |
| 928 |
|
| 929 |
PopActiveSnapshot(); |
| 930 |
} |
| 931 |
|
| 932 |
/* |
| 933 |
* There is no active login event trigger, but our |
| 934 |
* pg_database.dathasloginevt is set. Try to unset this flag. We use the |
| 935 |
* lock to prevent concurrent SetDatabaseHasLoginEventTriggers(), but we |
| 936 |
* don't want to hang the connection waiting on the lock. Thus, we are |
| 937 |
* just trying to acquire the lock conditionally. |
| 938 |
* |
| 939 |
* Skip this on a hot standby: the conditional AccessExclusiveLock on the |
| 940 |
* database object would fail with "cannot acquire lock mode ... while |
| 941 |
* recovery is in progress", which the caller would surface as a FATAL |
| 942 |
* connection error. On a standby, we cannot (and must not) clear the |
| 943 |
* pg_database flag ourselves; it will be cleared via WAL replay once the |
| 944 |
* primary's next login event trigger run clears it on the primary. |
| 945 |
*/ |
| 946 |
else if (!RecoveryInProgress() && |
| 947 |
ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId, |
| 948 |
0, AccessExclusiveLock)) |
| 949 |
{ |
| 950 |
/* |
| 951 |
* The lock is held. Now we need to recheck that login event triggers |
| 952 |
* list is still empty. Once the list is empty, we know that even if |
| 953 |
* there is a backend which concurrently inserts/enables a login event |
| 954 |
* trigger, it will update pg_database.dathasloginevt *afterwards*. |
| 955 |
*/ |
| 956 |
runlist = EventTriggerCommonSetup(NULL, |
| 957 |
EVT_Login, "login", |
| 958 |
&trigdata, true); |
| 959 |
|
| 960 |
if (runlist == NIL) |
| 961 |
{ |
| 962 |
Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); |
| 963 |
HeapTuple tuple; |
| 964 |
void *state; |
| 965 |
Form_pg_database db; |
| 966 |
ScanKeyData key[1]; |
| 967 |
|
| 968 |
/* Fetch a copy of the tuple to scribble on */ |
| 969 |
ScanKeyInit(&key[0], |
| 970 |
Anum_pg_database_oid, |
| 971 |
BTEqualStrategyNumber, F_OIDEQ, |
| 972 |
ObjectIdGetDatum(MyDatabaseId)); |
| 973 |
|
| 974 |
systable_inplace_update_begin(pg_db, DatabaseOidIndexId, true, |
| 975 |
NULL, 1, key, &tuple, &state); |
| 976 |
|
| 977 |
if (!HeapTupleIsValid(tuple)) |
| 978 |
elog(ERROR, "could not find tuple for database %u", MyDatabaseId); |
| 979 |
|
| 980 |
db = (Form_pg_database) GETSTRUCT(tuple); |
| 981 |
if (db->dathasloginevt) |
| 982 |
{ |
| 983 |
db->dathasloginevt = false; |
| 984 |
|
| 985 |
/* |
| 986 |
* Do an "in place" update of the pg_database tuple. Doing |
| 987 |
* this instead of regular updates serves two purposes. First, |
| 988 |
* that avoids possible waiting on the row-level lock. Second, |
| 989 |
* that avoids dealing with TOAST. |
| 990 |
*/ |
| 991 |
systable_inplace_update_finish(state, tuple); |
| 992 |
} |
| 993 |
else |
| 994 |
systable_inplace_update_cancel(state); |
| 995 |
table_close(pg_db, RowExclusiveLock); |
| 996 |
heap_freetuple(tuple); |
| 997 |
} |
| 998 |
else |
| 999 |
{ |
| 1000 |
list_free(runlist); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
CommitTransactionCommand(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
/* |
| 1008 |
* Fire table_rewrite triggers. |
| 1009 |
*/ |
| 1010 |
void |
| 1011 |
EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason) |
| 1012 |
{ |
| 1013 |
List *runlist; |
| 1014 |
EventTriggerData trigdata; |
| 1015 |
|
| 1016 |
/* |
| 1017 |
* See EventTriggerDDLCommandStart for a discussion about why event |
| 1018 |
* triggers are disabled in single user mode or via a GUC. |
| 1019 |
*/ |
| 1020 |
if (!IsUnderPostmaster || !event_triggers) |
| 1021 |
return; |
| 1022 |
|
| 1023 |
/* |
| 1024 |
* Also do nothing if our state isn't set up, which it won't be if there |
| 1025 |
* weren't any relevant event triggers at the start of the current DDL |
| 1026 |
* command. This test might therefore seem optional, but it's |
| 1027 |
* *necessary*, because EventTriggerCommonSetup might find triggers that |
| 1028 |
* didn't exist at the time the command started. |
| 1029 |
*/ |
| 1030 |
if (!currentEventTriggerState) |
| 1031 |
return; |
| 1032 |
|
| 1033 |
runlist = EventTriggerCommonSetup(parsetree, |
| 1034 |
EVT_TableRewrite, |
| 1035 |
"table_rewrite", |
| 1036 |
&trigdata, false); |
| 1037 |
if (runlist == NIL) |
| 1038 |
return; |
| 1039 |
|
| 1040 |
/* |
| 1041 |
* Make sure pg_event_trigger_table_rewrite_oid only works when running |
| 1042 |
* these triggers. Use PG_TRY to ensure table_rewrite_oid is reset even |
| 1043 |
* when one trigger fails. (This is perhaps not necessary, as the |
| 1044 |
* currentState variable will be removed shortly by our caller, but it |
| 1045 |
* seems better to play safe.) |
| 1046 |
*/ |
| 1047 |
currentEventTriggerState->table_rewrite_oid = tableOid; |
| 1048 |
currentEventTriggerState->table_rewrite_reason = reason; |
| 1049 |
|
| 1050 |
/* Run the triggers. */ |
| 1051 |
PG_TRY(); |
| 1052 |
{ |
| 1053 |
EventTriggerInvoke(runlist, &trigdata); |
| 1054 |
} |
| 1055 |
PG_FINALLY(); |
| 1056 |
{ |
| 1057 |
currentEventTriggerState->table_rewrite_oid = InvalidOid; |
| 1058 |
currentEventTriggerState->table_rewrite_reason = 0; |
| 1059 |
} |
| 1060 |
PG_END_TRY(); |
| 1061 |
|
| 1062 |
/* Cleanup. */ |
| 1063 |
list_free(runlist); |
| 1064 |
|
| 1065 |
/* |
| 1066 |
* Make sure anything the event triggers did will be visible to the main |
| 1067 |
* command. |
| 1068 |
*/ |
| 1069 |
CommandCounterIncrement(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
/* |
| 1073 |
* Invoke each event trigger in a list of event triggers. |
| 1074 |
*/ |
| 1075 |
static void |
| 1076 |
EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata) |
| 1077 |
{ |
| 1078 |
MemoryContext context; |
| 1079 |
MemoryContext oldcontext; |
| 1080 |
ListCell *lc; |
| 1081 |
bool first = true; |
| 1082 |
|
| 1083 |
/* Guard against stack overflow due to recursive event trigger */ |
| 1084 |
check_stack_depth(); |
| 1085 |
|
| 1086 |
/* |
| 1087 |
* Let's evaluate event triggers in their own memory context, so that any |
| 1088 |
* leaks get cleaned up promptly. |
| 1089 |
*/ |
| 1090 |
context = AllocSetContextCreate(CurrentMemoryContext, |
| 1091 |
"event trigger context", |
| 1092 |
ALLOCSET_DEFAULT_SIZES); |
| 1093 |
oldcontext = MemoryContextSwitchTo(context); |
| 1094 |
|
| 1095 |
/* Call each event trigger. */ |
| 1096 |
foreach(lc, fn_oid_list) |
| 1097 |
{ |
| 1098 |
LOCAL_FCINFO(fcinfo, 0); |
| 1099 |
Oid fnoid = lfirst_oid(lc); |
| 1100 |
FmgrInfo flinfo; |
| 1101 |
PgStat_FunctionCallUsage fcusage; |
| 1102 |
|
| 1103 |
elog(DEBUG1, "EventTriggerInvoke %u", fnoid); |
| 1104 |
|
| 1105 |
/* |
| 1106 |
* We want each event trigger to be able to see the results of the |
| 1107 |
* previous event trigger's action. Caller is responsible for any |
| 1108 |
* command-counter increment that is needed between the event trigger |
| 1109 |
* and anything else in the transaction. |
| 1110 |
*/ |
| 1111 |
if (first) |
| 1112 |
first = false; |
| 1113 |
else |
| 1114 |
CommandCounterIncrement(); |
| 1115 |
|
| 1116 |
/* Look up the function */ |
| 1117 |
fmgr_info(fnoid, &flinfo); |
| 1118 |
|
| 1119 |
/* Call the function, passing no arguments but setting a context. */ |
| 1120 |
InitFunctionCallInfoData(*fcinfo, &flinfo, 0, |
| 1121 |
InvalidOid, (Node *) trigdata, NULL); |
| 1122 |
pgstat_init_function_usage(fcinfo, &fcusage); |
| 1123 |
FunctionCallInvoke(fcinfo); |
| 1124 |
pgstat_end_function_usage(&fcusage, true); |
| 1125 |
|
| 1126 |
/* Reclaim memory. */ |
| 1127 |
MemoryContextReset(context); |
| 1128 |
} |
| 1129 |
|
| 1130 |
/* Restore old memory context and delete the temporary one. */ |
| 1131 |
MemoryContextSwitchTo(oldcontext); |
| 1132 |
MemoryContextDelete(context); |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* |
| 1136 |
* Do event triggers support this object type? |
| 1137 |
* |
| 1138 |
* See also event trigger documentation in event-trigger.sgml. |
| 1139 |
*/ |
| 1140 |
bool |
| 1141 |
EventTriggerSupportsObjectType(ObjectType obtype) |
| 1142 |
{ |
| 1143 |
switch (obtype) |
| 1144 |
{ |
| 1145 |
case OBJECT_DATABASE: |
| 1146 |
case OBJECT_TABLESPACE: |
| 1147 |
case OBJECT_ROLE: |
| 1148 |
case OBJECT_PARAMETER_ACL: |
| 1149 |
/* no support for global objects (except subscriptions) */ |
| 1150 |
return false; |
| 1151 |
case OBJECT_EVENT_TRIGGER: |
| 1152 |
/* no support for event triggers on event triggers */ |
| 1153 |
return false; |
| 1154 |
default: |
| 1155 |
return true; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
/* |
| 1160 |
* Do event triggers support this object class? |
| 1161 |
* |
| 1162 |
* See also event trigger documentation in event-trigger.sgml. |
| 1163 |
*/ |
| 1164 |
bool |
| 1165 |
EventTriggerSupportsObject(const ObjectAddress *object) |
| 1166 |
{ |
| 1167 |
switch (object->classId) |
| 1168 |
{ |
| 1169 |
case DatabaseRelationId: |
| 1170 |
case TableSpaceRelationId: |
| 1171 |
case AuthIdRelationId: |
| 1172 |
case AuthMemRelationId: |
| 1173 |
case ParameterAclRelationId: |
| 1174 |
/* no support for global objects (except subscriptions) */ |
| 1175 |
return false; |
| 1176 |
case EventTriggerRelationId: |
| 1177 |
/* no support for event triggers on event triggers */ |
| 1178 |
return false; |
| 1179 |
default: |
| 1180 |
return true; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* Prepare event trigger state for a new complete query to run, if necessary; |
| 1186 |
* returns whether this was done. If it was, EventTriggerEndCompleteQuery must |
| 1187 |
* be called when the query is done, regardless of whether it succeeds or fails |
| 1188 |
* -- so use of a PG_TRY block is mandatory. |
| 1189 |
*/ |
| 1190 |
bool |
| 1191 |
EventTriggerBeginCompleteQuery(void) |
| 1192 |
{ |
| 1193 |
EventTriggerQueryState *state; |
| 1194 |
MemoryContext cxt; |
| 1195 |
|
| 1196 |
/* |
| 1197 |
* Currently, sql_drop, table_rewrite, ddl_command_end events are the only |
| 1198 |
* reason to have event trigger state at all; so if there are none, don't |
| 1199 |
* install one. |
| 1200 |
*/ |
| 1201 |
if (!trackDroppedObjectsNeeded()) |
| 1202 |
return false; |
| 1203 |
|
| 1204 |
cxt = AllocSetContextCreate(TopMemoryContext, |
| 1205 |
"event trigger state", |
| 1206 |
ALLOCSET_DEFAULT_SIZES); |
| 1207 |
state = MemoryContextAlloc(cxt, sizeof(EventTriggerQueryState)); |
| 1208 |
state->cxt = cxt; |
| 1209 |
slist_init(&(state->SQLDropList)); |
| 1210 |
state->in_sql_drop = false; |
| 1211 |
state->table_rewrite_oid = InvalidOid; |
| 1212 |
|
| 1213 |
state->commandCollectionInhibited = currentEventTriggerState ? |
| 1214 |
currentEventTriggerState->commandCollectionInhibited : false; |
| 1215 |
state->currentCommand = NULL; |
| 1216 |
state->commandList = NIL; |
| 1217 |
state->previous = currentEventTriggerState; |
| 1218 |
currentEventTriggerState = state; |
| 1219 |
|
| 1220 |
return true; |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* |
| 1224 |
* Query completed (or errored out) -- clean up local state, return to previous |
| 1225 |
* one. |
| 1226 |
* |
| 1227 |
* Note: it's an error to call this routine if EventTriggerBeginCompleteQuery |
| 1228 |
* returned false previously. |
| 1229 |
* |
| 1230 |
* Note: this might be called in the PG_CATCH block of a failing transaction, |
| 1231 |
* so be wary of running anything unnecessary. (In particular, it's probably |
| 1232 |
* unwise to try to allocate memory.) |
| 1233 |
*/ |
| 1234 |
void |
| 1235 |
EventTriggerEndCompleteQuery(void) |
| 1236 |
{ |
| 1237 |
EventTriggerQueryState *prevstate; |
| 1238 |
|
| 1239 |
prevstate = currentEventTriggerState->previous; |
| 1240 |
|
| 1241 |
/* this avoids the need for retail pfree of SQLDropList items: */ |
| 1242 |
MemoryContextDelete(currentEventTriggerState->cxt); |
| 1243 |
|
| 1244 |
currentEventTriggerState = prevstate; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/* |
| 1248 |
* Do we need to keep close track of objects being dropped? |
| 1249 |
* |
| 1250 |
* This is useful because there is a cost to running with them enabled. |
| 1251 |
*/ |
| 1252 |
bool |
| 1253 |
trackDroppedObjectsNeeded(void) |
| 1254 |
{ |
| 1255 |
/* |
| 1256 |
* true if any sql_drop, table_rewrite, ddl_command_end event trigger |
| 1257 |
* exists |
| 1258 |
*/ |
| 1259 |
return (EventCacheLookup(EVT_SQLDrop) != NIL) || |
| 1260 |
(EventCacheLookup(EVT_TableRewrite) != NIL) || |
| 1261 |
(EventCacheLookup(EVT_DDLCommandEnd) != NIL); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* |
| 1265 |
* Support for dropped objects information on event trigger functions. |
| 1266 |
* |
| 1267 |
* We keep the list of objects dropped by the current command in current |
| 1268 |
* state's SQLDropList (comprising SQLDropObject items). Each time a new |
| 1269 |
* command is to start, a clean EventTriggerQueryState is created; commands |
| 1270 |
* that drop objects do the dependency.c dance to drop objects, which |
| 1271 |
* populates the current state's SQLDropList; when the event triggers are |
| 1272 |
* invoked they can consume the list via pg_event_trigger_dropped_objects(). |
| 1273 |
* When the command finishes, the EventTriggerQueryState is cleared, and |
| 1274 |
* the one from the previous command is restored (when no command is in |
| 1275 |
* execution, the current state is NULL). |
| 1276 |
* |
| 1277 |
* All this lets us support the case that an event trigger function drops |
| 1278 |
* objects "reentrantly". |
| 1279 |
*/ |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* Register one object as being dropped by the current command. |
| 1283 |
*/ |
| 1284 |
void |
| 1285 |
EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal) |
| 1286 |
{ |
| 1287 |
SQLDropObject *obj; |
| 1288 |
MemoryContext oldcxt; |
| 1289 |
|
| 1290 |
if (!currentEventTriggerState) |
| 1291 |
return; |
| 1292 |
|
| 1293 |
Assert(EventTriggerSupportsObject(object)); |
| 1294 |
|
| 1295 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1296 |
|
| 1297 |
obj = palloc0(sizeof(SQLDropObject)); |
| 1298 |
obj->address = *object; |
| 1299 |
obj->original = original; |
| 1300 |
obj->normal = normal; |
| 1301 |
|
| 1302 |
if (object->classId == NamespaceRelationId) |
| 1303 |
{ |
| 1304 |
/* Special handling is needed for temp namespaces */ |
| 1305 |
if (isTempNamespace(object->objectId)) |
| 1306 |
obj->istemp = true; |
| 1307 |
else if (isAnyTempNamespace(object->objectId)) |
| 1308 |
{ |
| 1309 |
/* don't report temp schemas except my own */ |
| 1310 |
pfree(obj); |
| 1311 |
MemoryContextSwitchTo(oldcxt); |
| 1312 |
return; |
| 1313 |
} |
| 1314 |
obj->objname = get_namespace_name(object->objectId); |
| 1315 |
} |
| 1316 |
else if (object->classId == AttrDefaultRelationId) |
| 1317 |
{ |
| 1318 |
/* We treat a column default as temp if its table is temp */ |
| 1319 |
ObjectAddress colobject; |
| 1320 |
|
| 1321 |
colobject = GetAttrDefaultColumnAddress(object->objectId); |
| 1322 |
if (OidIsValid(colobject.objectId)) |
| 1323 |
{ |
| 1324 |
if (!obtain_object_name_namespace(&colobject, obj)) |
| 1325 |
{ |
| 1326 |
pfree(obj); |
| 1327 |
MemoryContextSwitchTo(oldcxt); |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
else if (object->classId == TriggerRelationId) |
| 1333 |
{ |
| 1334 |
/* Similarly, a trigger is temp if its table is temp */ |
| 1335 |
/* Sadly, there's no lsyscache.c support for trigger objects */ |
| 1336 |
Relation pg_trigger_rel; |
| 1337 |
ScanKeyData skey[1]; |
| 1338 |
SysScanDesc sscan; |
| 1339 |
HeapTuple tuple; |
| 1340 |
Oid relid; |
| 1341 |
|
| 1342 |
/* Fetch the trigger's table OID the hard way */ |
| 1343 |
pg_trigger_rel = table_open(TriggerRelationId, AccessShareLock); |
| 1344 |
ScanKeyInit(&skey[0], |
| 1345 |
Anum_pg_trigger_oid, |
| 1346 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1347 |
ObjectIdGetDatum(object->objectId)); |
| 1348 |
sscan = systable_beginscan(pg_trigger_rel, TriggerOidIndexId, true, |
| 1349 |
NULL, 1, skey); |
| 1350 |
tuple = systable_getnext(sscan); |
| 1351 |
if (HeapTupleIsValid(tuple)) |
| 1352 |
relid = ((Form_pg_trigger) GETSTRUCT(tuple))->tgrelid; |
| 1353 |
else |
| 1354 |
relid = InvalidOid; /* shouldn't happen */ |
| 1355 |
systable_endscan(sscan); |
| 1356 |
table_close(pg_trigger_rel, AccessShareLock); |
| 1357 |
/* Do nothing if we didn't find the trigger */ |
| 1358 |
if (OidIsValid(relid)) |
| 1359 |
{ |
| 1360 |
ObjectAddress relobject; |
| 1361 |
|
| 1362 |
relobject.classId = RelationRelationId; |
| 1363 |
relobject.objectId = relid; |
| 1364 |
/* Arbitrarily set objectSubId nonzero so as not to fill objname */ |
| 1365 |
relobject.objectSubId = 1; |
| 1366 |
if (!obtain_object_name_namespace(&relobject, obj)) |
| 1367 |
{ |
| 1368 |
pfree(obj); |
| 1369 |
MemoryContextSwitchTo(oldcxt); |
| 1370 |
return; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
} |
| 1374 |
else if (object->classId == PolicyRelationId) |
| 1375 |
{ |
| 1376 |
/* Similarly, a policy is temp if its table is temp */ |
| 1377 |
/* Sadly, there's no lsyscache.c support for policy objects */ |
| 1378 |
Relation pg_policy_rel; |
| 1379 |
ScanKeyData skey[1]; |
| 1380 |
SysScanDesc sscan; |
| 1381 |
HeapTuple tuple; |
| 1382 |
Oid relid; |
| 1383 |
|
| 1384 |
/* Fetch the policy's table OID the hard way */ |
| 1385 |
pg_policy_rel = table_open(PolicyRelationId, AccessShareLock); |
| 1386 |
ScanKeyInit(&skey[0], |
| 1387 |
Anum_pg_policy_oid, |
| 1388 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1389 |
ObjectIdGetDatum(object->objectId)); |
| 1390 |
sscan = systable_beginscan(pg_policy_rel, PolicyOidIndexId, true, |
| 1391 |
NULL, 1, skey); |
| 1392 |
tuple = systable_getnext(sscan); |
| 1393 |
if (HeapTupleIsValid(tuple)) |
| 1394 |
relid = ((Form_pg_policy) GETSTRUCT(tuple))->polrelid; |
| 1395 |
else |
| 1396 |
relid = InvalidOid; /* shouldn't happen */ |
| 1397 |
systable_endscan(sscan); |
| 1398 |
table_close(pg_policy_rel, AccessShareLock); |
| 1399 |
/* Do nothing if we didn't find the policy */ |
| 1400 |
if (OidIsValid(relid)) |
| 1401 |
{ |
| 1402 |
ObjectAddress relobject; |
| 1403 |
|
| 1404 |
relobject.classId = RelationRelationId; |
| 1405 |
relobject.objectId = relid; |
| 1406 |
/* Arbitrarily set objectSubId nonzero so as not to fill objname */ |
| 1407 |
relobject.objectSubId = 1; |
| 1408 |
if (!obtain_object_name_namespace(&relobject, obj)) |
| 1409 |
{ |
| 1410 |
pfree(obj); |
| 1411 |
MemoryContextSwitchTo(oldcxt); |
| 1412 |
return; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
} |
| 1416 |
else |
| 1417 |
{ |
| 1418 |
/* Generic handling for all other object classes */ |
| 1419 |
if (!obtain_object_name_namespace(object, obj)) |
| 1420 |
{ |
| 1421 |
/* don't report temp objects except my own */ |
| 1422 |
pfree(obj); |
| 1423 |
MemoryContextSwitchTo(oldcxt); |
| 1424 |
return; |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
/* object identity, objname and objargs */ |
| 1429 |
obj->objidentity = |
| 1430 |
getObjectIdentityParts(&obj->address, &obj->addrnames, &obj->addrargs, |
| 1431 |
false); |
| 1432 |
|
| 1433 |
/* object type */ |
| 1434 |
obj->objecttype = getObjectTypeDescription(&obj->address, false); |
| 1435 |
|
| 1436 |
slist_push_head(&(currentEventTriggerState->SQLDropList), &obj->next); |
| 1437 |
|
| 1438 |
MemoryContextSwitchTo(oldcxt); |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* |
| 1442 |
* Fill obj->objname, obj->schemaname, and obj->istemp based on object. |
| 1443 |
* |
| 1444 |
* Returns true if this object should be reported, false if it should |
| 1445 |
* be ignored because it is a temporary object of another session. |
| 1446 |
*/ |
| 1447 |
static bool |
| 1448 |
obtain_object_name_namespace(const ObjectAddress *object, SQLDropObject *obj) |
| 1449 |
{ |
| 1450 |
/* |
| 1451 |
* Obtain schema names from the object's catalog tuple, if one exists; |
| 1452 |
* this lets us skip objects in temp schemas. We trust that |
| 1453 |
* ObjectProperty contains all object classes that can be |
| 1454 |
* schema-qualified. |
| 1455 |
* |
| 1456 |
* Currently, this function does nothing for object classes that are not |
| 1457 |
* in ObjectProperty, but we might sometime add special cases for that. |
| 1458 |
*/ |
| 1459 |
if (is_objectclass_supported(object->classId)) |
| 1460 |
{ |
| 1461 |
Relation catalog; |
| 1462 |
HeapTuple tuple; |
| 1463 |
|
| 1464 |
catalog = table_open(object->classId, AccessShareLock); |
| 1465 |
tuple = get_catalog_object_by_oid(catalog, |
| 1466 |
get_object_attnum_oid(object->classId), |
| 1467 |
object->objectId); |
| 1468 |
|
| 1469 |
if (tuple) |
| 1470 |
{ |
| 1471 |
AttrNumber attnum; |
| 1472 |
Datum datum; |
| 1473 |
bool isnull; |
| 1474 |
|
| 1475 |
attnum = get_object_attnum_namespace(object->classId); |
| 1476 |
if (attnum != InvalidAttrNumber) |
| 1477 |
{ |
| 1478 |
datum = heap_getattr(tuple, attnum, |
| 1479 |
RelationGetDescr(catalog), &isnull); |
| 1480 |
if (!isnull) |
| 1481 |
{ |
| 1482 |
Oid namespaceId; |
| 1483 |
|
| 1484 |
namespaceId = DatumGetObjectId(datum); |
| 1485 |
/* temp objects are only reported if they are my own */ |
| 1486 |
if (isTempNamespace(namespaceId)) |
| 1487 |
{ |
| 1488 |
obj->schemaname = "pg_temp"; |
| 1489 |
obj->istemp = true; |
| 1490 |
} |
| 1491 |
else if (isAnyTempNamespace(namespaceId)) |
| 1492 |
{ |
| 1493 |
/* no need to fill any fields of *obj */ |
| 1494 |
table_close(catalog, AccessShareLock); |
| 1495 |
return false; |
| 1496 |
} |
| 1497 |
else |
| 1498 |
{ |
| 1499 |
obj->schemaname = get_namespace_name(namespaceId); |
| 1500 |
obj->istemp = false; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
if (get_object_namensp_unique(object->classId) && |
| 1506 |
object->objectSubId == 0) |
| 1507 |
{ |
| 1508 |
attnum = get_object_attnum_name(object->classId); |
| 1509 |
if (attnum != InvalidAttrNumber) |
| 1510 |
{ |
| 1511 |
datum = heap_getattr(tuple, attnum, |
| 1512 |
RelationGetDescr(catalog), &isnull); |
| 1513 |
if (!isnull) |
| 1514 |
obj->objname = pstrdup(NameStr(*DatumGetName(datum))); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
table_close(catalog, AccessShareLock); |
| 1520 |
} |
| 1521 |
|
| 1522 |
return true; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* |
| 1526 |
* pg_event_trigger_dropped_objects |
| 1527 |
* |
| 1528 |
* Make the list of dropped objects available to the user function run by the |
| 1529 |
* Event Trigger. |
| 1530 |
*/ |
| 1531 |
Datum |
| 1532 |
pg_event_trigger_dropped_objects(PG_FUNCTION_ARGS) |
| 1533 |
{ |
| 1534 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 1535 |
slist_iter iter; |
| 1536 |
|
| 1537 |
/* |
| 1538 |
* Protect this function from being called out of context |
| 1539 |
*/ |
| 1540 |
if (!currentEventTriggerState || |
| 1541 |
!currentEventTriggerState->in_sql_drop) |
| 1542 |
ereport(ERROR, |
| 1543 |
(errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED), |
| 1544 |
errmsg("%s can only be called in a sql_drop event trigger function", |
| 1545 |
"pg_event_trigger_dropped_objects()"))); |
| 1546 |
|
| 1547 |
/* Build tuplestore to hold the result rows */ |
| 1548 |
InitMaterializedSRF(fcinfo, 0); |
| 1549 |
|
| 1550 |
slist_foreach(iter, &(currentEventTriggerState->SQLDropList)) |
| 1551 |
{ |
| 1552 |
SQLDropObject *obj; |
| 1553 |
int i = 0; |
| 1554 |
Datum values[12] = {0}; |
| 1555 |
bool nulls[12] = {0}; |
| 1556 |
|
| 1557 |
obj = slist_container(SQLDropObject, next, iter.cur); |
| 1558 |
|
| 1559 |
/* classid */ |
| 1560 |
values[i++] = ObjectIdGetDatum(obj->address.classId); |
| 1561 |
|
| 1562 |
/* objid */ |
| 1563 |
values[i++] = ObjectIdGetDatum(obj->address.objectId); |
| 1564 |
|
| 1565 |
/* objsubid */ |
| 1566 |
values[i++] = Int32GetDatum(obj->address.objectSubId); |
| 1567 |
|
| 1568 |
/* original */ |
| 1569 |
values[i++] = BoolGetDatum(obj->original); |
| 1570 |
|
| 1571 |
/* normal */ |
| 1572 |
values[i++] = BoolGetDatum(obj->normal); |
| 1573 |
|
| 1574 |
/* is_temporary */ |
| 1575 |
values[i++] = BoolGetDatum(obj->istemp); |
| 1576 |
|
| 1577 |
/* object_type */ |
| 1578 |
values[i++] = CStringGetTextDatum(obj->objecttype); |
| 1579 |
|
| 1580 |
/* schema_name */ |
| 1581 |
if (obj->schemaname) |
| 1582 |
values[i++] = CStringGetTextDatum(obj->schemaname); |
| 1583 |
else |
| 1584 |
nulls[i++] = true; |
| 1585 |
|
| 1586 |
/* object_name */ |
| 1587 |
if (obj->objname) |
| 1588 |
values[i++] = CStringGetTextDatum(obj->objname); |
| 1589 |
else |
| 1590 |
nulls[i++] = true; |
| 1591 |
|
| 1592 |
/* object_identity */ |
| 1593 |
if (obj->objidentity) |
| 1594 |
values[i++] = CStringGetTextDatum(obj->objidentity); |
| 1595 |
else |
| 1596 |
nulls[i++] = true; |
| 1597 |
|
| 1598 |
/* address_names and address_args */ |
| 1599 |
if (obj->addrnames) |
| 1600 |
{ |
| 1601 |
values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrnames)); |
| 1602 |
|
| 1603 |
if (obj->addrargs) |
| 1604 |
values[i++] = PointerGetDatum(strlist_to_textarray(obj->addrargs)); |
| 1605 |
else |
| 1606 |
values[i++] = PointerGetDatum(construct_empty_array(TEXTOID)); |
| 1607 |
} |
| 1608 |
else |
| 1609 |
{ |
| 1610 |
nulls[i++] = true; |
| 1611 |
nulls[i++] = true; |
| 1612 |
} |
| 1613 |
|
| 1614 |
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, |
| 1615 |
values, nulls); |
| 1616 |
} |
| 1617 |
|
| 1618 |
return (Datum) 0; |
| 1619 |
} |
| 1620 |
|
| 1621 |
/* |
| 1622 |
* pg_event_trigger_table_rewrite_oid |
| 1623 |
* |
| 1624 |
* Make the Oid of the table going to be rewritten available to the user |
| 1625 |
* function run by the Event Trigger. |
| 1626 |
*/ |
| 1627 |
Datum |
| 1628 |
pg_event_trigger_table_rewrite_oid(PG_FUNCTION_ARGS) |
| 1629 |
{ |
| 1630 |
/* |
| 1631 |
* Protect this function from being called out of context |
| 1632 |
*/ |
| 1633 |
if (!currentEventTriggerState || |
| 1634 |
currentEventTriggerState->table_rewrite_oid == InvalidOid) |
| 1635 |
ereport(ERROR, |
| 1636 |
(errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED), |
| 1637 |
errmsg("%s can only be called in a table_rewrite event trigger function", |
| 1638 |
"pg_event_trigger_table_rewrite_oid()"))); |
| 1639 |
|
| 1640 |
PG_RETURN_OID(currentEventTriggerState->table_rewrite_oid); |
| 1641 |
} |
| 1642 |
|
| 1643 |
/* |
| 1644 |
* pg_event_trigger_table_rewrite_reason |
| 1645 |
* |
| 1646 |
* Make the rewrite reason available to the user. |
| 1647 |
*/ |
| 1648 |
Datum |
| 1649 |
pg_event_trigger_table_rewrite_reason(PG_FUNCTION_ARGS) |
| 1650 |
{ |
| 1651 |
/* |
| 1652 |
* Protect this function from being called out of context |
| 1653 |
*/ |
| 1654 |
if (!currentEventTriggerState || |
| 1655 |
currentEventTriggerState->table_rewrite_reason == 0) |
| 1656 |
ereport(ERROR, |
| 1657 |
(errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED), |
| 1658 |
errmsg("%s can only be called in a table_rewrite event trigger function", |
| 1659 |
"pg_event_trigger_table_rewrite_reason()"))); |
| 1660 |
|
| 1661 |
PG_RETURN_INT32(currentEventTriggerState->table_rewrite_reason); |
| 1662 |
} |
| 1663 |
|
| 1664 |
/*------------------------------------------------------------------------- |
| 1665 |
* Support for DDL command deparsing |
| 1666 |
* |
| 1667 |
* The routines below enable an event trigger function to obtain a list of |
| 1668 |
* DDL commands as they are executed. There are three main pieces to this |
| 1669 |
* feature: |
| 1670 |
* |
| 1671 |
* 1) Within ProcessUtilitySlow, or some sub-routine thereof, each DDL command |
| 1672 |
* adds a struct CollectedCommand representation of itself to the command list, |
| 1673 |
* using the routines below. |
| 1674 |
* |
| 1675 |
* 2) Some time after that, ddl_command_end fires and the command list is made |
| 1676 |
* available to the event trigger function via pg_event_trigger_ddl_commands(); |
| 1677 |
* the complete command details are exposed as a column of type pg_ddl_command. |
| 1678 |
* |
| 1679 |
* 3) An extension can install a function capable of taking a value of type |
| 1680 |
* pg_ddl_command and transform it into some external, user-visible and/or |
| 1681 |
* -modifiable representation. |
| 1682 |
*------------------------------------------------------------------------- |
| 1683 |
*/ |
| 1684 |
|
| 1685 |
/* |
| 1686 |
* Inhibit DDL command collection. |
| 1687 |
*/ |
| 1688 |
void |
| 1689 |
EventTriggerInhibitCommandCollection(void) |
| 1690 |
{ |
| 1691 |
if (!currentEventTriggerState) |
| 1692 |
return; |
| 1693 |
|
| 1694 |
currentEventTriggerState->commandCollectionInhibited = true; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* |
| 1698 |
* Re-establish DDL command collection. |
| 1699 |
*/ |
| 1700 |
void |
| 1701 |
EventTriggerUndoInhibitCommandCollection(void) |
| 1702 |
{ |
| 1703 |
if (!currentEventTriggerState) |
| 1704 |
return; |
| 1705 |
|
| 1706 |
currentEventTriggerState->commandCollectionInhibited = false; |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* |
| 1710 |
* EventTriggerCollectSimpleCommand |
| 1711 |
* Save data about a simple DDL command that was just executed |
| 1712 |
* |
| 1713 |
* address identifies the object being operated on. secondaryObject is an |
| 1714 |
* object address that was related in some way to the executed command; its |
| 1715 |
* meaning is command-specific. |
| 1716 |
* |
| 1717 |
* For instance, for an ALTER obj SET SCHEMA command, objtype is the type of |
| 1718 |
* object being moved, objectId is its OID, and secondaryOid is the OID of the |
| 1719 |
* old schema. (The destination schema OID can be obtained by catalog lookup |
| 1720 |
* of the object.) |
| 1721 |
*/ |
| 1722 |
void |
| 1723 |
EventTriggerCollectSimpleCommand(ObjectAddress address, |
| 1724 |
ObjectAddress secondaryObject, |
| 1725 |
Node *parsetree) |
| 1726 |
{ |
| 1727 |
MemoryContext oldcxt; |
| 1728 |
CollectedCommand *command; |
| 1729 |
|
| 1730 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1731 |
if (!currentEventTriggerState || |
| 1732 |
currentEventTriggerState->commandCollectionInhibited) |
| 1733 |
return; |
| 1734 |
|
| 1735 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1736 |
|
| 1737 |
command = palloc(sizeof(CollectedCommand)); |
| 1738 |
|
| 1739 |
command->type = SCT_Simple; |
| 1740 |
command->in_extension = creating_extension; |
| 1741 |
|
| 1742 |
command->d.simple.address = address; |
| 1743 |
command->d.simple.secondaryObject = secondaryObject; |
| 1744 |
command->parsetree = copyObject(parsetree); |
| 1745 |
|
| 1746 |
currentEventTriggerState->commandList = lappend(currentEventTriggerState->commandList, |
| 1747 |
command); |
| 1748 |
|
| 1749 |
MemoryContextSwitchTo(oldcxt); |
| 1750 |
} |
| 1751 |
|
| 1752 |
/* |
| 1753 |
* EventTriggerAlterTableStart |
| 1754 |
* Prepare to receive data on an ALTER TABLE command about to be executed |
| 1755 |
* |
| 1756 |
* Note we don't collect the command immediately; instead we keep it in |
| 1757 |
* currentCommand, and only when we're done processing the subcommands we will |
| 1758 |
* add it to the command list. |
| 1759 |
*/ |
| 1760 |
void |
| 1761 |
EventTriggerAlterTableStart(Node *parsetree) |
| 1762 |
{ |
| 1763 |
MemoryContext oldcxt; |
| 1764 |
CollectedCommand *command; |
| 1765 |
|
| 1766 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1767 |
if (!currentEventTriggerState || |
| 1768 |
currentEventTriggerState->commandCollectionInhibited) |
| 1769 |
return; |
| 1770 |
|
| 1771 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1772 |
|
| 1773 |
command = palloc(sizeof(CollectedCommand)); |
| 1774 |
|
| 1775 |
command->type = SCT_AlterTable; |
| 1776 |
command->in_extension = creating_extension; |
| 1777 |
|
| 1778 |
command->d.alterTable.classId = RelationRelationId; |
| 1779 |
command->d.alterTable.objectId = InvalidOid; |
| 1780 |
command->d.alterTable.subcmds = NIL; |
| 1781 |
command->parsetree = copyObject(parsetree); |
| 1782 |
|
| 1783 |
command->parent = currentEventTriggerState->currentCommand; |
| 1784 |
currentEventTriggerState->currentCommand = command; |
| 1785 |
|
| 1786 |
MemoryContextSwitchTo(oldcxt); |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* |
| 1790 |
* Remember the OID of the object being affected by an ALTER TABLE. |
| 1791 |
* |
| 1792 |
* This is needed because in some cases we don't know the OID until later. |
| 1793 |
*/ |
| 1794 |
void |
| 1795 |
EventTriggerAlterTableRelid(Oid objectId) |
| 1796 |
{ |
| 1797 |
if (!currentEventTriggerState || |
| 1798 |
currentEventTriggerState->commandCollectionInhibited) |
| 1799 |
return; |
| 1800 |
|
| 1801 |
currentEventTriggerState->currentCommand->d.alterTable.objectId = objectId; |
| 1802 |
} |
| 1803 |
|
| 1804 |
/* |
| 1805 |
* EventTriggerCollectAlterTableSubcmd |
| 1806 |
* Save data about a single part of an ALTER TABLE. |
| 1807 |
* |
| 1808 |
* Several different commands go through this path, but apart from ALTER TABLE |
| 1809 |
* itself, they are all concerned with AlterTableCmd nodes that are generated |
| 1810 |
* internally, so that's all that this code needs to handle at the moment. |
| 1811 |
*/ |
| 1812 |
void |
| 1813 |
EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address) |
| 1814 |
{ |
| 1815 |
MemoryContext oldcxt; |
| 1816 |
CollectedATSubcmd *newsub; |
| 1817 |
|
| 1818 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1819 |
if (!currentEventTriggerState || |
| 1820 |
currentEventTriggerState->commandCollectionInhibited) |
| 1821 |
return; |
| 1822 |
|
| 1823 |
Assert(IsA(subcmd, AlterTableCmd)); |
| 1824 |
Assert(currentEventTriggerState->currentCommand != NULL); |
| 1825 |
Assert(OidIsValid(currentEventTriggerState->currentCommand->d.alterTable.objectId)); |
| 1826 |
|
| 1827 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1828 |
|
| 1829 |
newsub = palloc(sizeof(CollectedATSubcmd)); |
| 1830 |
newsub->address = address; |
| 1831 |
newsub->parsetree = copyObject(subcmd); |
| 1832 |
|
| 1833 |
currentEventTriggerState->currentCommand->d.alterTable.subcmds = |
| 1834 |
lappend(currentEventTriggerState->currentCommand->d.alterTable.subcmds, newsub); |
| 1835 |
|
| 1836 |
MemoryContextSwitchTo(oldcxt); |
| 1837 |
} |
| 1838 |
|
| 1839 |
/* |
| 1840 |
* EventTriggerAlterTableEnd |
| 1841 |
* Finish up saving an ALTER TABLE command, and add it to command list. |
| 1842 |
* |
| 1843 |
* FIXME this API isn't considering the possibility that an xact/subxact is |
| 1844 |
* aborted partway through. Probably it's best to add an |
| 1845 |
* AtEOSubXact_EventTriggers() to fix this. |
| 1846 |
*/ |
| 1847 |
void |
| 1848 |
EventTriggerAlterTableEnd(void) |
| 1849 |
{ |
| 1850 |
CollectedCommand *parent; |
| 1851 |
|
| 1852 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1853 |
if (!currentEventTriggerState || |
| 1854 |
currentEventTriggerState->commandCollectionInhibited) |
| 1855 |
return; |
| 1856 |
|
| 1857 |
parent = currentEventTriggerState->currentCommand->parent; |
| 1858 |
|
| 1859 |
/* If no subcommands, don't collect */ |
| 1860 |
if (currentEventTriggerState->currentCommand->d.alterTable.subcmds != NIL) |
| 1861 |
{ |
| 1862 |
MemoryContext oldcxt; |
| 1863 |
|
| 1864 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1865 |
|
| 1866 |
currentEventTriggerState->commandList = |
| 1867 |
lappend(currentEventTriggerState->commandList, |
| 1868 |
currentEventTriggerState->currentCommand); |
| 1869 |
|
| 1870 |
MemoryContextSwitchTo(oldcxt); |
| 1871 |
} |
| 1872 |
else |
| 1873 |
pfree(currentEventTriggerState->currentCommand); |
| 1874 |
|
| 1875 |
currentEventTriggerState->currentCommand = parent; |
| 1876 |
} |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* EventTriggerCollectGrant |
| 1880 |
* Save data about a GRANT/REVOKE command being executed |
| 1881 |
* |
| 1882 |
* This function creates a copy of the InternalGrant, as the original might |
| 1883 |
* not have the right lifetime. |
| 1884 |
*/ |
| 1885 |
void |
| 1886 |
EventTriggerCollectGrant(InternalGrant *istmt) |
| 1887 |
{ |
| 1888 |
MemoryContext oldcxt; |
| 1889 |
CollectedCommand *command; |
| 1890 |
InternalGrant *icopy; |
| 1891 |
ListCell *cell; |
| 1892 |
|
| 1893 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1894 |
if (!currentEventTriggerState || |
| 1895 |
currentEventTriggerState->commandCollectionInhibited) |
| 1896 |
return; |
| 1897 |
|
| 1898 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1899 |
|
| 1900 |
/* |
| 1901 |
* This is tedious, but necessary. |
| 1902 |
*/ |
| 1903 |
icopy = palloc(sizeof(InternalGrant)); |
| 1904 |
memcpy(icopy, istmt, sizeof(InternalGrant)); |
| 1905 |
icopy->objects = list_copy(istmt->objects); |
| 1906 |
icopy->grantees = list_copy(istmt->grantees); |
| 1907 |
icopy->col_privs = NIL; |
| 1908 |
foreach(cell, istmt->col_privs) |
| 1909 |
icopy->col_privs = lappend(icopy->col_privs, copyObject(lfirst(cell))); |
| 1910 |
|
| 1911 |
/* Now collect it, using the copied InternalGrant */ |
| 1912 |
command = palloc(sizeof(CollectedCommand)); |
| 1913 |
command->type = SCT_Grant; |
| 1914 |
command->in_extension = creating_extension; |
| 1915 |
command->d.grant.istmt = icopy; |
| 1916 |
command->parsetree = NULL; |
| 1917 |
|
| 1918 |
currentEventTriggerState->commandList = |
| 1919 |
lappend(currentEventTriggerState->commandList, command); |
| 1920 |
|
| 1921 |
MemoryContextSwitchTo(oldcxt); |
| 1922 |
} |
| 1923 |
|
| 1924 |
/* |
| 1925 |
* EventTriggerCollectAlterOpFam |
| 1926 |
* Save data about an ALTER OPERATOR FAMILY ADD/DROP command being |
| 1927 |
* executed |
| 1928 |
*/ |
| 1929 |
void |
| 1930 |
EventTriggerCollectAlterOpFam(AlterOpFamilyStmt *stmt, Oid opfamoid, |
| 1931 |
List *operators, List *procedures) |
| 1932 |
{ |
| 1933 |
MemoryContext oldcxt; |
| 1934 |
CollectedCommand *command; |
| 1935 |
|
| 1936 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1937 |
if (!currentEventTriggerState || |
| 1938 |
currentEventTriggerState->commandCollectionInhibited) |
| 1939 |
return; |
| 1940 |
|
| 1941 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1942 |
|
| 1943 |
command = palloc(sizeof(CollectedCommand)); |
| 1944 |
command->type = SCT_AlterOpFamily; |
| 1945 |
command->in_extension = creating_extension; |
| 1946 |
ObjectAddressSet(command->d.opfam.address, |
| 1947 |
OperatorFamilyRelationId, opfamoid); |
| 1948 |
command->d.opfam.operators = operators; |
| 1949 |
command->d.opfam.procedures = procedures; |
| 1950 |
command->parsetree = (Node *) copyObject(stmt); |
| 1951 |
|
| 1952 |
currentEventTriggerState->commandList = |
| 1953 |
lappend(currentEventTriggerState->commandList, command); |
| 1954 |
|
| 1955 |
MemoryContextSwitchTo(oldcxt); |
| 1956 |
} |
| 1957 |
|
| 1958 |
/* |
| 1959 |
* EventTriggerCollectCreateOpClass |
| 1960 |
* Save data about a CREATE OPERATOR CLASS command being executed |
| 1961 |
*/ |
| 1962 |
void |
| 1963 |
EventTriggerCollectCreateOpClass(CreateOpClassStmt *stmt, Oid opcoid, |
| 1964 |
List *operators, List *procedures) |
| 1965 |
{ |
| 1966 |
MemoryContext oldcxt; |
| 1967 |
CollectedCommand *command; |
| 1968 |
|
| 1969 |
/* ignore if event trigger context not set, or collection disabled */ |
| 1970 |
if (!currentEventTriggerState || |
| 1971 |
currentEventTriggerState->commandCollectionInhibited) |
| 1972 |
return; |
| 1973 |
|
| 1974 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 1975 |
|
| 1976 |
command = palloc0(sizeof(CollectedCommand)); |
| 1977 |
command->type = SCT_CreateOpClass; |
| 1978 |
command->in_extension = creating_extension; |
| 1979 |
ObjectAddressSet(command->d.createopc.address, |
| 1980 |
OperatorClassRelationId, opcoid); |
| 1981 |
command->d.createopc.operators = operators; |
| 1982 |
command->d.createopc.procedures = procedures; |
| 1983 |
command->parsetree = (Node *) copyObject(stmt); |
| 1984 |
|
| 1985 |
currentEventTriggerState->commandList = |
| 1986 |
lappend(currentEventTriggerState->commandList, command); |
| 1987 |
|
| 1988 |
MemoryContextSwitchTo(oldcxt); |
| 1989 |
} |
| 1990 |
|
| 1991 |
/* |
| 1992 |
* EventTriggerCollectAlterTSConfig |
| 1993 |
* Save data about an ALTER TEXT SEARCH CONFIGURATION command being |
| 1994 |
* executed |
| 1995 |
*/ |
| 1996 |
void |
| 1997 |
EventTriggerCollectAlterTSConfig(AlterTSConfigurationStmt *stmt, Oid cfgId, |
| 1998 |
Oid *dictIds, int ndicts) |
| 1999 |
{ |
| 2000 |
MemoryContext oldcxt; |
| 2001 |
CollectedCommand *command; |
| 2002 |
|
| 2003 |
/* ignore if event trigger context not set, or collection disabled */ |
| 2004 |
if (!currentEventTriggerState || |
| 2005 |
currentEventTriggerState->commandCollectionInhibited) |
| 2006 |
return; |
| 2007 |
|
| 2008 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 2009 |
|
| 2010 |
command = palloc0(sizeof(CollectedCommand)); |
| 2011 |
command->type = SCT_AlterTSConfig; |
| 2012 |
command->in_extension = creating_extension; |
| 2013 |
ObjectAddressSet(command->d.atscfg.address, |
| 2014 |
TSConfigRelationId, cfgId); |
| 2015 |
if (ndicts > 0) |
| 2016 |
{ |
| 2017 |
command->d.atscfg.dictIds = palloc_array(Oid, ndicts); |
| 2018 |
memcpy(command->d.atscfg.dictIds, dictIds, sizeof(Oid) * ndicts); |
| 2019 |
} |
| 2020 |
command->d.atscfg.ndicts = ndicts; |
| 2021 |
command->parsetree = (Node *) copyObject(stmt); |
| 2022 |
|
| 2023 |
currentEventTriggerState->commandList = |
| 2024 |
lappend(currentEventTriggerState->commandList, command); |
| 2025 |
|
| 2026 |
MemoryContextSwitchTo(oldcxt); |
| 2027 |
} |
| 2028 |
|
| 2029 |
/* |
| 2030 |
* EventTriggerCollectAlterDefPrivs |
| 2031 |
* Save data about an ALTER DEFAULT PRIVILEGES command being |
| 2032 |
* executed |
| 2033 |
*/ |
| 2034 |
void |
| 2035 |
EventTriggerCollectAlterDefPrivs(AlterDefaultPrivilegesStmt *stmt) |
| 2036 |
{ |
| 2037 |
MemoryContext oldcxt; |
| 2038 |
CollectedCommand *command; |
| 2039 |
|
| 2040 |
/* ignore if event trigger context not set, or collection disabled */ |
| 2041 |
if (!currentEventTriggerState || |
| 2042 |
currentEventTriggerState->commandCollectionInhibited) |
| 2043 |
return; |
| 2044 |
|
| 2045 |
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); |
| 2046 |
|
| 2047 |
command = palloc0(sizeof(CollectedCommand)); |
| 2048 |
command->type = SCT_AlterDefaultPrivileges; |
| 2049 |
command->d.defprivs.objtype = stmt->action->objtype; |
| 2050 |
command->in_extension = creating_extension; |
| 2051 |
command->parsetree = (Node *) copyObject(stmt); |
| 2052 |
|
| 2053 |
currentEventTriggerState->commandList = |
| 2054 |
lappend(currentEventTriggerState->commandList, command); |
| 2055 |
MemoryContextSwitchTo(oldcxt); |
| 2056 |
} |
| 2057 |
|
| 2058 |
/* |
| 2059 |
* In a ddl_command_end event trigger, this function reports the DDL commands |
| 2060 |
* being run. |
| 2061 |
*/ |
| 2062 |
Datum |
| 2063 |
pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS) |
| 2064 |
{ |
| 2065 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 2066 |
ListCell *lc; |
| 2067 |
|
| 2068 |
/* |
| 2069 |
* Protect this function from being called out of context |
| 2070 |
*/ |
| 2071 |
if (!currentEventTriggerState) |
| 2072 |
ereport(ERROR, |
| 2073 |
(errcode(ERRCODE_E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED), |
| 2074 |
errmsg("%s can only be called in an event trigger function", |
| 2075 |
"pg_event_trigger_ddl_commands()"))); |
| 2076 |
|
| 2077 |
/* Build tuplestore to hold the result rows */ |
| 2078 |
InitMaterializedSRF(fcinfo, 0); |
| 2079 |
|
| 2080 |
foreach(lc, currentEventTriggerState->commandList) |
| 2081 |
{ |
| 2082 |
CollectedCommand *cmd = lfirst(lc); |
| 2083 |
Datum values[9]; |
| 2084 |
bool nulls[9] = {0}; |
| 2085 |
ObjectAddress addr; |
| 2086 |
int i = 0; |
| 2087 |
|
| 2088 |
/* |
| 2089 |
* For IF NOT EXISTS commands that attempt to create an existing |
| 2090 |
* object, the returned OID is Invalid. Don't return anything. |
| 2091 |
* |
| 2092 |
* One might think that a viable alternative would be to look up the |
| 2093 |
* Oid of the existing object and run the deparse with that. But |
| 2094 |
* since the parse tree might be different from the one that created |
| 2095 |
* the object in the first place, we might not end up in a consistent |
| 2096 |
* state anyway. |
| 2097 |
*/ |
| 2098 |
if (cmd->type == SCT_Simple && |
| 2099 |
!OidIsValid(cmd->d.simple.address.objectId)) |
| 2100 |
continue; |
| 2101 |
|
| 2102 |
switch (cmd->type) |
| 2103 |
{ |
| 2104 |
case SCT_Simple: |
| 2105 |
case SCT_AlterTable: |
| 2106 |
case SCT_AlterOpFamily: |
| 2107 |
case SCT_CreateOpClass: |
| 2108 |
case SCT_AlterTSConfig: |
| 2109 |
{ |
| 2110 |
char *identity; |
| 2111 |
char *type; |
| 2112 |
char *schema = NULL; |
| 2113 |
|
| 2114 |
if (cmd->type == SCT_Simple) |
| 2115 |
addr = cmd->d.simple.address; |
| 2116 |
else if (cmd->type == SCT_AlterTable) |
| 2117 |
ObjectAddressSet(addr, |
| 2118 |
cmd->d.alterTable.classId, |
| 2119 |
cmd->d.alterTable.objectId); |
| 2120 |
else if (cmd->type == SCT_AlterOpFamily) |
| 2121 |
addr = cmd->d.opfam.address; |
| 2122 |
else if (cmd->type == SCT_CreateOpClass) |
| 2123 |
addr = cmd->d.createopc.address; |
| 2124 |
else if (cmd->type == SCT_AlterTSConfig) |
| 2125 |
addr = cmd->d.atscfg.address; |
| 2126 |
|
| 2127 |
/* |
| 2128 |
* If an object was dropped in the same command we may end |
| 2129 |
* up in a situation where we generated a message but can |
| 2130 |
* no longer look for the object information, so skip it |
| 2131 |
* rather than failing. This can happen for example with |
| 2132 |
* some subcommand combinations of ALTER TABLE. |
| 2133 |
*/ |
| 2134 |
identity = getObjectIdentity(&addr, true); |
| 2135 |
if (identity == NULL) |
| 2136 |
continue; |
| 2137 |
|
| 2138 |
/* The type can never be NULL. */ |
| 2139 |
type = getObjectTypeDescription(&addr, true); |
| 2140 |
|
| 2141 |
/* |
| 2142 |
* Obtain schema name, if any ("pg_temp" if a temp |
| 2143 |
* object). If the object class is not in the supported |
| 2144 |
* list here, we assume it's a schema-less object type, |
| 2145 |
* and thus "schema" remains set to NULL. |
| 2146 |
*/ |
| 2147 |
if (is_objectclass_supported(addr.classId)) |
| 2148 |
{ |
| 2149 |
AttrNumber nspAttnum; |
| 2150 |
|
| 2151 |
nspAttnum = get_object_attnum_namespace(addr.classId); |
| 2152 |
if (nspAttnum != InvalidAttrNumber) |
| 2153 |
{ |
| 2154 |
Relation catalog; |
| 2155 |
HeapTuple objtup; |
| 2156 |
Oid schema_oid; |
| 2157 |
bool isnull; |
| 2158 |
|
| 2159 |
catalog = table_open(addr.classId, AccessShareLock); |
| 2160 |
objtup = get_catalog_object_by_oid(catalog, |
| 2161 |
get_object_attnum_oid(addr.classId), |
| 2162 |
addr.objectId); |
| 2163 |
if (!HeapTupleIsValid(objtup)) |
| 2164 |
elog(ERROR, "cache lookup failed for object %u/%u", |
| 2165 |
addr.classId, addr.objectId); |
| 2166 |
schema_oid = |
| 2167 |
heap_getattr(objtup, nspAttnum, |
| 2168 |
RelationGetDescr(catalog), &isnull); |
| 2169 |
if (isnull) |
| 2170 |
elog(ERROR, |
| 2171 |
"invalid null namespace in object %u/%u/%d", |
| 2172 |
addr.classId, addr.objectId, addr.objectSubId); |
| 2173 |
schema = get_namespace_name_or_temp(schema_oid); |
| 2174 |
|
| 2175 |
table_close(catalog, AccessShareLock); |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
/* classid */ |
| 2180 |
values[i++] = ObjectIdGetDatum(addr.classId); |
| 2181 |
/* objid */ |
| 2182 |
values[i++] = ObjectIdGetDatum(addr.objectId); |
| 2183 |
/* objsubid */ |
| 2184 |
values[i++] = Int32GetDatum(addr.objectSubId); |
| 2185 |
/* command tag */ |
| 2186 |
values[i++] = CStringGetTextDatum(CreateCommandName(cmd->parsetree)); |
| 2187 |
/* object_type */ |
| 2188 |
values[i++] = CStringGetTextDatum(type); |
| 2189 |
/* schema */ |
| 2190 |
if (schema == NULL) |
| 2191 |
nulls[i++] = true; |
| 2192 |
else |
| 2193 |
values[i++] = CStringGetTextDatum(schema); |
| 2194 |
/* identity */ |
| 2195 |
values[i++] = CStringGetTextDatum(identity); |
| 2196 |
/* in_extension */ |
| 2197 |
values[i++] = BoolGetDatum(cmd->in_extension); |
| 2198 |
/* command */ |
| 2199 |
values[i++] = PointerGetDatum(cmd); |
| 2200 |
} |
| 2201 |
break; |
| 2202 |
|
| 2203 |
case SCT_AlterDefaultPrivileges: |
| 2204 |
/* classid */ |
| 2205 |
nulls[i++] = true; |
| 2206 |
/* objid */ |
| 2207 |
nulls[i++] = true; |
| 2208 |
/* objsubid */ |
| 2209 |
nulls[i++] = true; |
| 2210 |
/* command tag */ |
| 2211 |
values[i++] = CStringGetTextDatum(CreateCommandName(cmd->parsetree)); |
| 2212 |
/* object_type */ |
| 2213 |
values[i++] = CStringGetTextDatum(stringify_adefprivs_objtype(cmd->d.defprivs.objtype)); |
| 2214 |
/* schema */ |
| 2215 |
nulls[i++] = true; |
| 2216 |
/* identity */ |
| 2217 |
nulls[i++] = true; |
| 2218 |
/* in_extension */ |
| 2219 |
values[i++] = BoolGetDatum(cmd->in_extension); |
| 2220 |
/* command */ |
| 2221 |
values[i++] = PointerGetDatum(cmd); |
| 2222 |
break; |
| 2223 |
|
| 2224 |
case SCT_Grant: |
| 2225 |
/* classid */ |
| 2226 |
nulls[i++] = true; |
| 2227 |
/* objid */ |
| 2228 |
nulls[i++] = true; |
| 2229 |
/* objsubid */ |
| 2230 |
nulls[i++] = true; |
| 2231 |
/* command tag */ |
| 2232 |
values[i++] = CStringGetTextDatum(cmd->d.grant.istmt->is_grant ? |
| 2233 |
"GRANT" : "REVOKE"); |
| 2234 |
/* object_type */ |
| 2235 |
values[i++] = CStringGetTextDatum(stringify_grant_objtype(cmd->d.grant.istmt->objtype)); |
| 2236 |
/* schema */ |
| 2237 |
nulls[i++] = true; |
| 2238 |
/* identity */ |
| 2239 |
nulls[i++] = true; |
| 2240 |
/* in_extension */ |
| 2241 |
values[i++] = BoolGetDatum(cmd->in_extension); |
| 2242 |
/* command */ |
| 2243 |
values[i++] = PointerGetDatum(cmd); |
| 2244 |
break; |
| 2245 |
} |
| 2246 |
|
| 2247 |
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, |
| 2248 |
values, nulls); |
| 2249 |
} |
| 2250 |
|
| 2251 |
PG_RETURN_VOID(); |
| 2252 |
} |
| 2253 |
|
| 2254 |
/* |
| 2255 |
* Return the ObjectType as a string, as it would appear in GRANT and |
| 2256 |
* REVOKE commands. |
| 2257 |
*/ |
| 2258 |
static const char * |
| 2259 |
stringify_grant_objtype(ObjectType objtype) |
| 2260 |
{ |
| 2261 |
switch (objtype) |
| 2262 |
{ |
| 2263 |
case OBJECT_COLUMN: |
| 2264 |
return "COLUMN"; |
| 2265 |
case OBJECT_TABLE: |
| 2266 |
return "TABLE"; |
| 2267 |
case OBJECT_SEQUENCE: |
| 2268 |
return "SEQUENCE"; |
| 2269 |
case OBJECT_DATABASE: |
| 2270 |
return "DATABASE"; |
| 2271 |
case OBJECT_DOMAIN: |
| 2272 |
return "DOMAIN"; |
| 2273 |
case OBJECT_FDW: |
| 2274 |
return "FOREIGN DATA WRAPPER"; |
| 2275 |
case OBJECT_FOREIGN_SERVER: |
| 2276 |
return "FOREIGN SERVER"; |
| 2277 |
case OBJECT_FUNCTION: |
| 2278 |
return "FUNCTION"; |
| 2279 |
case OBJECT_LANGUAGE: |
| 2280 |
return "LANGUAGE"; |
| 2281 |
case OBJECT_LARGEOBJECT: |
| 2282 |
return "LARGE OBJECT"; |
| 2283 |
case OBJECT_SCHEMA: |
| 2284 |
return "SCHEMA"; |
| 2285 |
case OBJECT_PARAMETER_ACL: |
| 2286 |
return "PARAMETER"; |
| 2287 |
case OBJECT_PROCEDURE: |
| 2288 |
return "PROCEDURE"; |
| 2289 |
case OBJECT_ROUTINE: |
| 2290 |
return "ROUTINE"; |
| 2291 |
case OBJECT_TABLESPACE: |
| 2292 |
return "TABLESPACE"; |
| 2293 |
case OBJECT_TYPE: |
| 2294 |
return "TYPE"; |
| 2295 |
/* these currently aren't used */ |
| 2296 |
case OBJECT_ACCESS_METHOD: |
| 2297 |
case OBJECT_AGGREGATE: |
| 2298 |
case OBJECT_AMOP: |
| 2299 |
case OBJECT_AMPROC: |
| 2300 |
case OBJECT_ATTRIBUTE: |
| 2301 |
case OBJECT_CAST: |
| 2302 |
case OBJECT_COLLATION: |
| 2303 |
case OBJECT_CONVERSION: |
| 2304 |
case OBJECT_DEFAULT: |
| 2305 |
case OBJECT_DEFACL: |
| 2306 |
case OBJECT_DOMCONSTRAINT: |
| 2307 |
case OBJECT_EVENT_TRIGGER: |
| 2308 |
case OBJECT_EXTENSION: |
| 2309 |
case OBJECT_FOREIGN_TABLE: |
| 2310 |
case OBJECT_INDEX: |
| 2311 |
case OBJECT_MATVIEW: |
| 2312 |
case OBJECT_OPCLASS: |
| 2313 |
case OBJECT_OPERATOR: |
| 2314 |
case OBJECT_OPFAMILY: |
| 2315 |
case OBJECT_POLICY: |
| 2316 |
case OBJECT_PUBLICATION: |
| 2317 |
case OBJECT_PUBLICATION_NAMESPACE: |
| 2318 |
case OBJECT_PUBLICATION_REL: |
| 2319 |
case OBJECT_ROLE: |
| 2320 |
case OBJECT_RULE: |
| 2321 |
case OBJECT_STATISTIC_EXT: |
| 2322 |
case OBJECT_SUBSCRIPTION: |
| 2323 |
case OBJECT_TABCONSTRAINT: |
| 2324 |
case OBJECT_TRANSFORM: |
| 2325 |
case OBJECT_TRIGGER: |
| 2326 |
case OBJECT_TSCONFIGURATION: |
| 2327 |
case OBJECT_TSDICTIONARY: |
| 2328 |
case OBJECT_TSPARSER: |
| 2329 |
case OBJECT_TSTEMPLATE: |
| 2330 |
case OBJECT_USER_MAPPING: |
| 2331 |
case OBJECT_VIEW: |
| 2332 |
elog(ERROR, "unsupported object type: %d", (int) objtype); |
| 2333 |
} |
| 2334 |
|
| 2335 |
return "???"; /* keep compiler quiet */ |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* |
| 2339 |
* Return the ObjectType as a string; as above, but use the spelling |
| 2340 |
* in ALTER DEFAULT PRIVILEGES commands instead. Generally this is just |
| 2341 |
* the plural. |
| 2342 |
*/ |
| 2343 |
static const char * |
| 2344 |
stringify_adefprivs_objtype(ObjectType objtype) |
| 2345 |
{ |
| 2346 |
switch (objtype) |
| 2347 |
{ |
| 2348 |
case OBJECT_COLUMN: |
| 2349 |
return "COLUMNS"; |
| 2350 |
case OBJECT_TABLE: |
| 2351 |
return "TABLES"; |
| 2352 |
case OBJECT_SEQUENCE: |
| 2353 |
return "SEQUENCES"; |
| 2354 |
case OBJECT_DATABASE: |
| 2355 |
return "DATABASES"; |
| 2356 |
case OBJECT_DOMAIN: |
| 2357 |
return "DOMAINS"; |
| 2358 |
case OBJECT_FDW: |
| 2359 |
return "FOREIGN DATA WRAPPERS"; |
| 2360 |
case OBJECT_FOREIGN_SERVER: |
| 2361 |
return "FOREIGN SERVERS"; |
| 2362 |
case OBJECT_FUNCTION: |
| 2363 |
return "FUNCTIONS"; |
| 2364 |
case OBJECT_LANGUAGE: |
| 2365 |
return "LANGUAGES"; |
| 2366 |
case OBJECT_LARGEOBJECT: |
| 2367 |
return "LARGE OBJECTS"; |
| 2368 |
case OBJECT_SCHEMA: |
| 2369 |
return "SCHEMAS"; |
| 2370 |
case OBJECT_PROCEDURE: |
| 2371 |
return "PROCEDURES"; |
| 2372 |
case OBJECT_ROUTINE: |
| 2373 |
return "ROUTINES"; |
| 2374 |
case OBJECT_TABLESPACE: |
| 2375 |
return "TABLESPACES"; |
| 2376 |
case OBJECT_TYPE: |
| 2377 |
return "TYPES"; |
| 2378 |
/* these currently aren't used */ |
| 2379 |
case OBJECT_ACCESS_METHOD: |
| 2380 |
case OBJECT_AGGREGATE: |
| 2381 |
case OBJECT_AMOP: |
| 2382 |
case OBJECT_AMPROC: |
| 2383 |
case OBJECT_ATTRIBUTE: |
| 2384 |
case OBJECT_CAST: |
| 2385 |
case OBJECT_COLLATION: |
| 2386 |
case OBJECT_CONVERSION: |
| 2387 |
case OBJECT_DEFAULT: |
| 2388 |
case OBJECT_DEFACL: |
| 2389 |
case OBJECT_DOMCONSTRAINT: |
| 2390 |
case OBJECT_EVENT_TRIGGER: |
| 2391 |
case OBJECT_EXTENSION: |
| 2392 |
case OBJECT_FOREIGN_TABLE: |
| 2393 |
case OBJECT_INDEX: |
| 2394 |
case OBJECT_MATVIEW: |
| 2395 |
case OBJECT_OPCLASS: |
| 2396 |
case OBJECT_OPERATOR: |
| 2397 |
case OBJECT_OPFAMILY: |
| 2398 |
case OBJECT_PARAMETER_ACL: |
| 2399 |
case OBJECT_POLICY: |
| 2400 |
case OBJECT_PUBLICATION: |
| 2401 |
case OBJECT_PUBLICATION_NAMESPACE: |
| 2402 |
case OBJECT_PUBLICATION_REL: |
| 2403 |
case OBJECT_ROLE: |
| 2404 |
case OBJECT_RULE: |
| 2405 |
case OBJECT_STATISTIC_EXT: |
| 2406 |
case OBJECT_SUBSCRIPTION: |
| 2407 |
case OBJECT_TABCONSTRAINT: |
| 2408 |
case OBJECT_TRANSFORM: |
| 2409 |
case OBJECT_TRIGGER: |
| 2410 |
case OBJECT_TSCONFIGURATION: |
| 2411 |
case OBJECT_TSDICTIONARY: |
| 2412 |
case OBJECT_TSPARSER: |
| 2413 |
case OBJECT_TSTEMPLATE: |
| 2414 |
case OBJECT_USER_MAPPING: |
| 2415 |
case OBJECT_VIEW: |
| 2416 |
elog(ERROR, "unsupported object type: %d", (int) objtype); |
| 2417 |
} |
| 2418 |
|
| 2419 |
return "???"; /* keep compiler quiet */ |
| 2420 |
} |