session_variable
Registration and manipulation of session variables and constants
Repository
splendiddata/session_variable
https://github.com/splendiddata/session_variable
Source
session_variable-3.4.tar.gz
session_variable-3.4.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
session_variable | 3.4 | SIM | GPL-3.0 | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 9120 | session_variable | No | Yes | No | Yes | No | Yes | - |
| Related | orafce pgtt pg_statement_rollback plpgsql set_user oracle_fdw pg_dbms_lock babelfishpg_common |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 3.4 | 1817161514 | session_variable | - |
| RPM | PIGSTY | 3.4 | 1817161514 | session_variable_$v | - |
| DEB | PIGSTY | 3.4 | 1817161514 | postgresql-$v-session-variable | - |
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 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
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.