postgresql session function (2)

Posted by sanfly on Sun, 05 Apr 2020 15:44:16 +0200

Session information function (2)

Current "catalog and current" database () both return the name of the currently connected database.

testdb=# select current_database();
 current_database
------------------
 testdb
(1 Row record)


testdb=# select current_catalog;
 current_database
------------------
 testdb
(1 Row record)

Current ou query() queries the currently submitted query statements executed by the client. However, the actual measurement cannot return the query statement being executed by the current other session. The definition of this function is as follows, and the comments indicate that it will be useful in stored procedures.

/*
 * current_query()
 *  Expose the current query to the user (useful in stored procedures)
 *  We might want to use ActivePortal->sourceText someday.
 */
Datum
current_query(PG_FUNCTION_ARGS)
{
 /* there is no easy way to access the more concise 'query_string' */
 if (debug_query_string)
  PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
 else
  PG_RETURN_NULL();
}

Example:

mydb=# select current_query();
      current_query
-------------------------
 select current_query();
(1 row)

current_schema [()], returns the schema name of the current connection. Parentheses are optional.

mydb=# select current_schema();
 current_schema
----------------
 public
(1 row)

Current_schema (Boolean), which returns an array of all schema names in a search path. The Boolean option determines whether an implicitly contained system pattern such as PG? Catalog is included in the returned search path.

highgo=# select current_schemas(false);
 current_schemas
-----------------
 {public}
(1 Row record)

highgo=# select current_schemas(true);
                 current_schemas
-------------------------------------------------
 {oracle_catalog,hgdb_catalog,pg_catalog,public}
(1 Row record)

Current "user: is the user ID used for permission checking.

highgo=# select current_user;
 current_user
--------------
 highgo
(1 Row record)

Usually, a session user is queried. You can use SET ROLE to change the current user.

highgo=# set role test;
SET
highgo=> select current_user;
 current_user
--------------
 test
(1 Row record)
by z

Topics: Session Database