MariaDB CURRENT_ROLE() Function

In MariaDB, CURRENT_ROLE() is a built-in function that returns a string representing the roles currently active in the current session, with multiple roles separated by commas.

MariaDB CURRENT_ROLE() Syntax

Here is the syntax of the MariaDB CURRENT_ROLE() function:

CURRENT_ROLE()

Parameters

The MariaDB CURRENT_ROLE() function do not require any parameters.

Return value

The CURRENT_ROLE() function returns a UTF8 string containing the currently active role for the current session.

MariaDB CURRENT_ROLE() Examples

The following example shows how to use the CURRENT_ROLE() function to get current role information.

First, let’s create a user named testuser using the following statement:

CREATE USER 'testuser'@'%' IDENTIFIED BY 'testuser123';

Log in to the MariaDB server with the user you just created:

MariaDB --user=testuser --password=testuser123

Use the following statement to view the current role:

SELECT CURRENT_ROLE();

Output:

+----------------+
| CURRENT_ROLE() |
+----------------+
| NONE           |
+----------------+

Here, NONE means that the current session user does not have any role.

Then, let’s create two roles and grant them to testuser:

CREATE ROLE test_role1, test_role2;
GRANT 'test_role1', 'test_role2' TO 'testuser'@'%';
SET DEFAULT ROLE ALL TO 'testuser'@'%';

Log in to MariaDB again and check the roles:

SELECT CURRENT_ROLE();

Output:

+-----------------------------------+
| CURRENT_ROLE()                    |
+-----------------------------------+
| `test_role1`@`%`,`test_role2`@`%` |
+-----------------------------------+

Here, the result we expect is returned. The two roles separated with a comma.

Let us use the following statement to modify the role of the following current session to be test_role1:

SET ROLE 'test_role1';

View roles for the current session:

SELECT CURRENT_ROLE();

Output:

+------------------+
| CURRENT_ROLE()   |
+------------------+
| `test_role1`@`%` |
+------------------+

Here only one role is returned. Because we just used the SET ROLE 'test_role1'; statement to set the role of the current session to test_role1.

Conclusion

In MariaDB, CURRENT_ROLE() is a built-in function that returns a string representing the roles currently active in the current session, with multiple roles separated by commas.