Routines and triggers
How stored procedures, functions and triggers are stored and interpreted in the front-end, how several result sets are returned, and the limits.
Stored procedures, stored functions and triggers (D87, D88) are run by an interpreter inside the front-end. The core database knows nothing about them.
Storage#
A routine is one document in a hidden table of its database, __rdb_routines, with key p:name for a procedure or f:name for a function. A trigger is a document in __rdb_triggers. Both are read once and kept in a cache per database like the views (RaftDBCfg::routines_*, triggers_*). The body is parsed when the routine is made (a body that does not parse is refused) and again each time it runs. The triggers of a table go with it when it is dropped. information_schema.ROUTINES and information_schema.TRIGGERS expose them, and SHOW CREATE PROCEDURE | FUNCTION | TRIGGER, SHOW PROCEDURE | FUNCTION STATUS and SHOW TRIGGERS work.
The interpreter#
sql/routine.rs finds the structure of a body; routines.rs runs it.
- Structure:
BEGIN ... ENDwith labels;DECLAREof variables, conditions, handlers (CONTINUEorEXITforSQLEXCEPTION,SQLWARNING,NOT FOUND,SQLSTATE, an error number or a named condition) and cursors;SET,IF,CASE,WHILE,REPEAT,LOOP,LEAVE,ITERATE,RETURN,SELECT ... INTO,OPEN,FETCH,CLOSE,SIGNAL,RESIGNAL,GET DIAGNOSTICSandCALL. Every other statement stays text. - Execution: the local variables in a statement's text are replaced by their values (not after a
., not before(or., not an alias, not the column list of anINSERTor the target ofUPDATE ... SET), and then the ordinary parser and executor run the text. Result sets of statements go to the client; an error goes to the handlers of the routine. - Several result sets: a reply can be
Reply::Multi, the answers one after the other, all but the last sent with the "more results" status flag (SERVER_MORE_RESULTS_EXISTS), in the text and the binary protocol alike.
Triggers#
CREATE [OR REPLACE] TRIGGER [IF NOT EXISTS] name BEFORE|AFTER INSERT|UPDATE|DELETE ON table FOR EACH ROW [FOLLOWS|PRECEDES other] body. The body is the same language as a routine's. NEW.col and OLD.col are variables of the trigger, and SET NEW.col = ... in a BEFORE trigger changes the row, which is coerced to the column's type again.
Where triggers run:
| Statement | Triggers |
|---|---|
INSERT | BEFORE before each row is written; AFTER when the statement has written all its rows |
UPDATE, DELETE | including a multi-table UPDATE, which shares apply_updates |
REPLACE | the row in the way is a delete, then the insert |
INSERT ... ON DUPLICATE KEY UPDATE | the AFTER UPDATE triggers |
An error in a trigger (SIGNAL, or a statement that fails) fails the whole statement, which is all or nothing as before. A statement in a trigger may not change the table the trigger is for (error 1442).
Example:
DELIMITER //
CREATE TRIGGER acct_bi BEFORE INSERT ON acct FOR EACH ROW
BEGIN
IF NEW.balance < 0 THEN
SET NEW.balance = 0;
END IF;
SET NEW.name = UPPER(NEW.name);
END//
CREATE TRIGGER acct_bu BEFORE UPDATE ON acct FOR EACH ROW
BEGIN
IF NEW.balance < OLD.balance - 100 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'too big a withdrawal';
END IF;
SET NEW.updated = OLD.updated + 1;
END//
DELIMITER ;Checks#
Routines and triggers are checked against MySQL and MariaDB. Per D88 the trigger output is identical to MySQL 9.7 and MariaDB 11.4 except for the account host and the times.
Not supported yet#
From D88: the BEFORE triggers of the update that ON DUPLICATE KEY UPDATE makes, a multi-table DELETE, events, triggers on views (refused, as MySQL does), and the order of AFTER triggers relative to the later rows of the same statement (they run after the last row). Routines run by textual replacement of variables, so a statement in a body is parsed again each time it runs.