From: Alvaro Herrera Date: Mon, 23 Feb 2015 17:22:42 +0000 (-0300) Subject: Support more commands in event triggers X-Git-Tag: REL9_5_ALPHA1~719 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=296f3a6053844089bc533630fffafaba8f016384;p=postgresql.git Support more commands in event triggers COMMENT, SECURITY LABEL, and GRANT/REVOKE now also fire ddl_command_start and ddl_command_end event triggers, when they operate on database-local objects. Reviewed-By: Michael Paquier, Andres Freund, Stephen Frost --- diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml index 156c4639ab9..04353eac3d7 100644 --- a/doc/src/sgml/event-trigger.sgml +++ b/doc/src/sgml/event-trigger.sgml @@ -36,7 +36,9 @@ The ddl_command_start event occurs just before the - execution of a CREATE, ALTER, or DROP + execution of a CREATE, ALTER, DROP, + SECURITY LABEL, + COMMENT, GRANT or REVOKE command. No check whether the affected object exists or doesn't exist is performed before the event trigger fires. As an exception, however, this event does not occur for @@ -123,14 +125,15 @@ Event Trigger Support by Command Tag - + - command tag + Command Tag ddl_command_start ddl_command_end sql_drop table_rewrite + Notes @@ -140,6 +143,7 @@ X-- + ALTER COLLATION @@ -147,6 +151,7 @@ X - - + ALTER CONVERSION @@ -154,6 +159,7 @@ X - - + ALTER DOMAIN @@ -161,6 +167,7 @@ X - - + ALTER EXTENSION @@ -168,6 +175,7 @@ X - - + ALTER FOREIGN DATA WRAPPER @@ -175,6 +183,7 @@ X - - + ALTER FOREIGN TABLE @@ -182,6 +191,7 @@ X X - + ALTER FUNCTION @@ -189,6 +199,7 @@ X - - + ALTER LANGUAGE @@ -196,6 +207,7 @@ X - - + ALTER OPERATOR @@ -203,6 +215,7 @@ X - - + ALTER OPERATOR CLASS @@ -210,6 +223,7 @@ X - - + ALTER OPERATOR FAMILY @@ -217,6 +231,7 @@ X - - + ALTER POLICY @@ -224,6 +239,7 @@ X - - + ALTER SCHEMA @@ -231,6 +247,7 @@ X - - + ALTER SEQUENCE @@ -238,6 +255,7 @@ X - - + ALTER SERVER @@ -245,6 +263,7 @@ X - - + ALTER TABLE @@ -252,6 +271,7 @@ X X X + ALTER TEXT SEARCH CONFIGURATION @@ -259,6 +279,7 @@ X - - + ALTER TEXT SEARCH DICTIONARY @@ -266,6 +287,7 @@ X - - + ALTER TEXT SEARCH PARSER @@ -273,6 +295,7 @@ X - - + ALTER TEXT SEARCH TEMPLATE @@ -280,6 +303,7 @@ X - - + ALTER TRIGGER @@ -287,6 +311,7 @@ X - - + ALTER TYPE @@ -294,6 +319,7 @@ X - - + ALTER USER MAPPING @@ -301,6 +327,7 @@ X - - + ALTER VIEW @@ -308,6 +335,7 @@ X - - + CREATE AGGREGATE @@ -315,6 +343,15 @@ X - - + + + + COMMENT + X + X + - + - + Only for local objects CREATE CAST @@ -322,6 +359,7 @@ X - - + CREATE COLLATION @@ -329,6 +367,7 @@ X - - + CREATE CONVERSION @@ -336,6 +375,7 @@ X - - + CREATE DOMAIN @@ -343,6 +383,7 @@ X - - + CREATE EXTENSION @@ -350,6 +391,7 @@ X - - + CREATE FOREIGN DATA WRAPPER @@ -357,6 +399,7 @@ X - - + CREATE FOREIGN TABLE @@ -364,6 +407,7 @@ X - - + CREATE FUNCTION @@ -371,6 +415,7 @@ X - - + CREATE INDEX @@ -378,6 +423,7 @@ X - - + CREATE LANGUAGE @@ -385,6 +431,7 @@ X - - + CREATE OPERATOR @@ -392,6 +439,7 @@ X - - + CREATE OPERATOR CLASS @@ -399,6 +447,7 @@ X - - + CREATE OPERATOR FAMILY @@ -406,6 +455,7 @@ X - - + CREATE POLICY @@ -413,6 +463,7 @@ X - - + CREATE RULE @@ -420,6 +471,7 @@ X - - + CREATE SCHEMA @@ -427,6 +479,7 @@ X - - + CREATE SEQUENCE @@ -434,6 +487,7 @@ X - - + CREATE SERVER @@ -441,6 +495,7 @@ X - - + CREATE TABLE @@ -448,6 +503,7 @@ X - - + CREATE TABLE AS @@ -455,6 +511,7 @@ X - - + CREATE TEXT SEARCH CONFIGURATION @@ -462,6 +519,7 @@ X - - + CREATE TEXT SEARCH DICTIONARY @@ -469,6 +527,7 @@ X - - + CREATE TEXT SEARCH PARSER @@ -476,6 +535,7 @@ X - - + CREATE TEXT SEARCH TEMPLATE @@ -483,6 +543,7 @@ X - - + CREATE TRIGGER @@ -490,6 +551,7 @@ X - - + CREATE TYPE @@ -504,6 +566,7 @@ X - - + CREATE VIEW @@ -511,6 +574,7 @@ X - - + DROP AGGREGATE @@ -518,6 +582,7 @@ X X - + DROP CAST @@ -525,6 +590,7 @@ X X - + DROP COLLATION @@ -532,6 +598,7 @@ X X - + DROP CONVERSION @@ -539,6 +606,7 @@ X X - + DROP DOMAIN @@ -546,6 +614,7 @@ X X - + DROP EXTENSION @@ -553,6 +622,7 @@ X X - + DROP FOREIGN DATA WRAPPER @@ -560,6 +630,7 @@ X X - + DROP FOREIGN TABLE @@ -567,6 +638,7 @@ X X - + DROP FUNCTION @@ -574,6 +646,7 @@ X X - + DROP INDEX @@ -581,6 +654,7 @@ X X - + DROP LANGUAGE @@ -588,6 +662,7 @@ X X - + DROP OPERATOR @@ -595,6 +670,7 @@ X X - + DROP OPERATOR CLASS @@ -602,6 +678,7 @@ X X - + DROP OPERATOR FAMILY @@ -609,6 +686,7 @@ X X - + DROP OWNED @@ -616,6 +694,7 @@ X X - + DROP POLICY @@ -623,6 +702,7 @@ X X - + DROP RULE @@ -630,6 +710,7 @@ X X - + DROP SCHEMA @@ -637,6 +718,7 @@ X X - + DROP SEQUENCE @@ -644,6 +726,7 @@ X X - + DROP SERVER @@ -651,6 +734,7 @@ X X - + DROP TABLE @@ -658,6 +742,7 @@ X X - + DROP TEXT SEARCH CONFIGURATION @@ -665,6 +750,7 @@ X X - + DROP TEXT SEARCH DICTIONARY @@ -672,6 +758,7 @@ X X - + DROP TEXT SEARCH PARSER @@ -679,6 +766,7 @@ X X - + DROP TEXT SEARCH TEMPLATE @@ -686,6 +774,7 @@ X X - + DROP TRIGGER @@ -693,6 +782,7 @@ X X - + DROP TYPE @@ -700,6 +790,7 @@ X X - + DROP USER MAPPING @@ -707,6 +798,7 @@ X X - + DROP VIEW @@ -714,6 +806,15 @@ X X - + + + + GRANT + X + X + - + - + Only for local objects IMPORT FOREIGN SCHEMA @@ -721,6 +822,23 @@ X - - + + + + REVOKE + X + X + - + - + Only for local objects + + + SECURITY LABEL + X + X + - + - + Only for local objects SELECT INTO @@ -728,6 +846,7 @@ X - - + diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index a33a5ada1a9..dcf5b987477 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -267,8 +267,12 @@ check_ddl_tag(const char *tag) pg_strcasecmp(tag, "REFRESH MATERIALIZED VIEW") == 0 || pg_strcasecmp(tag, "ALTER DEFAULT PRIVILEGES") == 0 || pg_strcasecmp(tag, "ALTER LARGE OBJECT") == 0 || + pg_strcasecmp(tag, "COMMENT") == 0 || + pg_strcasecmp(tag, "GRANT") == 0 || + pg_strcasecmp(tag, "REVOKE") == 0 || pg_strcasecmp(tag, "DROP OWNED") == 0 || - pg_strcasecmp(tag, "IMPORT FOREIGN SCHEMA") == 0) + pg_strcasecmp(tag, "IMPORT FOREIGN SCHEMA") == 0 || + pg_strcasecmp(tag, "SECURITY LABEL") == 0) return EVENT_TRIGGER_COMMAND_TAG_OK; /* @@ -1149,6 +1153,34 @@ EventTriggerSupportsObjectClass(ObjectClass objclass) return true; } +bool +EventTriggerSupportsGrantObjectType(GrantObjectType objtype) +{ + switch (objtype) + { + case ACL_OBJECT_DATABASE: + case ACL_OBJECT_TABLESPACE: + /* no support for global objects */ + return false; + + case ACL_OBJECT_COLUMN: + case ACL_OBJECT_RELATION: + case ACL_OBJECT_SEQUENCE: + case ACL_OBJECT_DOMAIN: + case ACL_OBJECT_FDW: + case ACL_OBJECT_FOREIGN_SERVER: + case ACL_OBJECT_FUNCTION: + case ACL_OBJECT_LANGUAGE: + case ACL_OBJECT_LARGEOBJECT: + case ACL_OBJECT_NAMESPACE: + case ACL_OBJECT_TYPE: + return true; + default: + Assert(false); + return true; + } +} + /* * Prepare event trigger state for a new complete query to run, if necessary; * returns whether this was done. If it was, EventTriggerEndCompleteQuery must diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 3533cfa22d4..fcc2ecdbfb4 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -513,14 +513,6 @@ standard_ProcessUtility(Node *parsetree, ExecuteTruncate((TruncateStmt *) parsetree); break; - case T_CommentStmt: - CommentObject((CommentStmt *) parsetree); - break; - - case T_SecLabelStmt: - ExecSecLabelStmt((SecLabelStmt *) parsetree); - break; - case T_CopyStmt: { uint64 processed; @@ -548,11 +540,6 @@ standard_ProcessUtility(Node *parsetree, DeallocateQuery((DeallocateStmt *) parsetree); break; - case T_GrantStmt: - /* no event triggers for global objects */ - ExecuteGrantStmt((GrantStmt *) parsetree); - break; - case T_GrantRoleStmt: /* no event triggers for global objects */ GrantRole((GrantRoleStmt *) parsetree); @@ -783,6 +770,19 @@ standard_ProcessUtility(Node *parsetree, * in some cases, so we "fast path" them in the other cases. */ + case T_GrantStmt: + { + GrantStmt *stmt = (GrantStmt *) parsetree; + + if (EventTriggerSupportsGrantObjectType(stmt->objtype)) + ProcessUtilitySlow(parsetree, queryString, + context, params, + dest, completionTag); + else + ExecuteGrantStmt((GrantStmt *) parsetree); + } + break; + case T_DropStmt: { DropStmt *stmt = (DropStmt *) parsetree; @@ -835,6 +835,32 @@ standard_ProcessUtility(Node *parsetree, } break; + case T_CommentStmt: + { + CommentStmt *stmt = (CommentStmt *) parsetree; + + if (EventTriggerSupportsObjectType(stmt->objtype)) + ProcessUtilitySlow(parsetree, queryString, + context, params, + dest, completionTag); + else + CommentObject((CommentStmt *) parsetree); + break; + } + + case T_SecLabelStmt: + { + SecLabelStmt *stmt = (SecLabelStmt *) parsetree; + + if (EventTriggerSupportsObjectType(stmt->objtype)) + ProcessUtilitySlow(parsetree, queryString, + context, params, + dest, completionTag); + else + ExecSecLabelStmt(stmt); + break; + } + default: /* All other statement types have event trigger support */ ProcessUtilitySlow(parsetree, queryString, @@ -1315,6 +1341,14 @@ ProcessUtilitySlow(Node *parsetree, ExecAlterOwnerStmt((AlterOwnerStmt *) parsetree); break; + case T_CommentStmt: + CommentObject((CommentStmt *) parsetree, NULL); + break; + + case T_GrantStmt: + ExecuteGrantStmt((GrantStmt *) parsetree); + break; + case T_DropOwnedStmt: DropOwnedObjects((DropOwnedStmt *) parsetree); break; @@ -1331,6 +1365,10 @@ ProcessUtilitySlow(Node *parsetree, AlterPolicy((AlterPolicyStmt *) parsetree); break; + case T_SecLabelStmt: + ExecSecLabelStmt((SecLabelStmt *) parsetree; + break; + default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(parsetree)); diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h index e807e658e49..9ac9fc37356 100644 --- a/src/include/commands/event_trigger.h +++ b/src/include/commands/event_trigger.h @@ -48,6 +48,7 @@ extern void AlterEventTriggerOwner_oid(Oid, Oid newOwnerId); extern bool EventTriggerSupportsObjectType(ObjectType obtype); extern bool EventTriggerSupportsObjectClass(ObjectClass objclass); +extern bool EventTriggerSupportsGrantObjectType(GrantObjectType objtype); extern void EventTriggerDDLCommandStart(Node *parsetree); extern void EventTriggerDDLCommandEnd(Node *parsetree); extern void EventTriggerSQLDrop(Node *parsetree);