DO(7)
π 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 inCREATE 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 isplpgsql. Default queen behavior. π
πΈ NOTES πΈ
- The procedural language to be used must already have been installed into the current database by means of
CREATE EXTENSION.plpgsqlis 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
USAGEprivilege 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
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.
β οΈ okay so this part matters: transaction control statements insideDOonly work whenDOis 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
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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