云数据库 GAUSSDB-系统信息函数:会话信息函数

时间:2023-11-15 14:50:07

会话信息函数

  • current_catalog

    描述:当前数据库的名称(在标准SQL中称"catalog")。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_catalog;
     current_database
    ------------------  
     postgres
    (1 row)
    
  • current_database()

    描述:当前数据库的名称。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_database();
     current_database
    ------------------
     postgres
    (1 row)
    
  • current_query()

    描述:由客户端提交的当前执行语句(可能包含多个声明)。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_query();
          current_query
    -------------------------
     SELECT current_query();
    (1 row)
    
  • current_schema[()]

    描述:当前模式的名称。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_schema();
     current_schema
    ----------------
     public
    (1 row)
    

    备注:current_schema返回在搜索路径中第一个顺位有效的模式名。(如果搜索路径为空则返回NULL,没有有效的模式名也返回NULL)。如果创建表或者其他命名对象时没有声明目标模式,则将使用这些对象的模式。

  • current_schemas(Boolean)

    描述:搜索路径中的模式名称。

    返回值类型:name[]

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_schemas(true);
       current_schemas
    ---------------------
     {pg_catalog,public}
    (1 row)
    

    备注:

    current_schemas(Boolean)返回搜索路径中所有模式名称的数组。布尔选项决定像pg_catalog这样隐含包含的系统模式是否包含在返回的搜索路径中。

    搜索路径可以通过运行时设置更改。命令是:

    1
    SET search_path TO schema [, schema, ...]
    
  • current_user

    描述:当前执行环境下的用户名。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT current_user;
     current_user
    --------------
     omm
    (1 row)
    

    备注:current_user是用于权限检查的用户标识。通常,他表示会话用户,但是可以通过SET ROLE改变他。在函数执行的过程中随着属性SECURITY DEFINER的改变,其值也会改变。

  • definer_current_user

    描述:当前执行环境下的用户名。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT definer_current_user();
     definer_current_user
    ----------------------
     omm
    (1 row)
    

    备注:大多数情况下definer_current_user和current_user结果相同,但在存储过程中执行该函数会返回定义当前存储过程的用户名。

  • pg_current_sessionid()

    描述:当前执行环境下的会话ID。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_current_sessionid();
        pg_current_sessionid
    ----------------------------
     1579228402.140190434944768
    (1 row)
    

    备注:pg_current_sessionid()是用于获取当前执行环境下的会话ID。其组成结构为:时间戳.会话ID,当线程池模式开启(enable_thread_pool=on)时,会话ID为SessionID;而线程池模式关闭时,会话ID实际为线程ID。

  • pg_current_sessid

    描述:当前执行环境下的会话ID。

    返回值类型:text

    示例:

    openGauss=# select pg_current_sessid();
    pg_current_sessid
    -------------------
    140308875015936
    (1 row)

    备注:在线程池模式下获得当前会话的会话ID,非线程池模式下获得当前会话对应的后台线程ID。

  • pg_current_userid

    描述:当前用户ID。

    返回值类型:text

    示例:

    openGauss=# SELECT pg_current_userid();
    pg_current_userid
    -------------------
    10
    (1 row)
  • tablespace_oid_name()

    描述:根据表空间oid,查找表空间名称。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# select tablespace_oid_name(1663);
     tablespace_oid_name
    ---------------------
     pg_default
    (1 row)
    
  • inet_client_addr()

    描述:连接的远端地址。inet_client_addr返回当前客户端的IP地址。

    此函数只有在远程连接模式下有效。

    返回值类型:inet

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT inet_client_addr();
     inet_client_addr
    ------------------
     10.10.0.50
    (1 row)
    
  • inet_client_port()

    描述:连接的远端端口。inet_client_port返回当前客户端的端口号。

    此函数只有在远程连接模式下有效。

    返回值类型:int

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT inet_client_port();
     inet_client_port
    ------------------
                33143
    (1 row)
    
  • inet_server_addr()

    描述:连接的本地地址。inet_server_addr返回服务器接收当前连接用的IP地址。

    此函数只有在远程连接模式下有效。

    返回值类型:inet

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT inet_server_addr();
     inet_server_addr
    ------------------
     10.10.0.13
    (1 row)
    
  • inet_server_port()

    描述:连接的本地端口。inet_server_port返回接收当前连接的端口号。如果是通过Unix-domain socket连接的,则所有这些函数都返回NULL。

    此函数只有在远程连接模式下有效。

    返回值类型:int

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT inet_server_port();
     inet_server_port
    ------------------
                 8000
    (1 row)
    
  • pg_backend_pid()

    描述:当前会话连接的服务进程的进程ID。

    返回值类型:int

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_backend_pid();
     pg_backend_pid
    -----------------
     140229352617744
    (1 row)
    
  • pg_conf_load_time()

    描述:配置加载时间。pg_conf_load_time返回最后加载服务器配置文件的时间戳。

    返回值类型:timestamp with time zone

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_conf_load_time();
          pg_conf_load_time       
    ------------------------------
     2017-09-01 16:05:23.89868+08
    (1 row)
    
  • pg_my_temp_schema()

    描述:会话的临时模式的OID,不存在则为0。

    返回值类型:oid

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_my_temp_schema();
     pg_my_temp_schema 
    -------------------
                     0
    (1 row)
    

    备注:pg_my_temp_schema返回当前会话中临时模式的OID,如果不存在(没有创建临时表)的话则返回0。如果给定的OID是其它会话中临时模式的OID,pg_is_other_temp_schema则返回true。

  • pg_is_other_temp_schema(oid)

    描述:是否为另一个会话的临时模式。

    返回值类型:Boolean

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_is_other_temp_schema(25356);
     pg_is_other_temp_schema
    -------------------------
     f
    (1 row)
    
  • pg_listening_channels()

    描述:会话正在侦听的信道名称。

    返回值类型:setof text

    示例:

    1
    2
    3
    4
    openGauss=# SELECT pg_listening_channels();
     pg_listening_channels
    -----------------------
    (0 rows)
    

    备注:pg_listening_channels返回当前会话正在侦听的一组信道名称。

  • pg_postmaster_start_time()

    描述:服务器启动时间。pg_postmaster_start_time返回服务器启动时的timestamp with time zone。

    返回值类型:timestamp with time zone

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_postmaster_start_time();
       pg_postmaster_start_time   
    ------------------------------
     2017-08-30 16:02:54.99854+08
    (1 row)
    
  • sessionid2pid()

    描述:从sessionid中得到pid信息(如: pv_session_stat中sessid列)。

    返回值类型:int8

    示例:

    1
    2
    3
    4
    5
    6
    openGauss=# select sessionid2pid(sessid::cstring) from pv_session_stat limit 2;
      sessionid2pid
    -----------------
     139973107902208
     139973107902208
    (2 rows)
    
  • session_context( 'namespace' , 'parameter')

    描述:获取并返回指定namespace下参数parameter的值。

    返回值类型:VARCHAR

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT session_context('USERENV', 'CURRENT_SCHEMA');
     session_context 
    -------------
     public
    (1 row)
    

    根据当前所在的实际schema而变化。

    备注:目前仅支持SESSION_CONTEXT('USERENV', 'CURRENT_SCHEMA') 和SESSION_CONTEXT('USERENV', 'CURRENT_USER')两种格式。

  • pg_trigger_depth()

    描述:触发器的嵌套层次。

    返回值类型:int

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT pg_trigger_depth();
     pg_trigger_depth 
    ------------------
                    0
    (1 row)
    
  • opengauss_version()

    描述:引用的openGauss内核版本信息。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT opengauss_version();
     opengauss_version
    -------------------
     2.0.0
    (1 row)
    
  • gs_deployment()

    描述:当前系统的部署形态信息,对于分布式系统来说返回的是“Distribute”。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# select gs_deployment();
     gs_deployment
    ---------------
     Distribute
    (1 row)
    
  • session_user

    描述:会话用户名。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT session_user;
     session_user
    --------------
     omm
    (1 row)
    

    备注:session_user通常是连接当前数据库的初始用户,不过系统管理员可以用SET SESSION AUTHORIZATION修改这个设置。

  • user

    描述:等价于current_user。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT user;
     current_user
    --------------
     omm
    (1 row)
    
  • get_shard_oids_byname

    描述:输入node的名字返回node的oid。

    返回值类型:oid

    示例:

    1
    2
    3
    4
    5
    openGauss=# select get_shard_oids_byname('datanode1');
     get_shard_oids_byname
    -----------------------
     {16385}
    (1 row)
    
  • getpgusername()

    描述:获取数据库用户名。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# select getpgusername();
     getpgusername 
    ---------------
     GaussDB_userna
    (1 row)
    
  • getdatabaseencoding()

    描述:获取数据库编码方式。

    返回值类型:name

    示例:

    1
    2
    3
    4
    5
    openGauss=# select getdatabaseencoding();
     getdatabaseencoding 
    ---------------------
     SQL_ASCII
    (1 row)
    
  • version()

    描述:版本信息。version返回一个描述服务器版本信息的字符串。

    返回值类型:text

    示例:

    1
    2
    3
    4
    5
    openGauss=# SELECT version();
                                                                                             version
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     openGauss 2.0.0 (GaussDBl VxxxRxxxCxx build f521c606) compiled at 2021-09-16 14:55:22 commit 2935 last mr 6385 release
    (1 row)
    
support.huaweicloud.com/distributed-devg-v2-gaussdb/gaussdb_v5r2c10_0362.html