Home Explore Blog CI



postgresql

39th chunk of `doc/src/sgml/ref/psql-ref.sgml`
5b31b4b83f1899d256db3aa79534ec4ab4f1756177d499190000000100000fa5
 'my_table'::regclass AND attnum &gt; 0</userinput>
-&gt; <userinput>ORDER BY attnum</userinput>
-&gt; <userinput>\gexec</userinput>
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
</programlisting>
        </para>

        <para>
         The generated queries are executed in the order in which the rows
         are returned, and left-to-right within each row if there is more
         than one column.  NULL fields are ignored.  The generated queries
         are sent literally to the server for processing, so they cannot be
         <application>psql</application> meta-commands nor contain <application>psql</application>
         variable references.  If any individual query fails, execution of
         the remaining queries continues
         unless <varname>ON_ERROR_STOP</varname> is set.  Execution of each
         query is subject to <varname>ECHO</varname> processing.
         (Setting <varname>ECHO</varname> to <literal>all</literal>
         or <literal>queries</literal> is often advisable when
         using <command>\gexec</command>.)  Query logging, single-step mode,
         timing, and other query execution features apply to each generated
         query as well.
        </para>
        <para>
         If the current query buffer is empty, the most recently sent query
         is re-executed instead.
        </para>
        </listitem>
      </varlistentry>


      <varlistentry id="app-psql-meta-command-gset">
        <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>

        <listitem>
        <para>
         Sends the current query buffer to the server and stores the
         query's output into <application>psql</application> variables
         (see <xref linkend="app-psql-variables"/> below).
         The query to be executed must return exactly one row.  Each column of
         the row is stored into a separate variable, named the same as the
         column.  For example:
<programlisting>
=&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
-&gt; <userinput>\gset</userinput>
=&gt; <userinput>\echo :var1 :var2</userinput>
hello 10
</programlisting>
        </para>
        <para>
         If you specify a <replaceable class="parameter">prefix</replaceable>,
         that string is prepended to the query's column names to create the
         variable names to use:
<programlisting>
=&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
-&gt; <userinput>\gset result_</userinput>
=&gt; <userinput>\echo :result_var1 :result_var2</userinput>
hello 10
</programlisting>
        </para>
        <para>
         If a column result is NULL, the corresponding variable is unset
         rather than being set.
        </para>
        <para>
         If the query fails or does not return one row,
         no variables are changed.
        </para>
        <para>
         If the current query buffer is empty, the most recently sent query
         is re-executed instead.
        </para>
        </listitem>
      </varlistentry>


      <varlistentry id="app-psql-meta-command-gx">
        <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
        <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
        <listitem>
        <para>
        <literal>\gx</literal> is equivalent to <literal>\g</literal>, except
        that it forces expanded output mode for this query, as
        if <literal>expanded=on</literal> were included in the list of
        <literal>\pset</literal> options.  See also <literal>\x</literal>.
        </para>
        </listitem>
      </varlistentry>


      <varlistentry id="app-psql-meta-command-help">
        <term><literal>\h</literal>

Title: psql Meta-Commands: \gexec, \gset, and \gx
Summary
This section details the `psql` meta-commands `\gexec`, `\gset`, and `\gx`. * `\gexec`: Further elaborates on the `\gexec` command, emphasizing that generated queries are sent literally to the server, cannot be `psql` meta-commands, nor contain `psql` variable references. It also details query execution features like logging, single-step mode, and timing, and that it re-executes the most recently sent query if the current buffer is empty. * `\gset`: Sends the current query buffer to the server, stores the query's output into `psql` variables named after the columns, and if a prefix is specified, that string is prepended to the variable names. If the result is NULL, the variable is unset. No variables are changed if the query fails or doesn't return one row. It also re-executes the most recently sent query if the current buffer is empty. * `\gx`: Is equivalent to `\g` but forces expanded output mode for the query.