DO(7)
π
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 inCREATE 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 isplpgsql. 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.plpgsqlis installed by default, but other languages are not. so don't come crying to me whenplpython3uisn't there, you gotta install it first π - The user must have
USAGEprivilege 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
DOis executed in a transaction block, then the procedure code cannot execute transaction control statements. transaction control statements are only allowed ifDOis 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
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms