Global Variables in YSQL

There are a lot of configuration parameters in YSQL that affect the behavior of the Yugabyte database system.

The SHOW command in YSQL will display the current setting of run-time parameters.

Example:

				
					yugabyte=# SHOW yb_enable_expression_pushdown;
 yb_enable_expression_pushdown
-------------------------------
 off
(1 row)
				
			

What if we want to create our own parameters? We can via the SET statement!

Example:

				
					yugabyte=# SET my.dog = 'Lucy';
SET

yugabyte=# SHOW my.dog;
 my.dog
--------
 Lucy
(1 row)
				
			

We can use our user defined parameters in SQL statements, including stored procedures.

Example:

				
					yugabyte=# CREATE TABLE dog(name VARCHAR, breed VARCHAR);
CREATE TABLE

yugabyte=# INSERT INTO dog VALUES ('Lucy', 'Havenese'), ('Captain', 'Labrador Retriever');
INSERT 0 2

yugabyte=# CREATE OR REPLACE FUNCTION dog_breed()
yugabyte-# RETURNS VARCHAR AS $$
yugabyte$#   SELECT breed FROM dog WHERE name = current_setting('my.dog');
yugabyte$# $$ LANGUAGE sql;
CREATE FUNCTION

yugabyte=# SHOW my.dog;
 my.dog
--------
 Lucy
(1 row)

yugabyte=# SELECT dog_breed();
 dog_breed
-----------
 Havenese
(1 row)

yugabyte=# SET my.dog = 'Captain';
SET

yugabyte=# SELECT dog_breed();
     dog_breed
--------------------
 Labrador Retriever
(1 row)
				
			

Note tha the SET statement only sets the value for the current session. If you want the parameter to persist and be available in subsequent sessions, the ALTER DATABASE statement can me employed. 

Example:

				
					yugabyte=# SELECT current_database();
 current_database
------------------
 yugabyte
(1 row)

yugabyte=# ALTER DATABASE yugabyte SET my.dog = 'Lucy';
ALTER DATABASE
				
			

Have Fun!