Skip to content

session_variable

Registration and manipulation of session variables and constants

session_variable : Registration and manipulation of session variables and constants

Overview

ID Extension Package Version Category License Language
9120 session_variable session_variable 3.6 SIM GPL-3.0 C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-- No Yes No Yes no no
Relationships
Schemas session_variable
See Also orafce db2fce pgtt ivorysql_ora pg_statement_rollback pg_variables omni_var

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PIGSTY 3.6 18 17 16 15 14 session_variable -
RPM PIGSTY 3.6 18 17 16 15 14 session_variable_$v -
DEB PIGSTY 3.6 18 17 16 15 14 postgresql-$v-session-variable -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
el8.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
el9.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
el9.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
el10.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
el10.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
d12.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
d12.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
d13.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
d13.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u22.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u22.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u24.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u24.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u26.x86_64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
u26.aarch64 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6 PIGSTY 3.6
Package Version OS ORG SIZE File URL
session_variable_18 3.6 el8.x86_64 pigsty 61.3 KiB session_variable_18-3.6-1PGSTY.el8.x86_64.rpm
session_variable_18 3.6 el8.aarch64 pigsty 60.0 KiB session_variable_18-3.6-1PGSTY.el8.aarch64.rpm
session_variable_18 3.6 el9.x86_64 pigsty 61.6 KiB session_variable_18-3.6-1PGSTY.el9.x86_64.rpm
session_variable_18 3.6 el9.aarch64 pigsty 60.2 KiB session_variable_18-3.6-1PGSTY.el9.aarch64.rpm
session_variable_18 3.6 el10.x86_64 pigsty 61.8 KiB session_variable_18-3.6-1PGSTY.el10.x86_64.rpm
session_variable_18 3.6 el10.aarch64 pigsty 60.4 KiB session_variable_18-3.6-1PGSTY.el10.aarch64.rpm
postgresql-18-session-variable 3.6 d12.x86_64 pigsty 58.5 KiB postgresql-18-session-variable_3.6-1PGSTY~bookworm_amd64.deb
postgresql-18-session-variable 3.6 d12.aarch64 pigsty 57.4 KiB postgresql-18-session-variable_3.6-1PGSTY~bookworm_arm64.deb
postgresql-18-session-variable 3.6 d13.x86_64 pigsty 58.4 KiB postgresql-18-session-variable_3.6-1PGSTY~trixie_amd64.deb
postgresql-18-session-variable 3.6 d13.aarch64 pigsty 57.4 KiB postgresql-18-session-variable_3.6-1PGSTY~trixie_arm64.deb
postgresql-18-session-variable 3.6 u22.x86_64 pigsty 61.8 KiB postgresql-18-session-variable_3.6-1PGSTY~jammy_amd64.deb
postgresql-18-session-variable 3.6 u22.aarch64 pigsty 61.2 KiB postgresql-18-session-variable_3.6-1PGSTY~jammy_arm64.deb
postgresql-18-session-variable 3.6 u24.x86_64 pigsty 60.2 KiB postgresql-18-session-variable_3.6-1PGSTY~noble_amd64.deb
postgresql-18-session-variable 3.6 u24.aarch64 pigsty 59.7 KiB postgresql-18-session-variable_3.6-1PGSTY~noble_arm64.deb
postgresql-18-session-variable 3.6 u26.x86_64 pigsty 59.9 KiB postgresql-18-session-variable_3.6-1PGSTY~resolute_amd64.deb
postgresql-18-session-variable 3.6 u26.aarch64 pigsty 58.9 KiB postgresql-18-session-variable_3.6-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
session_variable_17 3.6 el8.x86_64 pigsty 61.3 KiB session_variable_17-3.6-1PGSTY.el8.x86_64.rpm
session_variable_17 3.6 el8.aarch64 pigsty 59.9 KiB session_variable_17-3.6-1PGSTY.el8.aarch64.rpm
session_variable_17 3.6 el9.x86_64 pigsty 61.5 KiB session_variable_17-3.6-1PGSTY.el9.x86_64.rpm
session_variable_17 3.6 el9.aarch64 pigsty 60.1 KiB session_variable_17-3.6-1PGSTY.el9.aarch64.rpm
session_variable_17 3.6 el10.x86_64 pigsty 61.7 KiB session_variable_17-3.6-1PGSTY.el10.x86_64.rpm
session_variable_17 3.6 el10.aarch64 pigsty 60.3 KiB session_variable_17-3.6-1PGSTY.el10.aarch64.rpm
postgresql-17-session-variable 3.6 d12.x86_64 pigsty 58.4 KiB postgresql-17-session-variable_3.6-1PGSTY~bookworm_amd64.deb
postgresql-17-session-variable 3.6 d12.aarch64 pigsty 57.4 KiB postgresql-17-session-variable_3.6-1PGSTY~bookworm_arm64.deb
postgresql-17-session-variable 3.6 d13.x86_64 pigsty 58.4 KiB postgresql-17-session-variable_3.6-1PGSTY~trixie_amd64.deb
postgresql-17-session-variable 3.6 d13.aarch64 pigsty 57.4 KiB postgresql-17-session-variable_3.6-1PGSTY~trixie_arm64.deb
postgresql-17-session-variable 3.6 u22.x86_64 pigsty 67.4 KiB postgresql-17-session-variable_3.6-1PGSTY~jammy_amd64.deb
postgresql-17-session-variable 3.6 u22.aarch64 pigsty 66.8 KiB postgresql-17-session-variable_3.6-1PGSTY~jammy_arm64.deb
postgresql-17-session-variable 3.6 u24.x86_64 pigsty 60.1 KiB postgresql-17-session-variable_3.6-1PGSTY~noble_amd64.deb
postgresql-17-session-variable 3.6 u24.aarch64 pigsty 59.6 KiB postgresql-17-session-variable_3.6-1PGSTY~noble_arm64.deb
postgresql-17-session-variable 3.6 u26.x86_64 pigsty 59.9 KiB postgresql-17-session-variable_3.6-1PGSTY~resolute_amd64.deb
postgresql-17-session-variable 3.6 u26.aarch64 pigsty 58.8 KiB postgresql-17-session-variable_3.6-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
session_variable_16 3.6 el8.x86_64 pigsty 61.3 KiB session_variable_16-3.6-1PGSTY.el8.x86_64.rpm
session_variable_16 3.6 el8.aarch64 pigsty 60.0 KiB session_variable_16-3.6-1PGSTY.el8.aarch64.rpm
session_variable_16 3.6 el9.x86_64 pigsty 61.5 KiB session_variable_16-3.6-1PGSTY.el9.x86_64.rpm
session_variable_16 3.6 el9.aarch64 pigsty 60.1 KiB session_variable_16-3.6-1PGSTY.el9.aarch64.rpm
session_variable_16 3.6 el10.x86_64 pigsty 61.7 KiB session_variable_16-3.6-1PGSTY.el10.x86_64.rpm
session_variable_16 3.6 el10.aarch64 pigsty 60.3 KiB session_variable_16-3.6-1PGSTY.el10.aarch64.rpm
postgresql-16-session-variable 3.6 d12.x86_64 pigsty 58.4 KiB postgresql-16-session-variable_3.6-1PGSTY~bookworm_amd64.deb
postgresql-16-session-variable 3.6 d12.aarch64 pigsty 57.4 KiB postgresql-16-session-variable_3.6-1PGSTY~bookworm_arm64.deb
postgresql-16-session-variable 3.6 d13.x86_64 pigsty 58.4 KiB postgresql-16-session-variable_3.6-1PGSTY~trixie_amd64.deb
postgresql-16-session-variable 3.6 d13.aarch64 pigsty 57.4 KiB postgresql-16-session-variable_3.6-1PGSTY~trixie_arm64.deb
postgresql-16-session-variable 3.6 u22.x86_64 pigsty 66.9 KiB postgresql-16-session-variable_3.6-1PGSTY~jammy_amd64.deb
postgresql-16-session-variable 3.6 u22.aarch64 pigsty 66.4 KiB postgresql-16-session-variable_3.6-1PGSTY~jammy_arm64.deb
postgresql-16-session-variable 3.6 u24.x86_64 pigsty 60.1 KiB postgresql-16-session-variable_3.6-1PGSTY~noble_amd64.deb
postgresql-16-session-variable 3.6 u24.aarch64 pigsty 59.6 KiB postgresql-16-session-variable_3.6-1PGSTY~noble_arm64.deb
postgresql-16-session-variable 3.6 u26.x86_64 pigsty 59.9 KiB postgresql-16-session-variable_3.6-1PGSTY~resolute_amd64.deb
postgresql-16-session-variable 3.6 u26.aarch64 pigsty 58.9 KiB postgresql-16-session-variable_3.6-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
session_variable_15 3.6 el8.x86_64 pigsty 61.6 KiB session_variable_15-3.6-1PGSTY.el8.x86_64.rpm
session_variable_15 3.6 el8.aarch64 pigsty 60.3 KiB session_variable_15-3.6-1PGSTY.el8.aarch64.rpm
session_variable_15 3.6 el9.x86_64 pigsty 62.1 KiB session_variable_15-3.6-1PGSTY.el9.x86_64.rpm
session_variable_15 3.6 el9.aarch64 pigsty 60.7 KiB session_variable_15-3.6-1PGSTY.el9.aarch64.rpm
session_variable_15 3.6 el10.x86_64 pigsty 62.2 KiB session_variable_15-3.6-1PGSTY.el10.x86_64.rpm
session_variable_15 3.6 el10.aarch64 pigsty 61.0 KiB session_variable_15-3.6-1PGSTY.el10.aarch64.rpm
postgresql-15-session-variable 3.6 d12.x86_64 pigsty 58.8 KiB postgresql-15-session-variable_3.6-1PGSTY~bookworm_amd64.deb
postgresql-15-session-variable 3.6 d12.aarch64 pigsty 57.6 KiB postgresql-15-session-variable_3.6-1PGSTY~bookworm_arm64.deb
postgresql-15-session-variable 3.6 d13.x86_64 pigsty 58.8 KiB postgresql-15-session-variable_3.6-1PGSTY~trixie_amd64.deb
postgresql-15-session-variable 3.6 d13.aarch64 pigsty 57.7 KiB postgresql-15-session-variable_3.6-1PGSTY~trixie_arm64.deb
postgresql-15-session-variable 3.6 u22.x86_64 pigsty 67.5 KiB postgresql-15-session-variable_3.6-1PGSTY~jammy_amd64.deb
postgresql-15-session-variable 3.6 u22.aarch64 pigsty 66.9 KiB postgresql-15-session-variable_3.6-1PGSTY~jammy_arm64.deb
postgresql-15-session-variable 3.6 u24.x86_64 pigsty 60.9 KiB postgresql-15-session-variable_3.6-1PGSTY~noble_amd64.deb
postgresql-15-session-variable 3.6 u24.aarch64 pigsty 60.0 KiB postgresql-15-session-variable_3.6-1PGSTY~noble_arm64.deb
postgresql-15-session-variable 3.6 u26.x86_64 pigsty 60.4 KiB postgresql-15-session-variable_3.6-1PGSTY~resolute_amd64.deb
postgresql-15-session-variable 3.6 u26.aarch64 pigsty 59.7 KiB postgresql-15-session-variable_3.6-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
session_variable_14 3.6 el8.x86_64 pigsty 61.6 KiB session_variable_14-3.6-1PGSTY.el8.x86_64.rpm
session_variable_14 3.6 el8.aarch64 pigsty 60.3 KiB session_variable_14-3.6-1PGSTY.el8.aarch64.rpm
session_variable_14 3.6 el9.x86_64 pigsty 62.1 KiB session_variable_14-3.6-1PGSTY.el9.x86_64.rpm
session_variable_14 3.6 el9.aarch64 pigsty 60.7 KiB session_variable_14-3.6-1PGSTY.el9.aarch64.rpm
session_variable_14 3.6 el10.x86_64 pigsty 62.3 KiB session_variable_14-3.6-1PGSTY.el10.x86_64.rpm
session_variable_14 3.6 el10.aarch64 pigsty 61.0 KiB session_variable_14-3.6-1PGSTY.el10.aarch64.rpm
postgresql-14-session-variable 3.6 d12.x86_64 pigsty 58.7 KiB postgresql-14-session-variable_3.6-1PGSTY~bookworm_amd64.deb
postgresql-14-session-variable 3.6 d12.aarch64 pigsty 57.6 KiB postgresql-14-session-variable_3.6-1PGSTY~bookworm_arm64.deb
postgresql-14-session-variable 3.6 d13.x86_64 pigsty 58.7 KiB postgresql-14-session-variable_3.6-1PGSTY~trixie_amd64.deb
postgresql-14-session-variable 3.6 d13.aarch64 pigsty 57.7 KiB postgresql-14-session-variable_3.6-1PGSTY~trixie_arm64.deb
postgresql-14-session-variable 3.6 u22.x86_64 pigsty 66.4 KiB postgresql-14-session-variable_3.6-1PGSTY~jammy_amd64.deb
postgresql-14-session-variable 3.6 u22.aarch64 pigsty 65.8 KiB postgresql-14-session-variable_3.6-1PGSTY~jammy_arm64.deb
postgresql-14-session-variable 3.6 u24.x86_64 pigsty 60.8 KiB postgresql-14-session-variable_3.6-1PGSTY~noble_amd64.deb
postgresql-14-session-variable 3.6 u24.aarch64 pigsty 59.9 KiB postgresql-14-session-variable_3.6-1PGSTY~noble_arm64.deb
postgresql-14-session-variable 3.6 u26.x86_64 pigsty 60.3 KiB postgresql-14-session-variable_3.6-1PGSTY~resolute_amd64.deb
postgresql-14-session-variable 3.6 u26.aarch64 pigsty 59.7 KiB postgresql-14-session-variable_3.6-1PGSTY~resolute_arm64.deb

Source

github.com/splendiddata/session_variable

pig build pkg session_variable;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install session_variable;		# install via package name, for the active PG version

pig install session_variable -v 18;   # install for PG 18
pig install session_variable -v 17;   # install for PG 17
pig install session_variable -v 16;   # install for PG 16
pig install session_variable -v 15;   # install for PG 15
pig install session_variable -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION session_variable;

Usage

Sources:

session_variable defines database-level variables and constants with a separate value in each session. Changes to session-local values do not affect other connections.

Creating Variables and Constants

CREATE EXTENSION session_variable;

-- Create a variable with initial value
SELECT session_variable.create_variable('my_var', 'text'::regtype, 'initial text'::text);

-- Create a variable with NULL initial value
SELECT session_variable.create_variable('my_date_var', 'date'::regtype);

-- Create a constant (cannot be changed via set())
SELECT session_variable.create_constant('my_env', 'text'::regtype, 'Production'::text);

Getting and Setting Values

-- Get variable value (second arg is type hint)
SELECT session_variable.get('my_var', null::text);

-- Set variable value (returns true on success)
SELECT session_variable.set('my_var', 'new text'::text);

Using in 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;

Administration Functions

-- Alter the initial/constant value (affects new sessions)
SELECT session_variable.alter_value('my_env', 'Development'::text);

-- Reload all variables from database definitions
SELECT session_variable.init();

-- Drop a variable or constant
SELECT session_variable.drop('my_var');

-- Check if a variable exists
SELECT session_variable.exists('my_var');

-- Get the type of a variable
SELECT session_variable.type_of('my_var');

Key Behaviors

  • Variables are defined at the database level; each session gets a local copy
  • set() only changes the session-local copy; other sessions are unaffected
  • alter_value() changes the stored value; new sessions see it, existing sessions need init() to refresh
  • Constants cannot be changed via set(), only via alter_value()
  • Variable and constant names must be unique across both types

Privileges, Reads and Version Boundaries

A superuser installs this non-relocatable extension in schema session_variable; no shared preload or restart is required for the SQL workflow. Grant session_variable_administrator_role to manage definitions and session_variable_user_role for ordinary access. The administrator role includes the user role.

session_variable.set and session_variable.alter_value return boolean true on success, not the previous value. A successful stored-definition change becomes visible in the caller immediately and in future sessions after commit; existing sessions keep their local state until session_variable.init() resets all values. Definitions are stored in session_variable.variables and included in logical backups.

session_variable.get_stable may cache a value during a statement; use the regular getter when a trigger changes that value within the same statement. session_variable.get_constant is IMMUTABLE and can return cached results after administrative constant changes; use the regular getter during such changes.

Release 3.6 removes obsolete version-1 initialization support. The preceding 3.5 rejects non-null initial values for collection and icollection because text initialization can crash a backend. For those types, create a null-initialized variable and follow the source’s initialization hook session_variable.variable_initialisation(); the README’s alternate hook name is inconsistent with the implementation. Upstream lists PostgreSQL 14-18 and provisional PostgreSQL 19 support.