Home Explore Blog CI



postgresql

doc/src/sgml/ref/do.sgml
0585cac30b1e50e5853441ad2a8d4930c5a4f09b491b80ad0000000300000e34
<!--
doc/src/sgml/ref/do.sgml
PostgreSQL documentation
-->

<refentry id="sql-do">
 <indexterm zone="sql-do">
  <primary>DO</primary>
 </indexterm>

 <indexterm zone="sql-do">
  <primary>anonymous code blocks</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>DO</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>DO</refname>
  <refpurpose>execute an anonymous code block</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
DO [ LANGUAGE <replaceable class="parameter">lang_name</replaceable> ] <replaceable class="parameter">code</replaceable>
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>DO</command> executes an anonymous code block, or in other
   words a transient anonymous function in a procedural language.
  </para>

  <para>
   The code block is treated as though it were the body of a function
   with no parameters, returning <type>void</type>.  It is parsed and
   executed a single time.
  </para>

  <para>
   The optional <literal>LANGUAGE</literal> clause can be written either
   before or after the code block.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">code</replaceable></term>
    <listitem>
     <para>
      The procedural language code to be executed.  This must be specified
      as a string literal, just as in <command>CREATE FUNCTION</command>.
      Use of a dollar-quoted literal is recommended.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">lang_name</replaceable></term>
    <listitem>
     <para>
      The name of the procedural language the code is written in.
      If omitted, the default is <literal>plpgsql</literal>.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   The procedural language to be used must already have been installed
   into the current database by means of <command>CREATE EXTENSION</command>.
   <literal>plpgsql</literal> is installed by default, but other languages are not.
  </para>

  <para>
   The user must have <literal>USAGE</literal> privilege for the procedural
   language, or must be a superuser if the language is untrusted.
   This is the same privilege requirement as for creating a function
   in the language.
  </para>

  <para>
   If <command>DO</command> is executed in a transaction block, then the
   procedure code cannot execute transaction control statements.  Transaction
   control statements are only allowed if <command>DO</command> is executed in
   its own transaction.
  </para>
 </refsect1>

 <refsect1 id="sql-do-examples">
  <title>Examples</title>
  <para>
   Grant all privileges on all views in schema <literal>public</literal> to
   role <literal>webuser</literal>:
<programlisting>
DO $$DECLARE r record;
BEGIN
    FOR r IN SELECT table_schema, table_name FROM information_schema.tables
             WHERE table_type = 'VIEW' AND table_schema = 'public'
    LOOP
        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
    END LOOP;
END$$;
</programlisting></para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>DO</command> statement in the SQL standard.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-createlanguage"/></member>
  </simplelist>
 </refsect1>
</refentry>

Chunks
4f70722a (1st chunk of `doc/src/sgml/ref/do.sgml`)
Title: DO: Execute an Anonymous Code Block
Summary
The SQL command `DO` executes an anonymous code block, which is essentially a temporary function in a procedural language. The code block is treated as the body of a parameter-less function returning `void`, parsed and executed once. The `LANGUAGE` clause specifies the procedural language (defaulting to `plpgsql`), and the user needs `USAGE` privilege for the language or superuser status for untrusted languages. Transaction control statements are only allowed if `DO` is executed in its own transaction.