Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txcz9-0004jr-I1 for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jan 2013 12:29:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Txcz7-0008Mm-P8 for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jan 2013 12:29:49 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txcz5-0008Mg-Qs for pgsql-hackers@postgresql.org; Tue, 22 Jan 2013 12:29:48 +0000 Received: from acidenitrix.villemain.org ([91.121.90.165]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txcz3-0004Os-Kj for pgsql-hackers@postgresql.org; Tue, 22 Jan 2013 12:29:47 +0000 Received: from [89.157.243.170] (helo=Dimitris-MacBook-Air.local) by acidenitrix.villemain.org with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1Txcz2-0007kE-I9; Tue, 22 Jan 2013 13:29:44 +0100 From: Dimitri Fontaine To: Robert Haas Cc: Alvaro Herrera , PostgreSQL Hackers Subject: Re: Event Triggers: adding information Organization: 2ndQuadrant References: <20130104205251.GB9191@alvh.no-ip.org> <20130109165829.GB4490@alvh.no-ip.org> User-Mail-Address: dimitri@2ndQuadrant.fr Date: Tue, 22 Jan 2013 13:29:26 +0100 In-Reply-To: (Dimitri Fontaine's message of "Tue, 22 Jan 2013 13:02:04 +0100") Message-ID: User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2 (darwin) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --=-=-= Content-Type: text/plain Dimitri Fontaine writes: > I added some in the attached patch. > > doc/src/sgml/event-trigger.sgml | 10 ++ > src/backend/commands/event_trigger.c | 6 +- > src/test/regress/expected/event_trigger.out | 106 +++++++++++++++++++ > src/test/regress/sql/event_trigger.sql | 47 ++++++++ > 4 files changed, 167 insertions(+), 2 deletions(-) And I did drop a comment line I didn't mean to when trying things out, so here's the update copy. There's a bug fix in there too, in both the versions of the patch, that the new regression tests exercise. Regards, -- Dimitri Fontaine http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=event_trigger_harness.v1.patch *** a/doc/src/sgml/event-trigger.sgml --- b/doc/src/sgml/event-trigger.sgml *************** *** 52,57 **** --- 52,67 ---- + Event Triggers themselves are not run in + a . That means that you can stop the + execution of the current command by issuing a RAISE + EXCEPTION command from anywhere in your trigger function, and + that if an error occurs in the command itself (e.g. + when DROP targets an object that does not exists), + the ddl_command_end triggers will not get run. + + + For a complete list of commands supported by the event trigger mechanism, see . *** a/src/backend/commands/event_trigger.c --- b/src/backend/commands/event_trigger.c *************** *** 551,557 **** filter_event_trigger(const char **tag, EventTriggerCacheItem *item) } /* Filter by tags, if any were specified. */ ! if (item->ntags != 0 && bsearch(&tag, item->tag, item->ntags, sizeof(char *), pg_qsort_strcmp) == NULL) return false; --- 551,557 ---- } /* Filter by tags, if any were specified. */ ! if (item->ntags != 0 && bsearch(tag, item->tag, item->ntags, sizeof(char *), pg_qsort_strcmp) == NULL) return false; *************** *** 752,757 **** EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata) --- 752,760 ---- ListCell *lc; bool first = true; + /* Guard against stack overflow due to recursive event trigger */ + check_stack_depth(); + /* * Let's evaluate event triggers in their own memory context, so * that any leaks get cleaned up promptly. *** a/src/test/regress/expected/event_trigger.out --- b/src/test/regress/expected/event_trigger.out *************** *** 67,72 **** alter event trigger regress_event_trigger enable; --- 67,73 ---- alter event trigger regress_event_trigger disable; -- regress_event_trigger2 should fire, but not regress_event_trigger create table event_trigger_fire1 (a int); + NOTICE: test_event_trigger: ddl_command_start CREATE TABLE NOTICE: test_event_trigger: ddl_command_end CREATE TABLE -- but nothing should fire here drop table event_trigger_fire1; *************** *** 90,95 **** ERROR: event trigger "regress_event_trigger" does not exist --- 91,201 ---- drop role regression_bob; ERROR: role "regression_bob" cannot be dropped because some objects depend on it DETAIL: owner of event trigger regress_event_trigger3 + -- now try something crazy to ensure we don't crash the backend + create function test_event_trigger_drop_function() + returns event_trigger + set max_stack_depth to '100' -- limit the size of the stack trace we output + as $$ + BEGIN + drop function test_event_trigger2() cascade; + END + $$ language plpgsql; + NOTICE: test_event_trigger: ddl_command_start CREATE FUNCTION + NOTICE: test_event_trigger: ddl_command_end CREATE FUNCTION + create function test_event_trigger2() returns event_trigger as $$ + BEGIN + RAISE NOTICE 'test_event_trigger2: % %', tg_event, tg_tag; + END + $$ language plpgsql; + NOTICE: test_event_trigger: ddl_command_start CREATE FUNCTION + NOTICE: test_event_trigger: ddl_command_end CREATE FUNCTION + create event trigger drop_test_a on "ddl_command_start" + execute procedure test_event_trigger_drop_function(); + create event trigger drop_test_b on "ddl_command_start" + execute procedure test_event_trigger2(); + -- now watch the fun + create table event_trigger_fire1 (a int); + ERROR: stack depth limit exceeded + HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate. + CONTEXT: SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + SQL statement "drop function test_event_trigger2() cascade" + PL/pgSQL function test_event_trigger_drop_function() line 3 at SQL statement + -- now limit the event trigger to avoid infinite recursion + drop event trigger drop_test_a; + create event trigger drop_test_a on "ddl_command_start" + when tag in ('create table') + execute procedure test_event_trigger_drop_function(); + -- and try again, see if we crash + -- + -- we want to avoid seeing the following in the regression tests: + -- ERROR: cache lookup failed for function 30201 + -- because the OID of course changes each time + -- + set client_min_messages to fatal; + create table event_trigger_fire1 (a int); + reset client_min_messages; + -- then cleanup the crazy test + drop event trigger drop_test_a; + drop event trigger drop_test_b; + drop function test_event_trigger_drop_function(); + NOTICE: test_event_trigger: ddl_command_end DROP FUNCTION -- these are all OK; the second one should emit a NOTICE drop event trigger if exists regress_event_trigger2; drop event trigger if exists regress_event_trigger2; *** a/src/test/regress/sql/event_trigger.sql --- b/src/test/regress/sql/event_trigger.sql *************** *** 95,100 **** drop event trigger regress_event_trigger; --- 95,147 ---- -- should fail, regression_bob owns regress_event_trigger2/3 drop role regression_bob; + -- now try something crazy to ensure we don't crash the backend + create function test_event_trigger_drop_function() + returns event_trigger + set max_stack_depth to '100' -- limit the size of the stack trace we output + as $$ + BEGIN + drop function test_event_trigger2() cascade; + END + $$ language plpgsql; + + create function test_event_trigger2() returns event_trigger as $$ + BEGIN + RAISE NOTICE 'test_event_trigger2: % %', tg_event, tg_tag; + END + $$ language plpgsql; + + create event trigger drop_test_a on "ddl_command_start" + execute procedure test_event_trigger_drop_function(); + + create event trigger drop_test_b on "ddl_command_start" + execute procedure test_event_trigger2(); + + -- now watch the fun + create table event_trigger_fire1 (a int); + + -- now limit the event trigger to avoid infinite recursion + drop event trigger drop_test_a; + + create event trigger drop_test_a on "ddl_command_start" + when tag in ('create table') + execute procedure test_event_trigger_drop_function(); + + -- and try again, see if we crash + -- + -- we want to avoid seeing the following in the regression tests: + -- ERROR: cache lookup failed for function 30201 + -- because the OID of course changes each time + -- + set client_min_messages to fatal; + create table event_trigger_fire1 (a int); + reset client_min_messages; + + -- then cleanup the crazy test + drop event trigger drop_test_a; + drop event trigger drop_test_b; + drop function test_event_trigger_drop_function(); + -- these are all OK; the second one should emit a NOTICE drop event trigger if exists regress_event_trigger2; drop event trigger if exists regress_event_trigger2; --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --=-=-=--