Skip to content

MDEV-30645: Generalized triggers - #3296

Open
andremralves wants to merge 9 commits into
MariaDB:11.6from
andremralves:trigger_as_event
Open

andremralves wants to merge 9 commits into
MariaDB:11.6from
andremralves:trigger_as_event

Conversation

@andremralves

@andremralves andremralves commented May 30, 2024 •

Copy link
Copy Markdown
Contributor
  • The Jira issue number for this PR is: MDEV-30645

Description

TL;DR

This PR implements some Generalized Triggers. It contains:

  • CREATE TRIGGER as a synonym for CREATE EVENT.
  • CREATE TRIGGER .. AFTER STARTUP DO ..
  • CREATE TRIGGER .. BEFORE SHUTDOWN DO ..

Making CREATE TRIGGER a synonym for CREATE EVENT

CREATE TRIGGER myevent
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
      UPDATE myschema.mytable SET mycol = mycol + 1;

Implementation:

I changed the rule trigger_tail to be either a trigger_tail or a event_tail.

I had to change some tests since the syntax now accepts the prefix create trigger [trigger_name] on.

Implementing CREATE TRIGGER .. AFTER STARTUP and CREATE TRIGGER .. BEFORE SHUTDOWN.

To run those triggers, I took advantage of the event scheduler infrastructure, and for storing them, I used the mysql.event table.

Startup trigger example:

CREATE TABLE test.t1 (inc INT);
CREATE TRIGGER startup_trg AFTER STARTUP DO UPDATE test.t1 SET inc = inc + 1;

Will run right after the server startup starts.

Shutdown trigger example:

CREATE TABLE test.t1 (inc INT);
CREATE TRIGGER shutdown_trg BEFORE SHUTDOWN DO UPDATE test.t1 SET inc = inc + 1;

Will run right before the server shutdown starts.

Last GSoC commit: Fixup: some rpl and embedded test errors and other minor issues

Release Notes

TODO: What should the release notes say about this change?
Include any changed system variables, status variables or behaviour. Optionally list any https://mariadb.com/kb/ pages that need changing.

How can this PR be tested?

You can use the automated test:

./mtr main.create_trigger_on_schedule
./mtr main.create_drop_startup_trigger
./mtr main.create_drop_shutdown_trigger

or manually try to create an event using CREATE TRIGGER and then check if there is any corner case.

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.
  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@andremralves
andremralves marked this pull request as draft May 30, 2024 02:28
@andremralves
andremralves marked this pull request as ready for review May 30, 2024 02:46
@vuvova

vuvova commented May 30, 2024

Copy link
Copy Markdown
Member

Looks good, but please add a test for a syntax error, like

CREATE OR REPLACE TRIGGER IF NOT EXISTS ev1 IN SCHEDULE EVERY 1 SECOND DO DROP DATABASE db1;

@andremralves

Copy link
Copy Markdown
Contributor Author

@vuvova I added some more tests like the one you suggested.

@andremralves
andremralves force-pushed the trigger_as_event branch 2 times, most recently from a8b655a to 38180c9 Compare May 31, 2024 22:57
@andremralves
andremralves requested a review from vuvova May 31, 2024 23:05

@vuvova vuvova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks.

It's a bit inconsistent that error messages still say "event", like in

CREATE TRIGGER ev1 ON SCHEDULE EVERY 1 SECOND DO INSERT INTO t1 VALUES (11);
ERROR HY000: Event 'ev1' already exists

but let's not spend time on it now. You can get back to it after the main GSoC project is done.

@vuvova

vuvova commented Jun 1, 2024

Copy link
Copy Markdown
Member

Note, this won't be pushed into 11.6 right now, it would just be seen as a strange and redundant alias of an existing feature.

We'll push everything together, then it'll be a logical part of the "generalized triggers"

@andremralves

andremralves commented Jun 1, 2024 •

Copy link
Copy Markdown
Contributor Author

Looks good, thanks.

It's a bit inconsistent that error messages still say "event", like in

CREATE TRIGGER ev1 ON SCHEDULE EVERY 1 SECOND DO INSERT INTO t1 VALUES (11);
ERROR HY000: Event 'ev1' already exists

but let's not spend time on it now. You can get back to it after the main GSoC project is done.

That is something I was thinking about. After we use CREATE TRIGGER .. ON, should the outcome be an event or a trigger? The name we use is trigger, but all the characteristics are from an event. Also, when we use SHOW EVENTS, should the ones created with CREATE TRIGGER appear as well? With this implementation, CREATE TRIGGER .. ON will just be another way to create an event. Would it be better to differentiate them?

@vuvova

vuvova commented Jun 1, 2024

Copy link
Copy Markdown
Member

I think it'll be the same object, so no need to differentiate. EVENT is very overloaded concept, e.g. very often people speak of "binlog events", which are completely different. What you're doing creates a notion of a "generalized trigger", like in some other databases. The syntax is CREATE TRIGGER <name> <condition> ... and the condition can be "AFTER INSERT" or "BEFORE SHUTDOWN" or whatever, may be "ON LOGIN" or "ON SLAVE CONNECT", etc. In this scheme "ON SCHEDULE" is just another condition for a trigger. And CREATE EVENT is a historical syntax to create a scheduled generalized trigger.

But it should be done in steps.

@andremralves

andremralves commented Jun 3, 2024 •

Copy link
Copy Markdown
Contributor Author

Thanks for the explanation. I thought that since they will be the same object (event), maybe the error message saying that is an event is good, but, on the other hand, it could confuse the user.

@andremralves andremralves changed the title Part of MDEV-30645: Make create trigger a synonym for create event MDEV-30645: Generalized triggers Jun 11, 2024
@andremralves
andremralves marked this pull request as draft June 23, 2024 21:42
@andremralves
andremralves force-pushed the trigger_as_event branch 4 times, most recently from 2b408a8 to d4a387d Compare July 8, 2024 04:57
@andremralves
andremralves requested a review from vuvova July 8, 2024 06:25

@vuvova vuvova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks very good so far

Comment thread sql/events.cc Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/sql_show.cc Resolved
@andremralves
andremralves force-pushed the trigger_as_event branch 2 times, most recently from 66a62e8 to b7dcdb7 Compare July 23, 2024 05:29
@andremralves
andremralves force-pushed the trigger_as_event branch 2 times, most recently from 856df15 to 1dee305 Compare August 24, 2024 07:36
@andremralves
andremralves marked this pull request as ready for review August 25, 2024 21:53
# restart
SELECT * FROM t1;
inc
1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add show triggers and show create trigger here, please.

Comment thread mysql-test/main/lock_kill.test Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/events.cc Resolved
Comment thread sql/mysqld.cc Resolved
Comment thread sql/sql_yacc.yy Outdated
build_trn_path(thd, lex->spname, (LEX_STRING*) &trn_path);
if (check_trn_exists(&trn_path) && !opt_bootstrap)
lex->set_command(SQLCOM_DROP_EVENT, $3);
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, no, this kind of decision cannot be done in the parser. it should be done later during execution. Consider, for example, a stored procedure:

create procedure drop_trigger_foo
begin
  drop trigger foo;
end

it's parsed once but can executed many times. you cannot decide during the parsing whether it'll be event or trigger, this might change between executions.

this should be fixed. and a test case added.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest? Maybe make this decision in mysql_create_or_drop_trigger()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to execute drop event if a trigger isn't found, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about handling it in sql_parser.cc? First, DROP TRIGGER is executed, and then, if it returns an error, we try to execute DROP EVENT. Like this:

  case SQLCOM_DROP_TRIGGER:
  {
    if (thd->variables.option_bits & OPTION_IF_EXISTS)
      lex->create_info.set(DDL_options_st::OPT_IF_EXISTS);

    /* Conditionally writes to binlog. */
    res= mysql_create_or_drop_trigger(thd, all_tables, 0);

    /* 
       If there is an error or error level warning
       try to drop from mysql.event.
    */
    if(!opt_bootstrap &&
       (thd->get_stmt_da()->sql_errno() + thd->get_stmt_da()->error_count()))
    {
      /* Clear errors and warnings */
      thd->clear_error();
      thd->get_stmt_da()->clear_warning_info(thd->query_id);
      if (!(res= Events::drop_event(thd,
                                    &lex->spname->m_db, &lex->spname->m_name,
                                    lex->if_exists())))
        my_ok(thd);
    }
    break;
  }

If that’s not the appropriate place to handle that decision, please let me know where it should be.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest? Maybe make this decision in mysql_create_or_drop_trigger()?

I think this is a good idea. add_table_for_trigger_internal starts from

  if (check_trn_exists(&trn_path)) {
    if (if_exists) {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
                          ER_TRG_DOES_NOT_EXIST,
                          ER_THD(thd, ER_TRG_DOES_NOT_EXIST));
      *table= NULL;
      DBUG_RETURN(FALSE);
    }
    my_error(ER_TRG_DOES_NOT_EXIST, MYF(0));
    DBUG_RETURN(TRUE);
  }

you can, for example, move error reporting to the caller and mysql_create_or_drop_trigger can call drop_event.

Please, add tests for CREATE OR REPLACE, CREATE IF NOT EXISTS, and DROP IF EXISTS. Particularly, when a trigger of one type exists, but you create (with OR REPLACE or IF NOT EXISTS) a trigger of a different type

@andremralves
andremralves force-pushed the trigger_as_event branch 16 times, most recently from e353d5f to 64dcb76 Compare September 10, 2024 00:18
@cvicentiu cvicentiu added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Nov 26, 2024
@vuvova vuvova added the GSoC label Jul 16, 2025
@gkodinov gkodinov added the Rework pull requests that are currently being re-implemented by the MariaDB developers label Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. GSoC Rework pull requests that are currently being re-implemented by the MariaDB developers

Development

Successfully merging this pull request may close these issues.

4 participants