Home Explore Blog CI



postgresql

62th chunk of `doc/src/sgml/ref/psql-ref.sgml`
74588b143720301164185f85f994c6bd95be849f352d4e8a0000000100000fa0
 (including non-Latin letters), digits, and underscores.
    </para>

    <para>
    To set a variable, use the <application>psql</application> meta-command
    <command>\set</command>.  For example,
<programlisting>
testdb=&gt; <userinput>\set foo bar</userinput>
</programlisting>
    sets the variable <literal>foo</literal> to the value
    <literal>bar</literal>. To retrieve the content of the variable, precede
    the name with a colon, for example:
<programlisting>
testdb=&gt; <userinput>\echo :foo</userinput>
bar
</programlisting>
    This works in both regular SQL commands and meta-commands; there is
    more detail in <xref linkend="app-psql-interpolation"/>, below.
    </para>

    <para>
    If you call <command>\set</command> without a second argument, the
    variable is set to an empty-string value. To unset (i.e., delete)
    a variable, use the command <command>\unset</command>.  To show the
    values of all variables, call <command>\set</command> without any argument.
    </para>

    <note>
    <para>
    The arguments of <command>\set</command> are subject to the same
    substitution rules as with other commands. Thus you can construct
    interesting references such as <literal>\set :foo
    'something'</literal> and get <quote>soft links</quote> or
    <quote>variable variables</quote> of <productname>Perl</productname>
    or <productname><acronym>PHP</acronym></productname> fame,
    respectively. Unfortunately (or fortunately?), there is no way to do
    anything useful with these constructs. On the other hand,
    <literal>\set bar :foo</literal> is a perfectly valid way to copy a
    variable.
    </para>
    </note>

    <para>
    A number of these variables are treated specially
    by <application>psql</application>. They represent certain option
    settings that can be changed at run time by altering the value of
    the variable, or in some cases represent changeable state of
    <application>psql</application>.
    By convention, all specially treated variables' names
    consist of all upper-case ASCII letters (and possibly digits and
    underscores). To ensure maximum compatibility in the future, avoid
    using such variable names for your own purposes.
   </para>

   <para>
    Variables that control <application>psql</application>'s behavior
    generally cannot be unset or set to invalid values.  An <literal>\unset</literal>
    command is allowed but is interpreted as setting the variable to its
    default value.  A <literal>\set</literal> command without a second argument is
    interpreted as setting the variable to <literal>on</literal>, for control
    variables that accept that value, and is rejected for others.  Also,
    control variables that accept the values <literal>on</literal>
    and <literal>off</literal> will also accept other common spellings of Boolean
    values, such as <literal>true</literal> and <literal>false</literal>.
   </para>

   <para>
    The specially treated variables are:
   </para>

    <variablelist>
      <varlistentry id="app-psql-variables-autocommit">
      <term>
       <varname>AUTOCOMMIT</varname>
       <indexterm>
        <primary>autocommit</primary>
        <secondary>psql</secondary>
       </indexterm>
      </term>
        <listitem>
        <para>
        When <literal>on</literal> (the default), each SQL command is automatically
        committed upon successful completion.  To postpone commit in this
        mode, you must enter a <command>BEGIN</command> or <command>START
        TRANSACTION</command> SQL command.  When <literal>off</literal> or unset, SQL
        commands are not committed until you explicitly issue
        <command>COMMIT</command> or <command>END</command>.  The autocommit-off
        mode works by issuing an implicit <command>BEGIN</command> for you, just
        before any command that is not already in a transaction block and
        is not itself a <command>BEGIN</command> or other transaction-control
      

Title: psql Variables: Setting, Retrieving, and Special Variables
Summary
This section describes how to use variables in psql using the \set and \unset commands. It details how to set variables to specific values or empty strings, retrieve their content using a colon prefix, and unset them. It also explains that arguments of \set are subject to the same substitution rules as other commands. Furthermore, it describes how certain variables are treated specially by psql to control options and represent changeable state, and details specifically how AUTOCOMMIT is treated.