@@ -2025,6 +2025,7 @@ REVOKE ALL ON accounts FROM PUBLIC;
20252025 For sequences, this privilege also allows use of the
20262026 <function>currval</function> function.
20272027 For large objects, this privilege allows the object to be read.
2028+ For session variables, this privilege allows the object to be read.
20282029 </para>
20292030 </listitem>
20302031 </varlistentry>
@@ -2060,6 +2061,8 @@ REVOKE ALL ON accounts FROM PUBLIC;
20602061 <function>setval</function> functions.
20612062 For large objects, this privilege allows writing or truncating the
20622063 object.
2064+ For session variables, this privilege allows to set a value to the
2065+ object.
20632066 </para>
20642067 </listitem>
20652068 </varlistentry>
@@ -2304,7 +2307,8 @@ REVOKE ALL ON accounts FROM PUBLIC;
23042307 <literal>LARGE OBJECT</literal>,
23052308 <literal>SEQUENCE</literal>,
23062309 <literal>TABLE</literal> (and table-like objects),
2307- table column
2310+ table column,
2311+ <literal>SESSION VARIABLE</literal>
23082312 </entry>
23092313 </row>
23102314 <row>
@@ -2319,7 +2323,8 @@ REVOKE ALL ON accounts FROM PUBLIC;
23192323 <literal>LARGE OBJECT</literal>,
23202324 <literal>SEQUENCE</literal>,
23212325 <literal>TABLE</literal>,
2322- table column
2326+ table column,
2327+ <literal>SESSION VARIABLE</literal>
23232328 </entry>
23242329 </row>
23252330 <row>
@@ -2506,6 +2511,12 @@ REVOKE ALL ON accounts FROM PUBLIC;
25062511 <entry><literal>U</literal></entry>
25072512 <entry><literal>\dT+</literal></entry>
25082513 </row>
2514+ <row>
2515+ <entry><literal>SESSION VARIABLE</literal></entry>
2516+ <entry><literal>rw</literal></entry>
2517+ <entry><literal>none</literal></entry>
2518+ <entry><literal>\dV+</literal></entry>
2519+ </row>
25092520 </tbody>
25102521 </tgroup>
25112522 </table>
@@ -5362,6 +5373,71 @@ EXPLAIN SELECT count(*) FROM measurement WHERE logdate >= DATE '2008-01-01';
53625373 </para>
53635374 </sect1>
53645375
5376+ <sect1 id="ddl-session-variables">
5377+ <title>Session Variables</title>
5378+
5379+ <indexterm zone="ddl-session-variables">
5380+ <primary>Session variables</primary>
5381+ </indexterm>
5382+
5383+ <indexterm>
5384+ <primary>session variable</primary>
5385+ </indexterm>
5386+
5387+ <para>
5388+ Session variables are database objects that can hold a value.
5389+ Session variables, like relations, exist within a schema and their access
5390+ is controlled via <command>GRANT</command> and <command>REVOKE</command>
5391+ commands. A session variable can be created by the <command>CREATE
5392+ VARIABLE</command> command.
5393+ </para>
5394+
5395+ <para>
5396+ The session variable holds value in session memory. This value is private
5397+ to each session and is released when the session ends.
5398+ </para>
5399+
5400+ <para>
5401+ In an query the session variable can be used only inside
5402+ <firstterm>variable fence</firstterm>. This is special syntax for
5403+ session variable identifier, and can be used only for session variable
5404+ identifier. The special syntax for accessing session variables removes
5405+ risk of collisions between variable identifiers and column names.
5406+ </para>
5407+
5408+ <para>
5409+ The value of a session variable is set with the SQL statement
5410+ <command>LET</command>. The value of a session variable can be retrieved
5411+ with the SQL statement <command>SELECT</command>.
5412+ <programlisting>
5413+ CREATE VARIABLE var1 AS date;
5414+ LET var1 = current_date;
5415+ SELECT VARIABLE(var1);
5416+ </programlisting>
5417+
5418+ or
5419+
5420+ <programlisting>
5421+ CREATE VARIABLE public.current_user_id AS integer;
5422+ GRANT SELECT ON VARIABLE public.current_user_id TO PUBLIC;
5423+ LET current_user_id = (SELECT id FROM users WHERE usename = session_user);
5424+ SELECT VARIABLE(current_user_id);
5425+ </programlisting>
5426+ </para>
5427+
5428+ <para>
5429+ The value of a session variable is local to the current session. Retrieving
5430+ a variable's value returns a <literal>NULL</literal>, unless its value has
5431+ been set to something else in the current session using the
5432+ <command>LET</command> command. Session variables are not transactional:
5433+ any changes made to the value of a session variable in a transaction won't
5434+ be undone if the transaction is rolled back (just like variables in
5435+ procedural languages). Session variables themselves are persistent, but
5436+ their values are neither persistent nor shared (like the content of
5437+ temporary tables).
5438+ </para>
5439+ </sect1>
5440+
53655441 <sect1 id="ddl-others">
53665442 <title>Other Database Objects</title>
53675443
0 commit comments