]> begriffs open source - ai-pg/blob - full-docs/man7/DO.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / DO.7
1 '\" t
2 .\"     Title: DO
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "DO" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 DO \- execute an anonymous code block
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 DO [ LANGUAGE \fIlang_name\fR ] \fIcode\fR
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBDO\fR
40 executes an anonymous code block, or in other words a transient anonymous function in a procedural language\&.
41 .PP
42 The code block is treated as though it were the body of a function with no parameters, returning
43 void\&. It is parsed and executed a single time\&.
44 .PP
45 The optional
46 LANGUAGE
47 clause can be written either before or after the code block\&.
48 .SH "PARAMETERS"
49 .PP
50 \fIcode\fR
51 .RS 4
52 The procedural language code to be executed\&. This must be specified as a string literal, just as in
53 \fBCREATE FUNCTION\fR\&. Use of a dollar\-quoted literal is recommended\&.
54 .RE
55 .PP
56 \fIlang_name\fR
57 .RS 4
58 The name of the procedural language the code is written in\&. If omitted, the default is
59 plpgsql\&.
60 .RE
61 .SH "NOTES"
62 .PP
63 The procedural language to be used must already have been installed into the current database by means of
64 \fBCREATE EXTENSION\fR\&.
65 plpgsql
66 is installed by default, but other languages are not\&.
67 .PP
68 The user must have
69 USAGE
70 privilege 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\&.
71 .PP
72 If
73 \fBDO\fR
74 is executed in a transaction block, then the procedure code cannot execute transaction control statements\&. Transaction control statements are only allowed if
75 \fBDO\fR
76 is executed in its own transaction\&.
77 .SH "EXAMPLES"
78 .PP
79 Grant all privileges on all views in schema
80 public
81 to role
82 webuser:
83 .sp
84 .if n \{\
85 .RS 4
86 .\}
87 .nf
88 DO $$DECLARE r record;
89 BEGIN
90     FOR r IN SELECT table_schema, table_name FROM information_schema\&.tables
91              WHERE table_type = \*(AqVIEW\*(Aq AND table_schema = \*(Aqpublic\*(Aq
92     LOOP
93         EXECUTE \*(AqGRANT ALL ON \*(Aq || quote_ident(r\&.table_schema) || \*(Aq\&.\*(Aq || quote_ident(r\&.table_name) || \*(Aq TO webuser\*(Aq;
94     END LOOP;
95 END$$;
96 .fi
97 .if n \{\
98 .RE
99 .\}
100 .SH "COMPATIBILITY"
101 .PP
102 There is no
103 \fBDO\fR
104 statement in the SQL standard\&.
105 .SH "SEE ALSO"
106 CREATE LANGUAGE (\fBCREATE_LANGUAGE\fR(7))