DO(7)

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

πŸ’– NAME πŸ’–

okay so bestie, welcome to the DO page from the PostgreSQL 15.19 docs β€” yes, the whole manual page, and it’s lowkey iconic. This is DO(7), the one that lets you run a lil anonymous code block without making a whole named function. Main character energy fr fr. πŸ’…

DO β€” execute an anonymous code block. That’s the whole vibe: anonymous, transient, no name, no commitment. ✨


✨ SYNOPSIS ✨

the synopsis is literally just:

DO [ LANGUAGE lang_name ] code

πŸ¦‹ DESCRIPTION πŸ¦‹

So DO executes an anonymous code block, or in other words a transient anonymous function in a procedural language. It’s giving β€œfunction but make it fleeting.” 🫢

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. Not me being a one-and-done icon. 😭

The optional LANGUAGE clause can be written either before or after the code block. Flexible? We love to see it. πŸ’–


πŸŽ€ PARAMETERS πŸŽ€

  • code β€” the procedural language code to be executed. This must be specified as a string literal, just as in CREATE FUNCTION. Use of a dollar-quoted literal is recommended. (iykyk, dollar quotes are the move.) πŸ”₯
  • lang_name β€” the name of the procedural language the code is written in. If omitted, the default is plpgsql. Default queen behavior. πŸ‘‘

🌸 NOTES 🌸

  • The procedural language to be used 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 just assume every language is already in the group chat. πŸ’€
  • 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. Permission check, no skipping. 🧚
  • 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.
⚠️ okay so this part matters: transaction control statements inside DO only work when DO is in its own transaction. Inside a transaction block? Nope, not allowed. Periodt. 🚫

🌈 EXAMPLES 🌈

okay so here’s the moment: grant all privileges on all views in schema public to role webuser. Obsessed with a practical example. πŸ’œ

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

that’s the whole example, no cap. πŸ“


πŸ’€ COMPATIBILITY πŸ’€

There is no DO statement in the SQL standard. So if someone asks, nope, not standard. We’re built different. πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

  • CREATE LANGUAGE (CREATE_LANGUAGE(7)) β€” go read that if you wanna know about languages. πŸ¦‹

and that’s the page, bestie. this masterpiece is from PostgreSQL 15.19, dated 2026, and it’s the DO(7) page. slay. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from