A newer version of this documentation is available. Use the version menu above to view the most up-to-date release of the Greenplum 6.x documentation.
Executes an anonymous code block as a transient anonymous function.
DO [ LANGUAGE lang_name ] code
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.
- 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).
- 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.
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.
This PL/pgSQL example grants all privileges on all views in schema public to role webuser:
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$$;
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 ;
There is no DO statement in the SQL standard.