PostgreSQL Source Code  git master
fastpath.c File Reference
#include "postgres.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 "port/pg_bswap.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)
 
void HandleFunctionRequest (StringInfo msgBuf)
 

Function Documentation

◆ fetch_fp_info()

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

Definition at line 196 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, ObjectIdGetDatum, OidIsValid, PROCOID, ReleaseSysCache(), fp_info::rettype, SearchSysCache1(), and strlcpy().

Referenced by HandleFunctionRequest().

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 = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
218  if (!HeapTupleIsValid(func_htp))
219  ereport(ERROR,
220  (errcode(ERRCODE_UNDEFINED_FUNCTION),
221  errmsg("function with OID %u does not exist", func_id)));
222  pp = (Form_pg_proc) GETSTRUCT(func_htp);
223 
224  /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
225  if (pp->pronargs > FUNC_MAX_ARGS)
226  elog(ERROR, "function %s has more than %d arguments",
227  NameStr(pp->proname), FUNC_MAX_ARGS);
228 
229  fip->namespace = pp->pronamespace;
230  fip->rettype = pp->prorettype;
231  memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
232  strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
233 
234  ReleaseSysCache(func_htp);
235 
236  /*
237  * This must be last!
238  */
239  fip->funcid = func_id;
240 }
FmgrInfo flinfo
Definition: fastpath.c:51
char fname[NAMEDATALEN]
Definition: fastpath.c:55
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
Oid rettype
Definition: fastpath.c:52
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:853
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
#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:122
#define ereport(elevel, rest)
Definition: elog.h:122
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:54
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
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 Assert(condition)
Definition: c.h:670
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:547
Oid funcid
Definition: fastpath.c:50
#define elog
Definition: elog.h:219

◆ GetOldFunctionMessage()

int GetOldFunctionMessage ( StringInfo  buf)

Definition at line 77 of file fastpath.c.

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

Referenced by SocketBackend().

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 }
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define FATAL
Definition: elog.h:52
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1094
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:264
#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:1166
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ HandleFunctionRequest()

void HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 259 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, 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().

260 {
261  Oid fid;
262  AclResult aclresult;
263  FunctionCallInfoData fcinfo;
264  int16 rformat;
265  Datum retval;
266  struct fp_info my_fp;
267  struct fp_info *fip;
268  bool callit;
269  bool was_logged = false;
270  char msec_str[32];
271 
272  /*
273  * We only accept COMMIT/ABORT if we are in an aborted transaction, and
274  * COMMIT/ABORT cannot be executed through the fastpath interface.
275  */
277  ereport(ERROR,
278  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
279  errmsg("current transaction is aborted, "
280  "commands ignored until end of transaction block")));
281 
282  /*
283  * Now that we know we are in a valid transaction, set snapshot in case
284  * needed by function itself or one of the datatype I/O routines.
285  */
287 
288  /*
289  * Begin parsing the buffer contents.
290  */
292  (void) pq_getmsgstring(msgBuf); /* dummy string */
293 
294  fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
295 
296  /*
297  * There used to be a lame attempt at caching lookup info here. Now we
298  * just do the lookups on every call.
299  */
300  fip = &my_fp;
301  fetch_fp_info(fid, fip);
302 
303  /* Log as soon as we have the function OID and name */
304  if (log_statement == LOGSTMT_ALL)
305  {
306  ereport(LOG,
307  (errmsg("fastpath function call: \"%s\" (OID %u)",
308  fip->fname, fid)));
309  was_logged = true;
310  }
311 
312  /*
313  * Check permission to access and call function. Since we didn't go
314  * through a normal name lookup, we need to check schema usage too.
315  */
316  aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
317  if (aclresult != ACLCHECK_OK)
319  get_namespace_name(fip->namespace));
320  InvokeNamespaceSearchHook(fip->namespace, true);
321 
322  aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
323  if (aclresult != ACLCHECK_OK)
324  aclcheck_error(aclresult, ACL_KIND_PROC,
325  get_func_name(fid));
327 
328  /*
329  * Prepare function call info block and insert arguments.
330  *
331  * Note: for now we pass collation = InvalidOid, so collation-sensitive
332  * functions can't be called this way. Perhaps we should pass
333  * DEFAULT_COLLATION_OID, instead?
334  */
335  InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
336 
338  rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
339  else
340  rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
341 
342  /* Verify we reached the end of the message where expected. */
343  pq_getmsgend(msgBuf);
344 
345  /*
346  * If func is strict, must not call it for null args.
347  */
348  callit = true;
349  if (fip->flinfo.fn_strict)
350  {
351  int i;
352 
353  for (i = 0; i < fcinfo.nargs; i++)
354  {
355  if (fcinfo.argnull[i])
356  {
357  callit = false;
358  break;
359  }
360  }
361  }
362 
363  if (callit)
364  {
365  /* Okay, do it ... */
366  retval = FunctionCallInvoke(&fcinfo);
367  }
368  else
369  {
370  fcinfo.isnull = true;
371  retval = (Datum) 0;
372  }
373 
374  /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
376 
377  SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
378 
379  /* We no longer need the snapshot */
381 
382  /*
383  * Emit duration logging if appropriate.
384  */
385  switch (check_log_duration(msec_str, was_logged))
386  {
387  case 1:
388  ereport(LOG,
389  (errmsg("duration: %s ms", msec_str)));
390  break;
391  case 2:
392  ereport(LOG,
393  (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
394  msec_str, fip->fname, fid)));
395  break;
396  }
397 }
signed short int16
Definition: c.h:283
FmgrInfo flinfo
Definition: fastpath.c:51
char fname[NAMEDATALEN]
Definition: fastpath.c:55
Oid rettype
Definition: fastpath.c:52
Oid GetUserId(void)
Definition: miscinit.c:284
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:371
int errcode(int sqlerrcode)
Definition: elog.c:575
static void SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
Definition: fastpath.c:136
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
#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:304
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4484
static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:544
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:174
int check_log_duration(char *msec_str, bool was_logged)
Definition: postgres.c:2129
#define ERROR
Definition: elog.h:43
bool fn_strict
Definition: fmgr.h:61
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1412
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3047
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:137
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define ACL_USAGE
Definition: parsenodes.h:80
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:178
uintptr_t Datum
Definition: postgres.h:372
static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:406
#define InvalidOid
Definition: postgres_ext.h:36
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
int log_statement
Definition: postgres.c:93
static void fetch_fp_info(Oid func_id, struct fp_info *fip)
Definition: fastpath.c:196
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:79
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4446
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:637
ProtocolVersion FrontendProtocol
Definition: globals.c:27

◆ parse_fcall_arguments()

static int16 parse_fcall_arguments ( StringInfo  msgBuf,
struct fp_info fip,
FunctionCallInfo  fcinfo 
)
static

Definition at line 406 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, OidInputFunctionCall(), OidReceiveFunctionCall(), palloc(), pfree(), pg_client_to_server(), pq_getmsgbytes(), pq_getmsgint(), and resetStringInfo().

Referenced by HandleFunctionRequest().

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

◆ parse_fcall_arguments_20()

static int16 parse_fcall_arguments_20 ( StringInfo  msgBuf,
struct fp_info fip,
FunctionCallInfo  fcinfo 
)
static

Definition at line 544 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, OidReceiveFunctionCall(), pq_getmsgbytes(), pq_getmsgint(), and resetStringInfo().

Referenced by HandleFunctionRequest().

546 {
547  int nargs;
548  int i;
549  StringInfoData abuf;
550 
551  nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
552 
553  if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
554  ereport(ERROR,
555  (errcode(ERRCODE_PROTOCOL_VIOLATION),
556  errmsg("function call message contains %d arguments but function requires %d",
557  nargs, fip->flinfo.fn_nargs)));
558 
559  fcinfo->nargs = nargs;
560 
561  initStringInfo(&abuf);
562 
563  /*
564  * Copy supplied arguments into arg vector. In protocol 2.0 these are
565  * always assumed to be supplied in binary format.
566  *
567  * Note: although the original protocol 2.0 code did not have any way for
568  * the frontend to specify a NULL argument, we now choose to interpret
569  * length == -1 as meaning a NULL.
570  */
571  for (i = 0; i < nargs; ++i)
572  {
573  int argsize;
574  Oid typreceive;
575  Oid typioparam;
576 
577  getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
578 
579  argsize = pq_getmsgint(msgBuf, 4);
580  if (argsize == -1)
581  {
582  fcinfo->argnull[i] = true;
583  fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, NULL,
584  typioparam, -1);
585  continue;
586  }
587  fcinfo->argnull[i] = false;
588  if (argsize < 0)
589  ereport(ERROR,
590  (errcode(ERRCODE_PROTOCOL_VIOLATION),
591  errmsg("invalid argument size %d in function call message",
592  argsize)));
593 
594  /* Reset abuf to empty, and insert raw data into it */
595  resetStringInfo(&abuf);
597  pq_getmsgbytes(msgBuf, argsize),
598  argsize);
599 
600  fcinfo->arg[i] = OidReceiveFunctionCall(typreceive, &abuf,
601  typioparam, -1);
602 
603  /* Trouble if it didn't eat the whole buffer */
604  if (abuf.cursor != abuf.len)
605  ereport(ERROR,
606  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
607  errmsg("incorrect binary data format in function argument %d",
608  i + 1)));
609  }
610 
611  /* Desired result format is always binary in protocol 2.0 */
612  return 1;
613 }
FmgrInfo flinfo
Definition: fastpath.c:51
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:510
#define FUNC_MAX_ARGS
#define ERROR
Definition: elog.h:43
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1751
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2679
#define ereport(elevel, rest)
Definition: elog.h:122
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:54
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ SendFunctionResult()

static void SendFunctionResult ( Datum  retval,
bool  isnull,
Oid  rettype,
int16  format 
)
static

Definition at line 136 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_sendint32(), VARDATA, VARHDRSZ, and VARSIZE.

Referenced by HandleFunctionRequest().

137 {
138  bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
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 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2646
#define VARDATA(PTR)
Definition: postgres.h:303
Oid rettype
Definition: fastpath.c:52
#define VARSIZE(PTR)
Definition: postgres.h:304
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
#define VARHDRSZ
Definition: c.h:493
int errcode(int sqlerrcode)
Definition: elog.c:575
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
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:87
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1761
static char * buf
Definition: pg_test_fsync.c:67
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2712
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1742
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
Definition: c.h:487
static char format
Definition: pg_basebackup.c:81
ProtocolVersion FrontendProtocol
Definition: globals.c:27