1 /*-------------------------------------------------------------------------
4 * routines to handle function requests from the frontend
6 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/tcop/fastpath.c
14 * This cruft is the server side of PQfn.
16 *-------------------------------------------------------------------------
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
23 #include "access/htup_details.h"
24 #include "access/xact.h"
25 #include "catalog/objectaccess.h"
26 #include "catalog/pg_proc.h"
27 #include "libpq/libpq.h"
28 #include "libpq/pqformat.h"
29 #include "mb/pg_wchar.h"
30 #include "miscadmin.h"
31 #include "tcop/fastpath.h"
32 #include "tcop/tcopprot.h"
33 #include "utils/acl.h"
34 #include "utils/lsyscache.h"
35 #include "utils/snapmgr.h"
36 #include "utils/syscache.h"
40 * Formerly, this code attempted to cache the function and type info
41 * looked up by fetch_fp_info, but only for the duration of a single
42 * transaction command (since in theory the info could change between
43 * commands). This was utterly useless, because postgres.c executes
44 * each fastpath call as a separate transaction command, and so the
45 * cached data could never actually have been reused. If it had worked
46 * as intended, it would have had problems anyway with dangling references
47 * in the FmgrInfo struct. So, forget about caching and just repeat the
48 * syscache fetches on each usage. They're not *that* expensive.
53 FmgrInfo flinfo; /* function lookup info for funcid */
54 Oid namespace; /* other stuff from pg_proc */
56 Oid argtypes[FUNC_MAX_ARGS];
57 char fname[NAMEDATALEN]; /* function name for logging */
61 static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
62 FunctionCallInfo fcinfo);
63 static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
64 FunctionCallInfo fcinfo);
68 * GetOldFunctionMessage
70 * In pre-3.0 protocol, there is no length word on the message, so we have
71 * to have code that understands the message layout to absorb the message
72 * into a buffer. We want to do this before we start execution, so that
73 * we do not lose sync with the frontend if there's an error.
75 * The caller should already have initialized buf to empty.
79 GetOldFunctionMessage(StringInfo buf)
84 /* Dummy string argument */
85 if (pq_getstring(buf))
88 if (pq_getbytes((char *) &ibuf, 4))
90 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
91 /* Number of arguments */
92 if (pq_getbytes((char *) &ibuf, 4))
94 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
96 /* For each argument ... */
102 if (pq_getbytes((char *) &ibuf, 4))
104 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
105 argsize = ntohl(ibuf);
108 /* FATAL here since no hope of regaining message sync */
110 (errcode(ERRCODE_PROTOCOL_VIOLATION),
111 errmsg("invalid argument size %d in function call message",
114 /* and arg contents */
117 /* Allocate space for arg */
118 enlargeStringInfo(buf, argsize);
120 if (pq_getbytes(buf->data + buf->len, argsize))
123 /* Place a trailing null per StringInfo convention */
124 buf->data[buf->len] = '\0';
133 * Note: although this routine doesn't check, the format had better be 1
134 * (binary) when talking to a pre-3.0 client.
138 SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
140 bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
143 pq_beginmessage(&buf, 'V');
148 pq_sendint(&buf, -1, 4);
153 pq_sendbyte(&buf, 'G');
161 getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
162 outputstr = OidOutputFunctionCall(typoutput, retval);
163 pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
166 else if (format == 1)
172 getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
173 outputbytes = OidSendFunctionCall(typsend, retval);
174 pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
175 pq_sendbytes(&buf, VARDATA(outputbytes),
176 VARSIZE(outputbytes) - VARHDRSZ);
181 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
182 errmsg("unsupported format code: %d", format)));
186 pq_sendbyte(&buf, '0');
194 * Performs catalog lookups to load a struct fp_info 'fip' for the
195 * function 'func_id'.
198 fetch_fp_info(Oid func_id, struct fp_info * fip)
203 Assert(OidIsValid(func_id));
207 * Since the validity of this structure is determined by whether the
208 * funcid is OK, we clear the funcid here. It must not be set to the
209 * correct value until we are about to return with a good struct fp_info,
210 * since we can be interrupted (i.e., with an ereport(ERROR, ...)) at any
211 * time. [No longer really an issue since we don't save the struct
212 * fp_info across transactions anymore, but keep it anyway.]
214 MemSet(fip, 0, sizeof(struct fp_info));
215 fip->funcid = InvalidOid;
217 fmgr_info(func_id, &fip->flinfo);
219 func_htp = SearchSysCache1(PROCOID, 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));
234 strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
236 ReleaseSysCache(func_htp);
241 fip->funcid = func_id;
246 * HandleFunctionRequest
248 * Server side of PQfn (fastpath function calls from the frontend).
249 * This corresponds to the libpq protocol symbol "F".
252 * In protocol version 3, postgres.c has already read the message body
253 * and will pass it in msgBuf.
254 * In old protocol, the passed msgBuf is empty and we must read the
258 * 0 if successful completion, EOF if frontend connection lost.
260 * Note: All ordinary errors result in ereport(ERROR,...). However,
261 * if we lose the frontend connection there is no one to ereport to,
262 * and no use in proceeding...
264 * Note: palloc()s done here and in the called function do not need to be
265 * cleaned up explicitly. We are called from PostgresMain() in the
266 * MessageContext memory context, which will be automatically reset when
267 * control returns to PostgresMain.
270 HandleFunctionRequest(StringInfo msgBuf)
274 FunctionCallInfoData fcinfo;
277 struct fp_info my_fp;
280 bool was_logged = false;
284 * Now that we've eaten the input message, check to see if we actually
285 * want to do the function call or not. It's now safe to ereport(); we
286 * won't lose sync with the frontend.
288 if (IsAbortedTransactionBlockState())
290 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
291 errmsg("current transaction is aborted, "
292 "commands ignored until end of transaction block")));
295 * Now that we know we are in a valid transaction, set snapshot in case
296 * needed by function itself or one of the datatype I/O routines.
298 PushActiveSnapshot(GetTransactionSnapshot());
301 * Begin parsing the buffer contents.
303 if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
304 (void) pq_getmsgstring(msgBuf); /* dummy string */
306 fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
309 * There used to be a lame attempt at caching lookup info here. Now we
310 * just do the lookups on every call.
313 fetch_fp_info(fid, fip);
315 /* Log as soon as we have the function OID and name */
316 if (log_statement == LOGSTMT_ALL)
319 (errmsg("fastpath function call: \"%s\" (OID %u)",
325 * Check permission to access and call function. Since we didn't go
326 * through a normal name lookup, we need to check schema usage too.
328 aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
329 if (aclresult != ACLCHECK_OK)
330 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
331 get_namespace_name(fip->namespace));
332 InvokeNamespaceSearchHook(fip->namespace, true);
334 aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
335 if (aclresult != ACLCHECK_OK)
336 aclcheck_error(aclresult, ACL_KIND_PROC,
338 InvokeFunctionExecuteHook(fid);
341 * Prepare function call info block and insert arguments.
343 * Note: for now we pass collation = InvalidOid, so collation-sensitive
344 * functions can't be called this way. Perhaps we should pass
345 * DEFAULT_COLLATION_OID, instead?
347 InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
349 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
350 rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
352 rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
354 /* Verify we reached the end of the message where expected. */
355 pq_getmsgend(msgBuf);
358 * If func is strict, must not call it for null args.
361 if (fip->flinfo.fn_strict)
365 for (i = 0; i < fcinfo.nargs; i++)
367 if (fcinfo.argnull[i])
377 /* Okay, do it ... */
378 retval = FunctionCallInvoke(&fcinfo);
382 fcinfo.isnull = true;
386 /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
387 CHECK_FOR_INTERRUPTS();
389 SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
391 /* We no longer need the snapshot */
395 * Emit duration logging if appropriate.
397 switch (check_log_duration(msec_str, was_logged))
401 (errmsg("duration: %s ms", msec_str)));
405 (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
406 msec_str, fip->fname, fid)));
414 * Parse function arguments in a 3.0 protocol message
416 * Argument values are loaded into *fcinfo, and the desired result format
420 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
421 FunctionCallInfo fcinfo)
426 int16 *aformats = NULL;
429 /* Get the argument format codes */
430 numAFormats = pq_getmsgint(msgBuf, 2);
433 aformats = (int16 *) palloc(numAFormats * sizeof(int16));
434 for (i = 0; i < numAFormats; i++)
435 aformats[i] = pq_getmsgint(msgBuf, 2);
438 nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
440 if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
442 (errcode(ERRCODE_PROTOCOL_VIOLATION),
443 errmsg("function call message contains %d arguments but function requires %d",
444 nargs, fip->flinfo.fn_nargs)));
446 fcinfo->nargs = nargs;
448 if (numAFormats > 1 && numAFormats != nargs)
450 (errcode(ERRCODE_PROTOCOL_VIOLATION),
451 errmsg("function call message contains %d argument formats but %d arguments",
452 numAFormats, nargs)));
454 initStringInfo(&abuf);
457 * Copy supplied arguments into arg vector.
459 for (i = 0; i < nargs; ++i)
464 argsize = pq_getmsgint(msgBuf, 4);
467 fcinfo->argnull[i] = true;
471 fcinfo->argnull[i] = false;
474 (errcode(ERRCODE_PROTOCOL_VIOLATION),
475 errmsg("invalid argument size %d in function call message",
478 /* Reset abuf to empty, and insert raw data into it */
479 resetStringInfo(&abuf);
480 appendBinaryStringInfo(&abuf,
481 pq_getmsgbytes(msgBuf, argsize),
486 aformat = aformats[i];
487 else if (numAFormats > 0)
488 aformat = aformats[0];
490 aformat = 0; /* default = text */
498 getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
501 * Since stringinfo.c keeps a trailing null in place even for
502 * binary data, the contents of abuf are a valid C string. We
503 * have to do encoding conversion before calling the typinput
509 pstring = pg_client_to_server(abuf.data, argsize);
511 fcinfo->arg[i] = OidInputFunctionCall(typinput, pstring,
513 /* Free result of encoding conversion, if any */
514 if (pstring && pstring != abuf.data)
517 else if (aformat == 1)
523 /* Call the argument type's binary input converter */
524 getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
531 fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, bufptr,
534 /* Trouble if it didn't eat the whole buffer */
535 if (argsize != -1 && abuf.cursor != abuf.len)
537 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
538 errmsg("incorrect binary data format in function argument %d",
543 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
544 errmsg("unsupported format code: %d", aformat)));
547 /* Return result format code */
548 return (int16) pq_getmsgint(msgBuf, 2);
552 * Parse function arguments in a 2.0 protocol message
554 * Argument values are loaded into *fcinfo, and the desired result format
558 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
559 FunctionCallInfo fcinfo)
565 nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
567 if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
569 (errcode(ERRCODE_PROTOCOL_VIOLATION),
570 errmsg("function call message contains %d arguments but function requires %d",
571 nargs, fip->flinfo.fn_nargs)));
573 fcinfo->nargs = nargs;
575 initStringInfo(&abuf);
578 * Copy supplied arguments into arg vector. In protocol 2.0 these are
579 * always assumed to be supplied in binary format.
581 * Note: although the original protocol 2.0 code did not have any way for
582 * the frontend to specify a NULL argument, we now choose to interpret
583 * length == -1 as meaning a NULL.
585 for (i = 0; i < nargs; ++i)
591 getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
593 argsize = pq_getmsgint(msgBuf, 4);
596 fcinfo->argnull[i] = true;
597 fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, NULL,
601 fcinfo->argnull[i] = false;
604 (errcode(ERRCODE_PROTOCOL_VIOLATION),
605 errmsg("invalid argument size %d in function call message",
608 /* Reset abuf to empty, and insert raw data into it */
609 resetStringInfo(&abuf);
610 appendBinaryStringInfo(&abuf,
611 pq_getmsgbytes(msgBuf, argsize),
614 fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, &abuf,
617 /* Trouble if it didn't eat the whole buffer */
618 if (abuf.cursor != abuf.len)
620 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
621 errmsg("incorrect binary data format in function argument %d",
625 /* Desired result format is always binary in protocol 2.0 */