Name

SET SESSION AUTHORIZATION Statement

The SET SESSION AUTHORIZATION statement sets the user identifier for the current session.

Platform

Command

MySQL

Not supported

Oracle

Not supported

PostgreSQL

Supported

SQL Server

Not supported

SQL2003 Syntax

SET SESSION AUTHORIZATION username

Keywords

username

Sets the session user and the current user of the SQL session to the context of username, where username may be a literal, a parameter, or a host variable.

Rules at a Glance

This command allows you to switch between users and to run under their permissions.

Programming Tips and Gotchas

Some platforms allow you to use special shortcut keywords like SESSION USER and CURRENT USER. These are usually the same thing: the username of the currently active session provided by the client. However, SESSION USER and CURRENT USER can diverge in a session when SETUID functions and other similar mechanisms are invoked.

Superuser permissions are required to invoke the SET SESSION AUTHORIZATION command, but you will still be able to switch back to the initial user session even if the current user session does not normally have permission to run SET SESSION AUTHORIZATION.

You might also wish to check the value of the SESSION_USER and CURRENT_USER functions with this SQL statement:

SELECT SESSION_USER, CURRENT_USER;

Normally, you should issue SET SESSION AUTHORIZATION before any transactions, to set the session and current user values for all transactions that follow. It must be issued as the only command in its transaction ...

Get SQL in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.