MariaDB SESSION_USER() Explained

In MariaDB, SESSION_USER() is a synonym for the USER() function. It returns the current MariaDB user name and host name, given when authenticating to MariaDB.

Syntax

The syntax goes like this:

SESSION_USER()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT SESSION_USER();

Result:

+------------------+
| SESSION_USER()   |
+------------------+
| barney@localhost |
+------------------+

SESSION_USER() vs CURRENT_USER()

There’s also another function called CURRENT_USER() that does a similar thing. However, it doesn’t always return the same result as SESSION_USER().

For example, if we connect using anonymous:

mariadb --user="anonymous"

Then run SESSION_USER() and CURRENT_USER():

SELECT 
    SESSION_USER(),
    CURRENT_USER;

Result:

+---------------------+--------------+
| SESSION_USER()      | CURRENT_USER |
+---------------------+--------------+
| anonymous@localhost | @localhost   |
+---------------------+--------------+

However, if we jump back to our previous session (in the original terminal window), each function returns the same results:

SELECT 
    SESSION_USER(),
    CURRENT_USER;

Result:

+------------------+------------------+
| SESSION_USER()   | CURRENT_USER     |
+------------------+------------------+
| barney@localhost | barney@localhost |
+------------------+------------------+

No Arguments are Accepted

Passing any arguments to SESSION_USER() results in an error:

SELECT SESSION_USER(123);

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '123)' at line 1

Statements using the SESSION_USER() function (or USER() and SYSTEM_USER()) are not safe for statement level replication.