PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fastpath.c File Reference
#include "postgres.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_proc.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "tcop/fastpath.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for fastpath.c:

Go to the source code of this file.

Data Structures

struct  fp_info
 

Functions

static int16 parse_fcall_arguments (StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
 
static int16 parse_fcall_arguments_20 (StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
 
int GetOldFunctionMessage (StringInfo buf)
 
static void SendFunctionResult (Datum retval, bool isnull, Oid rettype, int16 format)
 
static void fetch_fp_info (Oid func_id, struct fp_info *fip)
 
int HandleFunctionRequest (StringInfo msgBuf)
 

Function Documentation

static void fetch_fp_info ( Oid  func_id,
struct fp_info fip 
)
static

Definition at line 198 of file fastpath.c.

References fp_info::argtypes, Assert, elog, ereport, errcode(), errmsg(), ERROR, fp_info::flinfo, fmgr_info(), fp_info::fname, FUNC_MAX_ARGS, fp_info::funcid, GETSTRUCT, HeapTupleIsValid, InvalidOid, MemSet, NAMEDATALEN, NameStr, NULL, ObjectIdGetDatum, OidIsValid, PROCOID, ReleaseSysCache(), fp_info::rettype, SearchSysCache1, and strlcpy().

Referenced by HandleFunctionRequest().

199 {
200  HeapTuple func_htp;
201  Form_pg_proc pp;
202 
203  Assert(OidIsValid(func_id));
204  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  MemSet(fip, 0, sizeof(struct fp_info));
215  fip->funcid = InvalidOid;
216 
217  fmgr_info(func_id, &fip->flinfo);
218 
219  func_htp = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
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  strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
235 
236  ReleaseSysCache(func_htp);
237 
238  /*
239  * This must be last!
240  */
241  fip->funcid = func_id;
242 }
FmgrInfo flinfo
Definition: fastpath.c:53
char fname[NAMEDATALEN]
Definition: fastpath.c:57
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid rettype
Definition: fastpath.c:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#define FUNC_MAX_ARGS
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define ereport(elevel, rest)
Definition: elog.h:122
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:56
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
Oid funcid
Definition: fastpath.c:52
#define elog
Definition: elog.h:219
int GetOldFunctionMessage ( StringInfo  buf)

Definition at line 79 of file fastpath.c.

References appendBinaryStringInfo(), StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), FATAL, StringInfoData::len, pq_getbytes(), and pq_getstring().

Referenced by SocketBackend().

80 {
81  int32 ibuf;
82  int nargs;
83 
84  /* Dummy string argument */
85  if (pq_getstring(buf))
86  return EOF;
87  /* Function OID */
88  if (pq_getbytes((char *) &ibuf, 4))
89  return EOF;
90  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
91  /* Number of arguments */
92  if (pq_getbytes((char *) &ibuf, 4))
93  return EOF;
94  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
95  nargs = ntohl(ibuf);
96  /* For each argument ... */
97  while (nargs-- > 0)
98  {
99  int argsize;
100 
101  /* argsize */
102  if (pq_getbytes((char *) &ibuf, 4))
103  return EOF;
104  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
105  argsize = ntohl(ibuf);
106  if (argsize < -1)
107  {
108  /* FATAL here since no hope of regaining message sync */
109  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  if (argsize > 0)
116  {
117  /* Allocate space for arg */
118  enlargeStringInfo(buf, argsize);
119  /* And grab it */
120  if (pq_getbytes(buf->data + buf->len, argsize))
121  return EOF;
122  buf->len += argsize;
123  /* Place a trailing null per StringInfo convention */
124  buf->data[buf->len] = '\0';
125  }
126  }
127  return 0;
128 }
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define FATAL
Definition: elog.h:52
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1075
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:277
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
int pq_getstring(StringInfo s)
Definition: pqcomm.c:1147
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
int HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 270 of file fastpath.c.

References ACL_EXECUTE, ACL_KIND_NAMESPACE, ACL_KIND_PROC, ACL_USAGE, aclcheck_error(), ACLCHECK_OK, FunctionCallInfoData::argnull, CHECK_FOR_INTERRUPTS, check_log_duration(), ereport, errcode(), errmsg(), ERROR, fetch_fp_info(), fp_info::flinfo, FmgrInfo::fn_strict, fp_info::fname, FrontendProtocol, FunctionCallInvoke, get_func_name(), get_namespace_name(), GetTransactionSnapshot(), GetUserId(), i, InitFunctionCallInfoData, InvalidOid, InvokeFunctionExecuteHook, InvokeNamespaceSearchHook, IsAbortedTransactionBlockState(), FunctionCallInfoData::isnull, LOG, log_statement, LOGSTMT_ALL, FunctionCallInfoData::nargs, NULL, parse_fcall_arguments(), parse_fcall_arguments_20(), pg_namespace_aclcheck(), pg_proc_aclcheck(), PG_PROTOCOL_MAJOR, PopActiveSnapshot(), pq_getmsgend(), pq_getmsgint(), pq_getmsgstring(), PushActiveSnapshot(), fp_info::rettype, and SendFunctionResult().

Referenced by PostgresMain().

271 {
272  Oid fid;
273  AclResult aclresult;
274  FunctionCallInfoData fcinfo;
275  int16 rformat;
276  Datum retval;
277  struct fp_info my_fp;
278  struct fp_info *fip;
279  bool callit;
280  bool was_logged = false;
281  char msec_str[32];
282 
283  /*
284  * Now that we've eaten the input message, check to see if we actually
285  * want to do the function call or not. It's now safe to ereport(); we
286  * won't lose sync with the frontend.
287  */
289  ereport(ERROR,
290  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
291  errmsg("current transaction is aborted, "
292  "commands ignored until end of transaction block")));
293 
294  /*
295  * Now that we know we are in a valid transaction, set snapshot in case
296  * needed by function itself or one of the datatype I/O routines.
297  */
299 
300  /*
301  * Begin parsing the buffer contents.
302  */
304  (void) pq_getmsgstring(msgBuf); /* dummy string */
305 
306  fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
307 
308  /*
309  * There used to be a lame attempt at caching lookup info here. Now we
310  * just do the lookups on every call.
311  */
312  fip = &my_fp;
313  fetch_fp_info(fid, fip);
314 
315  /* Log as soon as we have the function OID and name */
316  if (log_statement == LOGSTMT_ALL)
317  {
318  ereport(LOG,
319  (errmsg("fastpath function call: \"%s\" (OID %u)",
320  fip->fname, fid)));
321  was_logged = true;
322  }
323 
324  /*
325  * Check permission to access and call function. Since we didn't go
326  * through a normal name lookup, we need to check schema usage too.
327  */
328  aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
329  if (aclresult != ACLCHECK_OK)
331  get_namespace_name(fip->namespace));
332  InvokeNamespaceSearchHook(fip->namespace, true);
333 
334  aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
335  if (aclresult != ACLCHECK_OK)
336  aclcheck_error(aclresult, ACL_KIND_PROC,
337  get_func_name(fid));
339 
340  /*
341  * Prepare function call info block and insert arguments.
342  *
343  * Note: for now we pass collation = InvalidOid, so collation-sensitive
344  * functions can't be called this way. Perhaps we should pass
345  * DEFAULT_COLLATION_OID, instead?
346  */
347  InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
348 
350  rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
351  else
352  rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
353 
354  /* Verify we reached the end of the message where expected. */
355  pq_getmsgend(msgBuf);
356 
357  /*
358  * If func is strict, must not call it for null args.
359  */
360  callit = true;
361  if (fip->flinfo.fn_strict)
362  {
363  int i;
364 
365  for (i = 0; i < fcinfo.nargs; i++)
366  {
367  if (fcinfo.argnull[i])
368  {
369  callit = false;
370  break;
371  }
372  }
373  }
374 
375  if (callit)
376  {
377  /* Okay, do it ... */
378  retval = FunctionCallInvoke(&fcinfo);
379  }
380  else
381  {
382  fcinfo.isnull = true;
383  retval = (Datum) 0;
384  }
385 
386  /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
388 
389  SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
390 
391  /* We no longer need the snapshot */
393 
394  /*
395  * Emit duration logging if appropriate.
396  */
397  switch (check_log_duration(msec_str, was_logged))
398  {
399  case 1:
400  ereport(LOG,
401  (errmsg("duration: %s ms", msec_str)));
402  break;
403  case 2:
404  ereport(LOG,
405  (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
406  msec_str, fip->fname, fid)));
407  break;
408  }
409 
410  return 0;
411 }
signed short int16
Definition: c.h:255
FmgrInfo flinfo
Definition: fastpath.c:53
char fname[NAMEDATALEN]
Definition: fastpath.c:57
Oid rettype
Definition: fastpath.c:54
Oid GetUserId(void)
Definition: miscinit.c:283
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:621
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:370
int errcode(int sqlerrcode)
Definition: elog.c:575
static void SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
Definition: fastpath.c:138
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4467
static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:558
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:174
int check_log_duration(char *msec_str, bool was_logged)
Definition: postgres.c:2102
#define ERROR
Definition: elog.h:43
bool fn_strict
Definition: fmgr.h:61
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1380
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:137
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
#define ACL_USAGE
Definition: parsenodes.h:73
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:179
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:420
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
int log_statement
Definition: postgres.c:91
static void fetch_fp_info(Oid func_id, struct fp_info *fip)
Definition: fastpath.c:198
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:72
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4429
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:677
ProtocolVersion FrontendProtocol
Definition: globals.c:27
static int16 parse_fcall_arguments ( StringInfo  msgBuf,
struct fp_info fip,
FunctionCallInfo  fcinfo 
)
static

Definition at line 420 of file fastpath.c.

References appendBinaryStringInfo(), FunctionCallInfoData::arg, FunctionCallInfoData::argnull, fp_info::argtypes, StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, fp_info::flinfo, FmgrInfo::fn_nargs, FUNC_MAX_ARGS, getTypeBinaryInputInfo(), getTypeInputInfo(), i, initStringInfo(), StringInfoData::len, FunctionCallInfoData::nargs, NULL, OidInputFunctionCall(), OidReceiveFunctionCall(), palloc(), pfree(), pg_client_to_server(), pq_getmsgbytes(), pq_getmsgint(), and resetStringInfo().

Referenced by HandleFunctionRequest().

422 {
423  int nargs;
424  int i;
425  int numAFormats;
426  int16 *aformats = NULL;
427  StringInfoData abuf;
428 
429  /* Get the argument format codes */
430  numAFormats = pq_getmsgint(msgBuf, 2);
431  if (numAFormats > 0)
432  {
433  aformats = (int16 *) palloc(numAFormats * sizeof(int16));
434  for (i = 0; i < numAFormats; i++)
435  aformats[i] = pq_getmsgint(msgBuf, 2);
436  }
437 
438  nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
439 
440  if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
441  ereport(ERROR,
442  (errcode(ERRCODE_PROTOCOL_VIOLATION),
443  errmsg("function call message contains %d arguments but function requires %d",
444  nargs, fip->flinfo.fn_nargs)));
445 
446  fcinfo->nargs = nargs;
447 
448  if (numAFormats > 1 && numAFormats != nargs)
449  ereport(ERROR,
450  (errcode(ERRCODE_PROTOCOL_VIOLATION),
451  errmsg("function call message contains %d argument formats but %d arguments",
452  numAFormats, nargs)));
453 
454  initStringInfo(&abuf);
455 
456  /*
457  * Copy supplied arguments into arg vector.
458  */
459  for (i = 0; i < nargs; ++i)
460  {
461  int argsize;
462  int16 aformat;
463 
464  argsize = pq_getmsgint(msgBuf, 4);
465  if (argsize == -1)
466  {
467  fcinfo->argnull[i] = true;
468  }
469  else
470  {
471  fcinfo->argnull[i] = false;
472  if (argsize < 0)
473  ereport(ERROR,
474  (errcode(ERRCODE_PROTOCOL_VIOLATION),
475  errmsg("invalid argument size %d in function call message",
476  argsize)));
477 
478  /* Reset abuf to empty, and insert raw data into it */
479  resetStringInfo(&abuf);
481  pq_getmsgbytes(msgBuf, argsize),
482  argsize);
483  }
484 
485  if (numAFormats > 1)
486  aformat = aformats[i];
487  else if (numAFormats > 0)
488  aformat = aformats[0];
489  else
490  aformat = 0; /* default = text */
491 
492  if (aformat == 0)
493  {
494  Oid typinput;
495  Oid typioparam;
496  char *pstring;
497 
498  getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
499 
500  /*
501  * Since stringinfo.c keeps a trailing null in place even for
502  * binary data, the contents of abuf are a valid C string. We
503  * have to do encoding conversion before calling the typinput
504  * routine, though.
505  */
506  if (argsize == -1)
507  pstring = NULL;
508  else
509  pstring = pg_client_to_server(abuf.data, argsize);
510 
511  fcinfo->arg[i] = OidInputFunctionCall(typinput, pstring,
512  typioparam, -1);
513  /* Free result of encoding conversion, if any */
514  if (pstring && pstring != abuf.data)
515  pfree(pstring);
516  }
517  else if (aformat == 1)
518  {
519  Oid typreceive;
520  Oid typioparam;
521  StringInfo bufptr;
522 
523  /* Call the argument type's binary input converter */
524  getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
525 
526  if (argsize == -1)
527  bufptr = NULL;
528  else
529  bufptr = &abuf;
530 
531  fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, bufptr,
532  typioparam, -1);
533 
534  /* Trouble if it didn't eat the whole buffer */
535  if (argsize != -1 && abuf.cursor != abuf.len)
536  ereport(ERROR,
537  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
538  errmsg("incorrect binary data format in function argument %d",
539  i + 1)));
540  }
541  else
542  ereport(ERROR,
543  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
544  errmsg("unsupported format code: %d", aformat)));
545  }
546 
547  /* Return result format code */
548  return (int16) pq_getmsgint(msgBuf, 2);
549 }
signed short int16
Definition: c.h:255
FmgrInfo flinfo
Definition: fastpath.c:53
short fn_nargs
Definition: fmgr.h:60
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:550
#define FUNC_MAX_ARGS
void pfree(void *pointer)
Definition: mcxt.c:950
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:556
#define ERROR
Definition: elog.h:43
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:2065
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:94
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2633
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
#define ereport(elevel, rest)
Definition: elog.h:122
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:56
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:2047
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
static int16 parse_fcall_arguments_20 ( StringInfo  msgBuf,
struct fp_info fip,
FunctionCallInfo  fcinfo 
)
static

Definition at line 558 of file fastpath.c.

References appendBinaryStringInfo(), FunctionCallInfoData::arg, FunctionCallInfoData::argnull, fp_info::argtypes, StringInfoData::cursor, ereport, errcode(), errmsg(), ERROR, fp_info::flinfo, FmgrInfo::fn_nargs, FUNC_MAX_ARGS, getTypeBinaryInputInfo(), i, initStringInfo(), StringInfoData::len, FunctionCallInfoData::nargs, NULL, OidReceiveFunctionCall(), pq_getmsgbytes(), pq_getmsgint(), and resetStringInfo().

Referenced by HandleFunctionRequest().

560 {
561  int nargs;
562  int i;
563  StringInfoData abuf;
564 
565  nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
566 
567  if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
568  ereport(ERROR,
569  (errcode(ERRCODE_PROTOCOL_VIOLATION),
570  errmsg("function call message contains %d arguments but function requires %d",
571  nargs, fip->flinfo.fn_nargs)));
572 
573  fcinfo->nargs = nargs;
574 
575  initStringInfo(&abuf);
576 
577  /*
578  * Copy supplied arguments into arg vector. In protocol 2.0 these are
579  * always assumed to be supplied in binary format.
580  *
581  * Note: although the original protocol 2.0 code did not have any way for
582  * the frontend to specify a NULL argument, we now choose to interpret
583  * length == -1 as meaning a NULL.
584  */
585  for (i = 0; i < nargs; ++i)
586  {
587  int argsize;
588  Oid typreceive;
589  Oid typioparam;
590 
591  getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
592 
593  argsize = pq_getmsgint(msgBuf, 4);
594  if (argsize == -1)
595  {
596  fcinfo->argnull[i] = true;
597  fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, NULL,
598  typioparam, -1);
599  continue;
600  }
601  fcinfo->argnull[i] = false;
602  if (argsize < 0)
603  ereport(ERROR,
604  (errcode(ERRCODE_PROTOCOL_VIOLATION),
605  errmsg("invalid argument size %d in function call message",
606  argsize)));
607 
608  /* Reset abuf to empty, and insert raw data into it */
609  resetStringInfo(&abuf);
611  pq_getmsgbytes(msgBuf, argsize),
612  argsize);
613 
614  fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, &abuf,
615  typioparam, -1);
616 
617  /* Trouble if it didn't eat the whole buffer */
618  if (abuf.cursor != abuf.len)
619  ereport(ERROR,
620  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
621  errmsg("incorrect binary data format in function argument %d",
622  i + 1)));
623  }
624 
625  /* Desired result format is always binary in protocol 2.0 */
626  return 1;
627 }
FmgrInfo flinfo
Definition: fastpath.c:53
short fn_nargs
Definition: fmgr.h:60
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:550
#define FUNC_MAX_ARGS
#define ERROR
Definition: elog.h:43
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:2065
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:94
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2633
#define ereport(elevel, rest)
Definition: elog.h:122
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:56
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
static void SendFunctionResult ( Datum  retval,
bool  isnull,
Oid  rettype,
int16  format 
)
static

Definition at line 138 of file fastpath.c.

References buf, ereport, errcode(), errmsg(), ERROR, FrontendProtocol, getTypeBinaryOutputInfo(), getTypeOutputInfo(), OidOutputFunctionCall(), OidSendFunctionCall(), pfree(), PG_PROTOCOL_MAJOR, pq_beginmessage(), pq_endmessage(), pq_sendbyte(), pq_sendbytes(), pq_sendcountedtext(), pq_sendint(), VARDATA, VARHDRSZ, and VARSIZE.

Referenced by HandleFunctionRequest().

139 {
140  bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
142 
143  pq_beginmessage(&buf, 'V');
144 
145  if (isnull)
146  {
147  if (newstyle)
148  pq_sendint(&buf, -1, 4);
149  }
150  else
151  {
152  if (!newstyle)
153  pq_sendbyte(&buf, 'G');
154 
155  if (format == 0)
156  {
157  Oid typoutput;
158  bool typisvarlena;
159  char *outputstr;
160 
161  getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
162  outputstr = OidOutputFunctionCall(typoutput, retval);
163  pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
164  pfree(outputstr);
165  }
166  else if (format == 1)
167  {
168  Oid typsend;
169  bool typisvarlena;
170  bytea *outputbytes;
171 
172  getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
173  outputbytes = OidSendFunctionCall(typsend, retval);
174  pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
175  pq_sendbytes(&buf, VARDATA(outputbytes),
176  VARSIZE(outputbytes) - VARHDRSZ);
177  pfree(outputbytes);
178  }
179  else
180  ereport(ERROR,
181  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
182  errmsg("unsupported format code: %d", format)));
183  }
184 
185  if (!newstyle)
186  pq_sendbyte(&buf, '0');
187 
188  pq_endmessage(&buf);
189 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
#define VARDATA(PTR)
Definition: postgres.h:303
void pq_sendbyte(StringInfo buf, int byt)
Definition: pqformat.c:105
Oid rettype
Definition: fastpath.c:54
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2075
static char * buf
Definition: pg_test_fsync.c:65
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:131
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2666
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:115
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2056
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
ProtocolVersion FrontendProtocol
Definition: globals.c:27