DO(7)
β¨ DO(7) β¨
okay bestie, this is the DO page from the PostgreSQL 18.6 Documentation, and it's literally the "just run this code real quick" icon we deserve π
πΈ NAME πΈ
DO β execute an anonymous code block
no cap, that's the whole vibe: anonymous code, zero name, all vibes π¦
π SYNOPSIS π
DO [ LANGUAGE lang_name ] code
look at her. so simple. so powerful. she ate π
β¨ DESCRIPTION β¨
So DO runs an anonymous code block β which is basically a transient anonymous function written in some procedural language. No name, no commitment, just vibes and execution. π
The code block gets treated like it's the body of a function that takes no parameters and returns void. It's parsed and executed a single time. One and done, periodt.
The optional LANGUAGE clause is flexible like that β you can write it either before or after the code block. She's not picky, she's iconic π
π PARAMETERS π
codeβ the procedural language code that's actually getting executed. It must be specified as a string literal, exactly like inCREATE FUNCTION. Using a dollar-quoted literal is the recommended move, fr fr π«Άlang_nameβ the name of the procedural language your code is written in. If you leave it out, the default isplpgsql. (yes queen, lowercase, exactly like that) π
π₯ NOTES π₯
- The procedural language you wanna use has to already be installed in the current database via
CREATE EXTENSION.plpgsqlcomes installed by default, but the other languages? Not so much bestie π - You need
USAGEprivilege on the procedural language β or you gotta be a superuser if the language is untrusted. Same privilege requirement as creating a function in that language, iykyk. - > β οΈ okay important one: if
DOis executed inside a transaction block, then the procedure code cannot execute transaction control statements. Transaction control statements are only allowed ifDOis executed in its own transaction. So like... don't do that. π«£
π EXAMPLES π
Grant all privileges on all views in schema public to role webuser β watch her work π
:
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$$;
literally a whole for-loop inside a DO block. she's serving β¨
π COMPATIBILITY π
There is no DO statement in the SQL standard. Not a thing. Never was. PostgreSQL just said "we're doing our own thing" and honestly? respect π
π¦ SEE ALSO π¦
CREATE LANGUAGE(CREATE_LANGUAGE(7))
and that's the whole page, bestie!! πΈ This was PostgreSQL 18.6, documented in 2026 β so go write your anonymous code blocks and slay responsibly πππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms