DO(7)
π
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 inCREATE 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 isplpgsql. 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.plpgsqlis installed by default β but other languages are not. so don't come for her when it's missing, okay? π - The user must have
USAGEprivilege 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
DOis executed in a transaction block, then the procedure code cannot execute transaction control statements. those are only allowed ifDOis 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
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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