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 ID

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.

Table 4-29. Comment Information Functions

NameReturn TypeDescription
obj_description(objectOID, tablename)textGet comment for a database object
obj_description(objectOID)textGet comment for a database object (deprecated)
col_description(tableOID, columnnumber)textGet comment for a table column

These functions extract comments previously stored with the COMMENT command. NULL is returned if no comment can be found matching the specified parameters.

The two-parameter form of obj_description() returns the comment for a database object specified by its OID and the name of the containing system catalog. For example, obj_description(123456,'pg_class') would retrieve the comment for a table with OID 123456. The one-parameter form of obj_description() requires only the object OID. It is now deprecated since there is no guarantee that OIDs are unique across different system catalogs; therefore, the wrong comment could be returned.

col_description() returns the comment for a table column, which is specified by the OID of its table and its column number. obj_description() cannot be used for table columns since columns do not have OIDs of their own.