Menu

MariaDB CURRENT_ROLE() Function

Updated on

In MariaDB, CURRENT_ROLE() returns the name of the role currently active in the session. It returns NULL when no role is active. Unlike MySQL, MariaDB permits only one current role at a time; see the MariaDB SET ROLE documentation.

Syntax

CURRENT_ROLE()

The function takes no parameters. Use it in a SELECT statement to see the session’s active role:

SELECT CURRENT_ROLE();

Example

Create a local user and two roles in an administrator session. Replace the password placeholder with a unique secret:

CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'replace-with-a-unique-secret';
CREATE ROLE test_role1, test_role2;
GRANT test_role1, test_role2 TO 'testuser'@'localhost';
SET DEFAULT ROLE test_role1 FOR 'testuser'@'localhost';

Connect as testuser and check the default active role:

mariadb --user=testuser --password

At the password prompt, enter the unique secret you assigned. Then run:

SELECT CURRENT_ROLE();

The result is test_role1. Switch the session to the other granted role and check again:

SET ROLE test_role2;
SELECT CURRENT_ROLE();

The result is now test_role2. Only that role is active in this session. To disable the active role:

SET ROLE NONE;
SELECT CURRENT_ROLE();

With no active role, CURRENT_ROLE() returns NULL. To configure which role is activated at login, use SET DEFAULT ROLE.