session_variable

Oracle兼容的会话变量/常量操作函数

概览

扩展包名版本分类许可证语言
session_variable3.4SIMGPL-3.0C
ID扩展名BinLibLoadCreateTrustReloc模式
9120session_variablesession_variable
相关扩展orafce pgtt pg_statement_rollback plpgsql set_user oracle_fdw pg_dbms_lock babelfishpg_common

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY3.41817161514session_variable-
RPMPIGSTY3.41817161514session_variable_$v-
DEBPIGSTY3.41817161514postgresql-$v-session-variable-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
el8.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
el9.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
el9.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
el10.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
el10.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
d12.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
d12.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
d13.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
d13.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
u22.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
u22.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
u24.x86_64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
u24.aarch64
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4
PIGSTY 3.4

构建

您可以使用 pig build 命令构建 session_variable 扩展的 RPM / DEB 包:

pig build pkg session_variable         # 构建 RPM / DEB 包

安装

您可以直接安装 session_variable 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install session_variable;          # 当前活跃 PG 版本安装
pig ext install -y session_variable -v 18  # PG 18
pig ext install -y session_variable -v 17  # PG 17
pig ext install -y session_variable -v 16  # PG 16
pig ext install -y session_variable -v 15  # PG 15
pig ext install -y session_variable -v 14  # PG 14
dnf install -y session_variable_18       # PG 18
dnf install -y session_variable_17       # PG 17
dnf install -y session_variable_16       # PG 16
dnf install -y session_variable_15       # PG 15
dnf install -y session_variable_14       # PG 14
apt install -y postgresql-18-session-variable   # PG 18
apt install -y postgresql-17-session-variable   # PG 17
apt install -y postgresql-16-session-variable   # PG 16
apt install -y postgresql-15-session-variable   # PG 15
apt install -y postgresql-14-session-variable   # PG 14

创建扩展

CREATE EXTENSION session_variable;

用法

session_variable: 会话变量和常量的注册与操作

创建变量和常量

CREATE EXTENSION session_variable;

-- 创建带初始值的变量
SELECT session_variable.create_variable('my_var', 'text'::regtype, 'initial text'::text);

-- 创建初始值为 NULL 的变量
SELECT session_variable.create_variable('my_date_var', 'date'::regtype);

-- 创建常量(不能通过 set() 更改)
SELECT session_variable.create_constant('my_env', 'text'::regtype, 'Production'::text);

获取和设置值

-- 获取变量值(第二个参数是类型提示)
SELECT session_variable.get('my_var', null::text);

-- 设置变量值(返回之前的值)
SELECT session_variable.set('my_var', 'new text'::text);

在 PL/pgSQL 中使用

DO $$
DECLARE
    my_field text;
BEGIN
    my_field := session_variable.get('my_var', my_field);
    RAISE NOTICE 'Value: %', my_field;
END
$$ LANGUAGE plpgsql;

管理函数

-- 修改初始/常量值(影响新会话)
SELECT session_variable.alter_value('my_env', 'Development'::text);

-- 从数据库定义重新加载所有变量
SELECT session_variable.init();

-- 删除变量或常量
SELECT session_variable.drop('my_var');

-- 检查变量是否存在
SELECT session_variable.exists('my_var');

-- 获取变量类型
SELECT session_variable.type_of('my_var');

关键行为

  • 变量在数据库级别定义;每个会话获取本地副本
  • set() 仅更改会话本地副本;其他会话不受影响
  • alter_value() 更改存储的值;新会话将看到它,现有会话需要 init() 来刷新
  • 常量不能通过 set() 更改,只能通过 alter_value()
  • 变量和常量名称在两种类型之间必须唯一

最后修改 2026-03-14: update extension metadata (953cbd0)