Home Explore Blog CI



postgresql

38th chunk of `doc/src/sgml/ref/psql-ref.sgml`
734f309e05f4ad33b959c8cec0ab1a414126d5478dfb71a30000000100000fa3
 instead.  Except for that behavior, <literal>\g</literal>
        without any arguments is essentially equivalent to a semicolon.
        With arguments, <literal>\g</literal> provides
        a <quote>one-shot</quote> alternative to the <command>\o</command>
        command, and additionally allows one-shot adjustments of the
        output formatting options normally set by <literal>\pset</literal>.
        </para>
        <para>
        When the last argument begins with <literal>|</literal>, the entire
        remainder of the line is taken to be
        the <replaceable class="parameter">command</replaceable> to execute,
        and neither variable interpolation nor backquote expansion are
        performed in it.  The rest of the line is simply passed literally to
        the shell.
        </para>
        </listitem>
      </varlistentry>


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

        <listitem>
        <para>
         Shows the description (that is, the column names and data types)
         of the result of the current query buffer.  The query is not
         actually executed; however, if it contains some type of syntax
         error, that error will be reported in the normal way.
        </para>

        <para>
         If the current query buffer is empty, the most recently sent query
         is described instead.
        </para>
        </listitem>
      </varlistentry>


      <varlistentry id="app-psql-meta-command-getenv">
        <term><literal>\getenv <replaceable class="parameter">psql_var</replaceable> <replaceable class="parameter">env_var</replaceable></literal></term>

        <listitem>
        <para>
         Gets the value of the environment
         variable <replaceable class="parameter">env_var</replaceable>
         and assigns it to the <application>psql</application>
         variable <replaceable class="parameter">psql_var</replaceable>.
         If <replaceable class="parameter">env_var</replaceable> is
         not defined in the <application>psql</application> process's
         environment, <replaceable class="parameter">psql_var</replaceable>
         is not changed.  Example:
<programlisting>
=&gt; <userinput>\getenv home HOME</userinput>
=&gt; <userinput>\echo :home</userinput>
/home/postgres
</programlisting></para>
        </listitem>
      </varlistentry>


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

        <listitem>
        <para>
         Sends the current query buffer to the server, then treats
         each column of each row of the query's output (if any) as an SQL
         statement to be executed.  For example, to create an index on each
         column of <structname>my_table</structname>:
<programlisting>
=&gt; <userinput>SELECT format('create index on my_table(%I)', attname)</userinput>
-&gt; <userinput>FROM pg_attribute</userinput>
-&gt; <userinput>WHERE attrelid = '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

Title: psql Meta-Commands: \gdesc, \getenv, and \gexec
Summary
This section details the `psql` meta-commands `\gdesc`, `\getenv`, and `\gexec`. * `\gdesc`: Shows the description (column names and data types) of the result of the current query buffer without executing it. If the buffer is empty, the most recent query is described. * `\getenv`: Gets the value of an environment variable and assigns it to a `psql` variable. If the environment variable is not defined, the `psql` variable remains unchanged. * `\gexec`: Sends the current query buffer to the server and then treats each column of each row of the query's output as an SQL statement to be executed. The generated queries are executed in the order the rows are returned. NULL fields are ignored. Execution continues even if individual queries fail, unless `ON_ERROR_STOP` is set.