DO(7)

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

πŸ’… DO(7) β€” bestie's guide to the DO statement

okay so real talk, you're looking at the PostgreSQL 17.11 docs page for DO, and honestly? she's a lil icon. a whole anonymous code block, no name, no permanent address, just vibes and execution. let's get into it 🫢


✨ NAME ✨

DO β€” execute an anonymous code block. that's it. that's the whole job. no cap. πŸ‘‘


✨ SYNOPSIS ✨

DO [ LANGUAGE lang_name ] code

✨ DESCRIPTION ✨

So DO executes an anonymous code block β€” or, to say it in a less nerdy way, a transient anonymous function in a procedural language. she's here for a good time, not a long time. πŸ’…

The code block is treated like it's the body of a function with no parameters, returning void. It gets parsed and executed exactly a single time. one and done, periodt. ✨

The optional LANGUAGE clause? You can write it either before or after the code block. order doesn't matter, she's flexible like that. πŸ¦‹


✨ PARAMETERS ✨

  • code β€” the procedural language code you actually want to run. It must be specified as a string literal, just like in CREATE FUNCTION. and listen: using a dollar-quoted literal is recommended. trust. πŸ’–
  • lang_name β€” the name of the procedural language your code is written in. if you omit it, the default is plpgsql. slay, but also know your defaults. πŸŽ€

✨ NOTES ✨

okay this is the part where you gotta pay attention, bestie, because these are the real ones:

  • The procedural language you want to use must already have been installed into the current database via CREATE EXTENSION. plpgsql is installed by default β€” but other languages are not. so don't come for her when it's missing, okay? 😭
  • The user must have USAGE privilege for the procedural language β€” or must be a superuser if the language is untrusted. this is literally the same privilege requirement as creating a function in that language. same rules, same energy. πŸ”₯
  • If DO is executed in a transaction block, then the procedure code cannot execute transaction control statements. those are only allowed if DO is executed in its own transaction. big caveat, don't @ me. πŸ’€

✨ EXAMPLES ✨

ok so say you wanna grant all privileges on all views in schema public to role webuser β€” here's the moment, here's the move:

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

ate. no crumbs. πŸ‘‘βœ¨


✨ COMPATIBILITY ✨

There is no DO statement in the SQL standard. she's a PostgreSQL original, iykyk. 🌈


✨ SEE ALSO ✨

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

and that's the whole page, bestie! this was PostgreSQL 17.11 documentation, dated 2026, for DO(7) β€” go forth and run your anonymous little code blocks πŸ’œπŸŽ€πŸ¦‹

πŸ“¦ where this page comes from