# 34.8. The Fast-Path Interface
PostgreSQL provides a fast-path interface to send simple function calls to the server.
# Tip
This interface is somewhat obsolete, as one can achieve similar performance and greater functionality by setting up a prepared statement to define the function call. Then, executing the statement with binary transmission of parameters and results substitutes for a fast-path function call.
The functionPQfn
requests execution of a server function via the fast-path interface:
PGresult *PQfn(PGconn *conn,
int fnid,
int *result_buf,
int *result_len,
int result_is_int,
const PQArgBlock *args,
int nargs);
typedef struct
{
int len;
int isint;
union
{
int *ptr;
int integer;
} u;
} PQArgBlock;
The*fnid
argument is the OID of the function to be executed.args
andnargs
define the parameters to be passed to the function; they must match the declared function argument list. When theisint
field of a parameter structure is true, theu.integer
value is sent to the server as an integer of the indicated length (this must be 2 or 4 bytes); proper byte-swapping occurs. Whenisint
is false, the indicated number of bytes at`u.ptr* are sent with no processing; the data must be in the format expected by the server for binary transmission of the function's argument data type. (The declaration of *
u.ptr* as being of type
intis historical; it would be better to consider it
void*.) *
result_buf* points to the buffer in which to place the function's return value. The caller must have allocated sufficient space to store the return value. (There is no check!) The actual result length in bytes will be returned in the integer pointed to by *
result_len*. If a 2- or 4-byte integer result is expected, set *
result_is_int* to 1, otherwise set it to 0. Setting *
result_is_int* to 1 causes libpq to byte-swap the value if necessary, so that it is delivered as a proper
intvalue for the client machine; note that a 4-byte integer is delivered into *
*result_buf`for either allowed result size. Whenresult_is_int
is 0, the binary-format byte string sent by the server is returned unmodified. (In this case it's better to considerresult_buf
*as being of typevoid *
.)
PQfn
always returns a validPGresult
pointer, with statusPGRES_COMMAND_OK
为了成功或PGRES_FATAL_ERROR
如果遇到一些问题。使用结果前应检查结果状态。调用者负责释放PG结果
和PQclear
当不再需要它时。
要将 NULL 参数传递给函数,请设置*连
该参数结构的字段-1
;这伊森特
和你
*那么字段就无关紧要了。
如果函数返回 NULL,*`*结果_len* 设定为
-1, 和 *
*结果缓冲区`*未修改。
请注意,使用此接口时无法处理设置值结果。此外,该函数必须是普通函数,而不是聚合、窗口函数或过程。