DO.xml 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
  <title id="af20941">DO</title>
  <body>
    <p id="sql_command_desc">Executes an anonymous code block as a transient anonymous function.</p>
    <section id="section2">
      <title>Synopsis</title>
      <codeblock id="sql_command_synopsis">DO [ LANGUAGE <varname>lang_name</varname> ] <varname>code</varname></codeblock>
    </section>
    <section id="section3">
      <title>Description</title>
      <p><codeph>DO</codeph> executes an anonymous code block, or in other words a transient
        anonymous function in a procedural language.</p>
      <p>The code block is treated as though it were the body of a function with no parameters,
        returning void. It is parsed and executed a single time.</p>
      <p>The optional <codeph>LANGUAGE</codeph> clause can appear either before or after the code
        block.</p>
      <p> Anonymous blocks are procedural language structures that provide the capability to create
        and execute procedural code on the fly without persistently storing the code as database
        objects in the system catalogs. The concept of anonymous blocks is similar to UNIX shell
        scripts, which enable several manually entered commands to be grouped and executed as one
        step. As the name implies, anonymous blocks do not have a name, and for this reason they
        cannot be referenced from other objects. Although built dynamically, anonymous blocks can be
        easily stored as scripts in the operating system files for repetitive execution. </p>
      <p>Anonymous blocks are standard procedural language blocks. They carry the syntax and obey
        the rules that apply to the procedural language, including declaration and scope of
        variables, execution, exception handling, and language usage. </p>
      <p>The compilation and execution of anonymous blocks are combined in one step, while a
        user-defined function needs to be re-defined before use each time its definition
        changes.</p>
    </section>
    <section id="section4">
      <title>Parameters</title>
      <parml>
        <plentry>
          <pt>
            <varname>code</varname>
          </pt>
          <pd>The procedural language code to be executed. This must be specified as a string
            literal, just as with the <codeph>CREATE FUNCTION</codeph> command. Use of a
            dollar-quoted literal is recommended. Optional keywords have no effect. These procedural
            languages are supported: PL/pgSQL (<codeph>plpgsql</codeph>), PL/Python
              (<codeph>plpythonu</codeph>), and PL/Perl (<codeph>plperl</codeph> and
              <codeph>plperlu</codeph>).</pd>
        </plentry>
        <plentry>
          <pt>
            <varname>lang_name</varname>
          </pt>
          <pd>The name of the procedural language that the code is written in. The default is
              <codeph>plpgsql</codeph>. The language must be installed on the Greenplum Database
            system and registered in the database.</pd>
        </plentry>
      </parml>
    </section>
    <section id="section5">
      <title>Notes</title>
      <p>The PL/pgSQL language is installed on the Greenplum Database system and is registered in a
61
        user created database. The PL/Python language is installed by default, but not registered. Other languages are
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        not installed or registered. The system catalog <codeph>pg_language</codeph> contains
        information about the registered languages in a database.</p>
      <p>The user must have <codeph>USAGE</codeph> 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.</p>
    </section>
    <section>
      <title>Examples</title>
      <p>This PL/pgSQL example grants all privileges on all views in schema <i>public</i> to role
          <codeph>webuser</codeph>:</p>
      <codeblock>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$$;</codeblock>
      <p>This PL/pgSQL example determines if a Greenplum Database user is a superuser. In the
        example, the anonymous block retrieves the input value from a temporary
        table.<codeblock>CREATE TEMP TABLE list AS VALUES ('gpadmin') DISTRIBUTED RANDOMLY;

DO $$ 
DECLARE
  name TEXT := 'gpadmin' ;
  superuser TEXT := '' ;
  t1_row   pg_authid%ROWTYPE;
BEGIN
  SELECT * INTO t1_row FROM pg_authid, list 
     WHERE pg_authid.rolname = name ;
  IF t1_row.rolsuper = 'f' THEN
    superuser := 'not ';
  END IF ;
  RAISE NOTICE 'user % is %a superuser', t1_row.rolname, superuser ;
END $$ LANGUAGE plpgsql ;</codeblock></p>
    </section>
    <note type="note">The example PL/pgSQL uses <codeph>SELECT</codeph> with the
        <codeph>INTO</codeph> clause. It is different from the SQL command <codeph>SELECT
        INTO</codeph>.</note>
    <section id="section6">
      <title>Compatibility</title>
      <p>There is no <codeph>DO</codeph> statement in the SQL standard.</p>
    </section>
    <section id="section7">
      <title>See Also</title>
      <p>
        <codeph>CREATE LANGUAGE</codeph>
        <xref href="../extensions/pl_sql.xml#topic1"/></p>
    </section>
  </body>
</topic>