3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "SPI_EXECUTE" "3" "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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 SPI_execute \- execute a command
35 int SPI_execute(const char * \fIcommand\fR, bool \fIread_only\fR, long \fIcount\fR)
40 executes the specified SQL command for
45 true, the command must be read\-only, and execution overhead is somewhat reduced\&.
47 This function can only be called from a connected C function\&.
51 is zero then the command is executed for all rows that it applies to\&. If
53 is greater than zero, then no more than
55 rows will be retrieved; execution stops when the count is reached, much like adding a
57 clause to the query\&. For example,
63 SPI_execute("SELECT * FROM foo", true, 5);
69 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,
75 SPI_execute("INSERT INTO foo SELECT * FROM bar", false, 5);
84 parameter\&. However, with
90 SPI_execute("INSERT INTO foo SELECT * FROM bar RETURNING *", false, 5);
96 at most 5 rows would be inserted, since execution would stop after the fifth
98 result row is retrieved\&.
100 You can pass multiple commands in one string;
102 returns the result for the command executed last\&. The
104 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\&.
111 increments the command counter and computes a new
113 before executing each command in the string\&. The snapshot does not actually change if the current transaction isolation level is
116 REPEATABLE READ, but in
118 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\&.
125 does not update either the snapshot or the command counter, and it allows only plain
127 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
129 functions to be built: since successive executions will all use the same snapshot, there will be no change in the results\&.
131 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\&.
133 The actual number of rows for which the (last) command was executed is returned in the global variable
134 \fISPI_processed\fR\&. If the return value of the function is
136 SPI_OK_INSERT_RETURNING,
137 SPI_OK_DELETE_RETURNING,
138 SPI_OK_UPDATE_RETURNING, or
139 SPI_OK_MERGE_RETURNING, then you can use the global pointer
140 SPITupleTable *SPI_tuptable
141 to access the result rows\&. Some utility commands (such as
142 \fBEXPLAIN\fR) also return row sets, and
144 will contain the result in these cases too\&. Some utility commands (\fBCOPY\fR,
145 \fBCREATE TABLE AS\fR) don\*(Aqt return a row set, so
147 is NULL, but they still return the number of rows processed in
148 \fISPI_processed\fR\&.
158 typedef struct SPITupleTable
161 TupleDesc tupdesc; /* tuple descriptor */
162 HeapTuple *vals; /* array of tuples */
163 uint64 numvals; /* number of valid tuples */
165 /* Private members, not intended for external callers */
166 uint64 alloced; /* allocated length of vals array */
167 MemoryContext tuptabcxt; /* memory context of result table */
168 slist_node next; /* link for internal bookkeeping */
169 SubTransactionId subid; /* subxact in which tuptable was created */
180 can be used by SPI callers; the remaining fields are internal\&.
182 is an array of pointers to rows\&. The number of rows is given by
184 (for somewhat historical reasons, this count is also returned in
185 \fISPI_processed\fR)\&.
187 is a row descriptor which you can pass to SPI functions dealing with rows\&.
191 SPITupleTables allocated during the current C function\&. You can free a particular result table earlier, if you are done with it, by calling
192 \fBSPI_freetuptable\fR\&.
195 const char * \fIcommand\fR
197 string containing command to execute
203 for read\-only execution
208 maximum number of rows to return, or
214 If the execution of the command was successful then one of the following (nonnegative) values will be returned:
221 \fBSELECT INTO\fR) was executed
259 SPI_OK_INSERT_RETURNING
262 \fBINSERT RETURNING\fR
266 SPI_OK_DELETE_RETURNING
269 \fBDELETE RETURNING\fR
273 SPI_OK_UPDATE_RETURNING
276 \fBUPDATE RETURNING\fR
280 SPI_OK_MERGE_RETURNING
283 \fBMERGE RETURNING\fR
289 if a utility command (e\&.g\&.,
290 \fBCREATE TABLE\fR) was executed
295 if the command was rewritten into another kind of command (e\&.g\&.,
302 On error, one of the following negative values is returned:
320 \fBCOPY FROM stdin\fR
324 SPI_ERROR_TRANSACTION
326 if a transaction manipulation command was attempted (\fBBEGIN\fR,
330 \fBPREPARE TRANSACTION\fR,
331 \fBCOMMIT PREPARED\fR,
332 \fBROLLBACK PREPARED\fR, or any variant thereof)
337 if the command type is unknown (shouldn\*(Aqt happen)
340 SPI_ERROR_UNCONNECTED
342 if called from an unconnected C function
346 All SPI query\-execution functions set both
350 (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
352 or another query\-execution function across later calls\&.