Database Reference
In-Depth Information
snprintf(buf + strlen (buf),
sizeof(buf) -
strlen(buf),
" %s(%s::%s)%s",
SPI_fname
(tupdesc, i),
SPI_getvalue
(tuple,
tupdesc, i),
SPI_gettype
(tupdesc,
i),
(i == tupdesc->natts)
? " " : " |");
ereport(INFO, (errmsg("ROW: %s",
buf)));
}
}
SPI_finish()
;
pfree(command);
PG_RETURN_INT32(proc);
}
After getting the arguments using the
PG_GETARG_*
macro, the first new thing
shown is opening an internal connection via
SPI_connect()
which sets up the in-
ternal state for the following
SPI_*()
function calls. The next step is to execute a
full SQL statement using
SPI_exec(command, cnt)
.
The
SPI_exec()
function is a convenience variant of
SPI_execute(...)
with
read_only
flag set to
false
. There is also a third version of execute at once SPI
function, the
SPI_execute_with_args(...)
which prepares the query, binds the
passed-in arguments, and executes in a single call.
After the query is executed, we save the
SPI_processed
value for returning the
number of rows processed at the end of the function. In this sample, it is not strictly
necessary, but in general you need to save any
SPI_*
global variable because they
could be overwritten by the next
SPI_*(...)
call.