A SQL SESSION begins when a client authenticates to the database and ends when the connection is closed or times out. Within this scope the server stores session-level data: configuration parameters, temporary tables, prepared statements, user or system variables, and the current transaction. Commands such as SET SESSION, SET LOCAL, or vendor-specific APIs read or modify these values. Every new connection starts with default settings, isolating one session’s changes from others. Sessions persist across multiple statements but never survive disconnects. Understanding sessions is essential for tuning performance (e.g., work_mem in PostgreSQL), enforcing security contexts (e.g., multi-tenant SESSION_CONTEXT in SQL Server), and guaranteeing repeatable behavior during batch jobs.
value
- any Literal or expression evaluated and stored for the sessionSET, TRANSACTION, CONNECTION, PREPARE, TEMPORARY TABLE, SESSION_CONTEXT
SQL-92 introduced SET SESSION CHARACTERISTICS
Session scope can include runtime parameters, temporary tables, prepared statements, cursors, and user-defined variables. Vendors may add more items such as role information or client tags.
No. When the server drops all connections during a restart, every session ends. All session data is lost unless saved elsewhere.
Yes, but always reset the parameter or use SET LOCAL inside a transaction to avoid leaking settings to the next borrower of the same pooled connection.
Most databases provide an introspection view. Example: SHOW ALL in PostgreSQL, SELECT @@GLOBAL and @@SESSION variables in MySQL, or sys.dm_exec_sessions in SQL Server.