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