Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

spi_prepare(3) [centos man page]

SPI_PREPARE(3)						  PostgreSQL 9.2.7 Documentation					    SPI_PREPARE(3)

NAME
SPI_prepare - prepare a statement, without executing it yet SYNOPSIS
SPIPlanPtr SPI_prepare(const char * command, int nargs, Oid * argtypes) DESCRIPTION
SPI_prepare creates and returns a prepared statement for the specified command, but doesn't execute the command. The prepared statement can later be executed repeatedly using SPI_execute_plan. When the same or a similar command is to be executed repeatedly, it is generally advantageous to perform parse analysis only once, and might furthermore be advantageous to re-use an execution plan for the command. SPI_prepare converts a command string into a prepared statement that encapsulates the results of parse analysis. The prepared statement also provides a place for caching an execution plan if it is found that generating a custom plan for each execution is not helpful. A prepared command can be generalized by writing parameters ($1, $2, etc.) in place of what would be constants in a normal command. The actual values of the parameters are then specified when SPI_execute_plan is called. This allows the prepared command to be used over a wider range of situations than would be possible without parameters. The statement returned by SPI_prepare can be used only in the current invocation of the procedure, since SPI_finish frees memory allocated for such a statement. But the statement can be saved for longer using the functions SPI_keepplan or SPI_saveplan. ARGUMENTS
const char * command command string int nargs number of input parameters ($1, $2, etc.) Oid * argtypes pointer to an array containing the OIDs of the data types of the parameters RETURN VALUE
SPI_prepare returns a non-null pointer to an SPIPlan, which is an opaque struct representing a prepared statement. On error, NULL will be returned, and SPI_result will be set to one of the same error codes used by SPI_execute, except that it is set to SPI_ERROR_ARGUMENT if command is NULL, or if nargs is less than 0, or if nargs is greater than 0 and argtypes is NULL. NOTES
If no parameters are defined, a generic plan will be created at the first use of SPI_execute_plan, and used for all subsequent executions as well. If there are parameters, the first few uses of SPI_execute_plan will generate custom plans that are specific to the supplied parameter values. After enough uses of the same prepared statement, SPI_execute_plan will build a generic plan, and if that is not too much more expensive than the custom plans, it will start using the generic plan instead of re-planning each time. If this default behavior is unsuitable, you can alter it by passing the CURSOR_OPT_GENERIC_PLAN or CURSOR_OPT_CUSTOM_PLAN flag to SPI_prepare_cursor, to force use of generic or custom plans respectively. This function should only be called from a connected procedure. SPIPlanPtr is declared as a pointer to an opaque struct type in spi.h. It is unwise to try to access its contents directly, as that makes your code much more likely to break in future revisions of PostgreSQL. The name SPIPlanPtr is somewhat historical, since the data structure no longer necessarily contains an execution plan. PostgreSQL 9.2.7 2014-02-17 SPI_PREPARE(3)

Check Out this Related Man Page

PREPARE(7)							   SQL Commands 							PREPARE(7)

NAME
PREPARE - create a prepared query SYNOPSIS
PREPARE plan_name [ (datatype [, ...] ) ] AS query INPUTS plan_name An arbitrary name given to this particular prepared query. It must be unique within a single session, and is used to execute or remove a previously prepared query. datatype The data-type of a parameter to the prepared query. To refer to the parameters in the prepared query itself, use $1, $2, etc. OUTPUTS PREPARE The query has been prepared successfully. DESCRIPTION
PREPARE creates a prepared query. A prepared query is a server-side object that can be used to optimize performance. When the PREPARE statement is executed, the specified query is parsed, rewritten, and planned. When a subsequent EXECUTE statement is issued, the prepared query need only be executed. Thus, the parsing, rewriting, and planning stages are only performed once, instead of every time the query is executed. Prepared queries can take parameters: values that are substituted into the query when it is executed. To specify the parameters to a pre- pared query, include a list of data-types with the PREPARE statement. In the query itself, you can refer to the parameters by position using $1, $2, etc. When executing the query, specify the actual values for these parameters in the EXECUTE statement -- refer to EXECUTE [execute(7)] for more information. Prepared queries are stored locally (in the current backend), and only exist for the duration of the current database session. When the client exits, the prepared query is forgotten, and so it must be re-created before being used again. This also means that a single prepared query cannot be used by multiple simultaneous database clients; however, each client can create their own prepared query to use. Prepared queries have the largest performance advantage when a single backend is being used to execute a large number of similar queries. The performance difference will be particularly significant if the queries are complex to plan or rewrite. For example, if the query involves a join of many tables or requires the application of several rules. If the query is relatively simple to plan and rewrite but rel- atively expensive to execute, the performance advantage of prepared queries will be less noticeable. NOTES In some situations, the query plan produced by PostgreSQL for a prepared query may be inferior to the plan produced if the query were sub- mitted and executed normally. This is because when the query is planned (and the optimizer attempts to determine the optimal query plan), the actual values of any parameters specified in the query are unavailable. PostgreSQL collects statistics on the distribution of data in the table, and can use constant values in a query to make guesses about the likely result of executing the query. Since this data is unavailable when planning prepared queries with parameters, the chosen plan may be sub-optimal. For more information on query planning and the statistics collected by PostgreSQL for query optimization purposes, see the ANALYZE [ana- lyze(7)] documentation. COMPATIBILITY
SQL92 SQL92 includes a PREPARE statement, but it is only for use in embedded SQL clients. The PREPARE statement implemented by PostgreSQL also uses a somewhat different syntax. SQL - Language Statements 2002-11-22 PREPARE(7)
Man Page