Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: devel
Unsupported versions: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

9.22. System Information Functions

Table 9-44 shows several functions that extract session and system information.

In addition to the functions listed in this section, there are a number of functions related to the statistics system that also provide system information. See Section 26.2.2 for more information.

Table 9-44. Session Information Functions

Name Return Type Description
current_database() name name of current database
current_schema() name name of current schema
current_schemas(boolean) name[] names of schemas in search path optionally including implicit schemas
current_user name user name of current execution context
inet_client_addr() inet address of the remote connection
inet_client_port() int port of the remote connection
inet_server_addr() inet address of the local connection
inet_server_port() int port of the local connection
pg_my_temp_schema() oid OID of session's temporary schema, or 0 if none
pg_is_other_temp_schema(oid) boolean is schema another session's temporary schema?
pg_postmaster_start_time() timestamp with time zone server start time
session_user name session user name
user name equivalent to current_user
version() text PostgreSQL version information

The session_user is normally the user who initiated the current database connection; but superusers can change this setting with SET SESSION AUTHORIZATION. The current_user is the user identifier that is applicable for permission checking. Normally, it is equal to the session user, but it can be changed with SET ROLE. It also changes during the execution of functions with the attribute SECURITY DEFINER. In Unix parlance, the session user is the "real user" and the current user is the "effective user".

Note: current_user, session_user, and user have special syntactic status in SQL: they must be called without trailing parentheses.

current_schema returns the name of the schema that is at the front of the search path (or a null value if the search path is empty). This is the schema that will be used for any tables or other named objects that are created without specifying a target schema. current_schemas(boolean) returns an array of the names of all schemas presently in the search path. The Boolean option determines whether or not implicitly included system schemas such as pg_catalog are included in the search path returned.

Note: The search path can be altered at run time. The command is:

SET search_path TO schema [, schema, ...]

inet_client_addr returns the IP address of the current client, and inet_client_port returns the port number. inet_server_addr returns the IP address on which the server accepted the current connection, and inet_server_port returns the port number. All these functions return NULL if the current connection is via a Unix-domain socket.

pg_my_temp_schema returns the OID of the current session's temporary schema, or 0 if it has none (because it has not created any temporary tables). pg_is_other_temp_schema returns true if the given OID is the OID of any other session's temporary schema. (This can be useful, for example, to exclude other sessions' temporary tables from a catalog display.)

pg_postmaster_start_time returns the timestamp with time zone when the server started.

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

Table 9-45 lists functions that allow the user to query object access privileges programmatically. See Section 5.6 for more information about privileges.

Table 9-45. Access Privilege Inquiry Functions

Name Return Type Description
has_database_privilege(user, database, privilege) boolean does user have privilege for database
has_database_privilege(database, privilege) boolean does current user have privilege for database
has_function_privilege(user, function, privilege) boolean does user have privilege for function
has_function_privilege(function, privilege) boolean does current user have privilege for function
has_language_privilege(user, language, privilege) boolean does user have privilege for language
has_language_privilege(language, privilege) boolean does current user have privilege for language
has_schema_privilege(user, schema, privilege) boolean does user have privilege for schema
has_schema_privilege(schema, privilege) boolean does current user have privilege for schema
has_table_privilege(user, table, privilege) boolean does user have privilege for table
has_table_privilege(table, privilege) boolean does current user have privilege for table
has_tablespace_privilege(user, tablespace, privilege) boolean does user have privilege for tablespace
has_tablespace_privilege(tablespace, privilege) boolean does current user have privilege for tablespace
pg_has_role(user, role, privilege) boolean does user have privilege for role
pg_has_role(role, privilege) boolean does current user have privilege for role

has_database_privilege checks whether a user can access a database in a particular way. The possibilities for its arguments are analogous to has_table_privilege. The desired access privilege type must evaluate to CREATE, CONNECT, TEMPORARY, or TEMP (which is equivalent to TEMPORARY).

has_function_privilege checks whether a user can access a function in a particular way. The possibilities for its arguments are analogous to has_table_privilege. When specifying a function by a text string rather than by OID, the allowed input is the same as for the regprocedure data type (see Section 8.16). The desired access privilege type must evaluate to EXECUTE. An example is:

SELECT has_function_privilege('joeuser', 'myfunc(int, text)', 'execute');

has_language_privilege checks whether a user can access a procedural language in a particular way. The possibilities for its arguments are analogous to has_table_privilege. The desired access privilege type must evaluate to USAGE.

has_schema_privilege checks whether a user can access a schema in a particular way. The possibilities for its arguments are analogous to has_table_privilege. The desired access privilege type must evaluate to CREATE or USAGE.

has_table_privilege checks whether a user can access a table in a particular way. The user can be specified by name or by OID (pg_authid.oid), 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.) When specifying by name, the name can be schema-qualified if necessary. The desired access privilege type is specified by a text string, which must evaluate to one of the values SELECT, INSERT, UPDATE, DELETE, REFERENCES, or TRIGGER. (Case of the string is not significant, however.) An example is:

SELECT has_table_privilege('myschema.mytable', 'select');

has_tablespace_privilege checks whether a user can access a tablespace in a particular way. The possibilities for its arguments are analogous to has_table_privilege. The desired access privilege type must evaluate to CREATE.

pg_has_role checks whether a user can access a role in a particular way. The possibilities for its arguments are analogous to has_table_privilege. The desired access privilege type must evaluate to MEMBER or USAGE. MEMBER denotes direct or indirect membership in the role (that is, the right to do SET ROLE), while USAGE denotes whether the privileges of the role are immediately available without doing SET ROLE.

To test whether a user holds a grant option on the privilege, append WITH GRANT OPTION to the privilege key word; for example 'UPDATE WITH GRANT OPTION'.

Table 9-46 shows functions that determine whether a certain object is visible in the current schema search path. For example, a table is said to be visible if its containing schema is in the search path and no table of the same name appears earlier in the search path. This is equivalent to the statement that the table can be referenced by name without explicit schema qualification. To list the names of all visible tables:

SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);