Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * fastpath.c
4 : * routines to handle function requests from the frontend
5 : *
6 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/tcop/fastpath.c
12 : *
13 : * NOTES
14 : * This cruft is the server side of PQfn.
15 : *
16 : *-------------------------------------------------------------------------
17 : */
18 : #include "postgres.h"
19 :
20 : #include <netinet/in.h>
21 : #include <arpa/inet.h>
22 :
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"
37 :
38 :
39 : /*
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.
49 : */
50 : struct fp_info
51 : {
52 : Oid funcid;
53 : FmgrInfo flinfo; /* function lookup info for funcid */
54 : Oid namespace; /* other stuff from pg_proc */
55 : Oid rettype;
56 : Oid argtypes[FUNC_MAX_ARGS];
57 : char fname[NAMEDATALEN]; /* function name for logging */
58 : };
59 :
60 :
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);
65 :
66 :
67 : /* ----------------
68 : * GetOldFunctionMessage
69 : *
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.
74 : *
75 : * The caller should already have initialized buf to empty.
76 : * ----------------
77 : */
78 : int
79 0 : GetOldFunctionMessage(StringInfo buf)
80 : {
81 : int32 ibuf;
82 : int nargs;
83 :
84 : /* Dummy string argument */
85 0 : if (pq_getstring(buf))
86 0 : return EOF;
87 : /* Function OID */
88 0 : if (pq_getbytes((char *) &ibuf, 4))
89 0 : return EOF;
90 0 : appendBinaryStringInfo(buf, (char *) &ibuf, 4);
91 : /* Number of arguments */
92 0 : if (pq_getbytes((char *) &ibuf, 4))
93 0 : return EOF;
94 0 : appendBinaryStringInfo(buf, (char *) &ibuf, 4);
95 0 : nargs = ntohl(ibuf);
96 : /* For each argument ... */
97 0 : while (nargs-- > 0)
98 : {
99 : int argsize;
100 :
101 : /* argsize */
102 0 : if (pq_getbytes((char *) &ibuf, 4))
103 0 : return EOF;
104 0 : appendBinaryStringInfo(buf, (char *) &ibuf, 4);
105 0 : argsize = ntohl(ibuf);
106 0 : if (argsize < -1)
107 : {
108 : /* FATAL here since no hope of regaining message sync */
109 0 : ereport(FATAL,
110 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
111 : errmsg("invalid argument size %d in function call message",
112 : argsize)));
113 : }
114 : /* and arg contents */
115 0 : if (argsize > 0)
116 : {
117 : /* Allocate space for arg */
118 0 : enlargeStringInfo(buf, argsize);
119 : /* And grab it */
120 0 : if (pq_getbytes(buf->data + buf->len, argsize))
121 0 : return EOF;
122 0 : buf->len += argsize;
123 : /* Place a trailing null per StringInfo convention */
124 0 : buf->data[buf->len] = '\0';
125 : }
126 : }
127 0 : return 0;
128 : }
129 :
130 : /* ----------------
131 : * SendFunctionResult
132 : *
133 : * Note: although this routine doesn't check, the format had better be 1
134 : * (binary) when talking to a pre-3.0 client.
135 : * ----------------
136 : */
137 : static void
138 258 : SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
139 : {
140 258 : bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
141 : StringInfoData buf;
142 :
143 258 : pq_beginmessage(&buf, 'V');
144 :
145 258 : if (isnull)
146 : {
147 0 : if (newstyle)
148 0 : pq_sendint(&buf, -1, 4);
149 : }
150 : else
151 : {
152 258 : if (!newstyle)
153 0 : pq_sendbyte(&buf, 'G');
154 :
155 258 : if (format == 0)
156 : {
157 : Oid typoutput;
158 : bool typisvarlena;
159 : char *outputstr;
160 :
161 0 : getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
162 0 : outputstr = OidOutputFunctionCall(typoutput, retval);
163 0 : pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
164 0 : pfree(outputstr);
165 : }
166 258 : else if (format == 1)
167 : {
168 : Oid typsend;
169 : bool typisvarlena;
170 : bytea *outputbytes;
171 :
172 258 : getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
173 258 : outputbytes = OidSendFunctionCall(typsend, retval);
174 258 : pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
175 258 : pq_sendbytes(&buf, VARDATA(outputbytes),
176 258 : VARSIZE(outputbytes) - VARHDRSZ);
177 258 : pfree(outputbytes);
178 : }
179 : else
180 0 : ereport(ERROR,
181 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
182 : errmsg("unsupported format code: %d", format)));
183 : }
184 :
185 258 : if (!newstyle)
186 0 : pq_sendbyte(&buf, '0');
187 :
188 258 : pq_endmessage(&buf);
189 258 : }
190 :
191 : /*
192 : * fetch_fp_info
193 : *
194 : * Performs catalog lookups to load a struct fp_info 'fip' for the
195 : * function 'func_id'.
196 : */
197 : static void
198 258 : fetch_fp_info(Oid func_id, struct fp_info *fip)
199 : {
200 : HeapTuple func_htp;
201 : Form_pg_proc pp;
202 :
203 258 : Assert(OidIsValid(func_id));
204 258 : Assert(fip != NULL);
205 :
206 : /*
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.]
213 : */
214 258 : MemSet(fip, 0, sizeof(struct fp_info));
215 258 : fip->funcid = InvalidOid;
216 :
217 258 : fmgr_info(func_id, &fip->flinfo);
218 :
219 258 : func_htp = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
220 258 : if (!HeapTupleIsValid(func_htp))
221 0 : ereport(ERROR,
222 : (errcode(ERRCODE_UNDEFINED_FUNCTION),
223 : errmsg("function with OID %u does not exist", func_id)));
224 258 : pp = (Form_pg_proc) GETSTRUCT(func_htp);
225 :
226 : /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
227 258 : if (pp->pronargs > FUNC_MAX_ARGS)
228 0 : elog(ERROR, "function %s has more than %d arguments",
229 : NameStr(pp->proname), FUNC_MAX_ARGS);
230 :
231 258 : fip->namespace = pp->pronamespace;
232 258 : fip->rettype = pp->prorettype;
233 258 : memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
234 258 : strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
235 :
236 258 : ReleaseSysCache(func_htp);
237 :
238 : /*
239 : * This must be last!
240 : */
241 258 : fip->funcid = func_id;
242 258 : }
243 :
244 :
245 : /*
246 : * HandleFunctionRequest
247 : *
248 : * Server side of PQfn (fastpath function calls from the frontend).
249 : * This corresponds to the libpq protocol symbol "F".
250 : *
251 : * INPUT:
252 : * postgres.c has already read the message body and will pass it in
253 : * msgBuf.
254 : *
255 : * Note: palloc()s done here and in the called function do not need to be
256 : * cleaned up explicitly. We are called from PostgresMain() in the
257 : * MessageContext memory context, which will be automatically reset when
258 : * control returns to PostgresMain.
259 : */
260 : void
261 258 : HandleFunctionRequest(StringInfo msgBuf)
262 : {
263 : Oid fid;
264 : AclResult aclresult;
265 : FunctionCallInfoData fcinfo;
266 : int16 rformat;
267 : Datum retval;
268 : struct fp_info my_fp;
269 : struct fp_info *fip;
270 : bool callit;
271 258 : bool was_logged = false;
272 : char msec_str[32];
273 :
274 : /*
275 : * We only accept COMMIT/ABORT if we are in an aborted transaction, and
276 : * COMMIT/ABORT cannot be executed through the fastpath interface.
277 : */
278 258 : if (IsAbortedTransactionBlockState())
279 0 : ereport(ERROR,
280 : (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
281 : errmsg("current transaction is aborted, "
282 : "commands ignored until end of transaction block")));
283 :
284 : /*
285 : * Now that we know we are in a valid transaction, set snapshot in case
286 : * needed by function itself or one of the datatype I/O routines.
287 : */
288 258 : PushActiveSnapshot(GetTransactionSnapshot());
289 :
290 : /*
291 : * Begin parsing the buffer contents.
292 : */
293 258 : if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
294 0 : (void) pq_getmsgstring(msgBuf); /* dummy string */
295 :
296 258 : fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
297 :
298 : /*
299 : * There used to be a lame attempt at caching lookup info here. Now we
300 : * just do the lookups on every call.
301 : */
302 258 : fip = &my_fp;
303 258 : fetch_fp_info(fid, fip);
304 :
305 : /* Log as soon as we have the function OID and name */
306 258 : if (log_statement == LOGSTMT_ALL)
307 : {
308 0 : ereport(LOG,
309 : (errmsg("fastpath function call: \"%s\" (OID %u)",
310 : fip->fname, fid)));
311 0 : was_logged = true;
312 : }
313 :
314 : /*
315 : * Check permission to access and call function. Since we didn't go
316 : * through a normal name lookup, we need to check schema usage too.
317 : */
318 258 : aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
319 258 : if (aclresult != ACLCHECK_OK)
320 0 : aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
321 0 : get_namespace_name(fip->namespace));
322 258 : InvokeNamespaceSearchHook(fip->namespace, true);
323 :
324 258 : aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
325 258 : if (aclresult != ACLCHECK_OK)
326 0 : aclcheck_error(aclresult, ACL_KIND_PROC,
327 0 : get_func_name(fid));
328 258 : InvokeFunctionExecuteHook(fid);
329 :
330 : /*
331 : * Prepare function call info block and insert arguments.
332 : *
333 : * Note: for now we pass collation = InvalidOid, so collation-sensitive
334 : * functions can't be called this way. Perhaps we should pass
335 : * DEFAULT_COLLATION_OID, instead?
336 : */
337 258 : InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
338 :
339 258 : if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
340 258 : rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
341 : else
342 0 : rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
343 :
344 : /* Verify we reached the end of the message where expected. */
345 258 : pq_getmsgend(msgBuf);
346 :
347 : /*
348 : * If func is strict, must not call it for null args.
349 : */
350 258 : callit = true;
351 258 : if (fip->flinfo.fn_strict)
352 : {
353 : int i;
354 :
355 766 : for (i = 0; i < fcinfo.nargs; i++)
356 : {
357 508 : if (fcinfo.argnull[i])
358 : {
359 0 : callit = false;
360 0 : break;
361 : }
362 : }
363 : }
364 :
365 258 : if (callit)
366 : {
367 : /* Okay, do it ... */
368 258 : retval = FunctionCallInvoke(&fcinfo);
369 : }
370 : else
371 : {
372 0 : fcinfo.isnull = true;
373 0 : retval = (Datum) 0;
374 : }
375 :
376 : /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
377 258 : CHECK_FOR_INTERRUPTS();
378 :
379 258 : SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
380 :
381 : /* We no longer need the snapshot */
382 258 : PopActiveSnapshot();
383 :
384 : /*
385 : * Emit duration logging if appropriate.
386 : */
387 258 : switch (check_log_duration(msec_str, was_logged))
388 : {
389 : case 1:
390 0 : ereport(LOG,
391 : (errmsg("duration: %s ms", msec_str)));
392 0 : break;
393 : case 2:
394 0 : ereport(LOG,
395 : (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
396 : msec_str, fip->fname, fid)));
397 0 : break;
398 : }
399 258 : }
400 :
401 : /*
402 : * Parse function arguments in a 3.0 protocol message
403 : *
404 : * Argument values are loaded into *fcinfo, and the desired result format
405 : * is returned.
406 : */
407 : static int16
408 258 : parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
409 : FunctionCallInfo fcinfo)
410 : {
411 : int nargs;
412 : int i;
413 : int numAFormats;
414 258 : int16 *aformats = NULL;
415 : StringInfoData abuf;
416 :
417 : /* Get the argument format codes */
418 258 : numAFormats = pq_getmsgint(msgBuf, 2);
419 258 : if (numAFormats > 0)
420 : {
421 258 : aformats = (int16 *) palloc(numAFormats * sizeof(int16));
422 516 : for (i = 0; i < numAFormats; i++)
423 258 : aformats[i] = pq_getmsgint(msgBuf, 2);
424 : }
425 :
426 258 : nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
427 :
428 258 : if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
429 0 : ereport(ERROR,
430 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
431 : errmsg("function call message contains %d arguments but function requires %d",
432 : nargs, fip->flinfo.fn_nargs)));
433 :
434 258 : fcinfo->nargs = nargs;
435 :
436 258 : if (numAFormats > 1 && numAFormats != nargs)
437 0 : ereport(ERROR,
438 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
439 : errmsg("function call message contains %d argument formats but %d arguments",
440 : numAFormats, nargs)));
441 :
442 258 : initStringInfo(&abuf);
443 :
444 : /*
445 : * Copy supplied arguments into arg vector.
446 : */
447 766 : for (i = 0; i < nargs; ++i)
448 : {
449 : int argsize;
450 : int16 aformat;
451 :
452 508 : argsize = pq_getmsgint(msgBuf, 4);
453 508 : if (argsize == -1)
454 : {
455 0 : fcinfo->argnull[i] = true;
456 : }
457 : else
458 : {
459 508 : fcinfo->argnull[i] = false;
460 508 : if (argsize < 0)
461 0 : ereport(ERROR,
462 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
463 : errmsg("invalid argument size %d in function call message",
464 : argsize)));
465 :
466 : /* Reset abuf to empty, and insert raw data into it */
467 508 : resetStringInfo(&abuf);
468 508 : appendBinaryStringInfo(&abuf,
469 : pq_getmsgbytes(msgBuf, argsize),
470 : argsize);
471 : }
472 :
473 508 : if (numAFormats > 1)
474 0 : aformat = aformats[i];
475 508 : else if (numAFormats > 0)
476 508 : aformat = aformats[0];
477 : else
478 0 : aformat = 0; /* default = text */
479 :
480 508 : if (aformat == 0)
481 : {
482 : Oid typinput;
483 : Oid typioparam;
484 : char *pstring;
485 :
486 0 : getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
487 :
488 : /*
489 : * Since stringinfo.c keeps a trailing null in place even for
490 : * binary data, the contents of abuf are a valid C string. We
491 : * have to do encoding conversion before calling the typinput
492 : * routine, though.
493 : */
494 0 : if (argsize == -1)
495 0 : pstring = NULL;
496 : else
497 0 : pstring = pg_client_to_server(abuf.data, argsize);
498 :
499 0 : fcinfo->arg[i] = OidInputFunctionCall(typinput, pstring,
500 : typioparam, -1);
501 : /* Free result of encoding conversion, if any */
502 0 : if (pstring && pstring != abuf.data)
503 0 : pfree(pstring);
504 : }
505 508 : else if (aformat == 1)
506 : {
507 : Oid typreceive;
508 : Oid typioparam;
509 : StringInfo bufptr;
510 :
511 : /* Call the argument type's binary input converter */
512 508 : getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
513 :
514 508 : if (argsize == -1)
515 0 : bufptr = NULL;
516 : else
517 508 : bufptr = &abuf;
518 :
519 508 : fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, bufptr,
520 : typioparam, -1);
521 :
522 : /* Trouble if it didn't eat the whole buffer */
523 508 : if (argsize != -1 && abuf.cursor != abuf.len)
524 0 : ereport(ERROR,
525 : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
526 : errmsg("incorrect binary data format in function argument %d",
527 : i + 1)));
528 : }
529 : else
530 0 : ereport(ERROR,
531 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
532 : errmsg("unsupported format code: %d", aformat)));
533 : }
534 :
535 : /* Return result format code */
536 258 : return (int16) pq_getmsgint(msgBuf, 2);
537 : }
538 :
539 : /*
540 : * Parse function arguments in a 2.0 protocol message
541 : *
542 : * Argument values are loaded into *fcinfo, and the desired result format
543 : * is returned.
544 : */
545 : static int16
546 0 : parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
547 : FunctionCallInfo fcinfo)
548 : {
549 : int nargs;
550 : int i;
551 : StringInfoData abuf;
552 :
553 0 : nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
554 :
555 0 : if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
556 0 : ereport(ERROR,
557 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
558 : errmsg("function call message contains %d arguments but function requires %d",
559 : nargs, fip->flinfo.fn_nargs)));
560 :
561 0 : fcinfo->nargs = nargs;
562 :
563 0 : initStringInfo(&abuf);
564 :
565 : /*
566 : * Copy supplied arguments into arg vector. In protocol 2.0 these are
567 : * always assumed to be supplied in binary format.
568 : *
569 : * Note: although the original protocol 2.0 code did not have any way for
570 : * the frontend to specify a NULL argument, we now choose to interpret
571 : * length == -1 as meaning a NULL.
572 : */
573 0 : for (i = 0; i < nargs; ++i)
574 : {
575 : int argsize;
576 : Oid typreceive;
577 : Oid typioparam;
578 :
579 0 : getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
580 :
581 0 : argsize = pq_getmsgint(msgBuf, 4);
582 0 : if (argsize == -1)
583 : {
584 0 : fcinfo->argnull[i] = true;
585 0 : fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, NULL,
586 : typioparam, -1);
587 0 : continue;
588 : }
589 0 : fcinfo->argnull[i] = false;
590 0 : if (argsize < 0)
591 0 : ereport(ERROR,
592 : (errcode(ERRCODE_PROTOCOL_VIOLATION),
593 : errmsg("invalid argument size %d in function call message",
594 : argsize)));
595 :
596 : /* Reset abuf to empty, and insert raw data into it */
597 0 : resetStringInfo(&abuf);
598 0 : appendBinaryStringInfo(&abuf,
599 : pq_getmsgbytes(msgBuf, argsize),
600 : argsize);
601 :
602 0 : fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, &abuf,
603 : typioparam, -1);
604 :
605 : /* Trouble if it didn't eat the whole buffer */
606 0 : if (abuf.cursor != abuf.len)
607 0 : ereport(ERROR,
608 : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
609 : errmsg("incorrect binary data format in function argument %d",
610 : i + 1)));
611 : }
612 :
613 : /* Desired result format is always binary in protocol 2.0 */
614 0 : return 1;
615 : }
|