OSDN Git Service

Standard pgindent run for 8.1.
[pg-rex/syncrep.git] / 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-2005, 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.83 2005/10/15 02:49:26 momjian 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 "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"
33
34
35 /*
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.
45  */
46 struct fp_info
47 {
48         Oid                     funcid;
49         FmgrInfo        flinfo;                 /* function lookup info for funcid */
50         Oid                     namespace;              /* other stuff from pg_proc */
51         Oid                     rettype;
52         Oid                     argtypes[FUNC_MAX_ARGS];
53 };
54
55
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);
60
61
62 /* ----------------
63  *              GetOldFunctionMessage
64  *
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.
69  *
70  * The caller should already have initialized buf to empty.
71  * ----------------
72  */
73 static int
74 GetOldFunctionMessage(StringInfo buf)
75 {
76         int32           ibuf;
77         int                     nargs;
78
79         /* Dummy string argument */
80         if (pq_getstring(buf))
81                 return EOF;
82         /* Function OID */
83         if (pq_getbytes((char *) &ibuf, 4))
84                 return EOF;
85         appendBinaryStringInfo(buf, (char *) &ibuf, 4);
86         /* Number of arguments */
87         if (pq_getbytes((char *) &ibuf, 4))
88                 return EOF;
89         appendBinaryStringInfo(buf, (char *) &ibuf, 4);
90         nargs = ntohl(ibuf);
91         /* For each argument ... */
92         while (nargs-- > 0)
93         {
94                 int                     argsize;
95
96                 /* argsize */
97                 if (pq_getbytes((char *) &ibuf, 4))
98                         return EOF;
99                 appendBinaryStringInfo(buf, (char *) &ibuf, 4);
100                 argsize = ntohl(ibuf);
101                 if (argsize < -1)
102                 {
103                         /* FATAL here since no hope of regaining message sync */
104                         ereport(FATAL,
105                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
106                                   errmsg("invalid argument size %d in function call message",
107                                                  argsize)));
108                 }
109                 /* and arg contents */
110                 if (argsize > 0)
111                 {
112                         /* Allocate space for arg */
113                         enlargeStringInfo(buf, argsize);
114                         /* And grab it */
115                         if (pq_getbytes(buf->data + buf->len, argsize))
116                                 return EOF;
117                         buf->len += argsize;
118                         /* Place a trailing null per StringInfo convention */
119                         buf->data[buf->len] = '\0';
120                 }
121         }
122         return 0;
123 }
124
125 /* ----------------
126  *              SendFunctionResult
127  *
128  * Note: although this routine doesn't check, the format had better be 1
129  * (binary) when talking to a pre-3.0 client.
130  * ----------------
131  */
132 static void
133 SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
134 {
135         bool            newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
136         StringInfoData buf;
137
138         pq_beginmessage(&buf, 'V');
139
140         if (isnull)
141         {
142                 if (newstyle)
143                         pq_sendint(&buf, -1, 4);
144         }
145         else
146         {
147                 if (!newstyle)
148                         pq_sendbyte(&buf, 'G');
149
150                 if (format == 0)
151                 {
152                         Oid                     typoutput;
153                         bool            typisvarlena;
154                         char       *outputstr;
155
156                         getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
157                         outputstr = DatumGetCString(OidFunctionCall1(typoutput,
158                                                                                                                  retval));
159                         pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
160                         pfree(outputstr);
161                 }
162                 else if (format == 1)
163                 {
164                         Oid                     typsend;
165                         bool            typisvarlena;
166                         bytea      *outputbytes;
167
168                         getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
169                         outputbytes = DatumGetByteaP(OidFunctionCall1(typsend,
170                                                                                                                   retval));
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);
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
196 fetch_fp_info(Oid func_id, struct fp_info * fip)
197 {
198         HeapTuple       func_htp;
199         Form_pg_proc pp;
200
201         Assert(OidIsValid(func_id));
202         Assert(fip != NULL);
203
204         /*
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.]
211          */
212         MemSet(fip, 0, sizeof(struct fp_info));
213         fip->funcid = InvalidOid;
214
215         fmgr_info(func_id, &fip->flinfo);
216
217         func_htp = SearchSysCache(PROCOID,
218                                                           ObjectIdGetDatum(func_id),
219                                                           0, 0, 0);
220         if (!HeapTupleIsValid(func_htp))
221                 ereport(ERROR,
222                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
223                                  errmsg("function with OID %u does not exist", func_id)));
224         pp = (Form_pg_proc) GETSTRUCT(func_htp);
225
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);
230
231         fip->namespace = pp->pronamespace;
232         fip->rettype = pp->prorettype;
233         memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
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
280         /*
281          * Read message contents if not already done.
282          */
283         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
284         {
285                 if (GetOldFunctionMessage(msgBuf))
286                 {
287                         ereport(COMMERROR,
288                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
289                                          errmsg("unexpected EOF on client connection")));
290                         return EOF;
291                 }
292         }
293
294         /*
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.
298          */
299         if (IsAbortedTransactionBlockState())
300                 ereport(ERROR,
301                                 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
302                                  errmsg("current transaction is aborted, "
303                                                 "commands ignored until end of transaction block")));
304
305         /*
306          * Begin parsing the buffer contents.
307          */
308         if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
309                 (void) pq_getmsgstring(msgBuf); /* dummy string */
310
311         fid = (Oid) pq_getmsgint(msgBuf, 4);            /* function oid */
312
313         /*
314          * There used to be a lame attempt at caching lookup info here. Now we
315          * just do the lookups on every call.
316          */
317         fip = &my_fp;
318         fetch_fp_info(fid, fip);
319
320         /*
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.
323          */
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));
328
329         aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
330         if (aclresult != ACLCHECK_OK)
331                 aclcheck_error(aclresult, ACL_KIND_PROC,
332                                            get_func_name(fid));
333
334         /*
335          * Prepare function call info block and insert arguments.
336          */
337         InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, NULL, NULL);
338
339         if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
340                 rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
341         else
342                 rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
343
344         /* Verify we reached the end of the message where expected. */
345         pq_getmsgend(msgBuf);
346
347         /*
348          * If func is strict, must not call it for null args.
349          */
350         callit = true;
351         if (fip->flinfo.fn_strict)
352         {
353                 int                     i;
354
355                 for (i = 0; i < fcinfo.nargs; i++)
356                 {
357                         if (fcinfo.argnull[i])
358                         {
359                                 callit = false;
360                                 break;
361                         }
362                 }
363         }
364
365         if (callit)
366         {
367                 /* Okay, do it ... */
368                 retval = FunctionCallInvoke(&fcinfo);
369         }
370         else
371         {
372                 fcinfo.isnull = true;
373                 retval = (Datum) 0;
374         }
375
376         SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
377
378         return 0;
379 }
380
381 /*
382  * Parse function arguments in a 3.0 protocol message
383  *
384  * Argument values are loaded into *fcinfo, and the desired result format
385  * is returned.
386  */
387 static int16
388 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
389                                           FunctionCallInfo fcinfo)
390 {
391         int                     nargs;
392         int                     i;
393         int                     numAFormats;
394         int16      *aformats = NULL;
395         StringInfoData abuf;
396
397         /* Get the argument format codes */
398         numAFormats = pq_getmsgint(msgBuf, 2);
399         if (numAFormats > 0)
400         {
401                 aformats = (int16 *) palloc(numAFormats * sizeof(int16));
402                 for (i = 0; i < numAFormats; i++)
403                         aformats[i] = pq_getmsgint(msgBuf, 2);
404         }
405
406         nargs = pq_getmsgint(msgBuf, 2);        /* # of arguments */
407
408         if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
409                 ereport(ERROR,
410                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
411                                  errmsg("function call message contains %d arguments but function requires %d",
412                                                 nargs, fip->flinfo.fn_nargs)));
413
414         fcinfo->nargs = nargs;
415
416         if (numAFormats > 1 && numAFormats != nargs)
417                 ereport(ERROR,
418                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
419                                  errmsg("function call message contains %d argument formats but %d arguments",
420                                                 numAFormats, nargs)));
421
422         initStringInfo(&abuf);
423
424         /*
425          * Copy supplied arguments into arg vector.
426          */
427         for (i = 0; i < nargs; ++i)
428         {
429                 int                     argsize;
430                 int16           aformat;
431
432                 argsize = pq_getmsgint(msgBuf, 4);
433                 if (argsize == -1)
434                 {
435                         fcinfo->argnull[i] = true;
436                         continue;
437                 }
438                 fcinfo->argnull[i] = false;
439                 if (argsize < 0)
440                         ereport(ERROR,
441                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
442                                   errmsg("invalid argument size %d in function call message",
443                                                  argsize)));
444
445                 /* Reset abuf to empty, and insert raw data into it */
446                 abuf.len = 0;
447                 abuf.data[0] = '\0';
448                 abuf.cursor = 0;
449
450                 appendBinaryStringInfo(&abuf,
451                                                            pq_getmsgbytes(msgBuf, argsize),
452                                                            argsize);
453
454                 if (numAFormats > 1)
455                         aformat = aformats[i];
456                 else if (numAFormats > 0)
457                         aformat = aformats[0];
458                 else
459                         aformat = 0;            /* default = text */
460
461                 if (aformat == 0)
462                 {
463                         Oid                     typinput;
464                         Oid                     typioparam;
465                         char       *pstring;
466
467                         getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
468
469                         /*
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
473                          * routine, though.
474                          */
475                         pstring = pg_client_to_server(abuf.data, argsize);
476                         fcinfo->arg[i] =
477                                 OidFunctionCall3(typinput,
478                                                                  CStringGetDatum(pstring),
479                                                                  ObjectIdGetDatum(typioparam),
480                                                                  Int32GetDatum(-1));
481                         /* Free result of encoding conversion, if any */
482                         if (pstring != abuf.data)
483                                 pfree(pstring);
484                 }
485                 else if (aformat == 1)
486                 {
487                         Oid                     typreceive;
488                         Oid                     typioparam;
489
490                         /* Call the argument type's binary input converter */
491                         getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
492
493                         fcinfo->arg[i] = OidFunctionCall3(typreceive,
494                                                                                           PointerGetDatum(&abuf),
495                                                                                           ObjectIdGetDatum(typioparam),
496                                                                                           Int32GetDatum(-1));
497
498                         /* Trouble if it didn't eat the whole buffer */
499                         if (abuf.cursor != abuf.len)
500                                 ereport(ERROR,
501                                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
502                                 errmsg("incorrect binary data format in function argument %d",
503                                            i + 1)));
504                 }
505                 else
506                         ereport(ERROR,
507                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
508                                          errmsg("unsupported format code: %d", aformat)));
509         }
510
511         /* Return result format code */
512         return (int16) pq_getmsgint(msgBuf, 2);
513 }
514
515 /*
516  * Parse function arguments in a 2.0 protocol message
517  *
518  * Argument values are loaded into *fcinfo, and the desired result format
519  * is returned.
520  */
521 static int16
522 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
523                                                  FunctionCallInfo fcinfo)
524 {
525         int                     nargs;
526         int                     i;
527         StringInfoData abuf;
528
529         nargs = pq_getmsgint(msgBuf, 4);        /* # of arguments */
530
531         if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
532                 ereport(ERROR,
533                                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
534                                  errmsg("function call message contains %d arguments but function requires %d",
535                                                 nargs, fip->flinfo.fn_nargs)));
536
537         fcinfo->nargs = nargs;
538
539         initStringInfo(&abuf);
540
541         /*
542          * Copy supplied arguments into arg vector.  In protocol 2.0 these are
543          * always assumed to be supplied in binary format.
544          *
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.
548          */
549         for (i = 0; i < nargs; ++i)
550         {
551                 int                     argsize;
552                 Oid                     typreceive;
553                 Oid                     typioparam;
554
555                 argsize = pq_getmsgint(msgBuf, 4);
556                 if (argsize == -1)
557                 {
558                         fcinfo->argnull[i] = true;
559                         continue;
560                 }
561                 fcinfo->argnull[i] = false;
562                 if (argsize < 0)
563                         ereport(ERROR,
564                                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
565                                   errmsg("invalid argument size %d in function call message",
566                                                  argsize)));
567
568                 /* Reset abuf to empty, and insert raw data into it */
569                 abuf.len = 0;
570                 abuf.data[0] = '\0';
571                 abuf.cursor = 0;
572
573                 appendBinaryStringInfo(&abuf,
574                                                            pq_getmsgbytes(msgBuf, argsize),
575                                                            argsize);
576
577                 /* Call the argument type's binary input converter */
578                 getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
579
580                 fcinfo->arg[i] = OidFunctionCall3(typreceive,
581                                                                                   PointerGetDatum(&abuf),
582                                                                                   ObjectIdGetDatum(typioparam),
583                                                                                   Int32GetDatum(-1));
584
585                 /* Trouble if it didn't eat the whole buffer */
586                 if (abuf.cursor != abuf.len)
587                         ereport(ERROR,
588                                         (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
589                            errmsg("incorrect binary data format in function argument %d",
590                                           i + 1)));
591         }
592
593         /* Desired result format is always binary in protocol 2.0 */
594         return 1;
595 }