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