1 /*-------------------------------------------------------------------------
4 * routines to handle function requests from the frontend
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.83 2005/10/15 02:49:26 momjian Exp $
14 * This cruft is the server side of PQfn.
16 *-------------------------------------------------------------------------
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
23 #include "catalog/pg_proc.h"
24 #include "libpq/libpq.h"
25 #include "libpq/pqformat.h"
26 #include "miscadmin.h"
27 #include "mb/pg_wchar.h"
28 #include "tcop/fastpath.h"
29 #include "utils/acl.h"
30 #include "utils/lsyscache.h"
31 #include "utils/syscache.h"
32 #include "utils/tqual.h"
36 * Formerly, this code attempted to cache the function and type info
37 * looked up by fetch_fp_info, but only for the duration of a single
38 * transaction command (since in theory the info could change between
39 * commands). This was utterly useless, because postgres.c executes
40 * each fastpath call as a separate transaction command, and so the
41 * cached data could never actually have been reused. If it had worked
42 * as intended, it would have had problems anyway with dangling references
43 * in the FmgrInfo struct. So, forget about caching and just repeat the
44 * syscache fetches on each usage. They're not *that* expensive.
49 FmgrInfo flinfo; /* function lookup info for funcid */
50 Oid namespace; /* other stuff from pg_proc */
52 Oid argtypes[FUNC_MAX_ARGS];
56 static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
57 FunctionCallInfo fcinfo);
58 static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
59 FunctionCallInfo fcinfo);
63 * GetOldFunctionMessage
65 * In pre-3.0 protocol, there is no length word on the message, so we have
66 * to have code that understands the message layout to absorb the message
67 * into a buffer. We want to do this before we start execution, so that
68 * we do not lose sync with the frontend if there's an error.
70 * The caller should already have initialized buf to empty.
74 GetOldFunctionMessage(StringInfo buf)
79 /* Dummy string argument */
80 if (pq_getstring(buf))
83 if (pq_getbytes((char *) &ibuf, 4))
85 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
86 /* Number of arguments */
87 if (pq_getbytes((char *) &ibuf, 4))
89 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
91 /* For each argument ... */
97 if (pq_getbytes((char *) &ibuf, 4))
99 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
100 argsize = ntohl(ibuf);
103 /* FATAL here since no hope of regaining message sync */
105 (errcode(ERRCODE_PROTOCOL_VIOLATION),
106 errmsg("invalid argument size %d in function call message",
109 /* and arg contents */
112 /* Allocate space for arg */
113 enlargeStringInfo(buf, argsize);
115 if (pq_getbytes(buf->data + buf->len, argsize))
118 /* Place a trailing null per StringInfo convention */
119 buf->data[buf->len] = '\0';
128 * Note: although this routine doesn't check, the format had better be 1
129 * (binary) when talking to a pre-3.0 client.
133 SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
135 bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
138 pq_beginmessage(&buf, 'V');
143 pq_sendint(&buf, -1, 4);
148 pq_sendbyte(&buf, 'G');
156 getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
157 outputstr = DatumGetCString(OidFunctionCall1(typoutput,
159 pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
162 else if (format == 1)
168 getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
169 outputbytes = DatumGetByteaP(OidFunctionCall1(typsend,
171 /* We assume the result will not have been toasted */
172 pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
173 pq_sendbytes(&buf, VARDATA(outputbytes),
174 VARSIZE(outputbytes) - VARHDRSZ);
179 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
180 errmsg("unsupported format code: %d", format)));
184 pq_sendbyte(&buf, '0');
192 * Performs catalog lookups to load a struct fp_info 'fip' for the
193 * function 'func_id'.
196 fetch_fp_info(Oid func_id, struct fp_info * fip)
201 Assert(OidIsValid(func_id));
205 * Since the validity of this structure is determined by whether the
206 * funcid is OK, we clear the funcid here. It must not be set to the
207 * correct value until we are about to return with a good struct fp_info,
208 * since we can be interrupted (i.e., with an ereport(ERROR, ...)) at any
209 * time. [No longer really an issue since we don't save the struct
210 * fp_info across transactions anymore, but keep it anyway.]
212 MemSet(fip, 0, sizeof(struct fp_info));
213 fip->funcid = InvalidOid;
215 fmgr_info(func_id, &fip->flinfo);
217 func_htp = SearchSysCache(PROCOID,
218 ObjectIdGetDatum(func_id),
220 if (!HeapTupleIsValid(func_htp))
222 (errcode(ERRCODE_UNDEFINED_FUNCTION),
223 errmsg("function with OID %u does not exist", func_id)));
224 pp = (Form_pg_proc) GETSTRUCT(func_htp);
226 /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
227 if (pp->pronargs > FUNC_MAX_ARGS)
228 elog(ERROR, "function %s has more than %d arguments",
229 NameStr(pp->proname), FUNC_MAX_ARGS);
231 fip->namespace = pp->pronamespace;
232 fip->rettype = pp->prorettype;
233 memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
235 ReleaseSysCache(func_htp);
240 fip->funcid = func_id;
245 * HandleFunctionRequest
247 * Server side of PQfn (fastpath function calls from the frontend).
248 * This corresponds to the libpq protocol symbol "F".
251 * In protocol version 3, postgres.c has already read the message body
252 * and will pass it in msgBuf.
253 * In old protocol, the passed msgBuf is empty and we must read the
257 * 0 if successful completion, EOF if frontend connection lost.
259 * Note: All ordinary errors result in ereport(ERROR,...). However,
260 * if we lose the frontend connection there is no one to ereport to,
261 * and no use in proceeding...
263 * Note: palloc()s done here and in the called function do not need to be
264 * cleaned up explicitly. We are called from PostgresMain() in the
265 * MessageContext memory context, which will be automatically reset when
266 * control returns to PostgresMain.
269 HandleFunctionRequest(StringInfo msgBuf)
273 FunctionCallInfoData fcinfo;
276 struct fp_info my_fp;
281 * Read message contents if not already done.
283 if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
285 if (GetOldFunctionMessage(msgBuf))
288 (errcode(ERRCODE_PROTOCOL_VIOLATION),
289 errmsg("unexpected EOF on client connection")));
295 * Now that we've eaten the input message, check to see if we actually
296 * want to do the function call or not. It's now safe to ereport(); we
297 * won't lose sync with the frontend.
299 if (IsAbortedTransactionBlockState())
301 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
302 errmsg("current transaction is aborted, "
303 "commands ignored until end of transaction block")));
306 * Begin parsing the buffer contents.
308 if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
309 (void) pq_getmsgstring(msgBuf); /* dummy string */
311 fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
314 * There used to be a lame attempt at caching lookup info here. Now we
315 * just do the lookups on every call.
318 fetch_fp_info(fid, fip);
321 * Check permission to access and call function. Since we didn't go
322 * through a normal name lookup, we need to check schema usage too.
324 aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
325 if (aclresult != ACLCHECK_OK)
326 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
327 get_namespace_name(fip->namespace));
329 aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
330 if (aclresult != ACLCHECK_OK)
331 aclcheck_error(aclresult, ACL_KIND_PROC,
335 * Prepare function call info block and insert arguments.
337 InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, NULL, NULL);
339 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
340 rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
342 rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
344 /* Verify we reached the end of the message where expected. */
345 pq_getmsgend(msgBuf);
348 * If func is strict, must not call it for null args.
351 if (fip->flinfo.fn_strict)
355 for (i = 0; i < fcinfo.nargs; i++)
357 if (fcinfo.argnull[i])
367 /* Okay, do it ... */
368 retval = FunctionCallInvoke(&fcinfo);
372 fcinfo.isnull = true;
376 SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
382 * Parse function arguments in a 3.0 protocol message
384 * Argument values are loaded into *fcinfo, and the desired result format
388 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
389 FunctionCallInfo fcinfo)
394 int16 *aformats = NULL;
397 /* Get the argument format codes */
398 numAFormats = pq_getmsgint(msgBuf, 2);
401 aformats = (int16 *) palloc(numAFormats * sizeof(int16));
402 for (i = 0; i < numAFormats; i++)
403 aformats[i] = pq_getmsgint(msgBuf, 2);
406 nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
408 if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
410 (errcode(ERRCODE_PROTOCOL_VIOLATION),
411 errmsg("function call message contains %d arguments but function requires %d",
412 nargs, fip->flinfo.fn_nargs)));
414 fcinfo->nargs = nargs;
416 if (numAFormats > 1 && numAFormats != nargs)
418 (errcode(ERRCODE_PROTOCOL_VIOLATION),
419 errmsg("function call message contains %d argument formats but %d arguments",
420 numAFormats, nargs)));
422 initStringInfo(&abuf);
425 * Copy supplied arguments into arg vector.
427 for (i = 0; i < nargs; ++i)
432 argsize = pq_getmsgint(msgBuf, 4);
435 fcinfo->argnull[i] = true;
438 fcinfo->argnull[i] = false;
441 (errcode(ERRCODE_PROTOCOL_VIOLATION),
442 errmsg("invalid argument size %d in function call message",
445 /* Reset abuf to empty, and insert raw data into it */
450 appendBinaryStringInfo(&abuf,
451 pq_getmsgbytes(msgBuf, argsize),
455 aformat = aformats[i];
456 else if (numAFormats > 0)
457 aformat = aformats[0];
459 aformat = 0; /* default = text */
467 getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
470 * Since stringinfo.c keeps a trailing null in place even for
471 * binary data, the contents of abuf are a valid C string. We
472 * have to do encoding conversion before calling the typinput
475 pstring = pg_client_to_server(abuf.data, argsize);
477 OidFunctionCall3(typinput,
478 CStringGetDatum(pstring),
479 ObjectIdGetDatum(typioparam),
481 /* Free result of encoding conversion, if any */
482 if (pstring != abuf.data)
485 else if (aformat == 1)
490 /* Call the argument type's binary input converter */
491 getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
493 fcinfo->arg[i] = OidFunctionCall3(typreceive,
494 PointerGetDatum(&abuf),
495 ObjectIdGetDatum(typioparam),
498 /* Trouble if it didn't eat the whole buffer */
499 if (abuf.cursor != abuf.len)
501 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
502 errmsg("incorrect binary data format in function argument %d",
507 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
508 errmsg("unsupported format code: %d", aformat)));
511 /* Return result format code */
512 return (int16) pq_getmsgint(msgBuf, 2);
516 * Parse function arguments in a 2.0 protocol message
518 * Argument values are loaded into *fcinfo, and the desired result format
522 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
523 FunctionCallInfo fcinfo)
529 nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
531 if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
533 (errcode(ERRCODE_PROTOCOL_VIOLATION),
534 errmsg("function call message contains %d arguments but function requires %d",
535 nargs, fip->flinfo.fn_nargs)));
537 fcinfo->nargs = nargs;
539 initStringInfo(&abuf);
542 * Copy supplied arguments into arg vector. In protocol 2.0 these are
543 * always assumed to be supplied in binary format.
545 * Note: although the original protocol 2.0 code did not have any way for the
546 * frontend to specify a NULL argument, we now choose to interpret length
547 * == -1 as meaning a NULL.
549 for (i = 0; i < nargs; ++i)
555 argsize = pq_getmsgint(msgBuf, 4);
558 fcinfo->argnull[i] = true;
561 fcinfo->argnull[i] = false;
564 (errcode(ERRCODE_PROTOCOL_VIOLATION),
565 errmsg("invalid argument size %d in function call message",
568 /* Reset abuf to empty, and insert raw data into it */
573 appendBinaryStringInfo(&abuf,
574 pq_getmsgbytes(msgBuf, argsize),
577 /* Call the argument type's binary input converter */
578 getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
580 fcinfo->arg[i] = OidFunctionCall3(typreceive,
581 PointerGetDatum(&abuf),
582 ObjectIdGetDatum(typioparam),
585 /* Trouble if it didn't eat the whole buffer */
586 if (abuf.cursor != abuf.len)
588 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
589 errmsg("incorrect binary data format in function argument %d",
593 /* Desired result format is always binary in protocol 2.0 */