DO(7)

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

πŸ’… DO(7) β€” the anonymous code block era ✨

okay so real talk, bestie: this is the DO page from the PostgreSQL 14.24 Documentation, and it's genuinely one of the most underrated little commands out there. no cap. let's get into it 🫢


✨ NAME ✨

DO β€” execute an anonymous code block. that's it. that's the whole vibe. she runs code and she does NOT leave a function behind. main character energy but with a quick exit πŸŽ€


🌸 SYNOPSIS 🌸

DO [ LANGUAGE lang_name ] code

look at her. so simple. so iconic. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

DO executes an anonymous code block β€” or, to say it another way, a transient anonymous function written in a procedural language. she's here for a good time, not a long time, yk? 😭

The code block gets treated like it were the body of a function with no parameters, returning void. It is parsed and executed a single time. one and done. no lingering attachment. we love a clean break ✨

The optional LANGUAGE clause can be written either before or after the code block. your call, bestie, both are valid πŸ’–


πŸŽ€ PARAMETERS πŸŽ€

  • code β€” the procedural language code to be executed, and listen, this must be specified as a string literal, just as in CREATE FUNCTION. Use of a dollar-quoted literal is recommended β€” trust, dollar quoting is the move here, iykyk πŸ’œ
  • lang_name β€” the name of the procedural language the code is written in. If you omit it, the default is plpgsql. she's the default for a reason, she's everywhere πŸ‘‘

πŸ”₯ NOTES πŸ”₯

okay important stuff, don't skip this part:

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 like, don't come for me when it doesn't work, go install your extension 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 energy, same rules ✨

⚠️ okay this one matters: 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. periodt. don't fight it πŸ’€

⭐ EXAMPLES ⭐

ok so imagine you wanna grant all privileges on all views in schema public to role webuser. here's the moment, here's the slay:

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$$;

she ate. she literally ate. dynamic SQL with quote_ident() and everything πŸ“


🌈 COMPATIBILITY 🌈

There is no DO statement in the SQL standard. she's built different. a PostgreSQL original. no notes πŸ’…βœ¨


🧚 SEE ALSO 🧚

  • CREATE LANGUAGE (CREATE_LANGUAGE(7)) β€” go read it, it's the whole backstory bestie πŸ¦‹

and that's the page!! πŸ’– written up for the PostgreSQL 14.24 Documentation, in the year 2026, and this absolutely iconic little DO(7) page is officially closed out. go run some anonymous code, you've earned it πŸ‘‘πŸŒΈ

πŸ“¦ where this page comes from