1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* fastpath.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines to handle function requests from the frontend
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-08-29 06:13:13 +02:00
|
|
|
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-08-29 07:07:03 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.75 2004/08/29 05:06:49 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* This cruft is the server side of PQfn.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/pg_proc.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "libpq/libpq.h"
|
|
|
|
#include "libpq/pqformat.h"
|
2003-01-09 19:00:24 +01:00
|
|
|
#include "miscadmin.h"
|
2003-05-09 20:08:48 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "tcop/fastpath.h"
|
2003-01-01 22:57:05 +01:00
|
|
|
#include "utils/acl.h"
|
2001-06-01 17:45:42 +02:00
|
|
|
#include "utils/lsyscache.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/syscache.h"
|
2003-01-01 22:57:05 +01:00
|
|
|
#include "utils/tqual.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
/*
|
|
|
|
* Formerly, this code attempted to cache the function and type info
|
|
|
|
* looked up by fetch_fp_info, but only for the duration of a single
|
|
|
|
* transaction command (since in theory the info could change between
|
|
|
|
* commands). This was utterly useless, because postgres.c executes
|
|
|
|
* each fastpath call as a separate transaction command, and so the
|
|
|
|
* cached data could never actually have been reused. If it had worked
|
|
|
|
* as intended, it would have had problems anyway with dangling references
|
|
|
|
* in the FmgrInfo struct. So, forget about caching and just repeat the
|
|
|
|
* syscache fetches on each usage. They're not *that* expensive.
|
|
|
|
*/
|
|
|
|
struct fp_info
|
|
|
|
{
|
|
|
|
Oid funcid;
|
|
|
|
FmgrInfo flinfo; /* function lookup info for funcid */
|
2003-05-09 20:08:48 +02:00
|
|
|
Oid namespace; /* other stuff from pg_proc */
|
|
|
|
Oid rettype;
|
|
|
|
Oid argtypes[FUNC_MAX_ARGS];
|
2003-05-08 20:16:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
|
|
|
|
FunctionCallInfo fcinfo);
|
|
|
|
static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
|
|
|
|
FunctionCallInfo fcinfo);
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/* ----------------
|
|
|
|
* GetOldFunctionMessage
|
|
|
|
*
|
|
|
|
* In pre-3.0 protocol, there is no length word on the message, so we have
|
|
|
|
* to have code that understands the message layout to absorb the message
|
|
|
|
* into a buffer. We want to do this before we start execution, so that
|
|
|
|
* we do not lose sync with the frontend if there's an error.
|
|
|
|
*
|
|
|
|
* The caller should already have initialized buf to empty.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
GetOldFunctionMessage(StringInfo buf)
|
|
|
|
{
|
|
|
|
int32 ibuf;
|
|
|
|
int nargs;
|
|
|
|
|
|
|
|
/* Dummy string argument */
|
|
|
|
if (pq_getstring(buf))
|
|
|
|
return EOF;
|
|
|
|
/* Function OID */
|
|
|
|
if (pq_getbytes((char *) &ibuf, 4))
|
|
|
|
return EOF;
|
|
|
|
appendBinaryStringInfo(buf, (char *) &ibuf, 4);
|
|
|
|
/* Number of arguments */
|
|
|
|
if (pq_getbytes((char *) &ibuf, 4))
|
|
|
|
return EOF;
|
|
|
|
appendBinaryStringInfo(buf, (char *) &ibuf, 4);
|
|
|
|
nargs = ntohl(ibuf);
|
|
|
|
/* For each argument ... */
|
|
|
|
while (nargs-- > 0)
|
|
|
|
{
|
|
|
|
int argsize;
|
|
|
|
|
|
|
|
/* argsize */
|
|
|
|
if (pq_getbytes((char *) &ibuf, 4))
|
|
|
|
return EOF;
|
|
|
|
appendBinaryStringInfo(buf, (char *) &ibuf, 4);
|
|
|
|
argsize = ntohl(ibuf);
|
2003-05-09 20:08:48 +02:00
|
|
|
if (argsize < -1)
|
2003-04-19 02:02:30 +02:00
|
|
|
{
|
|
|
|
/* FATAL here since no hope of regaining message sync */
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
2003-08-04 02:43:34 +02:00
|
|
|
errmsg("invalid argument size %d in function call message",
|
|
|
|
argsize)));
|
2003-04-19 02:02:30 +02:00
|
|
|
}
|
|
|
|
/* and arg contents */
|
|
|
|
if (argsize > 0)
|
|
|
|
{
|
|
|
|
/* Allocate space for arg */
|
|
|
|
enlargeStringInfo(buf, argsize);
|
|
|
|
/* And grab it */
|
|
|
|
if (pq_getbytes(buf->data + buf->len, argsize))
|
|
|
|
return EOF;
|
|
|
|
buf->len += argsize;
|
|
|
|
/* Place a trailing null per StringInfo convention */
|
|
|
|
buf->data[buf->len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* SendFunctionResult
|
2002-08-24 17:00:47 +02:00
|
|
|
*
|
2003-05-09 20:08:48 +02:00
|
|
|
* Note: although this routine doesn't check, the format had better be 1
|
|
|
|
* (binary) when talking to a pre-3.0 client.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
2003-05-09 20:08:48 +02:00
|
|
|
SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
|
1999-04-25 05:19:27 +02:00
|
|
|
StringInfoData buf;
|
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
pq_beginmessage(&buf, 'V');
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
if (isnull)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
if (newstyle)
|
2003-05-08 20:16:37 +02:00
|
|
|
pq_sendint(&buf, -1, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
if (!newstyle)
|
2003-05-08 20:16:37 +02:00
|
|
|
pq_sendbyte(&buf, 'G');
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
if (format == 0)
|
|
|
|
{
|
|
|
|
Oid typoutput,
|
2004-06-06 02:41:28 +02:00
|
|
|
typioparam;
|
2003-05-09 20:08:48 +02:00
|
|
|
bool typisvarlena;
|
|
|
|
char *outputstr;
|
|
|
|
|
2004-06-06 02:41:28 +02:00
|
|
|
getTypeOutputInfo(rettype, &typoutput, &typioparam, &typisvarlena);
|
2003-05-09 20:08:48 +02:00
|
|
|
outputstr = DatumGetCString(OidFunctionCall3(typoutput,
|
|
|
|
retval,
|
2004-08-29 07:07:03 +02:00
|
|
|
ObjectIdGetDatum(typioparam),
|
2003-08-04 02:43:34 +02:00
|
|
|
Int32GetDatum(-1)));
|
2003-05-09 20:08:48 +02:00
|
|
|
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
|
|
|
|
pfree(outputstr);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-05-09 20:08:48 +02:00
|
|
|
else if (format == 1)
|
|
|
|
{
|
|
|
|
Oid typsend,
|
2004-06-06 02:41:28 +02:00
|
|
|
typioparam;
|
2003-05-09 20:08:48 +02:00
|
|
|
bool typisvarlena;
|
|
|
|
bytea *outputbytes;
|
|
|
|
|
|
|
|
getTypeBinaryOutputInfo(rettype,
|
2004-06-06 02:41:28 +02:00
|
|
|
&typsend, &typioparam, &typisvarlena);
|
2003-05-09 20:08:48 +02:00
|
|
|
outputbytes = DatumGetByteaP(OidFunctionCall2(typsend,
|
|
|
|
retval,
|
2004-08-29 07:07:03 +02:00
|
|
|
ObjectIdGetDatum(typioparam)));
|
2003-05-09 20:08:48 +02:00
|
|
|
/* We assume the result will not have been toasted */
|
|
|
|
pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
|
|
|
|
pq_sendbytes(&buf, VARDATA(outputbytes),
|
|
|
|
VARSIZE(outputbytes) - VARHDRSZ);
|
|
|
|
pfree(outputbytes);
|
|
|
|
}
|
|
|
|
else
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unsupported format code: %d", format)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
if (!newstyle)
|
|
|
|
pq_sendbyte(&buf, '0');
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_endmessage(&buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-06-01 17:45:42 +02:00
|
|
|
* fetch_fp_info
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Performs catalog lookups to load a struct fp_info 'fip' for the
|
|
|
|
* function 'func_id'.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-01 17:45:42 +02:00
|
|
|
fetch_fp_info(Oid func_id, struct fp_info * fip)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-01 17:45:42 +02:00
|
|
|
HeapTuple func_htp;
|
1997-09-08 04:41:22 +02:00
|
|
|
Form_pg_proc pp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(OidIsValid(func_id));
|
2004-01-07 19:56:30 +01:00
|
|
|
Assert(fip != NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the validity of this structure is determined by whether the
|
|
|
|
* funcid is OK, we clear the funcid here. It must not be set to the
|
|
|
|
* correct value until we are about to return with a good struct
|
2003-07-22 21:00:12 +02:00
|
|
|
* fp_info, since we can be interrupted (i.e., with an ereport(ERROR,
|
2001-06-01 17:45:42 +02:00
|
|
|
* ...)) at any time. [No longer really an issue since we don't save
|
2001-10-25 07:50:21 +02:00
|
|
|
* the struct fp_info across transactions anymore, but keep it
|
|
|
|
* anyway.]
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-01 17:45:42 +02:00
|
|
|
MemSet((char *) fip, 0, sizeof(struct fp_info));
|
1997-09-07 07:04:48 +02:00
|
|
|
fip->funcid = InvalidOid;
|
|
|
|
|
2001-06-01 17:45:42 +02:00
|
|
|
fmgr_info(func_id, &fip->flinfo);
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
func_htp = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(func_id),
|
|
|
|
0, 0, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!HeapTupleIsValid(func_htp))
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function with OID %u does not exist", func_id)));
|
1997-09-07 07:04:48 +02:00
|
|
|
pp = (Form_pg_proc) GETSTRUCT(func_htp);
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
fip->namespace = pp->pronamespace;
|
|
|
|
fip->rettype = pp->prorettype;
|
|
|
|
memcpy(fip->argtypes, pp->proargtypes, FUNC_MAX_ARGS * sizeof(Oid));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(func_htp);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* This must be last!
|
|
|
|
*/
|
|
|
|
fip->funcid = func_id;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HandleFunctionRequest
|
|
|
|
*
|
|
|
|
* Server side of PQfn (fastpath function calls from the frontend).
|
|
|
|
* This corresponds to the libpq protocol symbol "F".
|
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* INPUT:
|
|
|
|
* In protocol version 3, postgres.c has already read the message body
|
|
|
|
* and will pass it in msgBuf.
|
|
|
|
* In old protocol, the passed msgBuf is empty and we must read the
|
|
|
|
* message here.
|
2003-08-04 02:43:34 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS:
|
1999-07-22 04:40:07 +02:00
|
|
|
* 0 if successful completion, EOF if frontend connection lost.
|
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Note: All ordinary errors result in ereport(ERROR,...). However,
|
2003-07-22 21:00:12 +02:00
|
|
|
* if we lose the frontend connection there is no one to ereport to,
|
1999-07-22 04:40:07 +02:00
|
|
|
* and no use in proceeding...
|
2000-10-24 22:59:35 +02:00
|
|
|
*
|
|
|
|
* Note: palloc()s done here and in the called function do not need to be
|
|
|
|
* cleaned up explicitly. We are called from PostgresMain() in the
|
2003-05-02 22:54:36 +02:00
|
|
|
* MessageContext memory context, which will be automatically reset when
|
2000-10-24 22:59:35 +02:00
|
|
|
* control returns to PostgresMain.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
2003-04-19 02:02:30 +02:00
|
|
|
HandleFunctionRequest(StringInfo msgBuf)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Oid fid;
|
2003-01-01 22:57:05 +01:00
|
|
|
AclResult aclresult;
|
2000-05-28 19:56:29 +02:00
|
|
|
FunctionCallInfoData fcinfo;
|
2003-05-09 20:08:48 +02:00
|
|
|
int16 rformat;
|
2000-05-28 19:56:29 +02:00
|
|
|
Datum retval;
|
2001-06-01 17:45:42 +02:00
|
|
|
struct fp_info my_fp;
|
1997-09-07 07:04:48 +02:00
|
|
|
struct fp_info *fip;
|
2003-05-09 20:18:54 +02:00
|
|
|
bool callit;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-24 22:59:35 +02:00
|
|
|
/*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Read message contents if not already done.
|
2000-10-24 22:59:35 +02:00
|
|
|
*/
|
2003-04-19 02:02:30 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
|
|
|
|
{
|
|
|
|
if (GetOldFunctionMessage(msgBuf))
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(COMMERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("unexpected EOF on client connection")));
|
2003-04-19 02:02:30 +02:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
2000-10-24 22:59:35 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/*
|
|
|
|
* Now that we've eaten the input message, check to see if we actually
|
2003-07-22 21:00:12 +02:00
|
|
|
* want to do the function call or not. It's now safe to ereport();
|
|
|
|
* we won't lose sync with the frontend.
|
2003-04-19 02:02:30 +02:00
|
|
|
*/
|
|
|
|
if (IsAbortedTransactionBlockState())
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
|
|
|
|
errmsg("current transaction is aborted, "
|
2004-08-29 07:07:03 +02:00
|
|
|
"commands ignored until end of transaction block")));
|
2003-04-19 02:02:30 +02:00
|
|
|
|
|
|
|
/*
|
2003-05-09 20:08:48 +02:00
|
|
|
* Begin parsing the buffer contents.
|
2003-04-19 02:02:30 +02:00
|
|
|
*/
|
2003-05-08 20:16:37 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
|
2003-08-04 02:43:34 +02:00
|
|
|
(void) pq_getmsgstring(msgBuf); /* dummy string */
|
2003-05-08 20:16:37 +02:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* There used to be a lame attempt at caching lookup info here. Now we
|
|
|
|
* just do the lookups on every call.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-01 17:45:42 +02:00
|
|
|
fip = &my_fp;
|
|
|
|
fetch_fp_info(fid, fip);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
/*
|
|
|
|
* Check permission to access and call function. Since we didn't go
|
|
|
|
* through a normal name lookup, we need to check schema usage too.
|
|
|
|
*/
|
|
|
|
aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(fip->namespace));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_PROC,
|
|
|
|
get_func_name(fid));
|
2003-04-19 02:02:30 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
/*
|
|
|
|
* Set up a query snapshot in case function needs one.
|
|
|
|
*/
|
|
|
|
SetQuerySnapshot();
|
|
|
|
|
|
|
|
/*
|
2003-05-08 20:16:37 +02:00
|
|
|
* Prepare function call info block and insert arguments.
|
2003-05-05 02:44:56 +02:00
|
|
|
*/
|
2003-05-08 20:16:37 +02:00
|
|
|
MemSet(&fcinfo, 0, sizeof(fcinfo));
|
|
|
|
fcinfo.flinfo = &fip->flinfo;
|
|
|
|
|
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
|
2003-05-09 20:08:48 +02:00
|
|
|
rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
|
2003-05-08 20:16:37 +02:00
|
|
|
else
|
2003-05-09 20:08:48 +02:00
|
|
|
rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
/* Verify we reached the end of the message where expected. */
|
|
|
|
pq_getmsgend(msgBuf);
|
|
|
|
|
2003-05-09 20:18:54 +02:00
|
|
|
/*
|
|
|
|
* If func is strict, must not call it for null args.
|
|
|
|
*/
|
|
|
|
callit = true;
|
|
|
|
if (fip->flinfo.fn_strict)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int i;
|
2003-05-09 20:18:54 +02:00
|
|
|
|
|
|
|
for (i = 0; i < fcinfo.nargs; i++)
|
|
|
|
{
|
|
|
|
if (fcinfo.argnull[i])
|
|
|
|
{
|
|
|
|
callit = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callit)
|
|
|
|
{
|
|
|
|
/* Okay, do it ... */
|
|
|
|
retval = FunctionCallInvoke(&fcinfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fcinfo.isnull = true;
|
|
|
|
retval = (Datum) 0;
|
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse function arguments in a 3.0 protocol message
|
2003-05-09 20:08:48 +02:00
|
|
|
*
|
|
|
|
* Argument values are loaded into *fcinfo, and the desired result format
|
|
|
|
* is returned.
|
2003-05-08 20:16:37 +02:00
|
|
|
*/
|
2003-05-09 20:08:48 +02:00
|
|
|
static int16
|
2003-08-04 02:43:34 +02:00
|
|
|
parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
|
2003-05-08 20:16:37 +02:00
|
|
|
FunctionCallInfo fcinfo)
|
|
|
|
{
|
|
|
|
int nargs;
|
|
|
|
int i;
|
|
|
|
int numAFormats;
|
|
|
|
int16 *aformats = NULL;
|
2003-05-09 20:08:48 +02:00
|
|
|
StringInfoData abuf;
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
/* Get the argument format codes */
|
|
|
|
numAFormats = pq_getmsgint(msgBuf, 2);
|
|
|
|
if (numAFormats > 0)
|
|
|
|
{
|
|
|
|
aformats = (int16 *) palloc(numAFormats * sizeof(int16));
|
|
|
|
for (i = 0; i < numAFormats; i++)
|
|
|
|
aformats[i] = pq_getmsgint(msgBuf, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
|
|
|
|
|
2000-05-28 19:56:29 +02:00
|
|
|
if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("function call message contains %d arguments but function requires %d",
|
|
|
|
nargs, fip->flinfo.fn_nargs)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
fcinfo->nargs = nargs;
|
2000-05-28 19:56:29 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
if (numAFormats > 1 && numAFormats != nargs)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("function call message contains %d argument formats but %d arguments",
|
|
|
|
numAFormats, nargs)));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
initStringInfo(&abuf);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2003-05-08 20:16:37 +02:00
|
|
|
* Copy supplied arguments into arg vector.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-05-28 19:56:29 +02:00
|
|
|
for (i = 0; i < nargs; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-04-19 02:02:30 +02:00
|
|
|
int argsize;
|
2003-05-09 20:08:48 +02:00
|
|
|
int16 aformat;
|
2003-04-19 02:02:30 +02:00
|
|
|
|
|
|
|
argsize = pq_getmsgint(msgBuf, 4);
|
2003-05-09 20:08:48 +02:00
|
|
|
if (argsize == -1)
|
|
|
|
{
|
|
|
|
fcinfo->argnull[i] = true;
|
|
|
|
continue;
|
2000-05-28 19:56:29 +02:00
|
|
|
}
|
2003-05-09 20:08:48 +02:00
|
|
|
if (argsize < 0)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
2003-08-04 02:43:34 +02:00
|
|
|
errmsg("invalid argument size %d in function call message",
|
|
|
|
argsize)));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Reset abuf to empty, and insert raw data into it */
|
|
|
|
abuf.len = 0;
|
|
|
|
abuf.data[0] = '\0';
|
|
|
|
abuf.cursor = 0;
|
|
|
|
|
|
|
|
appendBinaryStringInfo(&abuf,
|
|
|
|
pq_getmsgbytes(msgBuf, argsize),
|
|
|
|
argsize);
|
|
|
|
|
|
|
|
if (numAFormats > 1)
|
|
|
|
aformat = aformats[i];
|
|
|
|
else if (numAFormats > 0)
|
|
|
|
aformat = aformats[0];
|
2000-05-28 19:56:29 +02:00
|
|
|
else
|
2003-05-09 20:08:48 +02:00
|
|
|
aformat = 0; /* default = text */
|
|
|
|
|
|
|
|
if (aformat == 0)
|
|
|
|
{
|
2004-06-06 02:41:28 +02:00
|
|
|
Oid typinput;
|
|
|
|
Oid typioparam;
|
2003-05-09 20:08:48 +02:00
|
|
|
char *pstring;
|
|
|
|
|
2004-06-06 02:41:28 +02:00
|
|
|
getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Since stringinfo.c keeps a trailing null in place even for
|
|
|
|
* binary data, the contents of abuf are a valid C string. We
|
|
|
|
* have to do encoding conversion before calling the typinput
|
2003-05-09 20:08:48 +02:00
|
|
|
* routine, though.
|
|
|
|
*/
|
|
|
|
pstring = (char *)
|
|
|
|
pg_client_to_server((unsigned char *) abuf.data,
|
|
|
|
argsize);
|
|
|
|
fcinfo->arg[i] =
|
2004-06-06 02:41:28 +02:00
|
|
|
OidFunctionCall3(typinput,
|
2003-05-09 20:08:48 +02:00
|
|
|
CStringGetDatum(pstring),
|
2004-06-06 02:41:28 +02:00
|
|
|
ObjectIdGetDatum(typioparam),
|
2003-05-09 20:08:48 +02:00
|
|
|
Int32GetDatum(-1));
|
|
|
|
/* Free result of encoding conversion, if any */
|
|
|
|
if (pstring != abuf.data)
|
|
|
|
pfree(pstring);
|
|
|
|
}
|
|
|
|
else if (aformat == 1)
|
|
|
|
{
|
2004-06-06 02:41:28 +02:00
|
|
|
Oid typreceive;
|
|
|
|
Oid typioparam;
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Call the argument type's binary input converter */
|
2004-06-06 02:41:28 +02:00
|
|
|
getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
|
2003-05-09 20:08:48 +02:00
|
|
|
|
2004-06-06 02:41:28 +02:00
|
|
|
fcinfo->arg[i] = OidFunctionCall2(typreceive,
|
2003-05-09 20:08:48 +02:00
|
|
|
PointerGetDatum(&abuf),
|
2004-08-29 07:07:03 +02:00
|
|
|
ObjectIdGetDatum(typioparam));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Trouble if it didn't eat the whole buffer */
|
|
|
|
if (abuf.cursor != abuf.len)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
|
|
|
errmsg("incorrect binary data format in function argument %d",
|
|
|
|
i + 1)));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-05-09 20:08:48 +02:00
|
|
|
else
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unsupported format code: %d", aformat)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
/* Return result format code */
|
|
|
|
return (int16) pq_getmsgint(msgBuf, 2);
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
2003-01-01 22:57:05 +01:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
/*
|
|
|
|
* Parse function arguments in a 2.0 protocol message
|
2003-05-09 20:08:48 +02:00
|
|
|
*
|
|
|
|
* Argument values are loaded into *fcinfo, and the desired result format
|
|
|
|
* is returned.
|
2003-05-08 20:16:37 +02:00
|
|
|
*/
|
2003-05-09 20:08:48 +02:00
|
|
|
static int16
|
2003-08-04 02:43:34 +02:00
|
|
|
parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
|
2003-05-08 20:16:37 +02:00
|
|
|
FunctionCallInfo fcinfo)
|
|
|
|
{
|
|
|
|
int nargs;
|
|
|
|
int i;
|
2003-05-09 20:08:48 +02:00
|
|
|
StringInfoData abuf;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("function call message contains %d arguments but function requires %d",
|
|
|
|
nargs, fip->flinfo.fn_nargs)));
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
fcinfo->nargs = nargs;
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
initStringInfo(&abuf);
|
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
/*
|
2003-05-09 20:08:48 +02:00
|
|
|
* Copy supplied arguments into arg vector. In protocol 2.0 these are
|
|
|
|
* always assumed to be supplied in binary format.
|
|
|
|
*
|
|
|
|
* Note: although the original protocol 2.0 code did not have any way for
|
|
|
|
* the frontend to specify a NULL argument, we now choose to interpret
|
|
|
|
* length == -1 as meaning a NULL.
|
2003-05-08 20:16:37 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < nargs; ++i)
|
|
|
|
{
|
|
|
|
int argsize;
|
2004-06-06 02:41:28 +02:00
|
|
|
Oid typreceive;
|
|
|
|
Oid typioparam;
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
argsize = pq_getmsgint(msgBuf, 4);
|
2003-05-09 20:08:48 +02:00
|
|
|
if (argsize == -1)
|
|
|
|
{
|
|
|
|
fcinfo->argnull[i] = true;
|
|
|
|
continue;
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
2003-05-09 20:08:48 +02:00
|
|
|
if (argsize < 0)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
2003-08-04 02:43:34 +02:00
|
|
|
errmsg("invalid argument size %d in function call message",
|
|
|
|
argsize)));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Reset abuf to empty, and insert raw data into it */
|
|
|
|
abuf.len = 0;
|
|
|
|
abuf.data[0] = '\0';
|
|
|
|
abuf.cursor = 0;
|
|
|
|
|
|
|
|
appendBinaryStringInfo(&abuf,
|
|
|
|
pq_getmsgbytes(msgBuf, argsize),
|
|
|
|
argsize);
|
|
|
|
|
|
|
|
/* Call the argument type's binary input converter */
|
2004-06-06 02:41:28 +02:00
|
|
|
getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
|
2003-05-09 20:08:48 +02:00
|
|
|
|
2004-06-06 02:41:28 +02:00
|
|
|
fcinfo->arg[i] = OidFunctionCall2(typreceive,
|
2003-05-09 20:08:48 +02:00
|
|
|
PointerGetDatum(&abuf),
|
2004-06-06 02:41:28 +02:00
|
|
|
ObjectIdGetDatum(typioparam));
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Trouble if it didn't eat the whole buffer */
|
|
|
|
if (abuf.cursor != abuf.len)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
2003-08-04 02:43:34 +02:00
|
|
|
errmsg("incorrect binary data format in function argument %d",
|
|
|
|
i + 1)));
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
2003-05-09 20:08:48 +02:00
|
|
|
|
|
|
|
/* Desired result format is always binary in protocol 2.0 */
|
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|