4.13. Miscellaneous Functions

Table 4-25. Session Information Functions

NameReturn TypeDescription
current_usernameuser name of current execution context
session_usernamesession user name
usernameequivalent to current_user

The session_user is the user that initiated a database connection; it is fixed for the duration of that connection. The current_user is the user identifier that is applicable for permission checking. Currently it is always equal to the session user, but in the future there might be "setuid" functions and other facilities to allow the current user to change temporarily. In Unix parlance, the session user is the "real user" and the current user is the "effective user".

Note that these functions have special syntactic status in SQL: they must be called without trailing parentheses.

Deprecated: The function getpgusername() is an obsolete equivalent of current_user.

Table 4-26. System Information Functions

NameReturn TypeDescription
versiontextPostgreSQL version information

version() returns a string describing the PostgreSQL server's version.

Table 4-27. Access Privilege Inquiry Functions

NameReturn TypeDescription
has_table_privilege(user, table, access) booleandoes user have access to table
has_table_privilege(table, access) booleandoes current user have access to table

has_table_privilege determines whether a user can access a table in a particular way. The user can be specified by name or by ID (pg_user.usesysid), or if the argument is omitted current_user is assumed. The table can be specified by name or by OID. (Thus, there are actually six variants of has_table_privilege, which can be distinguished by the number and types of their arguments.) The desired access type is specified by a text string, which must evaluate to one of the values SELECT, INSERT, UPDATE, DELETE, RULE, REFERENCES, or TRIGGER. (Case of the string is not significant, however.)

Table 4-28. Catalog Information Functions

NameReturn TypeDescription
pg_get_viewdef(viewname)textGet CREATE VIEW command for view
pg_get_ruledef(rulename)textGet CREATE RULE command for rule
pg_get_indexdef(indexOID)textGet CREATE INDEX command for index
pg_get_userbyid(userid)nameGet user name given sysid

These functions extract information from the system catalogs. pg_get_viewdef(), pg_get_ruledef(), and pg_get_indexdef() respectively reconstruct the creating command for a view, rule, or index. (Note that this is a decompiled reconstruction, not the verbatim text of the command.) pg_get_userbyid() extracts a user's name given a usesysid value.