DO

DO

Synopsis

Description

DO executes an anonymous code block, or in other words a transient
anonymous function in a procedural language.

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.

The optional LANGUAGE clause can appear either before or after the code
block.

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.

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.

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.

Parameters

code

The procedural language code to be executed. This must be specified as a string
literal, just as with the CREATE FUNCTION command. Use of a
dollar-quoted literal is recommended. Optional keywords have no effect. These procedural
languages are supported: PL/pgSQL (plpgsql), PL/Python
(plpythonu), and PL/Perl (plperl and
plperlu).

lang_name

The name of the procedural language that the code is written in. The default is
plpgsql. The language must be installed on the Greenplum Database
system and registered in the database.

Notes

The PL/pgSQL language is installed on the Greenplum Database system and is registered in a
user created database. The PL/Python and PL/Perl languages are installed by default, but not
registered. Other languages are not installed or registered. The system catalog
pg_language contains information about the registered languages in a
database.

The user must have USAGE 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.

Anonymous blocks do not support function volatility or EXECUTE ON
attributes.

Examples

This PL/pgSQL example grants all privileges on all views in schema public to role
webuser: