CURRENT_USER returns the name of the role executing the current transaction.
CURRENT_USER returns the role name that owns the current database session.It is evaluated once per transaction and is not affected by SET ROLE.
Use it in audit tables, row-level security (RLS) policies, or dynamic SQL that must adapt permissions to the executing role.
Place CURRENT_USER wherever an expression is valid: SELECT CURRENT_USER;
or add it as a column in a larger query.
Yes.SELECT CURRENT_USER AS session_user;
is common when logging user activity.
SESSION_USER returns the role that authenticated the connection.CURRENT_USER switches when you execute SET ROLE
; SESSION_USER does not.
Insert the current role into an audit trail: INSERT INTO order_audit(order_id, changed_by) VALUES (42, CURRENT_USER);
CREATE POLICY customer_is_owner ON Orders USING (customer_id = (SELECT id FROM Customers WHERE email = CURRENT_USER));
1) Cast CURRENT_USER to text
if concatenating. 2) Avoid relying on it for security without RLS or GRANTs. 3) Log it early in triggers for traceability.
.
No. It is a special SQL keyword, not a function call.
Yes within a transaction; it resets only after a new transaction starts.
Use row-level security policies referencing CURRENT_USER for fine-grained access.