SET statement (session variables)
The SET statement has two uses: changing the value of a system parameter, and assigning user-defined variables that keep a value for the rest of your session.
System parameters
Section titled “System parameters”SET SYSTEM PARAMETERS 'parameter_name=value [; parameter_name=value ...]'Only some parameters can be changed while the database is running; which ones, and whether a change applies to your session or to the whole server, is listed in Using SQL statements of the configuration chapter. Set several parameters at once by separating them with semicolons:
SET SYSTEM PARAMETERS 'cte_max_recursions=100; lock_timeout=10';Use DEFAULT as the value to go back to the parameter's default:
SET SYSTEM PARAMETERS 'lock_timeout=DEFAULT; cte_max_recursions=DEFAULT';User-defined variables
Section titled “User-defined variables”A user-defined variable is a name that starts with @ and holds one value for the rest of your session (connection). Variables are private to the session that created them and disappear when it ends.
SET @name = expression [, @name = expression ...]SET @name := expression [, ...]
-- inside another statement@name := expression
{DEALLOCATE | DROP} VARIABLE @name [, @name ...]- Names are made of letters, digits and underscores, and are not case-sensitive:
@V1and@v1are the same variable. - A name cannot be a reserved word:
@limitor@lastis a syntax error. Choose names such as@max_rowsor@last_day. - In
SET, both=and:=assign. Inside other statements, use:=, because=is the comparison operator there. - Reading a variable that was never set, or was dropped, is an error:
Session variable '@v1' not defined.
Assigning and using variables
Section titled “Assigning and using variables”Store a value, then use it in a later query:
SET @min_price = 120;
SELECT id, name, unit_priceFROM productWHERE unit_price > @min_priceORDER BY unit_price DESC;| id | name | unit_price |
|---|---|---|
| 38 | Côte de Blaye | 263.50 |
| 29 | Thüringer Rostbratwurst | 123.79 |
Capture a query result with := and reuse it. The last order date is stored in @last_day, and the next query counts the orders of that day:
SELECT @last_day := MAX(order_date) FROM orders;SELECT COUNT(*) FROM orders WHERE order_date = @last_day;5Variables are also a convenient way to pass values to prepared statements:
SET @min_price = 120, @max_rows = 2;PREPARE by_price FROM 'SELECT id, name FROM product WHERE unit_price > ? LIMIT ?';EXECUTE by_price USING @min_price, @max_rows;Assigning in every row
Section titled “Assigning in every row”An assignment in the select list runs once per row, so a variable can number the rows of a result. The five latest orders of customer 440:
SET @n = 0;
SELECT @n := @n + 1 AS row_no, id, order_dateFROM ordersWHERE customer_id = 440ORDER BY order_date DESCLIMIT 5;| row_no | id | order_date |
|---|---|---|
| 1 | 1651 | 2025-12-27 |
| 2 | 462 | 2025-12-15 |
| 3 | 2544 | 2025-11-16 |
| 4 | 1611 | 2025-11-13 |
| 5 | 1831 | 2025-11-03 |
For numbering rows, the window function ROW_NUMBER() is usually the clearer choice. When a statement both assigns and reads the same variable in different clauses (for example, assigns it in the select list and reads it in HAVING), the order of evaluation is not guaranteed.
Variable types
Section titled “Variable types”A variable takes the type of the value assigned to it, and assigning a new value can change the type:
SET @v1 = 1, @v2 = CAST(1 AS BIGINT), @v3 = '123', @v4 = DATE'2025-12-01';SELECT TYPEOF(@v1), TYPEOF(@v2), TYPEOF(@v3), TYPEOF(@v4);| typeof(@v1) | typeof(@v2) | typeof(@v3) | typeof(@v4) |
|---|---|---|---|
| integer | bigint | character (-1) | character varying (1073741823) |
Variables can hold SHORT, INTEGER, BIGINT, FLOAT, DOUBLE, NUMERIC, CHAR, VARCHAR, BIT and BIT VARYING values. Values of other types are converted to VARCHAR; that is why the date in @v4 is stored as a character string. Comparisons such as order_date = @last_day above still work, because the string is converted back to a date for the comparison.
Dropping variables
Section titled “Dropping variables”DEALLOCATE VARIABLE and DROP VARIABLE are the same statement:
DEALLOCATE VARIABLE @v1, @v2;DROP VARIABLE @v3, @v4;Other SET and GET statements
Section titled “Other SET and GET statements”Several other statements start with SET and change a setting of the current session; most have a GET counterpart that reads the setting, optionally into a host variable with INTO :variable (or TO :variable):
| Statement | Reads with | Described in |
|---|---|---|
SET TRANSACTION ISOLATION LEVEL level |
GET TRANSACTION ISOLATION LEVEL |
Isolation levels |
SET TRANSACTION LOCK TIMEOUT { INFINITE | OFF | seconds } |
GET TRANSACTION LOCK TIMEOUT |
Locking & deadlocks |
SET OPTIMIZATION LEVEL level |
GET OPTIMIZATION LEVEL |
Query plans |
SET TRACE { ON | OFF } [ OUTPUT { TEXT | JSON } ] |
SHOW TRACE |
Query profiling |
SET TRIGGER TRACE { ON | OFF } |
GET TRIGGER TRACE |
Triggers |
SET TRIGGER [MAXIMUM] DEPTH count |
GET TRIGGER [MAXIMUM] DEPTH |
Triggers |
SET NAMES charset [COLLATE collation] |
SET NAMES | |
GET STATISTICS 'item' OF table |
Statistics |
SET TRANSACTION LOCK TIMEOUT 5;GET TRANSACTION LOCK TIMEOUT INTO :lt;GET TRANSACTION ISOLATION LEVEL INTO :iso;SELECT :lt, :iso; -- 5, 4 (4 = READ COMMITTED)SET OPTIMIZATION COST and GET OPTIMIZATION COST adjust and read internal cost settings of the optimizer, for diagnosis only; leave them at their defaults.