Updated on 2025-10-23 GMT+08:00

DO

Description

This syntax is for internal tools only.

Executes an anonymous code block.

A code block is regarded as a function body without parameters. The return value type is void. It is parsed and executed a single time.

Precautions

  • Currently, only PL/pgSQL can be installed.
  • To use an untrusted language, you must have the USAGE permission on the programming language or the system administrator permission.

Syntax

DO code;

Parameters

  • code

    Specifies the programming language code that can be executed. The value must be a character string.

Examples

-- Create the webuser user.
m_db=# CREATE USER webuser PASSWORD '********';

-- Grant all permissions on all views in the tpcds schema to the webuser user.
m_db=# DO $$DECLARE r record;
BEGIN
    FOR r IN SELECT c.relname table_name,n.nspname table_schema FROM pg_class c,pg_namespace n 
             WHERE c.relnamespace = n.oid AND n.nspname = 'tpcds' AND relkind IN ('r','v')
    LOOP
        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
    END LOOP;
END$$;

-- Delete the webuser user.
m_db=# DROP USER webuser CASCADE;