1SPI_PREPARE(3)           PostgreSQL 14.3 Documentation          SPI_PREPARE(3)
2
3
4

NAME

6       SPI_prepare - prepare a statement, without executing it yet
7

SYNOPSIS

9       SPIPlanPtr SPI_prepare(const char * command, int nargs, Oid * argtypes)
10

DESCRIPTION

12       SPI_prepare creates and returns a prepared statement for the specified
13       command, but doesn't execute the command. The prepared statement can
14       later be executed repeatedly using SPI_execute_plan.
15
16       When the same or a similar command is to be executed repeatedly, it is
17       generally advantageous to perform parse analysis only once, and might
18       furthermore be advantageous to re-use an execution plan for the
19       command.  SPI_prepare converts a command string into a prepared
20       statement that encapsulates the results of parse analysis. The prepared
21       statement also provides a place for caching an execution plan if it is
22       found that generating a custom plan for each execution is not helpful.
23
24       A prepared command can be generalized by writing parameters ($1, $2,
25       etc.) in place of what would be constants in a normal command. The
26       actual values of the parameters are then specified when
27       SPI_execute_plan is called. This allows the prepared command to be used
28       over a wider range of situations than would be possible without
29       parameters.
30
31       The statement returned by SPI_prepare can be used only in the current
32       invocation of the C function, since SPI_finish frees memory allocated
33       for such a statement. But the statement can be saved for longer using
34       the functions SPI_keepplan or SPI_saveplan.
35

ARGUMENTS

37       const char * command
38           command string
39
40       int nargs
41           number of input parameters ($1, $2, etc.)
42
43       Oid * argtypes
44           pointer to an array containing the OIDs of the data types of the
45           parameters
46

RETURN VALUE

48       SPI_prepare returns a non-null pointer to an SPIPlan, which is an
49       opaque struct representing a prepared statement. On error, NULL will be
50       returned, and SPI_result will be set to one of the same error codes
51       used by SPI_execute, except that it is set to SPI_ERROR_ARGUMENT if
52       command is NULL, or if nargs is less than 0, or if nargs is greater
53       than 0 and argtypes is NULL.
54

NOTES

56       If no parameters are defined, a generic plan will be created at the
57       first use of SPI_execute_plan, and used for all subsequent executions
58       as well. If there are parameters, the first few uses of
59       SPI_execute_plan will generate custom plans that are specific to the
60       supplied parameter values. After enough uses of the same prepared
61       statement, SPI_execute_plan will build a generic plan, and if that is
62       not too much more expensive than the custom plans, it will start using
63       the generic plan instead of re-planning each time. If this default
64       behavior is unsuitable, you can alter it by passing the
65       CURSOR_OPT_GENERIC_PLAN or CURSOR_OPT_CUSTOM_PLAN flag to
66       SPI_prepare_cursor, to force use of generic or custom plans
67       respectively.
68
69       Although the main point of a prepared statement is to avoid repeated
70       parse analysis and planning of the statement, PostgreSQL will force
71       re-analysis and re-planning of the statement before using it whenever
72       database objects used in the statement have undergone definitional
73       (DDL) changes since the previous use of the prepared statement. Also,
74       if the value of search_path changes from one use to the next, the
75       statement will be re-parsed using the new search_path. (This latter
76       behavior is new as of PostgreSQL 9.3.) See PREPARE(7) for more
77       information about the behavior of prepared statements.
78
79       This function should only be called from a connected C function.
80
81       SPIPlanPtr is declared as a pointer to an opaque struct type in spi.h.
82       It is unwise to try to access its contents directly, as that makes your
83       code much more likely to break in future revisions of PostgreSQL.
84
85       The name SPIPlanPtr is somewhat historical, since the data structure no
86       longer necessarily contains an execution plan.
87
88
89
90PostgreSQL 14.3                      2022                       SPI_PREPARE(3)
Impressum