1 /*-------------------------------------------------------------------------
4 * routines to handle function requests from the frontend
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/tcop/fastpath.c,v 1.12 1998/01/26 01:41:28 scrappy Exp $
13 * This cruft is the server side of PQfn.
17 * no longer rely on return sizes provided by the frontend. Always
18 * use the true lengths for the catalogs. Assume that the frontend
19 * has allocated enough space to handle the result value returned.
21 * trust that the user knows what he is doing with the args. If the
22 * sys catalog says it is a varlena, assume that the user is only sending
23 * down VARDATA and that the argsize is the VARSIZE. If the arg is
24 * fixed len, assume that the argsize given by the user is correct.
26 * if the function returns by value, then only send 4 bytes value
27 * back to the frontend. If the return returns by reference,
28 * send down only the data portion and set the return size appropriately.
32 * The VAR_LENGTH_{ARGS,RESULT} stuff is limited to MAX_STRING_LENGTH
33 * (see src/backend/tmp/fastpath.h) for no obvious reason. Since its
34 * primary use (for us) is for Inversion path names, it should probably
35 * be increased to 256 (MAXPATHLEN for Inversion, hidden in pg_type
36 * as well as utils/adt/filename.c).
38 * Quoth PMA on 08/15/93:
40 * This code has been almost completely rewritten with an eye to
41 * keeping it as compatible as possible with the previous (broken)
44 * The previous implementation would assume (1) that any value of
45 * length <= 4 bytes was passed-by-value, and that any other value
46 * was a struct varlena (by-reference). There was NO way to pass a
47 * fixed-length by-reference argument (like char16) or a struct
48 * varlena of size <= 4 bytes.
50 * The new implementation checks the catalogs to determine whether
51 * a value is by-value (type "0" is null-delimited character string,
52 * as it is for, e.g., the parser). The only other item obtained
53 * from the catalogs is whether or not the value should be placed in
54 * a struct varlena or not. Otherwise, the size given by the
55 * frontend is assumed to be correct (probably a bad decision, but
56 * we do strange things in the name of compatibility).
58 *-------------------------------------------------------------------------
64 #include "tcop/tcopdebug.h"
66 #include "utils/palloc.h"
68 #include "utils/builtins.h" /* for oideq */
69 #include "tcop/fastpath.h"
70 #include "libpq/libpq.h"
72 #include "access/xact.h" /* for TransactionId/CommandId protos */
74 #include "utils/syscache.h"
75 #include "catalog/pg_proc.h"
76 #include "catalog/pg_type.h"
84 SendFunctionResult(Oid fid, /* function id */
85 char *retval,/* actual return value */
87 int retlen /* the length according to the catalogs */
98 pq_putint((int) (Datum) retval, retlen);
101 { /* by-reference ... */
104 pq_putint(VARSIZE(retval) - VARHDRSZ, VARHDRSZ);
105 pq_putnchar(VARDATA(retval), VARSIZE(retval) - VARHDRSZ);
109 pq_putint(retlen, 4);
110 pq_putnchar(retval, retlen);
120 * This structure saves enough state so that one can avoid having to
121 * do catalog lookups over and over again. (Each RPC can require up
122 * to MAXFMGRARGS+2 lookups, which is quite tedious.)
124 * The previous incarnation of this code just assumed that any argument
125 * of size <= 4 was by value; this is not correct. There is no cheap
126 * way to determine function argument length etc.; one must simply pay
127 * the price of catalog lookups.
133 bool argbyval[MAXFMGRARGS];
134 int32 arglen[MAXFMGRARGS]; /* signed (for varlena) */
136 int32 retlen; /* signed (for varlena) */
142 * We implement one-back caching here. If we need to do more, we can.
143 * Most routines in tight loops (like PQfswrite -> F_LOWRITE) will do
144 * the same thing repeatedly.
146 static struct fp_info last_fp = {InvalidOid};
152 * 1 if the state in 'fip' is valid
156 * The saved state was either uninitialized, for another function,
157 * or from a previous command. (Commands can do updates, which
158 * may invalidate catalog entries for subsequent commands. This
159 * is overly pessimistic but since there is no smarter invalidation
163 valid_fp_info(Oid func_id, struct fp_info * fip)
165 Assert(OidIsValid(func_id));
166 Assert(fip != (struct fp_info *) NULL);
168 return (OidIsValid(fip->funcid) &&
169 oideq(func_id, fip->funcid) &&
170 TransactionIdIsCurrentTransactionId(fip->xid) &&
171 CommandIdIsCurrentCommandId(fip->cid));
177 * Performs catalog lookups to load a struct fp_info 'fip' for the
178 * function 'func_id'.
181 * The correct information in 'fip'. Sets 'fip->funcid' to
182 * InvalidOid if an exception occurs.
185 update_fp_info(Oid func_id, struct fp_info * fip)
187 Oid *argtypes; /* an oid8 */
195 Assert(OidIsValid(func_id));
196 Assert(fip != (struct fp_info *) NULL);
199 * Since the validity of this structure is determined by whether the
200 * funcid is OK, we clear the funcid here. It must not be set to the
201 * correct value until we are about to return with a good struct
202 * fp_info, since we can be interrupted (i.e., with an elog(ERROR,
205 MemSet((char *) fip, 0, (int) sizeof(struct fp_info));
206 fip->funcid = InvalidOid;
208 func_htp = SearchSysCacheTuple(PROOID, ObjectIdGetDatum(func_id),
210 if (!HeapTupleIsValid(func_htp))
212 elog(ERROR, "update_fp_info: cache lookup for function %d failed",
215 pp = (Form_pg_proc) GETSTRUCT(func_htp);
216 fip->nargs = pp->pronargs;
217 rettype = pp->prorettype;
218 argtypes = pp->proargtypes;
220 for (i = 0; i < fip->nargs; ++i)
222 if (OidIsValid(argtypes[i]))
224 type_htp = SearchSysCacheTuple(TYPOID,
225 ObjectIdGetDatum(argtypes[i]),
227 if (!HeapTupleIsValid(type_htp))
229 elog(ERROR, "update_fp_info: bad argument type %d for %d",
230 argtypes[i], func_id);
232 tp = (TypeTupleForm) GETSTRUCT(type_htp);
233 fip->argbyval[i] = tp->typbyval;
234 fip->arglen[i] = tp->typlen;
235 } /* else it had better be VAR_LENGTH_ARG */
238 if (OidIsValid(rettype))
240 type_htp = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(rettype),
242 if (!HeapTupleIsValid(type_htp))
244 elog(ERROR, "update_fp_info: bad return type %d for %d",
247 tp = (TypeTupleForm) GETSTRUCT(type_htp);
248 fip->retbyval = tp->typbyval;
249 fip->retlen = tp->typlen;
250 } /* else it had better by VAR_LENGTH_RESULT */
252 fip->xid = GetCurrentTransactionId();
253 fip->cid = GetCurrentCommandId();
258 fip->funcid = func_id;
263 * HandleFunctionRequest
265 * Server side of PQfn (fastpath function calls from the frontend).
266 * This corresponds to the libpq protocol symbol "F".
269 * nothing of significance.
270 * All errors result in elog(ERROR,...).
273 HandleFunctionRequest()
285 fid = (Oid) pq_getint(4); /* function oid */
286 nargs = pq_getint(4); /* # of arguments */
289 * This is where the one-back caching is done. If you want to save
290 * more state, make this a loop around an array.
293 if (!valid_fp_info(fid, fip))
295 update_fp_info(fid, fip);
298 if (fip->nargs != nargs)
300 elog(ERROR, "HandleFunctionRequest: actual arguments (%d) != registered arguments (%d)",
305 * Copy arguments into arg vector. If we palloc() an argument, we
306 * need to remember, so that we pfree() it after the call.
309 for (i = 0; i < 8; ++i)
313 arg[i] = (char *) NULL;
317 argsize = pq_getint(4);
320 if (fip->argbyval[i])
322 Assert(argsize <= 4);
323 arg[i] = (char *) pq_getint(argsize);
326 { /* by-reference ... */
327 if (fip->arglen[i] < 0)
329 if (!(p = palloc(argsize + VARHDRSZ + 1))) /* Added +1 to solve memory leak - Peter 98 Jan 6 */
331 elog(ERROR, "HandleFunctionRequest: palloc failed");
333 VARSIZE(p) = argsize + VARHDRSZ;
334 pq_getnchar(VARDATA(p), 0, argsize);
338 /* XXX cross our fingers and trust "argsize" */
339 if (!(p = palloc(argsize + 1)))
341 elog(ERROR, "HandleFunctionRequest: palloc failed");
343 pq_getnchar(p, 0, argsize);
345 palloced |= (1 << i);
353 arg[0], arg[1], arg[2], arg[3],
354 arg[4], arg[5], arg[6], arg[7]);
358 #endif /* NO_FASTPATH */
360 /* free palloc'ed arguments */
361 for (i = 0; i < nargs; ++i)
363 if (palloced & (1 << i))
368 * If this is an ordinary query (not a retrieve portal p ...), then we
369 * return the data to the user. If the return value was palloc'ed,
370 * then it must also be freed.
373 SendFunctionResult(fid, retval, fip->retbyval, fip->retlen);
375 SendFunctionResult(fid, retval, fip->retbyval, 0);
376 #endif /* NO_FASTPATH */