session_variable
session_variable
session_variable : Registration and manipulation of session variables and constants
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 9120 | session_variable | session_variable | 3.4 | 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 pgtt pg_statement_rollback plpgsql set_user oracle_fdw pg_dbms_lock babelfishpg_common |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 3.4 | 18 17 16 15 14 | session_variable | - |
| RPM | PIGSTY | 3.4 | 18 17 16 15 14 | session_variable_$v | - |
| DEB | PIGSTY | 3.4 | 18 17 16 15 14 | postgresql-$v-session-variable | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
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 |
Source
pig build pkg session_variable; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall 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 14Create this extension with:
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 unaffectedalter_value()changes the stored value; new sessions see it, existing sessions needinit()to refresh- Constants cannot be changed via
set(), only viaalter_value() - Variable and constant names must be unique across both types
Last updated on