DO(7)
π DO(7) β the anonymous code block era β¨
okay so real talk, bestie: this is the DO page from the PostgreSQL 14.24 Documentation, and it's genuinely one of the most underrated little commands out there. no cap. let's get into it π«Ά
β¨ NAME β¨
DO β execute an anonymous code block. that's it. that's the whole vibe. she runs code and she does NOT leave a function behind. main character energy but with a quick exit π
πΈ SYNOPSIS πΈ
DO [ LANGUAGE lang_name ] code
look at her. so simple. so iconic. π
π¦ DESCRIPTION π¦
DO executes an anonymous code block β or, to say it another way, a transient anonymous function written in a procedural language. she's here for a good time, not a long time, yk? π
The code block gets treated like it were the body of a function with no parameters, returning void. It is parsed and executed a single time. one and done. no lingering attachment. we love a clean break β¨
The optional LANGUAGE clause can be written either before or after the code block. your call, bestie, both are valid π
π PARAMETERS π
codeβ the procedural language code to be executed, and listen, this must be specified as a string literal, just as inCREATE FUNCTION. Use of a dollar-quoted literal is recommended β trust, dollar quoting is the move here, iykyk πlang_nameβ the name of the procedural language the code is written in. If you omit it, the default isplpgsql. she's the default for a reason, she's everywhere π
π₯ NOTES π₯
okay important stuff, don't skip this part:
The procedural language you wanna use 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 come for me when it doesn't work, go install your extension first π
The user must have USAGE privilege 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 energy, same rules β¨
β οΈ okay this one matters: ifDOis 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. periodt. don't fight it π
β EXAMPLES β
ok so imagine you wanna grant all privileges on all views in schema public to role webuser. here's the moment, here's the slay:
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$$;
she ate. she literally ate. dynamic SQL with quote_ident() and everything π
π COMPATIBILITY π
There is no DO statement in the SQL standard. she's built different. a PostgreSQL original. no notes π
β¨
π§ SEE ALSO π§
CREATE LANGUAGE (CREATE_LANGUAGE(7))β go read it, it's the whole backstory bestie π¦
and that's the page!! π written up for the PostgreSQL 14.24 Documentation, in the year 2026, and this absolutely iconic little DO(7) page is officially closed out. go run some anonymous code, you've earned it ππΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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