summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man3/SPI_execute.3
blob: 34cc250797bc579415378088d0aba0705e3e6b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'\" t
.\"     Title: SPI_execute
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2024
.\"    Manual: PostgreSQL 16.2 Documentation
.\"    Source: PostgreSQL 16.2
.\"  Language: English
.\"
.TH "SPI_EXECUTE" "3" "2024" "PostgreSQL 16.2" "PostgreSQL 16.2 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
SPI_execute \- execute a command
.SH "SYNOPSIS"
.sp
.nf
int SPI_execute(const char * \fIcommand\fR, bool \fIread_only\fR, long \fIcount\fR)
.fi
.SH "DESCRIPTION"
.PP
\fBSPI_execute\fR
executes the specified SQL command for
\fIcount\fR
rows\&. If
\fIread_only\fR
is
true, the command must be read\-only, and execution overhead is somewhat reduced\&.
.PP
This function can only be called from a connected C function\&.
.PP
If
\fIcount\fR
is zero then the command is executed for all rows that it applies to\&. If
\fIcount\fR
is greater than zero, then no more than
\fIcount\fR
rows will be retrieved; execution stops when the count is reached, much like adding a
LIMIT
clause to the query\&. For example,
.sp
.if n \{\
.RS 4
.\}
.nf
SPI_execute("SELECT * FROM foo", true, 5);
.fi
.if n \{\
.RE
.\}
.sp
will retrieve at most 5 rows from the table\&. Note that such a limit is only effective when the command actually returns rows\&. For example,
.sp
.if n \{\
.RS 4
.\}
.nf
SPI_execute("INSERT INTO foo SELECT * FROM bar", false, 5);
.fi
.if n \{\
.RE
.\}
.sp
inserts all rows from
bar, ignoring the
\fIcount\fR
parameter\&. However, with
.sp
.if n \{\
.RS 4
.\}
.nf
SPI_execute("INSERT INTO foo SELECT * FROM bar RETURNING *", false, 5);
.fi
.if n \{\
.RE
.\}
.sp
at most 5 rows would be inserted, since execution would stop after the fifth
RETURNING
result row is retrieved\&.
.PP
You can pass multiple commands in one string;
\fBSPI_execute\fR
returns the result for the command executed last\&. The
\fIcount\fR
limit applies to each command separately (even though only the last result will actually be returned)\&. The limit is not applied to any hidden commands generated by rules\&.
.PP
When
\fIread_only\fR
is
false,
\fBSPI_execute\fR
increments the command counter and computes a new
snapshot
before executing each command in the string\&. The snapshot does not actually change if the current transaction isolation level is
SERIALIZABLE
or
REPEATABLE READ, but in
READ COMMITTED
mode the snapshot update allows each command to see the results of newly committed transactions from other sessions\&. This is essential for consistent behavior when the commands are modifying the database\&.
.PP
When
\fIread_only\fR
is
true,
\fBSPI_execute\fR
does not update either the snapshot or the command counter, and it allows only plain
\fBSELECT\fR
commands to appear in the command string\&. The commands are executed using the snapshot previously established for the surrounding query\&. This execution mode is somewhat faster than the read/write mode due to eliminating per\-command overhead\&. It also allows genuinely
stable
functions to be built: since successive executions will all use the same snapshot, there will be no change in the results\&.
.PP
It is generally unwise to mix read\-only and read\-write commands within a single function using SPI; that could result in very confusing behavior, since the read\-only queries would not see the results of any database updates done by the read\-write queries\&.
.PP
The actual number of rows for which the (last) command was executed is returned in the global variable
\fISPI_processed\fR\&. If the return value of the function is
SPI_OK_SELECT,
SPI_OK_INSERT_RETURNING,
SPI_OK_DELETE_RETURNING, or
SPI_OK_UPDATE_RETURNING, then you can use the global pointer
SPITupleTable *SPI_tuptable
to access the result rows\&. Some utility commands (such as
\fBEXPLAIN\fR) also return row sets, and
SPI_tuptable
will contain the result in these cases too\&. Some utility commands (\fBCOPY\fR,
\fBCREATE TABLE AS\fR) don\*(Aqt return a row set, so
SPI_tuptable
is NULL, but they still return the number of rows processed in
\fISPI_processed\fR\&.
.PP
The structure
SPITupleTable
is defined thus:
.sp
.if n \{\
.RS 4
.\}
.nf
typedef struct SPITupleTable
{
    /* Public members */
    TupleDesc   tupdesc;        /* tuple descriptor */
    HeapTuple  *vals;           /* array of tuples */
    uint64      numvals;        /* number of valid tuples */

    /* Private members, not intended for external callers */
    uint64      alloced;        /* allocated length of vals array */
    MemoryContext tuptabcxt;    /* memory context of result table */
    slist_node  next;           /* link for internal bookkeeping */
    SubTransactionId subid;     /* subxact in which tuptable was created */
} SPITupleTable;
.fi
.if n \{\
.RE
.\}
.sp
The fields
tupdesc,
vals, and
numvals
can be used by SPI callers; the remaining fields are internal\&.
vals
is an array of pointers to rows\&. The number of rows is given by
numvals
(for somewhat historical reasons, this count is also returned in
\fISPI_processed\fR)\&.
tupdesc
is a row descriptor which you can pass to SPI functions dealing with rows\&.
.PP
\fBSPI_finish\fR
frees all
SPITupleTables allocated during the current C function\&. You can free a particular result table earlier, if you are done with it, by calling
\fBSPI_freetuptable\fR\&.
.SH "ARGUMENTS"
.PP
const char * \fIcommand\fR
.RS 4
string containing command to execute
.RE
.PP
bool \fIread_only\fR
.RS 4
true
for read\-only execution
.RE
.PP
long \fIcount\fR
.RS 4
maximum number of rows to return, or
0
for no limit
.RE
.SH "RETURN VALUE"
.PP
If the execution of the command was successful then one of the following (nonnegative) values will be returned:
.PP
SPI_OK_SELECT
.RS 4
if a
\fBSELECT\fR
(but not
\fBSELECT INTO\fR) was executed
.RE
.PP
SPI_OK_SELINTO
.RS 4
if a
\fBSELECT INTO\fR
was executed
.RE
.PP
SPI_OK_INSERT
.RS 4
if an
\fBINSERT\fR
was executed
.RE
.PP
SPI_OK_DELETE
.RS 4
if a
\fBDELETE\fR
was executed
.RE
.PP
SPI_OK_UPDATE
.RS 4
if an
\fBUPDATE\fR
was executed
.RE
.PP
SPI_OK_MERGE
.RS 4
if a
\fBMERGE\fR
was executed
.RE
.PP
SPI_OK_INSERT_RETURNING
.RS 4
if an
\fBINSERT RETURNING\fR
was executed
.RE
.PP
SPI_OK_DELETE_RETURNING
.RS 4
if a
\fBDELETE RETURNING\fR
was executed
.RE
.PP
SPI_OK_UPDATE_RETURNING
.RS 4
if an
\fBUPDATE RETURNING\fR
was executed
.RE
.PP
SPI_OK_UTILITY
.RS 4
if a utility command (e\&.g\&.,
\fBCREATE TABLE\fR) was executed
.RE
.PP
SPI_OK_REWRITTEN
.RS 4
if the command was rewritten into another kind of command (e\&.g\&.,
\fBUPDATE\fR
became an
\fBINSERT\fR) by a
rule\&.
.RE
.PP
On error, one of the following negative values is returned:
.PP
SPI_ERROR_ARGUMENT
.RS 4
if
\fIcommand\fR
is
NULL
or
\fIcount\fR
is less than 0
.RE
.PP
SPI_ERROR_COPY
.RS 4
if
\fBCOPY TO stdout\fR
or
\fBCOPY FROM stdin\fR
was attempted
.RE
.PP
SPI_ERROR_TRANSACTION
.RS 4
if a transaction manipulation command was attempted (\fBBEGIN\fR,
\fBCOMMIT\fR,
\fBROLLBACK\fR,
\fBSAVEPOINT\fR,
\fBPREPARE TRANSACTION\fR,
\fBCOMMIT PREPARED\fR,
\fBROLLBACK PREPARED\fR, or any variant thereof)
.RE
.PP
SPI_ERROR_OPUNKNOWN
.RS 4
if the command type is unknown (shouldn\*(Aqt happen)
.RE
.PP
SPI_ERROR_UNCONNECTED
.RS 4
if called from an unconnected C function
.RE
.SH "NOTES"
.PP
All SPI query\-execution functions set both
\fISPI_processed\fR
and
\fISPI_tuptable\fR
(just the pointer, not the contents of the structure)\&. Save these two global variables into local C function variables if you need to access the result table of
\fBSPI_execute\fR
or another query\-execution function across later calls\&.