DO(7)

πŸ“¦ version 16.15, in opensuse-leap 16.0

πŸ’… DO(7) β€” the DO page, bestie edition ✨

okay so real talk: this is the DO page from the PostgreSQL 16.15 Documentation and it's genuinely one of the most underrated little commands ever. like, it's giving "sleeper hit." let me walk you through it 🫢


✨ NAME ✨

DO β€” execute an anonymous code block πŸ’«

✨ SYNOPSIS ✨

DO [ LANGUAGE lang_name ] code

πŸ¦‹ DESCRIPTION πŸ¦‹

So DO executes an anonymous code block β€” or, if we're being fancy about it, a transient anonymous function written in a procedural language. it doesn't get a name, it doesn't get saved anywhere, it just exists for one glorious moment and then dips. main character energy honestly πŸ’…

The code block gets treated as though it were the body of a function with no parameters, returning void. It is parsed and executed a single time. one and done. no cap.

The optional LANGUAGE clause can be written either before or after the code block β€” your choice, no wrong answers, we love flexibility πŸŽ€

🌸 PARAMETERS 🌸

  • code β€” the procedural language code to be executed, obviously. this must be specified as a string literal, just as in CREATE FUNCTION. and listen: use of a dollar-quoted literal is recommended. that's not me being dramatic, that's the docs. dollar quotes are the moment πŸ’–
  • lang_name β€” the name of the procedural language the code is written in. if you omit it, the default is plpgsql. and yes it's lowercase, keep it lowercase, we don't shout at our languages 🫢

πŸ”₯ NOTES πŸ”₯

okay important stuff, pay attention bestie:

  • The procedural language you wanna use must already have been installed into the current database by means of CREATE EXTENSION. plpgsql is installed by default, but other languages are not. so don't come crying to me when plpython3u isn't there, you gotta install it first 😭
  • The user must have USAGE privilege for the procedural language, or must be a superuser if the language is untrusted. this is the exact same privilege requirement as for creating a function in the language. same vibes, same rules.
  • If DO is executed in a transaction block, then the procedure code cannot execute transaction control statements. transaction control statements are only allowed if DO is executed in its own transaction. like, it's not a suggestion, it's a rule. periodt πŸ’€

⭐ EXAMPLES ⭐

Here's the iconic one β€” grant 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$$;

look at her go. loops, dynamic SQL, quote_ident, all of it. she ate and left no crumbs πŸ‘‘

🌈 COMPATIBILITY 🌈

There is no DO statement in the SQL standard. we're built different, we're off-book, we're the moment. iykyk πŸ’…

πŸ’œ SEE ALSO πŸ’œ

  • CREATE LANGUAGE (CREATE_LANGUAGE(7)) β€” go read that one next, it's the prequel 🧚

and that's the whole page bestie!! ✨ this was DO(7) from PostgreSQL 16.15, dated 2026 β€” go write some anonymous code blocks and slay, and remember: dollar-quote your literals πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from