session_variable

Registration and manipulation of session variables and constants

Overview

PackageVersionCategoryLicenseLanguage
session_variable3.4SIMGPL-3.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
9120session_variableNoYesNoYesNoYes-
Relatedorafce pgtt pg_statement_rollback plpgsql set_user oracle_fdw pg_dbms_lock babelfishpg_common

Version

TypeRepoVersionPG VerPackageDeps
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

Build

You can build the RPM / DEB packages for session_variable using pig build:

pig build pkg session_variable         # build RPM / DEB packages

Install

You can install session_variable directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install session_variable;          # Install for current active PG version
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:

CREATE EXTENSION session_variable;

Usage

session_variable: Registration and manipulation of session variables and constants

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 previous value)
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

Last Modified 2026-03-12: add pg extension catalog (95749bf)